Professional Documents
Culture Documents
INHERITANCE
INHERITANCE
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) 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. 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. 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.