Which of the following is a valid declaration of a double?
A. double price = "3.50";
B. double price = 3.5;
C. price = double 3.5;
D. decimal price = 3.5;
E. double = price 3.5;
What is the return type of a method that doesn’t return a value?
A. void
B. null
C. None
D. blank
E. static
What keyword is used to indicate one class inherits another?
A. child
B. inherits
C. extends
D. instanceOf
E. new
Which loop is best when you know exactly how many times you want to repeat an action?
A. while
B. do-while
C. for
D. loop
E. repeat
How do you declare an ArrayList of Strings?
A. ArrayList list = new ArrayList();
B. ArrayList<String> list = new ArrayList<String>();
C. List<String> list = List();
D. new ArrayList<String> list;
E. ArrayList<String> list = {};
What is the result of 7 % 3 in Java?
A. 2
B. 1
C. 0
D. 3
E. 7
What does this method return?
public int square(int x) {
return x * x;
}
A. x
B. x + x
C. x * x
D. Nothing
E. An error
Which method can be overridden?
A. private method
B. static method
C. final method
D. public method
E. constructor
What does i += 2 do?
A. Decreases i by 2
B. Assigns 2 to i
C. Multiplies i by 2
D. Adds 2 to i
E. Skips the loop
Which method adds an element to an ArrayList?
A. addElement()
B. insert()
C. push()
D. add()
E. put()
What does the following code output?
int a = 4;
int b = a++;
System.out.println(b);
A. 4
B. 5
C. 0
D. 8
E. Error
What keyword allows a method to access instance variables?
A. new
B. static
C. self
D. this
E. return
What is polymorphism in Java?
A. Using the same variable for multiple data types
B. A class having multiple names
C. Using the same method name for different behaviors
D. Creating a clone of an object
E. Making all methods public
What is the output of the following code?
int count = 0;
while (count < 3) {
count++;
}
System.out.println(count);
A. 0
B. 2
C. 3
D. 4
E. Infinite loop
What does list.size() return?
A. Number of variables
B. Capacity of the ArrayList
C. Index of the last item
D. Number of elements
E. Total memory used
What happens when you try to store a double in an int without casting?
A. It rounds it automatically
B. It throws a runtime error
C. It truncates silently
D. It causes a compile-time error
E. It promotes the int
Which is true about method overloading?
A. You can only overload constructors
B. It requires inheritance
C. Overloaded methods must have the same number of parameters
D. Overloaded methods must have different parameter types or counts
E. It's not allowed in Java
What does it mean if a subclass overrides a method from its superclass?
A. The method is deleted from the superclass
B. The method must be static
C. The subclass provides a new implementation of the method
D. The method becomes final
E. The superclass cannot use that method anymore
What does this code print?
for (int i = 0; i < 5; i += 2) {
System.out.print(i + " ");
}
A. 0 1 2 3 4
B. 0 2 4
C. 1 3 5
D. 2 4 6
E. 1 2 3
How do you access the element at row 2, column 1 of a 2D array called grid?
A. grid[1][2]
B. grid[2][1]
C. grid(2,1)
D. grid[1][1]
E. grid[2,1]
Which statement best describes Java’s primitive data types?
A. They are objects with methods
B. They cannot be used in arrays
C. They are passed by reference
D. They are not part of the Java language
E. They store simple values like int, double, and boolean
Which of the following is a valid method header for a method that takes two integers and returns a boolean?
A. boolean isValid(int a, int b)
B. bool isValid(a, b)
C. boolean isValid(int a; int b)
D. int isValid(int a, int b)
E. isValid(boolean a, boolean b)
Why can’t static methods be overridden?
A. They are too fast
B. They are private
C. They belong to the class, not an instance
D. They return void
E. They are automatically final
What is the output of this loop?
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum += i * 2;
}
System.out.println(sum);
A. 6
B. 10
C. 12
D. 8
E. 0
What is true about 2D arrays in Java?
A. All rows must have the same length
B. They must store strings
C. They are arrays of arrays
D. They are fixed at 10x10
E. They don’t work with loops