What will be the output of System.out.println(3 + 4 + “Java”);?
A. 7Java
B. Java34
C. 34Java
D. 3 + 4 + Java
Answer: A
Explanation: The expression evaluates to "7Java" due to string concatenation.
Question 42
What is the purpose of the finally block in Java?
A. It always executes after try-catch
B. It is used for resource cleanup
C. It handles exceptions
D. Both A and B
Answer: D
Explanation: The finally block always executes after the try-catch blocks and is typically used for resource cleanup.
Question 43
How do you compare two objects for reference equality?
A. ==
B. equals()
C. compareTo()
D. isEqual()
Answer: A
Explanation: The == operator checks for reference equality, determining if two object references point to the same object.
Question 44
Which keyword is used to define a constant variable in Java?
A. const
B. final
C. static
D. constant
Answer: B
Explanation: The final keyword is used to define a constant variable that cannot be modified.
Question 45
What is the default value of an uninitialized int variable in Java?
A. 0
B. null
C. -1
D. Undefined
Answer: A
Explanation: The default value of an uninitialized int variable in Java is 0.
Question 46
How do you handle an unchecked exception in Java?
A. You must catch it with a try-catch block
B. You can ignore it
C. It must be declared in the method signature
D. It cannot be handled
Answer: B
Explanation: Unchecked exceptions (like RuntimeExceptions) do not need to be caught or declared.
Question 47
Which of the following methods can be used to iterate over elements in a List?
A. Using a for loop
B. Using an enhanced for loop
C. Using an iterator
D. All of the above
Answer: D
Explanation: You can iterate over elements in a List using a standard for loop, an enhanced for loop, or an iterator.
Question 48
What is the purpose of the static keyword?
A. To declare a class variable that can be accessed without an object
B. To prevent method overriding
C. To create an instance variable
D. To define an abstract class
Answer: A
Explanation: The static keyword declares a class variable that can be accessed without creating an object of the class.
Question 49
How do you convert a string to a character array in Java?
A. str.toCharArray()
B. str.getChars()
C. str.charAt()
D. str.toChar()
Answer: A
Explanation: The toCharArray() method converts a string to a character array.
Question 50
What is the purpose of the volatile keyword in Java?
A. To indicate that a variable may be changed unexpectedly
B. To prevent method overriding
C. To define a constant
D. To create a synchronized block
Answer: A
Explanation: The volatile keyword indicates that a variable's value may be changed by different threads unexpectedly, ensuring visibility of its changes.