Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 771–780 of 958
Topics in Java Programming
Q.771 Easy OOP in Java
What is the default access modifier for a class member in Java if no modifier is specified?
A public
B private
C protected
D package-private (default)
Correct Answer:  D. package-private (default)
EXPLANATION

When no access modifier is specified, the member has package-private (default) access, visible only within the same package.

Take Test
Q.772 Hard OOP in Java
What is the key difference between composition and inheritance in object design?
A Composition is always better than inheritance
B Inheritance represents 'is-a' relationship; composition represents 'has-a' relationship
C Composition cannot access private members
D Inheritance is only for abstract classes
Correct Answer:  B. Inheritance represents 'is-a' relationship; composition represents 'has-a' relationship
EXPLANATION

Inheritance models an 'is-a' relationship (Dog is-an Animal), while composition models a 'has-a' relationship (Car has-an Engine). Composition is often preferred for flexibility.

Take Test
Q.773 Hard OOP in Java
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
A Method Overloading
B Method Overriding with Covariant Return Types
C Runtime Polymorphism
D Method Hiding
Correct Answer:  B. Method Overriding with Covariant Return Types
EXPLANATION

Covariant return types (Java 5+) allow a method to return a subtype of the parent class method's return type. For example, if parent returns Animal, child can return Dog (subclass of Animal).

Take Test
Q.774 Hard OOP in Java
What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?
A Child constructor -> Parent constructor -> Grandparent constructor
B Grandparent constructor -> Parent constructor -> Child constructor
C All constructors execute simultaneously
D Only the child constructor executes
Correct Answer:  B. Grandparent constructor -> Parent constructor -> Child constructor
EXPLANATION

Constructor execution follows the inheritance chain from top to bottom. super() is implicitly called, executing parent constructors first.

Take Test
Q.775 Hard OOP in Java
In a real-world e-commerce system, you want to prevent direct instantiation of a base Product class but allow creation of Laptop, Mobile, and Tablet. Which approach is best?
A Make Product class final
B Make Product class abstract with abstract methods for subclasses to implement
C Make Product class private
D Use an interface for Product
Correct Answer:  B. Make Product class abstract with abstract methods for subclasses to implement
EXPLANATION

An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.

Take Test
Q.776 Hard OOP in Java
What will be the compilation result of this code?
interface A { void method(); }
interface B { void method(); }
class C implements A, B {
public void method() { }
}
A Compilation error: ambiguous method
B Compiles successfully
C Runtime error
D Compilation error: method signature mismatch
Correct Answer:  B. Compiles successfully
EXPLANATION

Java allows implementing multiple interfaces even if they have the same method signature. The class provides a single implementation that satisfies both interfaces. This compiles successfully.

Take Test
Q.777 Hard OOP in Java
Design scenario: You need to create a payment system where Credit Card, Debit Card, and UPI are payment methods. What's the best OOP approach?
A Create a concrete PaymentCard class with if-else for each type
B Create an interface PaymentMethod with implementations for each type
C Use a single abstract method for all payment types
D Use static methods to handle all payment types
Correct Answer:  B. Create an interface PaymentMethod with implementations for each type
EXPLANATION

Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.

Take Test
Q.778 Hard OOP in Java
Which of the following best demonstrates the Liskov Substitution Principle in OOP?
A Child class can override parent methods with different return types
B Child class objects should be usable wherever parent class objects are expected
C Parent class should not know about child class
D Child class should inherit all methods from parent
Correct Answer:  B. Child class objects should be usable wherever parent class objects are expected
EXPLANATION

The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Option B correctly represents this principle.

Take Test
Q.779 Medium OOP in Java
What is the output of this code?
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(x++);
}
}
A 10
B 11
C Compilation error
D Runtime exception
Correct Answer:  A. 10
EXPLANATION

The post-increment operator (x++) returns the value before incrementing. So it prints 10, and then x becomes 11.

Take Test
Q.780 Medium OOP in Java
Consider a class hierarchy: Animal -> Dog -> Puppy. A Puppy reference can access which of the following?
A Only Puppy methods
B Puppy and Dog methods, but not Animal methods
C Puppy, Dog, and Animal methods
D Only methods declared as public
Correct Answer:  C. Puppy, Dog, and Animal methods
EXPLANATION

Through inheritance, a Puppy reference can access all non-private methods from Puppy, Dog, and Animal classes through the inheritance chain.

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