1.
Refer to the following variable declaration for this question:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
Which of the following is a valid conclusion based on this variable declaration?
Correct Answer
D. Numbers[5] = 6;
Explanation
The valid conclusion based on the given variable declaration is that the value at index 5 of the array "numbers" is being changed to 6.
2.
Refer to the following variable declaration for this question:
double[] decimals = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
What does this variable declaration represent?
Correct Answer
B. A list
Explanation
The given variable declaration represents a list. It is an array of double values that stores decimal numbers ranging from 0.1 to 1.0. The use of square brackets and the presence of multiple values indicate that it is a list or an array.
3.
When should one choose a for() loop instead of a while() loop?
Correct Answer
C. When you know the number of times the loop should execute
Explanation
A for() loop should be chosen instead of a while() loop when you know the number of times the loop should execute. The for() loop is designed to execute a specific number of times, which is determined by the initialization, condition, and increment/decrement statements within the loop structure. This makes it ideal for situations where the number of iterations is known in advance, such as iterating over arrays or performing a fixed number of calculations.
4.
When running any java class, which of the following is the starting point?
Correct Answer
E. None of the above - not enough information is given
Explanation
The question asks about the starting point when running any Java class, but the given options do not provide enough information to determine the correct answer. The starting point for running a Java class is the main() method, which serves as the entry point for the program. However, since this option is not provided, none of the given options can be considered as the correct answer.
5.
Each time that a loop "loops" is known as a(n):
Correct Answer
D. Iteration
Explanation
In programming, a loop is a structure that repeats a certain block of code multiple times. Each repetition of the loop is known as an iteration. Therefore, the correct answer is "iteration".
6.
Which of the following most specifically defines a package?
Correct Answer
A. A group of java classes with a similar purpose
Explanation
A package in Java is a way to organize and group related classes together. It allows for better organization and modularization of code. By grouping classes with a similar purpose, it becomes easier to manage and maintain the codebase. This also helps in avoiding naming conflicts and provides better code reusability. Therefore, the correct answer is "a group of java classes with a similar purpose".
7.
Which of the following is an example of a method header?
Correct Answer
B. Public static int stringToInteger()
Explanation
The given example "public static int stringToInteger()" is an example of a method header because it includes the access modifier "public", the keyword "static" indicating that the method belongs to the class rather than an instance of the class, the return type "int" indicating the type of value the method will return, and the method name "stringToInteger()".
8.
An overloaded method is:
Correct Answer
C. A method with the same name, but different parameters
Explanation
An overloaded method is a method with the same name as another method, but with different parameters. This allows multiple methods with the same name to be defined in a class, each with a different set of parameters. The compiler distinguishes between these methods based on the number, type, and order of the parameters. This allows for more flexibility and versatility in method implementation, as different versions of the method can be called depending on the arguments provided.
9.
Which of the following is an example of a call to a method to draw a rectangle that is 100 by 200 pixels?
Correct Answer
B. DrawRectangle(100, 200);
Explanation
The correct answer is "drawRectangle(100, 200);" because it is the only option that correctly calls the method "drawRectangle" with the parameters "100" and "200", specifying the dimensions of the rectangle as 100 by 200 pixels.
10.
Which of the following data types are ideal for numbers?
Correct Answer
E. Both a and d
Explanation
Both float and double are ideal data types for representing numbers with decimal points. Float is a 32-bit data type that can store decimal numbers with a precision of up to 7 digits, while double is a 64-bit data type that can store decimal numbers with a precision of up to 15 digits. Boolean is a data type used for representing true/false values, and char is used for representing single characters. Therefore, the correct answer is both a and d, as float and double are suitable for storing numbers.
11.
Which of the following would be an example of only instantiating an object?
Correct Answer
B. George = new Turtle();
Explanation
The correct answer is "george = new Turtle();". This line of code is an example of only instantiating an object because it creates a new instance of the Turtle class and assigns it to the variable "george". The other options either declare a variable without instantiating an object or assign an existing object to a variable.
12.
Which of the following would be an example of only declaring a number variable?
Correct Answer
A. Double number;
Explanation
The correct answer is "double number;" because it only declares a variable named "number" of type double without assigning any value to it. The other options either assign a value to the variable or create a new instance of a float variable.
13.
Pseudocode is:
Correct Answer
C. Used by programmers to help map out what a program is supposed to do
Explanation
Pseudocode is a formal programming language that is written after the program design phase. It is used by programmers to help map out what a program is supposed to do. It is not actual code that can be executed, but rather a way to outline the logic and structure of a program before writing the actual code. Pseudocode allows programmers to plan and organize their code without worrying about the specific syntax of a programming language.
14.
Which of the following is an example of a constructor?
Correct Answer
D. Private class Calculator
Explanation
A constructor is a special method in a class that is used to initialize objects of that class. It is typically declared with the same name as the class and has no return type. In this case, the answer "private class Calculator" is an example of a constructor because it is a class with the same name as the class it is defined in and it has the private access modifier, indicating that it is a constructor.
15.
Refer to the following code for this question:
public class HelloWorld
{
public static void main (String [] args)
{
System.out.println("Hello World!!");
}
}
What would be the only appropriate file name for this program?
Correct Answer
B. HelloWorld.java
Explanation
The only appropriate file name for this program would be "HelloWorld.java" because in Java, the file name must match the name of the public class defined in the file. In this case, the public class is named "HelloWorld", so the file name should be "HelloWorld.java".
16.
Refer to the following code for this question:
public class HelloWorld
{
public static void main (String [] args)
{
System.out.println("Hello World!!");
}
}
What type of class is the class above?
Correct Answer
A. A driver class
Explanation
The class above is a driver class because it contains the main() method, which serves as the entry point for the program. The main() method is responsible for starting the execution of the program and it is required in order to run the code. Therefore, this class can be considered as the driver or starting point of the program.
17.
Following proper method naming conventions, which of the following would be a good method constructor?
Correct Answer
E. None of the above
Explanation
None of the above options are suitable for a method constructor. In Java, a method constructor should have the same name as the class, and it should not have a return type, not even void.
So, a proper method constructor would have a signature like public MyClass().
18.
Which of the following would be used to import all the classes in the java.awt class?
Correct Answer
D. Import java.awt.*;
Explanation
The correct answer is "import java.awt.*;". This statement is used to import all the classes in the java.awt package. The asterisk (*) is a wildcard character that represents all classes in the package. By using this import statement, all the classes in the java.awt package can be accessed without specifying their individual names.
19.
What is the use of the java.awt package?
Correct Answer
B. Creating user interfaces and for painting grapHics and images.
Explanation
The java.awt package is used for creating user interfaces and for painting graphics and images. This package provides classes and methods for creating windows, buttons, menus, and other graphical components. It also provides functionality for drawing shapes, images, and text on the screen. Therefore, the correct answer is creating user interfaces and for painting graphics and images.
20.
Which of the following classes is included in the java.util package?
Correct Answer
C. Scanner
Explanation
The correct answer is Scanner. Scanner is a class included in the java.util package. It is used to read input from various sources like files, strings, and the console. It provides methods to parse primitive types and strings, making it useful for input validation and data extraction.
21.
Which of the following best describes a contructor method?
Correct Answer
B. A public method that shares the same name as the class, but cannot return a value
Explanation
A constructor method is a special type of method in object-oriented programming that is used to initialize objects of a class. It has the same name as the class and does not have a return type. It is typically declared as public and is used to set initial values for the instance variables of the class. Therefore, the answer "A public method that shares the same name as the class, but cannot return a value" accurately describes a constructor method.
22.
Given the Turtle object t1, how can you call a method to make it move forward 100 pixels, using a distance parameter?
Correct Answer
D. T1.backward(-100);
Explanation
The correct answer is t1.backward(-100). This is because the backward() method is used to move the turtle in the opposite direction it is facing. In this case, the turtle is moved backwards by a distance of 100 pixels. The negative sign in the parameter is used to indicate the opposite direction.
23.
Which of the following is not a primitive data type?
Correct Answer
A. String
Explanation
The data types int, double, and char are all primitive data types in programming. They are used to represent basic types of data such as whole numbers, decimal numbers, and individual characters, respectively. However, String is not a primitive data type. It is a class in Java and other programming languages that represents a sequence of characters. Unlike primitive data types, objects of the String class can have methods and properties associated with them.