Java Questions & Answers – Method overriding

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.
advertisement
advertisement

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”

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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.
advertisement

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?

advertisement
  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.

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.