Showing 451–460 of 476 questions
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.
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 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.
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.
Which statement about Java's 'this' keyword is correct?
A
'this' refers to the parent class
B
'this' refers to the current object instance
C
'this' is a static reference
D
'this' cannot be used in constructors
Correct Answer:
B. 'this' refers to the current object instance
EXPLANATION
'this' is a reference to the current object instance. It's used to distinguish instance variables from parameters and to call other constructors.
What is the output of: double d = 5.7; int i = (int) d; System.out.println(i);
EXPLANATION
Explicit casting from double to int truncates the decimal part. 5.7 becomes 5 (not rounded).
What will be the output: int a = 10; int b = 20; System.out.println(a > b ? "A" : "B");
EXPLANATION
Ternary operator: condition ? trueValue : falseValue. Since 10 > 20 is false, "B" is printed.
Which of the following correctly initializes a 2D array in Java?
A
int[][] arr = new int[3][4];
B
int[] arr[] = new int[3][4];
C
Both A and B
D
Neither A nor B
Correct Answer:
C. Both A and B
EXPLANATION
Both syntaxes are valid in Java for declaring 2D arrays. The brackets can be placed after the type or variable name.