• Like
Java Inheritance
Upcoming SlideShare
Loading in...5
×

Java Inheritance

  • 505 views
Uploaded on

Java, Inheritance, Frequently asked questions in technical aptitude. SCJP Dumbs.

Java, Inheritance, Frequently asked questions in technical aptitude. SCJP Dumbs.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
505
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
46
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. InheritanceDuration : 30 mins.
  • 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 optionsa 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. Q2You 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 Clasesb Overload the reproduce method in LivingThing Class.c Override the reproduce method in Plant and Animal Clasesd Either overload or override, it depends on the taste of the designer.Ans: c
  • 4. Q3An abstract method cannot be overridden.Select 1 correct option.a Trueb FalseAns: b
  • 5. Q4What 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 4b 2c It will not compile.d It will throw an exception at runtime.e None of the above.Ans:b
  • 6. Q5Consider the contents of following two files://File A.javapackage a;public class A{ A(){ } public void print(){ System.out.println("A"); }}//File B.javapackage 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. Q6Consider 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 supere 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 - MethodBb base - MethodA , sub - MethodBc Compile time error at //1d Compile time error at //2e Compile time error at //3 Ans: e
  • 9. Q8What will the following program print when run?// Filename: TestClass.javapublic 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. Q9Which of the following access control keywords can be used to enable all thesubclasses to access a method defined in the base class?Select 2 correct optionsa publicb privatec protectedd No keyword is needed.Ans:a,c
  • 11. Q10Consider the following class...class MyString extends String{ MyString(){ super(); }}The above code will not compile.Select 1 correct option.a Trueb FalseAns:a
  • 12. Q11Which of the following method definitions will prevent overriding of that method?Select 4 correct optionsa 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. Q12Given the following classes, what will be the output of compiling and running the classTruck?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. Q13What 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. Q14Consider 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, Ac It will print 0, 4d It will print 4, 4e None of the above. Ans: c
  • 16. Q15What 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 43b 7c It will not compile.d It will throw an exception at runtime.e None of the above. Ans: a