When method in a subclass has the same name and type signature as a method in the superclass that the method in subclass the method in superclass?

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

1. Which of this keyword can be used in a 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 a 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. At line number 2 in the following code, choose 3 valid data-type attributes/qualifiers among “final, static, native, public, private, abstract, protected”

  1. public interface Status
  2.    {
  3.         /* insert qualifier here */ int MY_VALUE = 10;
  4.    }

a) final, native, private
b) final, static, protected
c) final, private, abstract
d) final, static, public
View Answer

Answer: d
Explanation: Every interface variable is implicitly public static and final.

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 will be the output of the following Java program?

  1.  class Alligator 
  2.  {
  3.   public static void main(String[] args) 
  4.    {
  5.    int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
  6.    int [][]y = x;
  7.    System.out.println(y[2][1]);
  8.    }
  9.  }

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

Answer: c
Explanation: Both x,and y are pointing to the same array.

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

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

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 will be the output of the following Java program?

  1.   class Abc
  2.   {
  3.       public static void main(String[]args)
  4.       {
  5.           String[] elements = { "for", "tea", "too" };
  6.           String first = (elements.length > 0) ? elements[0]: null;
  7.       }
  8.   }

a) Compilation error
b) An exception is thrown at run time
c) The variable first is set to null
d) The variable first is set to elements[0]
View Answer

Answer: d
Explanation: The value at the 0th position will be assigned to the variable first.

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

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

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.

Next Steps:

  • Get Free Certificate of Merit in Java Programming
  • Participate in Java Programming Certification Contest
  • Become a Top Ranker in Java Programming
  • Take Java Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

When method in a subclass has the same name and type signature as a method in the superclass that the method in subclass the method in superclass?

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 & technical discussions at Telegram SanfoundryClasses.

When a method in a subclass has the same name and type as method in superclass it is known as?

Answer: When methods in the superclass and subclass have the same name and signature is called Overridden methods.

Can a subclass have a method with the same name and the same signature as its superclass?

If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.

Which of the following is the process of defining a method in subclass having same name and signature as a method in its super class?

Method overriding is when a subclass redefines a method of its superclass, of course the redefined method (of the subclass) has the same name and the same parameter types of the method of its superclass.

When a method has same signature in the base class?

If a method in a derived class has the same method signature as its base class's method, it's referred to as an overridden method. Overridden methods are discussed along with polymorphism in chapter 6.