Showing 911–920 of 958 questions
What does the 'super' keyword do in Java?
A
Refers to the current object
B
Refers to the parent class
C
Creates a new instance
D
Marks a method as superior
Correct Answer:
B. Refers to the parent class
EXPLANATION
The 'super' keyword is used to refer to parent class members and invoke parent class constructors.
Which of the following cannot be inherited in Java?
A
Public methods
B
Protected methods
C
Private methods
D
Static methods can be inherited
Correct Answer:
C. Private methods
EXPLANATION
Private members are not inherited. They remain accessible only within the class they are declared in.
What is the result of: int x = 5; System.out.println(x++ + ++x);?
EXPLANATION
x++ returns 5 (then increments to 6), ++x increments to 7 and returns 7. Sum = 5+7 = 12. Note: Actual result may vary due to JVM optimization but typically 11 or 12.
Which of the following about static variables is incorrect?
A
Static variables are shared among all instances
B
Static variables are initialized when class loads
C
Each object has its own copy of static variables
D
Static variables can be accessed using class name
Correct Answer:
C. Each object has its own copy of static variables
EXPLANATION
Static variables are shared among ALL instances of a class, not individual copies per object.
What is the output of: System.out.println(5 % 2 + 3 * 2 - 1);?
EXPLANATION
Following operator precedence: 5%2=1, 3*2=6, then 1+6-1=6
Which of the following is a valid variable declaration?
A
int 2num;
B
int num-2;
C
int num$2;
D
int num.2;
Correct Answer:
C. int num$2;
EXPLANATION
Variable names can contain letters, digits, underscores, and dollar signs, but cannot start with a digit or contain hyphens or dots.
Which statement about constructor overloading is true?
A
Constructors cannot be overloaded
B
Constructors can be overloaded with different parameter types and counts
C
Only one constructor is allowed per class
D
Constructor return type must differ for overloading
Correct Answer:
B. Constructors can be overloaded with different parameter types and counts
EXPLANATION
Constructors can be overloaded based on parameter count and types, but not on return type.
What is the correct way to declare a constant in Java?
A
const int MAX = 100;
B
static int MAX = 100;
C
public static final int MAX = 100;
D
final static int MAX = 100;
Correct Answer:
C. public static final int MAX = 100;
EXPLANATION
'const' is not a Java keyword. The correct convention for constants is public static final. Option D also works but C follows the standard convention.
Which access modifier allows access within the same package and subclasses?
A
public
B
private
C
protected
D
default
Correct Answer:
C. protected
EXPLANATION
protected allows access within the same package and to subclasses even outside the package.
What is the difference between == and .equals() for String comparison?
A
Both check string content
B
== checks reference, .equals() checks content
C
== checks content, .equals() checks reference
D
They are identical in functionality
Correct Answer:
B. == checks reference, .equals() checks content
EXPLANATION
== compares object references (memory location), while .equals() compares the actual string content.