ExamVeda
Login
Home
21
In Java, can a subclass override a default method from its superclass?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
22
What is the purpose of the "protected" access modifier in Java inheritance?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
23
In Java, can a subclass access public members (fields and methods) of its superclass defined in another package?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
24
What is the result of the following code snippet?

class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
25
In Java, can a subclass override a static method from its superclass?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
26
What is the result of the following code snippet?

class Parent {
static void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
27
In Java, can a subclass inherit private fields from its superclass?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
28
What is the result of the following code snippet?

class Parent {
private int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(super.x + " " + x);
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option B
Solution:
The given code snippet involves inheritance in Java. It defines two classes: Parent and Child. The Child class extends the Parent class, which means it inherits its fields and methods.

Inside the Parent class, there is a private integer variable x with a value of 10. In the Child class, there is another integer variable x with a value of 20. Additionally, the Child class has a display() method that prints the values of super.x and x.

In Java, when you access a variable with the super keyword, it refers to the variable of the superclass. When you access a variable without super, it refers to the variable of the current class.

Now, let's analyze what happens in the display() method:

- super.x refers to the x variable in the Parent class, which is 10.
- x refers to the x variable in the Child class, which is 20.

So, the display() method will print "10 20" because it first accesses the x variable of the superclass (Parent) using super.x and then accesses the x variable of the current class (Child) directly.

Therefore, the correct answer is:

Option B: 10 20
29
In Java, can a subclass override a final method from its superclass?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
30
What is the result of the following code snippet?

class Parent {
final void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
System.out.println("Child");
}
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board