OA Exams
Which of the following is a valid way to create an immutable class?
Answer: D
Explanation: All the options contribute to creating an immutable class.
How do you create a synchronized block in Java?
Explanation: Both synchronized void myMethod() {} and synchronized (object) {} create synchronized blocks.
What does the instanceof keyword do in Java?
Answer: A
Explanation: The instanceof keyword checks if an object is an instance of a specified class.
Which method is used to remove all elements from a List in Java?
Explanation: The clear() method is used to remove all elements from a List.
What is the output of System.out.println(“Hello” + 5 + 2);?
Answer: C
Explanation: The expression evaluates to "Hello52" due to string concatenation.
Which of the following is a valid way to declare a map in Java?
Answer: B
Explanation: The correct syntax to declare a map is Map<String, String> map = new HashMap<>();.
What is the purpose of the synchronized keyword in Java?
Explanation: The synchronized keyword is used to ensure that only one thread can access a method at a time.
How do you check if a List contains a specific element in Java?
Explanation: The contains() method is used to check if a List contains a specific element.
What will the following code output? System.out.println(1 + “2” + 3);?
Explanation: The expression evaluates to "123" due to string concatenation.
Which of the following is true about Java exceptions?
Explanation: Only runtime exceptions do not need to be explicitly caught or declared.