Java Multiple Choice Questions & Answers on Inheritance for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers.html
1. Which of these keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends
Answer: d
2. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) None of the mentioned
Answer: b
Explanation: whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
3. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
Answer: b
Explanation: A class member declared protected becomes private member of subclass.
4. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}
Answer: c
5. Which two classes use the Shape class correctly? (Choose two.)
A. public class Circle implements Shape { private int radius; } B. public abstract class Circle extends Shape { private int radius; } C. public class Circle extends Shape { private int radius; public void draw(); } D. public abstract class Circle implements Shape { private int radius; public void draw(); } E. public class Circle extends Shape { private int radius; public void draw() { /* code here */ } } F. public abstract class Circle implements Shape { private int radius; public void draw() { /* code here */ } }
a) B,E
b) A,C
c) C,E
d) T,H
b) A,C
c) C,E
d) T,H
Answer: a
Explanation: If one is extending any class, then they should use extends keyword not implements.
6. What is the output of this program?
class A {
int i;
void display() {
System.out.println(i);
}
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class inheritance_demo {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
b) 1
c) 2
d) Compilation Error
Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
7. What is the output of this program?
class A {
int i;
}
class B extends A {
int j;
void display() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2
b) 3 3
c) 2 3
d) 3 2
Answer: c
8. What is the output of this program?
class A {
public int i;
private int j;
}
class B extends A {
void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
9. What is the output of this program?
class A {
public int i;
public int j;
A() {
i = 1;
j = 2;
}
}
class B extends A {
int a;
B() {
super();
}
}
class super_use {
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
b) 2 1
c) Runtime Error
d) Compilation Error
Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
10. What is the output of this program?
class A {
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 1 2
b) 2 1
c) 1 3
d) 3 1
b) 2 1
c) 1 3
d) 3 1
Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.