-
web.groovymark@gmail.com
- December 26, 2024
Question 21
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() {}.
Question 22
What is the result of System.out.println(5 + “5”);?
A. 10
B. 55
C. 5
D. Error
Answer: B
Explanation: The expression evaluates to "55" due to string concatenation.
Question 23
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.
Question 24
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.
Question 25
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.
Answer: B
Explanation: Accessing a null reference results in a NullPointerException.
Question 26
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)
Answer: A
Explanation: The remove(index) method is used to remove an element from a List.
Question 27
What is the output of System.out.println(“Hello World”.substring(0, 5));?
A. Hello
B. Hello World
C. World
D. Error
Answer: A
Explanation: The substring(0, 5) method extracts characters from index 0 to 4, resulting in "Hello".
Question 28
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.
Question 29
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
Answer: D
Explanation: Constructors have the same name as the class and are called when an object is created.
Question 30
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
Answer: D
Explanation: The final keyword can prevent method overriding, inheritance, and can be used for immutable variables.
Question 31
Which method is used to convert a string to an integer?
A. Integer.valueOf(str);
B. str.toInt();
C. Integer.parseInt(str);
D. Both A and C
Answer: D
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); convert a string to an integer.
Question 32
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.
Answer: A
Explanation: The public keyword allows access to the member from anywhere.
Question 33
How do you find the size of a List in Java?
A. list.length();
B. list.size();
C. list.count();
D. list.getSize();
Answer: B
Explanation: The size() method returns the number of elements in a List.
Question 34
What is the output of System.out.println(“Java”.equals(“java”));?
A. true
B. false
C. Error
D. null
Answer: B
Explanation: The equals() method is case-sensitive, so "Java" is not equal to "java".
Question 35
Which of the following is a valid constructor declaration?
A. public void MyConstructor() {}
B. MyConstructor() {}
C. public MyConstructor() {}
D. static MyConstructor() {}
Answer: C
Explanation: A constructor does not have a return type, and it must match the class name.
Question 36
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 {}
Answer: A
Explanation: You can define a method that throws an exception using throws in the method signature.
Question 37
What is the result of System.out.println(7 / 2);?
A. 3.5
B. 3
C. 4
D. Error
Answer: B
Explanation: Integer division truncates the decimal part, resulting in 3.
Question 38
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<>();
D. All of the above
Answer: D
Explanation: All options are valid ways to create an empty HashMap.
Question 39
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);
Answer: A
Explanation: The containsKey(key) method checks if a map contains a specific key.
Question 40
Which of the following is not a feature of Java?
A. Platform independence
B. Object-oriented
C. High performance
D. Multiple inheritance
Answer: D
Explanation: Java does not support multiple inheritance directly through classes.