Object Oriented Programming using Java Questions and Answers – Derived Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Derived Class”.

1. What will be the output of the following Java code?

class one 
{ 
    public void print1() 
    { 
        System.out.println("Hello"); 
    } 
} 
class two extends one 
{ 
    public void print2() 
    { 
        System.out.println("World"); 
    } 
} 
class Main 
{ 
    public static void main(String[] args) 
    { 
        two g = new two();
        g.print2(); 
        g.print1(); 
    } 
}

a)

World
Hello

b)

advertisement
advertisement
Hello
World

c) Compilation Error
d)

World
World
Hello
View Answer
Answer: a
Explanation: In single inheritance, subclasses inherit the features of one superclass. In this case, class two is the subclass which inherits the properties of class ‘one’, which is the superclass. Hence, print2() method is executed first as it is called earlier than print1() method.
Output:

$ javac Derivedclass1.java
$ java Derivedclass1
World
Hello

advertisement

2. What will be the output of the following Java code?

class Super
{
    public void pfunc()
    {
        System.out.println("Parent function");
    }
}
class Sub extends Super 
{
    public void cfunc()
    {
        System.out.println("Child function");
    }
    public static void main(String[] args)
    {
        Sub obj = new Sub();
        obj.cfunc(); 
        obj.pfunc(); 
    }
}

a)

advertisement
Child function
Parent function

b)

Parent function
Child function

c) Compilation error
d) Child function
View Answer

Answer: a
Explanation: In single inheritance, subclasses inherit the features of one superclass. Single inheritance is one in which the derived class inherits the single base class. In this case, subclass Sub inherits the features of Super and calls the methods using the object of the subclass.
Output:

$ javac SubClass.java
$ java SubClass
Child function
Parent function

3. What will be the output of the following Java code?

class Pulsar
{
    String name;
}
class Bike extends Pulsar 
{
    String modelType;
    public void showDetail()
    {
        name = "Pulsar 2.0";
        modelType = "Sports";
        System.out.println(name);
    }
    public static void main(String[] args)
    {
        Bike car = new Bike();
        car.showDetail();
    }
}

a) Compilation error
b) Sports
c) Pulsar 2.0
d)

Pulsar 2.0
Sports
View Answer
Answer: c
Explanation: All the members of super class implicitly inherits to the child class. Member consists of instance variable and methods of the class. In this case, the subclass Bike uses the variables defined in the superclass Pulsar.
Output:

$ javac Bike.java
$ java Bike
Pulsar 2.0

4. What will be the output of the following Java code?

class Base
{
    String name="base";
}
class Derived extends Base 
{
    String name;
    public void details()
    { 
        name = "Derived";
        System.out.println(super.name+" and "+name);
    }
    public static void main(String[] args)
    {
        Derived obj = new Derived();
        obj.details();
    }
}

a) Derived and base
b) base and Derived
c) base and
d) Derived and
View Answer

Answer: b
Explanation: In Java, super keyword is used to refer to immediate parent class of a child class. It can be used to call parent class’ variables and methods. In this case, Base is the parent class and Derived is the child class. The variable in Base class is used in the Derived class for printing the output.
Output:

$ javac Derived.java
$ java Derived
base and Derived

5. What will be the output of the following Java code?

class Person
{
    public void show()
    {
        System.out.println("None");
    }
}
class College extends Person
{
    public void show1()
    {
        System.out.println("I am in a College");
    }
}
class School extends Person
{
    public void show2()
    {
        System.out.println("I am in a School");
    }
}
class University extends Person
{
    public void show3()
    {
        System.out.println("I am in a University");
    }
}
class HierarchicalInheritance
{
    public static void main(String args[])
    {
        School teacher = new School();
        College student = new College();
        University doctor = new University();
        student.show();
        student.show1();
        teacher.show2();
        doctor.show3();
    }
}

a)

None
I am in a College
I am in a School 
I am in a University

b) Compilation error
c)

I am in a University
I am in a School
I am in a College
None

d) None
View Answer

Answer: a
Explanation: In Hierarchical Inheritance, one class acts as a superclass (base class) for more than one subclass. More than one subclass can inherit the features of a base class. In this case, there are three subclasses – School, College and University and one base class named Person.
Output:

$ javac HierarchicalInheritance.java
$ java HierarchicalInheritance
None
I am in a College
I am in a School
I am in a University

6. What will be the output of the following Java code?

class Base
{  
    void msg()
    {
        System.out.println("Hello");
    }  
}  
class Intermediate
{  
    void msg()
    {
        System.out.println("Welcome");
    }  
}  
class Derived extends Base,Intermediate
{  
    public static void main(String args[])
    {  
        C obj=new C();  
        obj.msg();
    }  
}

a)

Hello
Welcome

b)

Welcome
Hello

c) Compilation error
d) Hello
View Answer

Answer: c
Explanation: Multiple Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class. A class can also be derived from more than one base class according to this concept. Java doesn’t allow Multiple Inheritance, hence it throws an error.
Output:

$ javac DerivedClass2.java
$ java DerivedClass2
java.lang.RuntimeException: Uncompilable source code

7. What will be the output of the following Java code?

class one 
{ 
    public void print_one() 
    { 
        System.out.print("one"); 
    } 
} 
class two extends one 
{ 
    public void print_two() 
    { 
        System.out.print("two"); 
    } 
    public void print_one() 
    { 
        System.out.print("three"); 
    } 
} 
class three extends two 
{ 
    public static void main(String[] args) 
    { 
        three obj = new three(); 
        obj.print_one(); 
        obj.print_two(); 
        obj.print_one(); 
    } 
}

a) one two one
b) two three three
c) three two three
d) two three one
View Answer

Answer:c
Explanation: The above question illustrates the concept of multilevel inheritance. When a class extends a class, which extends anther class then this is called multilevel inheritance. Here, an object is created of classtype three and all methods are called using that object.
Output:

$ javac three.java
$ java three
three two three

8. What will be the output of the following Java code?

class Line 
{
    public void print()
    {
        System.out.println("Inside display");
    }
}
class Rectangle extends Line 
{
    public void area()
    {
        System.out.println("Inside area");
    }
}
class Square extends Rectangle 
{
    public void volume() 
    {
        System.out.println("Inside volume");
    }
}
class Main 
{
    public static void main(String[] arguments) 
    {
        Square obj = new Square();
        obj.print();
    }
}

a) Inside display
b) Inside area
c) Inside volume
d) Compilation error
View Answer

Answer: a
Explanation: The above program is an example of multilevel inheritance. When a class extends a class, which extends another class then this is called multilevel inheritance. An object of class Square is created and is used to call the print() method in Class ‘Line’.
Output:

$ javac Shape.java
$ java Shape
Inside display

9. What will be the output of the following Java code?

class one 
{ 
    public void print1() 
    { 
        System.out.print("4"); 
    } 
} 
class two extends one 
{ 
    public void print2() 
    { 
        System.out.println("5"); 
    } 
} 
class three extends one 
{  
    void print()
    {
        System.out.println(" ");
    }
}
class Main 
{ 
    public static void main(String[] args) 
    { 
        three g = new three(); 
        g.print1(); 
        two t = new two(); 
        t.print2(); 
 
    } 
}

a) 45
b) 54
c) 5
d) 4
View Answer

Answer: a
Explanation: The above program is a case of hierarchical inheritance. In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class. In this case, two objects of class three and two are created and are used to call the print1() and print2() methods.
Output:

$ javac DerivedClass3.java
$ java DerivedClass3
45

10. What will be the output of the following Java code?

class Method1 
{ 
    void fun() 
    { 
        System.out.println("Parent1"); 
    } 
} 
class Method2 
{ 
    void fun() 
    { 
        System.out.println("Parent2"); 
    } 
} 
class Test extends Method1, Method2 
{ 
    public static void main(String args[]) 
    { 
        Test t = new Test(); 
        t.fun(); 
    }
}

a) Compilation error
b) Parent1
c) Parent2
d)

Parent1
Parent2
View Answer
Answer: a
Explanation: The above question uses the concept of multiple inheritance. When one class extends more than one classes, it is called multiple inheritance. Java does not support multiple inheritance and hence an error is thrown on execution.
Output:

$ javac Test.java
$ java Test
java.lang.RuntimeException: Uncompilable source code

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.