- What is the output of the following code?
int x = 5;
int y = 2;
System.out.println(x / y);
A) 2.5
B) 2
C) 2.0
D) 2.5 with a runtime error
Answer: B) 2
- Which of the following operators in Java is used to perform concatenation of strings?
A) +
B) -
C) *
D) /
Answer: A) +
- What is the purpose of the 'default' keyword in Java switch statements?
A) It specifies a default value for a variable.
B) It is used to define default values for class fields.
C) It specifies the default case to be executed if no other case matches.
D) It is used to create default constructors for classes.
Answer: C) It specifies the default case to be executed if no other case matches.
- Which of the following is NOT a valid loop construct in Java?
A) for
B) foreach
C) dowhile
D) repeat
Answer: D) repeat
- What is the output of the following code?
int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); }
A) 1 2 3 4 5
B) 5 4 3 2 1
C) 1, 2, 3, 4, 5
D) 5, 4, 3, 2, 1
Answer: A) 1 2 3 4 5
- What does the 'super()' keyword do in Java?
A) It calls the superclass constructor.
B) It calls the current class constructor.
C) It calls the default constructor of the superclass.
D) It returns the superclass instance.
Answer: A) It calls the superclass constructor.
- Which of the following is true about the 'instanceof' operator in Java?
A) It checks if an object is an instance of a particular class.
B) It checks if two objects are identical.
C) It compares the memory addresses of two objects.
D) It checks if an object is null.
Answer: A) It checks if an object is an instance of a particular class.
- What is the output of the following code?
int x = 5; int y = 2; System.out.println(x % y);
A) 2.5
B) 2
C) 2.0
D) 1
Answer: D) 1
- Which of the following statements is true about method overriding in Java?
A) Method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its superclass.
B) Method overriding occurs when a subclass provides a new method with the same name as a method in its superclass.
C) Method overriding allows a subclass to access private methods of its superclass.
D) Method overriding is not allowed in Java.
Answer: A) Method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its superclass.
- Which of the following access modifiers restricts access the most in Java?
A) public
B) protected
C) private
D) default (no modifier)
Answer: C) private
Comments