Vous êtes sur la page 1sur 18

Inheritance Questions

Question 1:
class X
{
//Class X Members
}
class Y
{
//Class Y Members
}
class Z extends X, Y
{
//Class Z Members
}
Answer : Compilation error

Question 2:
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}

Answer: 10

Question 3:
class A
{
{
System.out.println(1);
}
}
class B extends A
{
{
System.out.println(2);
}
}
class C extends B
{
{
System.out.println(3);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}

Answer :
1
2
3

Question 4:

class A
{
String s = "Class A";
}
class B extends A
{
String s = "Class B";
{
System.out.println(super.s);
}
}
class C extends B
{
String s = "Class C";
{
System.out.println(super.s);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
System.out.println(c.s);
}
}

Answer:
Class A
Class B
Class C

Question 5:
class A

{
static
{
System.out.println("THIRD");
}
}
class B extends A
{
static
{
System.out.println("SECOND");
}
}
class C extends B
{
static
{
System.out.println("FIRST");
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}

Answer:

THIRD SECOND FIRST


Question 6:
class A
{
public A()
{
System.out.println("Class A Constructor");
}
}
class B extends A
{
public B()
{
System.out.println("Class B Constructor");
}
}
class C extends B
{
public C()
{

System.out.println("Class C Constructor");
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}

Answer :
Class A Constructor
Class B Constructor
Class C Constructor

Question 7:
class X
{
static void staticMethod()
{
System.out.println("Class X");
}
}
class Y extends X
{
static void staticMethod()
{
System.out.println("Class Y");
}
}
public class MainClass
{
public static void main(String[] args)
{
Y.staticMethod();
}
}

Answer : Class Y

Question 8:
Below code is showing compile time error. Can you suggest the corrections?

class X
{
public X(int i)
{
System.out.println(1);
}
}
class Y extends X
{
public Y()
{
System.out.println(2);
}
}

Question 9:
What is wrong with the below code? Why it is showing compile time error?
?

public class A
{
public A()
{
System.out.println(1);
super();
System.out.println(2);
}

Question 10:
Can you find out the error in the below code?

public class A
{
public A(int i)
{
}
}
class B extends A
{
}

Answer :explicit super() call should be there


Question 11:
class M
{
static
{
System.out.println('A');
}
{

System.out.println('B');

public M()
{
System.out.println('C');
}

class N extends M
{
static
{
System.out.println('D');
}
{

System.out.println('E');

public N()
{
System.out.println('F');
}

public class MainClass


{
public static void main(String[] args)

{
}

N n = new N();

View Answer
ADBCEF
Question 12:
class M
{
int i;
public M(int i)
{
this.i = i--;
}
}
class N extends M
{
public N(int i)
{
super(++i);
System.out.println(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
}
}

Answer :27

Question 13:
class M
{
int i = 51;

public M(int j)
{
System.out.println(i);
this.i = j * 10;
}
}
class N extends M
{
public N(int j)
{
super(j);
System.out.println(i);
this.i = j * 20;
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
System.out.println(n.i);
}
}
Answer :

51
260
520

Question 14:
Why Line 10 in the below code is showing compilation error?
?

class X{private int m = 48;}


class Y extends X{
void methodOfY()
{
System.out.println(m);
}
}

Answer:
Question 15:
class X
{
int m = 1111;

{
m = m++;
System.out.println(m);
}
}
class Y extends X
{
{
System.out.println(methodOfY());
}
int methodOfY()
{
return m-- + --m;
}
}
public class MainClass
{
public static void main(String[] args)
{
Y y = new Y();
}
}

Answer: 1111 , 2220


Question 16:
class A
{
static String s = "AAA";
static
{
s = s + "BBB";
}
{
s = "AAABBB";
}
}
class B extends A
{
static
{
s = s + "BBBAAA";
}
{
System.out.println(s);
}
}

public class MainClass


{
public static void main(String[] args)
{
B b = new B();
}
}

Answer : AAABBB

Question 17:
class X
{
int i = 101010;
public X()
{
i = i++ + i-- - i;
}
static int staticMethod(int i)
{
return --i;
}
}
class Y extends X
{
public Y()
{
System.out.println(staticMethod(i));
}
}
public class MainClass
{
public static void main(String[] args)
{
Y y = new Y();
}
}

Answer : 101010

Question 18:
class A
{
void A()

{
System.out.println(1);
}
}
class B extends A
{
void B()
{
A();
}
}
public class MainClass
{
public static void main(String[] args)
{
new B().B();
}
}
Answer : Yes, but with a warning that method has a constructor name. Output will be

Question 18:
class A
{
int i = 1212;
}
class B extends A
{
A a;
public B(A a)
{
this.a = a;
}
}
public class MainClass
{
public static void main(String[] args)
{
A a = new A();
B b = new B(a);
System.out.println(a.i);
System.out.println(b.i);
System.out.println(b.a.i);
b.a.i = 2121;

System.out.println("--------");
System.out.println(a.i);
System.out.println(b.i);

}
}

Answer:
1212
1212
1212

2121
1212
Question 19:
class ClassOne
{
static int i, j = 191919;
{
--i;
}
{
j++;
}
}
public class ClassTwo extends ClassOne
{
static
{
i++;
}
static
{
--j;
}
public static void main(String[] args)
{
System.out.println(i);
System.out.println(j);
}
}

Answer : 1 , 191918
Question 20:
4. class Chemical {
5. int ph() { return 7; }
6. }
7. public class Acid {
8. public static void main(String[] args) {
9. new Acid().go();
10. }
11. void go() {
12. System.out.println(ph() + " " + super.ph() + " " + getPh());
13. }
14. int getPh() { return 4; }
15. int ph() { return 3; }
15. }
A
B
C
D
E

374
474
774
Compilation fails
An exception is thrown at runtime

D is correct. Acid does not extend Chemical.


Question 21:
public class Certified {
public void getCertified() throws GreatGrandChildException { }
}
class SunCertified extends Certified {
}
class ChildException extends Exception{}
class GrandChildException extends ChildException{}
class GreatGrandChildException extends GrandChildException {}
a) public void getCertified() throws Exception { }
b) public void getCertified() throws ChildException { }
c) public void getCertified() throws GrandChildException { }
d) public void getCertified() throws GreatGrandChildException { }
Answer: d

Question 22:
public class Certified {
public void getCertified() throws ChildException { }

}
class SunCertified extends Certified {
}
class ChildException extends Exception{}
class GrandChildException extends ChildException{}
class GreatGrandChildException extends GrandChildException {}
a) public void getCertified() throws Exception { }
b) public void getCertified() throws ChildException { }
c) public void getCertified() throws GrandChildException { }
d) public void getCertified() throws GreatGrandChildException { }
Options b) c) and d) are correct.
Question 23:

class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
1. prints "Mammal eats food"
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.

Question 24

class Rectangle{
public int area(int length , int width) {

return

length * width;

}
}
class Square extends Rectangle{
public int area(long length , long width) {
return (int) Math.pow(length ,2);
}
}
class Test{
public static void main(String args[]) {
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
1. Will not compile.
2. Will compile and run printing out 20
3. Runtime error
4. Will compile and run printing out 25

Question 25:

class Base{}
class Derived extends Base{}
public class Test {
public static void main(String[] args){
Derived d = (Derived) new Base();
}
}
1. Will not compile
2. Compiles and runs without error.
3. Runtime error

Question 26:

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());
}
1.
2.
3.
4.

10
20
30
40

Question 26:
class Base{
static

int value = 0;

Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();

}
static void addValue(){
value +=

20;

}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}

1.
2.
3.
4.

10
20
30
40

Vous aimerez peut-être aussi