OA Exams
How do you create a synchronized method in Java?
A. synchronized myMethod() {}
B. public synchronized void myMethod() {}
C. private synchronized myMethod() {}
D. synchronized void myMethod() {}
Answer: B
Explanation: The correct way to declare a synchronized method is public synchronized void myMethod() {}.
What is the result of System.out.println(5 + “5”);?
A. 10
B. 55
C. 5
D. Error
Explanation: The expression evaluates to "55" due to string concatenation.
Which of the following is not a primitive data type in Java?
A. int
B. double
C. String
D. boolean
Answer: C
Explanation: String is not a primitive data type; it is a reference type.
How can you declare an abstract method in a class?
A. abstract void myMethod();
B. void myMethod() {}
C. abstract void myMethod() {}
D. final void myMethod();
Answer: A
Explanation: An abstract method is declared with the abstract keyword and has no body.
What will happen if you try to access a null reference in Java?
A. It returns null.
B. It throws a NullPointerException.
C. It causes a memory leak.
D. It returns 0.
Explanation: Accessing a null reference results in a NullPointerException.
Which of the following is a method to remove an element from a List?
A. remove(index)
B. delete(index)
C. clear(index)
D. discard(index)
Explanation: The remove(index) method is used to remove an element from a List.
What is the output of System.out.println(“Hello World”.substring(0, 5));?
A. Hello
B. Hello World
C. World
Explanation: The substring(0, 5) method extracts characters from index 0 to 4, resulting in "Hello".
How do you declare an interface in Java?
A. interface MyInterface {}
B. public interface MyInterface {}
C. abstract interface MyInterface {}
D. Both A and B
Answer: D
Explanation: Both interface MyInterface {} and public interface MyInterface {} are valid ways to declare an interface.
Which of the following is true about constructors in Java?
A. Constructors have the same name as the class.
B. Constructors can have a return type.
C. Constructors are called when an object is created.
D. Both A and C
Explanation: Constructors have the same name as the class and are called when an object is created.
What is the purpose of the final keyword in Java?
A. To prevent method overriding
B. To prevent inheritance
C. To create immutable variables
D. All of the above
Explanation: The final keyword can prevent method overriding, inheritance, and can be used for immutable variables.
Which method is used to convert a string to an integer?
A. Integer.valueOf(str);
B. str.toInt();
C. Integer.parseInt(str);
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); convert a string to an integer.
What does the public keyword signify in Java?
A. The member can be accessed from anywhere.
B. The member is accessible only within its package.
C. The member is accessible only by subclasses.
D. The member is hidden from all other classes.
Explanation: The public keyword allows access to the member from anywhere.
How do you find the size of a List in Java?
A. list.length();
B. list.size();
C. list.count();
D. list.getSize();
Explanation: The size() method returns the number of elements in a List.
What is the output of System.out.println(“Java”.equals(“java”));?
A. true
B. false
C. Error
D. null
Explanation: The equals() method is case-sensitive, so "Java" is not equal to "java".
Which of the following is a valid constructor declaration?
A. public void MyConstructor() {}
B. MyConstructor() {}
C. public MyConstructor() {}
D. static MyConstructor() {}
Explanation: A constructor does not have a return type, and it must match the class name.
How do you define a method that can throw an exception?
A. void myMethod() throws Exception {}
B. void myMethod() catch Exception {}
C. void myMethod() on Exception {}
D. void myMethod() raise Exception {}
Explanation: You can define a method that throws an exception using throws in the method signature.
What is the result of System.out.println(7 / 2);?
A. 3.5
B. 3
C. 4
Explanation: Integer division truncates the decimal part, resulting in 3.
Which of the following will create an empty HashMap?
A. Map<String, String> map = new HashMap();
B. Map<String, String> map = new HashMap<>();
C. HashMap<String, String> map = new HashMap<>();
Explanation: All options are valid ways to create an empty HashMap.
How do you check if a map contains a specific key?
A. map.contains(key);
B. map.has(key);
C. map.exists(key);
D. map.find(key);
Explanation: The containsKey(key) method checks if a map contains a specific key.
Which of the following is not a feature of Java?
A. Platform independence
B. Object-oriented
C. High performance
D. Multiple inheritance
Explanation: Java does not support multiple inheritance directly through classes.