SlideShare a Scribd company logo
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

More Related Content

PPT
Java language fundamentals
PPT
Java Threads
PDF
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
PPTX
12. Java Exceptions and error handling
PPT
Exception handling
DOC
Final JAVA Practical of BCA SEM-5.
Java language fundamentals
Java Threads
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
12. Java Exceptions and error handling
Exception handling
Final JAVA Practical of BCA SEM-5.

What's hot (20)

PDF
Java practical(baca sem v)
PPTX
Navigating the xDD Alphabet Soup
PPTX
SoCal Code Camp 2015: An introduction to Java 8
PDF
Java Simple Programs
PDF
Java Programming - 03 java control flow
KEY
Inside PyMongo - MongoNYC
PPTX
12. Exception Handling
PDF
Fnt software solutions placement paper
PPTX
Chap2 class,objects contd
DOCX
PPT
Simple Java Programs
PDF
E5
PPTX
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
PPTX
Part - 2 Cpp programming Solved MCQ
PPTX
PPT
conditional statements
DOCX
Simulado java se 7 programmer
DOCX
Java PRACTICAL file
PPT
Inheritance and-polymorphism
PPTX
20.4 Java interfaces and abstraction
Java practical(baca sem v)
Navigating the xDD Alphabet Soup
SoCal Code Camp 2015: An introduction to Java 8
Java Simple Programs
Java Programming - 03 java control flow
Inside PyMongo - MongoNYC
12. Exception Handling
Fnt software solutions placement paper
Chap2 class,objects contd
Simple Java Programs
E5
UGC-NET, GATE and all IT Companies Interview C++ Solved Questions PART - 2
Part - 2 Cpp programming Solved MCQ
conditional statements
Simulado java se 7 programmer
Java PRACTICAL file
Inheritance and-polymorphism
20.4 Java interfaces and abstraction
Ad

Viewers also liked (6)

PPTX
Chap3 inheritance
PPTX
Inheritance
PDF
itft-Inheritance in java
PPT
Java inheritance
PPTX
Inheritance in JAVA PPT
PPTX
Inheritance
Chap3 inheritance
Inheritance
itft-Inheritance in java
Java inheritance
Inheritance in JAVA PPT
Inheritance
Ad

Similar to Java Inheritance (20)

PDF
2 object orientation-copy
PDF
Core java
TXT
Indus Valley Partner aptitude questions and answers
PPTX
Java concurrency questions and answers
DOCX
Uta005
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
PDF
Review Questions for Exam 10182016 1. public class .pdf
PDF
sample_midterm.pdf
DOCX
Multiple choice questions for Java io,files and inheritance
PDF
Second chapter-java
PDF
Scjp6.0
PPTX
OCJP Samples Questions: Exceptions and assertions
PDF
DOCX
Comp 328 final guide
DOCX
Unit-3 Practice Programs-5.docx
PPTX
Technical aptitude test 2 CSE
PPTX
Language fundamentals ocjp
PDF
Java and j2ee_lab-manual
PPTX
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
PDF
Class 6 2ciclo
2 object orientation-copy
Core java
Indus Valley Partner aptitude questions and answers
Java concurrency questions and answers
Uta005
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
Review Questions for Exam 10182016 1. public class .pdf
sample_midterm.pdf
Multiple choice questions for Java io,files and inheritance
Second chapter-java
Scjp6.0
OCJP Samples Questions: Exceptions and assertions
Comp 328 final guide
Unit-3 Practice Programs-5.docx
Technical aptitude test 2 CSE
Language fundamentals ocjp
Java and j2ee_lab-manual
FINAL_DAY9_METHOD_OVERRIDING_Role and benefits .pptx
Class 6 2ciclo

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
20250228 LYD VKU AI Blended-Learning.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Reach Out and Touch Someone: Haptics and Empathic Computing

Java Inheritance

  • 2. 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
  • 3. 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
  • 4. Q3 An abstract method cannot be overridden. Select 1 correct option. a True b False Ans: b
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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