Object Oriented Programming using Java Questions and Answers – Method Overriding

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

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

class Base 
{
    public void show() 
    {
       System.out.println("Base");
    }
}
class Derived extends Base 
{
    public void show() 
    {
       System.out.println("Derived");
    }
}
public class Overriding 
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.show();
    }
}

a) Base
b) Compilation Error
c) Derived
d) Runtime Error
View Answer

Answer: c
Explanation: Here, the method show() is being overridden by the classes Derived and Base. Method overriding allows a child class to have it’s own implementation of a method provided by the parent class, hence Derived is printed.
Output:

$ javac Overriding.java
$ java Overriding
Derived

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

advertisement
advertisement
class Base 
{
    public void show(int a) 
    {
       System.out.println("1");
    }
    public void show(double a) 
    {
       System.out.println("2");
    }
}  
class Derived extends Base 
{
    public void show(int a) 
    {
       System.out.println("3");
    }
    public void show(double a) 
    {
       System.out.println("4");
    }
}  
public class Override
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.show(10.0);
    }
}

a) 3
b) 1
c) 4
d) 2
View Answer

Answer: c
Explanation: Here the method show() is being overridden and overloaded simultaneously. Method overriding allows a child class to have it’s own implementation of a method provided by the parent class, and since a double value is passed, 4 is printed.
Output:

$ javac Override.java
$ java Override
4

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

advertisement
public class Main
{   
    public String toString()
    {
        return "Obj";
    }
    public static void main(String[] args)
    {
        System.out.println(new Main());
    }
}

a) Compilation Error
b) Main@2a139a55
c) Obj
d) Runtime Error
View Answer

Answer: c
Explanation: Here, the default toString() method of the Object class, which returns the object in as a string in the format ClassName@HashCode, is being overridden by the toString() method of the Main class, and returns Obj.
Output:

advertisement
$ javac Main.java
$ java Main
Obj

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

public class A
{   
    public void toString()
    {
        System.out.println("Obj");
    }
    public static void main(String[] args)
    {
        A a = new A();
        a.toString();
    }
}

a) Compilation Error
b) Main@2a139a55
c) Obj
d) Runtime Error
View Answer

Answer: a
Explanation: Here, the default toString() method of the Object class, which returns the object in as a string in the format ClassName@HashCode, is being overridden by the toString() method of the Main class which doesn’t return any string, and hence leads to a compilation error due to incompatible datatypes.
Output:

$ javac A.java
$ java A
error: toString() in A cannot override toString() in Object
    public void toString()
                ^
  return type void is not compatible with string

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

class B
{ 
    protected final void Display() 
    { 
        System.out.println("B"); 
    } 
}  
public class C extends B 
{ 
    protected final void Display() 
    { 
        System.out.println("C"); 
    } 
    public static void main(String[] args) 
    { 
        B obj = new B(); 
        obj.Display(); 
    } 
}

a) B
b) C
c) Runtime error
d) Compilation error
View Answer

Answer: d
Explanation: Here, the function Display() of the classes B and C are being overridden. This leads to a compilation error as the function is a final function, and final functions cannot be overridden.
Output:

$ javac C.java
$ java C
error: Display() in C cannot override Display() in B
    protected final void Display() 
                         ^
  overridden method is final

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

class Derived1
{ 
    public int print() 
    { 
        System.out.println("Derived class"); 
        return 0;
    } 
} 
public class Base1 extends Derived1
{ 
    public void print() 
    { 
        System.out.println("Base class"); 
    } 
    public static void main(String[] args) 
    { 
        Derived1 obj = new Derived1(); 
        obj.print(); 
    } 
}

a) Base Class
b) Derived Class
c) Runtime error
d) Compilation error
View Answer

Answer: d
Explanation: Here, the function print() of the classes Base1 and Derived1 are being overridden. This leads to a compilation error as the functions do not have the same function signature in both cases, one of them returns an integer, while the other doesn’t return anything.
Output:

$ javac Base1.java
$ java Base1
 error: print() in Base1 cannot override print() in Derived1
    public void print() 
                ^
  return type void is not compatible with int

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

class Derived  
{ 
    public void print() 
    { 
        System.out.println("Derived class"); 
    } 
}   
public class Over extends Derived 
{ 
    protected void print() 
    { 
        System.out.println("Base class"); 
    } 
    public static void main(String[] args) 
    { 
        Derived obj = new Over();
        obj.print(); 
    } 
}

a) Base Class
b) Derived Class
c) Runtime error
d) Compilation error
View Answer

Answer: d
Explanation: Here, the function print of the classes Derived and Over are being overridden. This leads to a compilation error because the method which is overriding the print method has a more restricted access specifier than the overridden function.
Output:

$ javac Over.java
$ java Over
error: print() in Over cannot override print() in Derived
    protected void print() 
                   ^
  attempting to assign weaker access privileges; was public

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

class Derived  
{ 
    protected void print() 
    { 
        System.out.println("Derived class"); 
    } 
}   
public class Override1 extends Derived 
{ 
    public void print() 
    { 
        System.out.println("Base class"); 
    } 
    public static void main(String[] args) 
    { 
        Derived obj = new Override1();
        obj.print(); 
    } 
}

a) Base Class
b) Derived Class
c) Runtime error
d) Compilation error
View Answer

Answer: a
Explanation: Here the function print() of the classes Derived and Override1 are being overridden. As the method being overridden has a more restricted access specifier compared to that of the method overriding it, it executes without any problem and prints “Base Class”.
Output:

$ javac Override1.java
$ java Override1
Base Class

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

class Derived  
{ 
    public void display() 
    { 
        System.out.print("Derived class"); 
    } 
}   
public class OverRiding extends Derived 
{ 
    public void display() 
    { 
        System.out.print("Base class"); 
        super.display(); 
    } 
    public static void main(String[] args) 
    { 
        Derived obj = new OverRiding(); 
        obj.display(); 
    } 
}

a) Base ClassDerived Class
b) Derived ClassBase Class
c) Runtime error
d) Compilation error
View Answer

Answer: a
Explanation: Here the function display() of the classes Derived and OverRiding are being overridden. Here the method display() of the OverRiding class is calling super.display() after printing Base Class, hence the display() function of the Derived class is also called.
Output:

$ javac OverRiding.java
$ java OverRiding
Base ClassDerived Class

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

import java.io.IOException; 
 
class Derived  
{ 
    public void print() throws IOException
    { 
        System.out.println("IOE"); 
    } 
} 
public class Exceptions extends Derived 
{ 
    public void print() throws Exception
    { 
        System.out.println("Exception"); 
    } 
    public static void main(String[] args) throws IOException
    { 
        Derived obj = new Exceptions(); 
        obj.print(); 
    } 
}

a) Exception
b) IOE
c) Runtime error
d) Compilation error
View Answer

Answer: d
Explanation: Here, the method print of the classes Exceptions and Derived are being overridden. The print function of the class Exceptions throws Exception, which is a broader exception and different from IOException, hence leads to a compilation error.
Output:

$ javac Exceptions.java
$ java Exceptions
error: print() in Exceptions cannot override print() in Derived
    public void print() throws Exception
                ^
  overridden method does not throw Exception

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

class Base 
{
    public void display() 
    {
        String s = "San";
        System.out.print(s);
 
    }
}
class Derived extends Base 
{
    public void display() 
    {
        String s = "foundry";
        System.out.print(s);
        super.show();
    }
}  
public class Display 
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.display();
    }
}

a) Sanfoundry
b) foundrySan
c) San
d) Compilation Error
View Answer

Answer: a
Explanation: The fumnction display() of the classes Derived and Base are overridden in this program. The method of the class Derived is called first, and in-turn calls the method of Base, hence Sanfoundry is printed.
Output:

$ javac Display.java
$ java Display
Sanfoundry

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

class Base 
{
    public void position() 
    {
        String s = "Sanfoundry";
        System.out.println(s.charAt(3));   
    }
}
class Derived extends Base 
{
    public void position() 
    {
        String s = "JAVA";
        System.out.print(s.charAt(2));
    }
}
public class Position 
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.position();
    }
}

a) A
b) V
c) n
d) f
View Answer

Answer: b
Explanation: The position() function of the Base and Derived classes are being overridden in this program. Here, the position method of the Derived class overrides the other method and is hence executed first, thus printing the character at the third position(2nd index) of the string “JAVA”, which is ‘V’.
Output:

$ javac Position.java
$ java Position
V

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

class Base 
{
    public void show() 
    {
        System.out.println(this);   
    }
} 
class Derived extends Base 
{
    public void show() 
    {
        System.out.println(this);
    }
}  
public class PrintObject 
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.show();
    }
}

a) this
b) Derived
c) Base@2a139a55
d) Derived@2a139a55
View Answer

Answer: d
Explanation: Here the method show of the classes Derived and Base are being overridden. The show method of the Derived class overrides the method of the base class, and hence it is executed upon call. The object calling it is printed, in the form ClassName@HashCode.
Output:

$ javac PrintObject.java
$ java PrintObject
Derived@2a139a55

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

class Base 
{
    public void display() 
    {
        String s = "San";
        System.out.print(s);        
    }
} 
class Derived extends Base 
{
    public void display() 
    {
        String s = "foundry";
        System.out.print(s);
        super.show();
    }
}
public class Print 
{
    public static void main(String[] args) 
    {
        Base i = new Base();
        i.display();
    }
}

a) Sanfoundry
b) foundrySan
c) San
d) Compilation Error
View Answer

Answer: c
Explanation: The functions display of the classes Base and Derived are being overridden in this program. Here, althought the display method of the Derived class is overriding the display method of the base class, the object i is an object of the class Base, hence it calls the display method of the Base class, and thus prints “San”.
Output:

$ javac Print.java
$ java Print
San

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

class Base 
{
    private void show() 
    {
        System.out.println("A");        
    }
}  
class Derived extends Base 
{
    private void show() 
    {
        System.out.println("B");
    }
}  
public class Private 
{
    public static void main(String[] args) 
    {
        Base i = new Derived();
        i.show();
    }
}

a) A
b) B
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: Here the methods show() of the classes Base and Derived are being overridden. This leads to a compilation error as the methods have a private access specifier, and hence cannot override each other as they belong to different classes.
Output:

$ javac Private.java
$ java Private
error: show() has private access in Base
        i.show();
         ^

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.