Inheritance
Duration : 30 mins.
Q1 .Consider the following code:
class Base
{
        private float f = 1.0f;
        protected void setF(float f1){ this.f = f1; }
}
class Base2 extends Base
{
        private float f = 2.0f;
        //1
}

Which of the following options is a valid example of overriding?

Select 2 correct options
a protected void setF(float f1){ this.f = 2*f1; }
b public void setF(double f1){ this.f = (float) 2*f1; }
c public void setF(float f1){ this.f = 2*f1; }
d private void setF(float f1){ this.f = 2*f1; }
e float setF(float f1){ this.f = 2*f1; return f;}

Ans: a,c
Q2
You are modeling a class hierarchy for living things.
You have a class LivingThing which has an abstract method reproduce().
Now, you want to have 2 subclasses of LivingThing, Plant and Animal.
Obviously, both do reproduce but the mechanisms are different.
What would you do?

Select 1 correct option.
a Overload the reproduce method in Plant and Animal Clases
b Overload the reproduce method in LivingThing Class.
c Override the reproduce method in Plant and Animal Clases
d Either overload or override, it depends on the taste of the designer.




Ans: c
Q3
An abstract method cannot be overridden.

Select 1 correct option.
a True
b False




Ans: b
Q4
What will be printed when the following program is compiled and run?
class Super
{
  public int getNumber( int a)
  {
    return 2;
  }
}
public class SubClass extends Super
{
  public int getNumber( int a, char ch)
  {
    return 4;
  }
  public static void main(String[] args)
  {
    System.out.println( new SubClass().getNumber(4) );
  }
}
Select 1 correct option.
a 4
b 2
c It will not compile.
d It will throw an exception at runtime.
e None of the above.
Ans:b
Q5
Consider the contents of following two files:
//File A.java
package a;
public class A
{
  A(){ }
  public void print(){ System.out.println("A"); }
}
//File B.java
package b;
import a.*;
public class B extends A
{
  B(){ }
  public void print(){ System.out.println("B"); }
  public static void main(String[] args)
  {
     new B();
  }
}
What will be printed when you try to compile and run class B?
 Select 1 correct option.
a It will print A.
b It will print B.
c It will not compile.
d It will compile but will not run.
e None of the above.


Ans:c
Q6
Consider the following code:
class Super
{
  static{ System.out.print("super "); }
}
class One
{
  static { System.out.print("one "); }
}
class Two extends Super
{
  static { System.out.print("two "); }
}
class Test
{
  public static void main(String[] args)
  {
    One o = null;
    Two t = new Two();
  }
}
What will be the output when class Test is run ?
Select 1 correct option.
a It will print one, super and two.
b It will print one, two and super.
c It will print super and two.
d It will print two and super
e None of the above.

Ans:c
Q7 What will the following code print when compiled and run?
class Base
{
           void methodA()
           {
                        System.out.println("base - MethodA");
           }
}
class Sub extends Base
{
           public void methodA()
           {
                        System.out.println("sub - MethodA");
           }
           public void methodB()
           {
                        System.out.println("sub - MethodB");
           }
           public static void main(String args[])
           {
                        Base b=new Sub(); //1
                        b.methodA(); //2
                        b.methodB(); //3
           }
}
Select 1 correct option.
a sub - MethodA , sub - MethodB
b base - MethodA , sub - MethodB
c Compile time error at //1
d Compile time error at //2
e Compile time error at //3                               Ans:   e
Q8
What will the following program print when run?

// Filename: TestClass.java
public class TestClass
{
   public static void main(String args[] ){ A b = new B("good bye"); }
}
class A
{
   A() { this("hello", " world"); }
   A(String s) { System.out.println(s); }
   A(String s1, String s2){ this(s1 + s2); }
}
class B extends A
{
   B(){ super("good bye"); };
   B(String s){ super(s, " world "); }
   B(String s1, String s2){ this(s1 + s2 + " ! "); }
}

Select 1 correct option.
a It will print "good bye".
b It will print "hello world".
c It will print "good bye world".
d It will print "good bye" followed by "hello world".
e It will print "hello world" followed by "good bye".           Ans: c
Q9
Which of the following access control keywords can be used to enable all the
subclasses to access a method defined in the base class?

Select 2 correct options
a public
b private
c protected
d No keyword is needed.




Ans:a,c
Q10
Consider the following class...

class MyString extends String
{
  MyString(){ super(); }
}

The above code will not compile.

Select 1 correct option.
a True
b False




Ans:a
Q11
Which of the following method definitions will prevent overriding of that method?

Select 4 correct options
a public final void method m1()
b public static void method m1()
c public static final void method m1()
d public abstract void method m1()
e private void method m1()




Ans:a,b,c,e
Q12
Given the following classes, what will be the output of compiling and running the class
Truck?
class Automobile
{
  public void drive() { System.out.println("Automobile: drive"); }
}
public class Truck extends Automobile
{
  public void drive() { System.out.println("Truck: drive"); }
  public static void main (String args [ ])
  {
    Automobile a = new Automobile();
    Truck t = new Truck();
    a.drive(); //1
    t.drive(); //2
    a = t; //3
    a.drive(); //4
  }
}
Select 1 correct option.
a Compiler error at line 3.
b Runtime error at line 3.
c It will print: Automobile: drive, Truck: drive, Automobile: drive, in that order.
d It will print: Automobile: drive, Truck: drive, Truck: drive, in that order.
e It will print: Automobile: drive, Automobile: drive, Automobile: drive, in that order.
Ans:d
Q13
What will be the result of attempting to compile and run the following program?

public class TestClass
{
  public static void main(String args[ ] )
  {
    A o1 = new C( );
    B o2 = (B) o1;
    System.out.println(o1.m1( ) );
    System.out.println(o2.i );
  }
}
class A { int i = 10; int m1( ) { return i; } }
class B extends A {at int i = 20; int m1() { return i; } }
class C extends B { int i = 30; int m1() { return i; } }

Select 1 correct option.
a The progarm will fail to compile.
b Class cast exception runtime.
c It will print 30, 20.
d It will print 30, 30.
e It will print 20, 20.                           Ans: c
Q14
Consider the following code:
class A
{
  A() { print(); }
  void print() { System.out.println("A"); }
}
class B extends A
{
  int i = Math.round(3.5f);
  public static void main(String[] args)
  {
    A a = new B();
    a.print();
  }
  void print() { System.out.println(i); }
}
What will be the output when class B is run ?
Select 1 correct option.
a It will print A, 4.
b It will print A, A
c It will print 0, 4
d It will print 4, 4
e None of the above.                          Ans: c
Q15
What will the following program print when run?
class Super
{
  public String toString()
  {
    return "4";
  }
}
public class SubClass extends Super
{
  public String toString()
  {
    return super.toString()+"3";
  }
  public static void main(String[] args)
  {
    System.out.println( new SubClass() );
  }
}
Select 1 correct option.
a 43
b 7
c It will not compile.
d It will throw an exception at runtime.
e None of the above.                              Ans: a

Java Inheritance

  • 1.
  • 2.
    Q1 .Consider thefollowing code: class Base { private float f = 1.0f; protected void setF(float f1){ this.f = f1; } } class Base2 extends Base { private float f = 2.0f; //1 } Which of the following options is a valid example of overriding? Select 2 correct options a protected void setF(float f1){ this.f = 2*f1; } b public void setF(double f1){ this.f = (float) 2*f1; } c public void setF(float f1){ this.f = 2*f1; } d private void setF(float f1){ this.f = 2*f1; } e float setF(float f1){ this.f = 2*f1; return f;} Ans: a,c
  • 3.
    Q2 You are modelinga class hierarchy for living things. You have a class LivingThing which has an abstract method reproduce(). Now, you want to have 2 subclasses of LivingThing, Plant and Animal. Obviously, both do reproduce but the mechanisms are different. What would you do? Select 1 correct option. a Overload the reproduce method in Plant and Animal Clases b Overload the reproduce method in LivingThing Class. c Override the reproduce method in Plant and Animal Clases d Either overload or override, it depends on the taste of the designer. Ans: c
  • 4.
    Q3 An abstract methodcannot be overridden. Select 1 correct option. a True b False Ans: b
  • 5.
    Q4 What will beprinted when the following program is compiled and run? class Super { public int getNumber( int a) { return 2; } } public class SubClass extends Super { public int getNumber( int a, char ch) { return 4; } public static void main(String[] args) { System.out.println( new SubClass().getNumber(4) ); } } Select 1 correct option. a 4 b 2 c It will not compile. d It will throw an exception at runtime. e None of the above. Ans:b
  • 6.
    Q5 Consider the contentsof following two files: //File A.java package a; public class A { A(){ } public void print(){ System.out.println("A"); } } //File B.java package b; import a.*; public class B extends A { B(){ } public void print(){ System.out.println("B"); } public static void main(String[] args) { new B(); } } What will be printed when you try to compile and run class B? Select 1 correct option. a It will print A. b It will print B. c It will not compile. d It will compile but will not run. e None of the above. Ans:c
  • 7.
    Q6 Consider the followingcode: class Super { static{ System.out.print("super "); } } class One { static { System.out.print("one "); } } class Two extends Super { static { System.out.print("two "); } } class Test { public static void main(String[] args) { One o = null; Two t = new Two(); } } What will be the output when class Test is run ? Select 1 correct option. a It will print one, super and two. b It will print one, two and super. c It will print super and two. d It will print two and super e None of the above. Ans:c
  • 8.
    Q7 What willthe following code print when compiled and run? class Base { void methodA() { System.out.println("base - MethodA"); } } class Sub extends Base { public void methodA() { System.out.println("sub - MethodA"); } public void methodB() { System.out.println("sub - MethodB"); } public static void main(String args[]) { Base b=new Sub(); //1 b.methodA(); //2 b.methodB(); //3 } } Select 1 correct option. a sub - MethodA , sub - MethodB b base - MethodA , sub - MethodB c Compile time error at //1 d Compile time error at //2 e Compile time error at //3 Ans: e
  • 9.
    Q8 What will thefollowing program print when run? // Filename: TestClass.java public class TestClass { public static void main(String args[] ){ A b = new B("good bye"); } } class A { A() { this("hello", " world"); } A(String s) { System.out.println(s); } A(String s1, String s2){ this(s1 + s2); } } class B extends A { B(){ super("good bye"); }; B(String s){ super(s, " world "); } B(String s1, String s2){ this(s1 + s2 + " ! "); } } Select 1 correct option. a It will print "good bye". b It will print "hello world". c It will print "good bye world". d It will print "good bye" followed by "hello world". e It will print "hello world" followed by "good bye". Ans: c
  • 10.
    Q9 Which of thefollowing access control keywords can be used to enable all the subclasses to access a method defined in the base class? Select 2 correct options a public b private c protected d No keyword is needed. Ans:a,c
  • 11.
    Q10 Consider the followingclass... class MyString extends String { MyString(){ super(); } } The above code will not compile. Select 1 correct option. a True b False Ans:a
  • 12.
    Q11 Which of thefollowing method definitions will prevent overriding of that method? Select 4 correct options a public final void method m1() b public static void method m1() c public static final void method m1() d public abstract void method m1() e private void method m1() Ans:a,b,c,e
  • 13.
    Q12 Given the followingclasses, what will be the output of compiling and running the class Truck? class Automobile { public void drive() { System.out.println("Automobile: drive"); } } public class Truck extends Automobile { public void drive() { System.out.println("Truck: drive"); } public static void main (String args [ ]) { Automobile a = new Automobile(); Truck t = new Truck(); a.drive(); //1 t.drive(); //2 a = t; //3 a.drive(); //4 } } Select 1 correct option. a Compiler error at line 3. b Runtime error at line 3. c It will print: Automobile: drive, Truck: drive, Automobile: drive, in that order. d It will print: Automobile: drive, Truck: drive, Truck: drive, in that order. e It will print: Automobile: drive, Automobile: drive, Automobile: drive, in that order. Ans:d
  • 14.
    Q13 What will bethe result of attempting to compile and run the following program? public class TestClass { public static void main(String args[ ] ) { A o1 = new C( ); B o2 = (B) o1; System.out.println(o1.m1( ) ); System.out.println(o2.i ); } } class A { int i = 10; int m1( ) { return i; } } class B extends A {at int i = 20; int m1() { return i; } } class C extends B { int i = 30; int m1() { return i; } } Select 1 correct option. a The progarm will fail to compile. b Class cast exception runtime. c It will print 30, 20. d It will print 30, 30. e It will print 20, 20. Ans: c
  • 15.
    Q14 Consider the followingcode: class A { A() { print(); } void print() { System.out.println("A"); } } class B extends A { int i = Math.round(3.5f); public static void main(String[] args) { A a = new B(); a.print(); } void print() { System.out.println(i); } } What will be the output when class B is run ? Select 1 correct option. a It will print A, 4. b It will print A, A c It will print 0, 4 d It will print 4, 4 e None of the above. Ans: c
  • 16.
    Q15 What will thefollowing program print when run? class Super { public String toString() { return "4"; } } public class SubClass extends Super { public String toString() { return super.toString()+"3"; } public static void main(String[] args) { System.out.println( new SubClass() ); } } Select 1 correct option. a 43 b 7 c It will not compile. d It will throw an exception at runtime. e None of the above. Ans: a