Which of the following keywords is used to create a subclass in Java?
A. extends
B. implements
C. inherits
D. includes
Answer: A
Explanation: The extends keyword is used to create a subclass from a superclass
Question 42
What is the purpose of the final keyword in Java?
A. To define an abstract class
B. To restrict method overriding
C. To create a constant variable
D. All of the above
Answer: D
Explanation: The final keyword can be used to define constants, restrict method overriding, and prevent inheritance of a class.
Question 43
Which of the following statements will throw a NullPointerException?
A. String str = null; str.length();
B. int[] arr = new int[5]; arr[0] = 10;
C. Object obj = new Object();
D. String str = “Hello”;
Answer: A
Explanation: Calling length() on a null string will throw a NullPointerException.
Question 44
How do you create a copy of an array in Java?
A. array.clone();
B. array.copy();
C. array.clone();
D. array.copyOf();
Answer: A
Explanation: The clone() method creates a copy of the array.
Question 45
What will be the output of the following code snippet? System.out.println(10 % 3);
A. 3
B. 1
C. 10
D. 0
Answer: B
Explanation: The % operator returns the remainder of the division, which is 1.
Question 46
What does the System.exit(0); statement do?
A. Terminates the current thread
B. Exits the Java Virtual Machine
C. Returns to the previous method
D. Stops the program temporarily
Answer: B
Explanation: The System.exit(0); statement exits the Java Virtual Machine.
Question 47
How do you read data from a file in Java?
A. FileReader fr = new FileReader(“file.txt”);
B. Scanner sc = new Scanner(“file.txt”);
C. InputStreamReader isr = new InputStreamReader(“file.txt”);
D. BufferedReader br = new BufferedReader(“file.txt”);
Answer: A
Explanation: The FileReader class is used to read data from a file.
Question 48
What is an interface in Java?
A. A class that cannot be instantiated
B. A reference type that can contain only constants and method signatures
C. A way to implement multiple inheritance
D. All of the above
Answer: D
Explanation: An interface is a reference type in Java that can contain only constants and method signatures and serves as a way to implement multiple inheritance.
Question 49
How do you declare a method that accepts parameters in Java?
A. void methodName() { … }
B. void methodName(int param) { … }
C. methodName(int param) { … }
D. void methodName(int, int) { … }
Answer: B
Explanation: The correct way to declare a method with parameters is void methodName(int param) { ... }.
Question 50
Which keyword is used to indicate that a method will not be implemented in the current class?
A. abstract
B. static
C. final
D. void
Answer: A
Explanation: The abstract keyword is used to declare a method that does not have an implementation in the current class and must be implemented by subclasses.