What is the primary purpose of a constructor in Java?
To control access to class members
To initialize object attributes
To define methods
To declare variables
What is essential to prevent infinite recursion in a Java method?
Using a different method name for each recursive call
A loop within the recursive method
A return statement in all code paths
A conditional statement that terminates the recursion
Which keyword is used to define a block of code that might throw an exception?
catch
finally
try
throw
If a method in Java is declared 'private,' where can it be accessed?
From any class within the same package
From any class in the application
Only from within the same class
From subclasses, even in different packages
Which of the following describes method overloading in Java?
Returning different data types from the same method
Calling a method from within itself
Changing the behavior of an existing method
Defining multiple methods with the same name and different parameters
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 enforce strict coding standards and prevent compilation errors.
To provide a mechanism for handling unexpected events during program execution.
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"); }
Error
x is 5
Less than or equal to 10
Greater than 10
Which of the following is NOT a valid way to declare an array in Java?
int arr[] = new int[5];
int []arr;
int arr[];
int[] arr;
Which component of the Java Development Kit (JDK) is responsible for converting Java source code (.java files) into bytecode (.class files)?
Java Runtime Environment (JRE)
Integrated Development Environment (IDE)
Java Compiler (javac)
Java Virtual Machine (JVM)
Can you overload a method in Java by changing only the return type?
Yes
No