Which Java keyword is used to make a decision between two blocks of code based on a condition?
for
switch
if
while
What is the default access modifier for a class member in Java if none is specified?
default (package-private)
private
protected
public
Which loop in Java is guaranteed to execute at least once, even if the condition is initially false?
while loop
for loop
do-while loop
enhanced for loop
What is the time complexity of the get() method for an ArrayList?
get()
O(1)
O(n)
O(log n)
O(n log n)
What are fields in a Java class?
Constructors used to create objects
Keywords reserved by the Java language
Methods that belong to the class
Variables that hold the state of an object
What is the output of the following code snippet?
int x = 5; if (x > 10) { System.out.println("Greater than 10"); } else { System.out.println("Less than or equal to 10"); }
Greater than 10
Error
Less than or equal to 10
x is 5
What is the base class for all checked exceptions in Java?
Throwable
Exception
RuntimeException
Can a class have multiple constructors?
Yes, as long as they have different parameter lists.
Constructors are optional, so a class may or may not have any.
No, a class can only have one constructor.
Yes, but they must have the same parameter list.
If a method in Java is declared 'private,' where can it be accessed?
From subclasses, even in different packages
From any class in the application
Only from within the same class
From any class within the same package
What is the purpose of the 'switch' statement in Java?
To check a condition and execute different code blocks based on the result
To define a method
To declare a variable
To execute a block of code repeatedly