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? Answer: a 2. What is the process of defining a method in a subclass having same name & type signature as a method in its superclass? Answer: b 3. Which of these keywords can be used to prevent Method overriding? Answer: d 4. Which of these is correct way of calling a constructor having no parameters, of superclass A by subclass B? Answer: d 5. At line number 2 in the following code, choose 3 valid data-type attributes/qualifiers among “final, static, native, public, private, abstract, protected”
a) super
b) this
c) extent
d) extends
View Answer
Explanation:
None.
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
View Answer
Explanation: None.
a) static
b) constant
c) protected
d)
final
View Answer
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.
a) super(void);
b) superclass.();
c) super.A();
d) super();
View Answer
Explanation: None.
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?
- class Alligator
- {
- public static void main(String[] args)
- {
- int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
- int [][]y = x;
- System.out.println(y[2][1]);
- }
- }
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?
- final class A
- {
- int i;
- }
- class B extends A
- {
- int j;
- System.out.println(j + " " + i);
- }
- class inheritance
- {
- public static void main(String args[])
- {
- B obj = new B();
- obj.display();
- }
- }
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?
- class Abc
- {
- public static void main(String[]args)
- {
- String[] elements = { "for", "tea", "too" };
- String first = (elements.length > 0) ? elements[0]: null;
- }
- }
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?
- class A
- {
- int i;
- public void display()
- {
- System.out.println(i);
- }
- }
- class B extends A
- {
- int j;
- public void display()
- {
- System.out.println(j);
- }
- }
- class Dynamic_dispatch
- {
- public static void main(String args[])
- {
- B obj2 = new B();
- obj2.i = 1;
- obj2.j = 2;
- A r;
- r = obj2;
- r.display();
- }
- }
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
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.