Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 791–800 of 958
Topics in Java Programming
Q.791 Hard OOP in Java
What is the output of the following code?
interface I1 { default void show() { System.out.println("I1"); } }
interface I2 { default void show() { System.out.println("I2"); } }
class C implements I1, I2 { public void show() { I1.super.show(); } public static void main(String[] args) { new C().show(); } }
A I1
B I2
C Compile-time error due to ambiguity
D I1 I2
Correct Answer:  A. I1
EXPLANATION

When a class implements multiple interfaces with the same default method, it must explicitly override the method. Using I1.super.show() calls I1's default implementation.

Take Test
Q.792 Medium OOP in Java
Which of the following correctly demonstrates composition over inheritance?
A class Engine { } class Car extends Engine { }
B class Engine { } class Car { Engine e = new Engine(); }
C Both approaches are equally good
D Composition is never preferred in Java
Correct Answer:  B. class Engine { } class Car { Engine e = new Engine(); }
EXPLANATION

Composition (HAS-A relationship) is often preferred over inheritance (IS-A relationship) because it's more flexible and avoids tight coupling.

Take Test
Q.793 Hard OOP in Java
What happens with exception handling in method overriding?
A Child class method can throw any exception
B Child class method can only throw checked exceptions, not unchecked
C Child class method can throw the same or narrower (subclass) exceptions only
D Exception handling rules don't apply to overridden methods
Correct Answer:  C. Child class method can throw the same or narrower (subclass) exceptions only
EXPLANATION

Liskov Substitution Principle: A child class method can throw the same exception or a narrower exception than the parent method, not a broader one.

Take Test
Q.794 Hard OOP in Java
A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?
A Use interface because it's more flexible
B Use abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
C Use both abstract class and interface together
D Neither, use a concrete class
Correct Answer:  B. Use abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
EXPLANATION

Abstract classes are suitable when you have shared state and constructors needed. An Account has properties like balance and account number, making abstract class the better choice.

Take Test
Q.795 Medium OOP in Java
Which of the following is true about interface default methods?
A They must be implemented by all implementing classes
B They provide a default implementation that can be overridden or inherited
C They are the same as abstract methods
D Only one interface can have default methods
Correct Answer:  B. They provide a default implementation that can be overridden or inherited
EXPLANATION

Default methods in interfaces (Java 8+) provide a default implementation. Implementing classes can use this default implementation or override it as needed.

Take Test
Q.796 Hard OOP in Java
What does the following code output?
class A { static void display() { System.out.println("A"); } }
class B extends A { static void display() { System.out.println("B"); } }
public class Test { public static void main(String[] args) { A ref = new B(); ref.display(); } }
A A
B B
C Compile-time error
D Runtime error
Correct Answer:  A. A
EXPLANATION

Static methods are resolved at compile-time based on the reference type, not the object type. Hence, A.display() is called, not B.display(). This is method hiding, not overriding.

Take Test
Q.797 Medium OOP in Java
What is the relationship between an abstract class and an interface in Java 8+?
A Abstract classes and interfaces are identical
B Interfaces can have default and static methods, while abstract classes can have instance variables and constructors
C Only abstract classes can be extended
D Interfaces are always preferred over abstract classes
Correct Answer:  B. Interfaces can have default and static methods, while abstract classes can have instance variables and constructors
EXPLANATION

From Java 8, interfaces can have default and static methods, but abstract classes can have instance variables, constructors, and private methods which interfaces cannot have.

Take Test
Q.798 Medium OOP in Java
Consider a scenario where class B extends class A. If class A has a constructor with parameters, what must class B do?
A Ignore the parent constructor completely
B Must explicitly call parent constructor using super()
C Parent constructor is automatically called without any action
D Class B becomes invalid and cannot be compiled
Correct Answer:  B. Must explicitly call parent constructor using super()
EXPLANATION

If a parent class has a parameterized constructor and no default constructor, the child class must explicitly call it using super() to initialize parent class members.

Take Test
Q.799 Medium OOP in Java
What is the difference between 'this' and 'super' keywords?
A 'this' refers to parent class and 'super' refers to current class
B 'this' refers to current class instance and 'super' refers to parent class
C They are interchangeable
D 'super' is used only in abstract classes
Correct Answer:  B. 'this' refers to current class instance and 'super' refers to parent class
EXPLANATION

'this' is a reference to the current object instance, while 'super' is a reference to the parent class. They serve different purposes in inheritance hierarchy.

Take Test
Q.800 Medium OOP in Java
Which of the following statements about the 'final' keyword is correct?
A A final class can be extended
B A final method can be overridden
C A final variable's value cannot be changed after initialization
D A final class can implement multiple interfaces
Correct Answer:  C. A final variable's value cannot be changed after initialization
EXPLANATION

The 'final' keyword when applied to a variable makes it a constant. When applied to a class, it cannot be extended. When applied to a method, it cannot be overridden.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips