Java Questions & Answers – Method overriding

This section of our 1000+ Java MCQs focuses on method overriding in Java Programming Language.

1. Which of these keyword can be used in subclass to call the constructor of superclass?
a) super
b) this
c) extent
d) extends
View Answer

Answer: a
Explanation: None.

2. What is the process of defining a method in subclass having same name & type signature as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

3. Which of these keywords can be used to prevent Method overriding?
a) static
b) constant
c) protected
d) final
View Answer

Answer: d
Explanation: To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.

4. Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B?
a) super(void);
b) superclass.();
c) super.A();
d) super();
View Answer

Answer: d
Explanation: None.

5. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program.
b) private members of class can only be accessed by other members of the class.
c) private members of class can be inherited by a sub class, and become protected members in sub class.
d) protected members of a class can be inherited by a sub class, and become private members of the sub class.
View Answer

Answer: c
Explanation: private members of a class cannot be inherited by a sub class.

6. Which of these is supported by method overriding in Java?
a) Abstraction
b) Encapsulation
c) Polymorphism
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

7. What is the output of this program?

  1.     class A {
  2.         int i;
  3.         void display() {
  4.             System.out.println(i);
  5.         }
  6.     }    
  7.     class B extends A {
  8.         int j;
  9.         void display() {
  10.             System.out.println(j);
  11.         }
  12.     }    
  13.     class method_overriding {
  14.         public static void main(String args[])
  15.         {
  16.             B obj = new B();
  17.             obj.i=1;
  18.             obj.j=2;   
  19.             obj.display();     
  20.         }
  21.    }

a) 0
b) 1
c) 2
d) Compilation Error
View Answer

Answer: c
Explanation: class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
output:
$ javac method_overriding.java
$ java method_overriding
2

8. What is the output of this program?

  1.    final class A {
  2.          int i;
  3.     }    
  4.     class B extends A {
  5.         int j;
  6.         System.out.println(j + " " + i);  
  7.     }    
  8.     class inheritance {
  9.         public static void main(String args[])
  10.         {
  11.             B obj = new B();
  12.             obj.display();     
  13.         }
  14.    }

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.
output:
$ javac inheritance.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
i cannot be resolved or is not a field

9. What is the output of this program?

  1.     class A {
  2.         public int i;
  3.         private int j;
  4.     }    
  5.     class B extends A {
  6.         void display() {
  7.             super.j = super.i + 1;
  8.             System.out.println(super.i + " " + super.j);
  9.         }
  10.     }    
  11.     class inheritance {
  12.         public static void main(String args[])
  13.         {
  14.             B obj = new B();
  15.             obj.i=1;
  16.             obj.j=2;   
  17.             obj.display();     
  18.         }
  19.    }

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:
$ javac inheritance.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field A.j is not visible

9. What is the output of this program?

  1.     class A {
  2.         public void display() {
  3.             System.out.println("A");
  4.         }    
  5.     }    
  6.     class B extends A {
  7.         public void display() {
  8.             System.out.println("B");
  9.         } 
  10.     }    
  11.     class Dynamic_dispatch {
  12.         public static void main(String args[])
  13.         {
  14.             A obj1 = new A();
  15.             B obj2 = new B();
  16.             A r;
  17.             r = obj1;
  18.             r.display();
  19.             r = obj2;
  20.             r.display();     
  21.         }
  22.    }

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

Answer: a
Explanation: Call to display function of class A and class B is made by using dynamic method dispatch, by using this method a call to an overridden function is resolved at run time, rather than at compilation time.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
A B

10. What is the output of this program?

  1.     class A {
  2.         int i;
  3.         public void display() {
  4.             System.out.println(i);
  5.         }    
  6.     }    
  7.     class B extends A {
  8.         int j;
  9.         public void display() {
  10.             System.out.println(j);
  11.         } 
  12.     }    
  13.     class Dynamic_dispatch {
  14.         public static void main(String args[])
  15.         {
  16.             B obj2 = new B();
  17.             obj2.i = 1;
  18.             obj2.j = 2;
  19.             A r;
  20.             r = obj2;
  21.             r.display();     
  22.         }
  23.    }

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

Answer: b
Explanation: r is reference of type A, the program assigns a reference of object obj2 to r and uses that reference to call function display() of class B.
output:
$ javac Dynamic_dispatch.java
$ java Dynamic_dispatch
2

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry