What will be the output of the following code snippet?
ArrayList<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); System.out.println(list.get(1));
apple
null
banana
IndexOutOfBoundsException
What is the primary purpose of exception handling in Java?
To improve the performance of Java applications.
To completely prevent runtime errors from occurring.
To provide a mechanism for handling unexpected events during program execution.
To enforce strict coding standards and prevent compilation errors.
What is the default access modifier for a class member in Java if none is specified?
private
public
protected
default (package-private)
What is the correct syntax to print 'Hello World!' to the console in Java?
echo 'Hello World!';
System.out.println('Hello World!');
print('Hello World!');
console.log('Hello World!');
What is recursion in the context of Java methods?
Calling a method from a different class
A method calling itself within its own definition
Returning multiple values from a method
Overloading a method with different parameters
What is the purpose of the 'break' statement in a Java 'switch' statement?
It prints an error message to the console.
It exits the 'switch' block after a matching case is found.
It skips the current iteration of a loop.
It terminates the entire program.
What is the primary difference between an array and an ArrayList in Java?
Arrays can store primitive data types, while ArrayLists cannot.
Arrays are fixed in size, while ArrayLists are dynamically sized.
Arrays can store objects of different classes, while ArrayLists can only store objects of the same class.
Arrays are part of the Java Collections Framework, while ArrayLists are not.
Which of the following is NOT a valid way to declare an array in Java?
int []arr;
int arr[];
int[] arr;
int arr[] = new int[5];
What is essential to prevent infinite recursion in a Java method?
A loop within the recursive method
Using a different method name for each recursive call
A return statement in all code paths
A conditional statement that terminates the recursion
What is the correct way to declare a variable in Java?
x int = 10;
int x = 10
int x;
x = 10;