What is the primary purpose of exception handling in Java?
To completely prevent runtime errors from occurring.
To enforce strict coding standards and prevent compilation errors.
To provide a mechanism for handling unexpected events during program execution.
To improve the performance of Java applications.
Which loop in Java is guaranteed to execute at least once, even if the condition is initially false?
do-while loop
while loop
enhanced for loop
for loop
What will the following Java code snippet print to the console?
for (int i = 1; i <= 3; i++) { System.out.print(i + " "); }
123
The code will result in a compilation error.
1 2 3
3 2 1
Can you overload a method in Java by changing only the return type?
Yes
No
What are fields in a Java class?
Keywords reserved by the Java language
Methods that belong to the class
Variables that hold the state of an object
Constructors used to create objects
What is the purpose of the 'break' statement in a Java 'switch' statement?
It prints an error message to the console.
It terminates the entire program.
It exits the 'switch' block after a matching case is found.
It skips the current iteration of a loop.
What is the primary purpose of a method in Java?
To store data
To define a class
To perform a specific task
To control program flow
What is the default access modifier for a class member in Java if none is specified?
protected
public
default (package-private)
private
What is the purpose of the 'this' keyword within a class?
To create a new instance of the class.
To call a method from the superclass.
To access static members of the class.
To refer to the current object.
Which Java keyword is used to make a decision between two blocks of code based on a condition?
if
while
switch
for