You are on page 1of 10

INHERITANCE

1. Which of this keyword must be used to inherit a class? Answer: d


a) super b) this c) extent d) extends
2. Which of these is correct way of inheriting class A by class B? Answer: C
a) class B + class A {} c) class B extends A {}
b) class B inherits class A {} d) class B extends class A {}
3. What will be the output of the following Java program? Answer: c
1. class A Explanation:
2. { Class A &
3. int i; class B both
4. void display() contain
5. { display()
6. System.out.println(i); method,
7. } class B
8. } inherits
9. class B extends A
class A,
10. {
when
11. int j;
display()
12. void display()
method is
13. {
called by
14. System.out.println(j);
object of
15. }
class B,
16. }
17. class inheritance_demo
display()
18. {
method of
19. public static void main(String args[]) class B is
20. { executed
21. B obj = new B(); rather than
22. obj.i=1; that of Class
23. obj.j=2; A.
24. obj.display(); output:
25. }
26. }

a) 0 b) 1 c) 2 d) Compilation Error
4. Which of the following is used for implementing inheritance through class? Answer: c
a) inherited b) using c) extends d) implements Explanation:
Class can be
extended
using
extends
keyword.
One class
can extend
only one
class. A final
class cannot
be
extended.
5. What will be the output of the following Java program? Answer: c

1. class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
8. void display()
9. {
10. super.i = j + 1;
11. System.out.println(j + " " + i);
12. }
13. }
14. class inheritance
15. {
16. public static void main(String args[])
17. {
18. B obj = new B();
19. obj.i=1;
20. obj.j=2;
21. obj.display();
22. }
23. }

a) 2 2 b) 3 3 c) 2 3 d) 3 2
6. What will be the output of the following Java program? Answer: a
1. class A Explanation:
2. { Keyword
3. public int i; super is
4. public int j; used to call
5. A() constructor
6. { of class A by
7. i = 1; constructor
8. j = 2; of class B.
9. }
Constructor
10. }
of a
11. class B extends A
initializes i &
12. {
j to 1 & 2
13. int a;
respectively.
14. B()
15. {
16. super();
17. }
18. }
19. class super_use
20. {
21. public static void main(String args[])
22. {
23. B obj = new B();
24. System.out.println(obj.i + " " + obj.j);
25. }
26. }

a) 1 2 b) 2 1 c) Runtime Error d) Compilation Error


7. Determine output: Answer: C
1. class Small
2. {
3. public Small() {
4. System.out.println("a ");
5. }
6. }
7. class Small2 extends Small
8. {
9. public Small2() {
10. System.out.println("b ");
11. }
12. }
13. class Small3 extends2 Small
14. {
15. public Small3() {
16. System.out.println("c ");
17. }
18. }
19. public class Test
20. {
21. public static void main(String args[])
22. {
23. new Small3();
24. }
25. }
26. }

a) a b) c c) abc d) cba
8. Inheritance means  Ans :-  B
A. Sub class extends Base class C. Sub class create object of super class
B. Sub class extends super class D. All of the above
9. What are the features reused using Inheritance in Java?  Ans :-  D
A. Variables B. Constants C. Methods D. All the above
10. What is subclass in java?  Ans :-  B
A. A subclass is a class declared inside a class.
B. A subclass is a class that extends another class.
C. Both above.
D. None of the above.
11. Inheritance relationship in Java language is  Ans :-  A
A. Is-A B. Has-A C. Association D. None
12. Which of these is correct way of calling a constructor having no parameters, of  Ans :- D 
superclass A by subclass B?
A. super(void); C. super.A();
B. superclass.(); D. super();
13. What is the output of the below Java program on the references of Superclass and Ans :-  B
Subclass? Explanation:- 
You can only
invoke
1. class Food methods of
2. { the Superclass
3. void show() using a
4. { Superclass
reference
5. System.out.print("FOOD ");
variable
6. }
pointing to a
7. } Subclass
8. class Bread extends Food object.
9. {
10. void toast()
11. {
12. System.out.print("TOASTED ");
13. }
14. }
15. public class Inheritance5
16. {
17. public static void main(String[] args)
18. {
19. Food foo = new Food();
20. foo.show();
21. Food foo2 = new Bread();
22. foo2.show();
23. Bread br = new Bread();
24. br.toast();
25. br.show();
26. }
27. }

A. FOOD FOOD FOOD FOOD C. FOOD TOASTED FOOD FOOD


B. FOOD FOOD TOASTED FOOD D. Compiler error
14. What will be the output of the following Java program?  Ans :-  C
Explanation:- 
1. class A
Class A &
2. { class B both
3. int i; contain
4. void display() display()
method, class
5. {
B inherits class
6. System.out.println(i); A, when
7. } display()
8. } method is
called by
9. class B extends A
object of class
10. { B, display()
11. int j; method of
12. void display() class B is
13. {
executed
rather than
14. System.out.println(j); that of Class
15. } A.
16. } output:
17. class inheritance_demo
18. {
19. public static void main(String args[])
20. {
21. B obj = new B();
22. obj.i=1;
23. obj.j=2;
24. obj.display();
25. }
26. }

A. 0 B. 1 C. 2 D. Compilation Error

15. A subclass object can be used to invoke (call) a method or property of the superclass. State TRUE  Ans :-  A
or FALSE.
A. TRUE B. FALSE C. May be TRUE or FALSE D. Can't Say
16. It be defined as the process where one class acquires the properties (methods and Ans : B
fields) of another.
A. Overriding B. Inheritance C. Polymorphism D. Abstraction Explanation:
Inheritance
can be defined
as the process
where one
class acquires
the properties
(methods and
fields) of
another
17. The class which inherits the properties of other. Ans : C
A. superclass B. parent class C. subclass D. None of the above
Explanation:
The class
which inherits
the properties
of other is
known as
subclass
18. Subclass also known as Ans : D
A. derived class B. child class C. base class D. Both A and B
Explanation:
subclass also
known as
derived class,
child class.
19. The super keyword is similar to _________ keyword. Ans : B
A. construct B. this C. class D. extends
Explanation:
The super key
word is
similar
to this keywor
d.
20. A subclass inherits all the members (fields, methods, and nested classes) from its Ans : A
superclass.
A. Yes B. NO C. Can be yes or no D. Can not say Explanation:
YES, A
subclass
inherits all the
members
(fields,
methods, and
nested classes)
from its
superclass.
21. A Subclass can become a Superclass to another class extending from it in Java. State Ans : A
TRUE or FALSE.
A. True B. False C. May be TRUE or FALSE D. Can not say
22. What is the superclass and subclass in the Java code below? Answer: C
class B
{
void show(){}
}
class A
{
void hide(){}
}
a. B is superclass and A is subclass.
b. A is superclass and B is a subclass.
c. There is no superclass or subclass present.
d. None of the above
23. What is the superclass and subclass in the Java code below? Answer: A
class Liquid
{
void pour(){}
}
class Juice extends Liquid
{
void filter(){}
}
a. The Liquid is a superclass and Juice is a subclass.
b. The Liquid is a Subclass and Juice is a Superclass.
c. There is no superclass or subclass present.
d. None of the above
24. The following Syntax is used for? Answer: B
class Subclass-name extends Superclass-name
{
//methods and fields
}

A. Polymorphism B. Inheritance C. Encapsulation D. None of these


25. Which of these cannot be passed down through inheritance? Answer: C
a. Public variables
b. Methods
c. Private variables
d. Data
26. The process by which objects of one class acquire the properties of objects of Answer: C
another class is known as
A. Polymorphism B. Data Hiding C. Inheritance D. Association
27. An object is an instance of a: Answer: B
A. parameter B. class C. method D. application
28. It uses extends keyword. Answer: D
A. Encapsulation B. Constructors C. Polymorphism D. Inheritance
29. Inheritance should establish what kind of relationship between child and parent? Answer: D
A. A B. With a C. As a D. Is a
30. Inheritance is when a new class, aka _______, is created from an existing class, aka _______. Answer: B
A. superclass, subclass C. headclass, subheadclass
B. subclass, superclass D. method, application
31. To specify the inheritance relationship between a subclass and a superclass, use the Answer: C
keyword
A. defines B. implements C. extends D. overrides
32. It is a way of saying: This object is a type of that object. Answer: A
A.  IS-A B.  HAS-A C.  ARE-A D.  HAD-A
33. What is the output of the following code? Answer: A
class Base {
    public void show() {
       System.out.println("Base::show() called");
    }
}
  
class Derived extends Base {
    public void show() {
       System.out.println("Derived::show() called");
    }
}
  
public class Main {
    public static void main(String[] args) {
        Base b = new Derived();
        b.show();
    }
}
A. Derived::show() called C. Base::show() called
B. Derived::show() called D. Base::show() called
Base::show() called Derived::show() called
34. What is the output of the following code? Answer: B
class Base {
    public void Print() {
        System.out.println("Base");
    }        
}
 
class Derived extends Base {   
    public void Print() {
        System.out.println("Derived");
    }
}
 
class Main{
    public static void DoPrint( Base o ) {
        o.Print();  
    }
    public static void main(String[] args) {
        Base x = new Base();
        Base y = new Derived();
        Derived z = new Derived();
        DoPrint(x);
        DoPrint(y);
        DoPrint(z);
    }
}

A. Base C. Base
Base Derived
Derived Base
B. Base D. Error
Derived
Derived
35. What will be the output after executing the following code? Answer: C
class Base {
int value = 0;
Base() {
addValue();
}
void addValue() {
value +=10;
}
int getValue() {
return value;
}
}
class Derived extends Base {
Derived() {
addValue();
}
void addValue() {
value +=20;
}
}
public class Test {
public static void main(String[] args) {
Base b = new Derived();
System.out.println(b.getValue());
}
}

A. 30 B. 10 C. 40 D. 20
36. What will be the output after executing the following code? Answer: C
1 class X
2 {
3     static void staticMethod()
4     {
5         System.out.println("Class X");
6     }
7 }
8  
9 class Y extends X
10 {
11     static void staticMethod()
12     {
13         System.out.println("Class Y");
14     }
15 }
16  
17 public class MainClass
18 {
19     public static void main(String[] args)
20     {
21         Y.staticMethod();
22     }
23 }

A. Class X C. Class Y
B. Class X D. Class Y
Class Y Class X
37. What will be the output after executing the following code? Answer: D
1 class A
2 {
3     int methodOfA(int i)
4     {
5         i /= 10;
6  
7         return i;
8     }
9 }
10  
11 class B extends A
12 {
13     int methodOfB(int i)
14     {
15         i *= 20;
16  
17         return methodOfA(i);
18     }
19 }
20  
21 public class MainClass
22 {
23     public static void main(String[] args)
24     {
25         B b = new B();
26  
27         System.out.println(b.methodOfB(100));
28     }
29 }

A. 10 B. 20 C. 100 D. 200

39.

You might also like