What is the time complexity of the get() method for an ArrayList?
get()
O(log n)
O(1)
O(n log n)
O(n)
What does it mean for a method to have a 'void' return type?
The method throws an exception.
The method returns an empty string.
The method doesn't return any value.
The method is not yet implemented.
Which of the following is NOT a valid access modifier in Java?
protected
public
private
static
If a method in Java is declared 'private,' where can it be accessed?
Only from within the same class
From any class within the same package
From any class in the application
From subclasses, even in different packages
What are fields in a Java class?
Variables that hold the state of an object
Keywords reserved by the Java language
Constructors used to create objects
Methods that belong to the class
What is the base case in a recursive function?
The return statement of the function.
The most complex part of the recursion.
The condition that stops the recursion.
The initial call to the function.
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"); }
Less than or equal to 10
x is 5
Error
Greater than 10
What is the primary purpose of a constructor in Java?
To define methods
To declare variables
To control access to class members
To initialize object attributes
Which access modifier allows a subclass in a different package to access a member of its superclass?
default
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 execute a block of code repeatedly
To declare a variable