Object-Oriented Programming (OOP) Interview Questions Hindi mein

Object-Oriented Programming (OOP) कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Classes and Objects

1. Which list names the four commonly cited pillars of OOP?

  1. Encapsulation, abstraction, inheritance, polymorphism
  2. Variables, loops, functions, arrays
  3. Compilation, linking, loading, execution
  4. Classes, files, packages, modules
जवाब देखें

जवाब: Encapsulation, abstraction, inheritance, polymorphism

These four principles are the classic foundation of object-oriented design.

पाठ: Why Object-Oriented Programming

2. After `Student b = a;` in Java, what does b refer to?

  1. A new copy of the object
  2. Nothing until it is initialised
  3. The Student class itself
  4. The same object that a refers to
जवाब देखें

जवाब: The same object that a refers to

Assignment copies the reference, so both variables point to one object.

पाठ: Classes, Objects, State and Identity

3. What happens to Java's default no-argument constructor once you write any constructor for a class?

  1. It is still generated automatically
  2. It becomes static
  3. It is no longer provided; you must write it yourself if you need it
  4. It becomes private
जवाब देखें

जवाब: It is no longer provided; you must write it yourself if you need it

Java only supplies the default constructor when the class declares no constructors.

पाठ: Constructors, this and Static Members

Encapsulation and Abstraction

4. Which Java access modifier makes a member visible only inside its own class?

  1. public
  2. protected
  3. private
  4. package-private (no modifier)
जवाब देखें

जवाब: private

private restricts access to the declaring class.

पाठ: Encapsulation and Access Modifiers

5. Why does an immutable class make defensive copies of mutable constructor arguments such as lists?

  1. To save memory
  2. So the caller cannot change the object's state later through the list it still holds
  3. Because Java requires it
  4. To make the class faster
जवाब देखें

जवाब: So the caller cannot change the object's state later through the list it still holds

Without a copy, the caller's reference to the same list could modify the object.

पाठ: Immutable Objects

6. Which is true about Java interfaces and abstract classes?

  1. A class can implement several interfaces but extend only one class
  2. A class can extend several abstract classes
  3. Interfaces can hold instance fields
  4. Abstract classes can be instantiated with new
जवाब देखें

जवाब: A class can implement several interfaces but extend only one class

Java supports single class inheritance and multiple interface implementation.

पाठ: Abstraction, Abstract Classes and Interfaces

Inheritance

7. In a Java subclass constructor, where must an explicit call to super(...) appear?

  1. As the first statement
  2. Anywhere in the constructor
  3. As the last statement
  4. Only in static methods
जवाब देखें

जवाब: As the first statement

The parent must be initialised before the subclass constructor body runs, so super(...) comes first.

पाठ: Inheritance, super and Constructor Chaining

8. Which change is allowed when overriding a public method in Java?

  1. Making the override private
  2. Changing the parameter types
  3. Throwing a broader checked exception
  4. Returning a subtype of the original return type
जवाब देखें

जवाब: Returning a subtype of the original return type

Covariant return types are allowed; narrower access, different parameters or broader checked exceptions are not.

पाठ: Method Overriding Rules

9. How does Java handle a class that implements two interfaces with conflicting default methods?

  1. It picks one at random
  2. It is a runtime error
  3. The class must override the method and resolve the conflict explicitly
  4. The first interface listed always wins
जवाब देखें

जवाब: The class must override the method and resolve the conflict explicitly

The compiler requires the class to override and may call InterfaceName.super.method().

पाठ: When Inheritance Goes Wrong

Polymorphism

10. Which pair of Java methods is a valid overload?

  1. int total(int a) and double total(int a)
  2. void run() and private void run()
  3. void log(String s) and void log(String s, int level)
  4. static void x() and void x()
जवाब देखें

जवाब: void log(String s) and void log(String s, int level)

Overloads must differ in their parameter lists; return type or modifiers alone are not enough.

पाठ: Compile-Time Polymorphism: Overloading

11. When is the method body to execute chosen for an overridden method called through a superclass reference in Java?

  1. At compile time, from the reference's declared type
  2. At run time, from the actual object's class
  3. When the class is loaded
  4. It is chosen randomly
जवाब देखें

जवाब: At run time, from the actual object's class

Dynamic dispatch selects the implementation of the object's actual class at run time.

पाठ: Run-Time Polymorphism and Dynamic Dispatch

12. What is duck typing?

  1. An object is usable if it has the required methods, regardless of its declared type
  2. A class must declare which interfaces it implements
  3. Only primitive types can be used
  4. A naming convention for Python classes
जवाब देखें

जवाब: An object is usable if it has the required methods, regardless of its declared type

Duck typing checks behaviour (available methods) rather than declared type.

पाठ: Programming to Interfaces and Duck Typing

Relationships Between Classes

13. An Order creates its OrderLine objects, and they cannot exist without it. Which relationship is this?

  1. Composition
  2. Association
  3. Aggregation
  4. Dependency
जवाब देखें

जवाब: Composition

Strong ownership with a shared lifetime is composition.

पाठ: Association, Aggregation and Composition

14. What is a main advantage of composition over inheritance?

  1. It always runs faster
  2. It removes the need for interfaces
  3. It makes all fields public
  4. Behaviour can be combined and changed by swapping collaborators, with less coupling to internals
जवाब देखें

जवाब: Behaviour can be combined and changed by swapping collaborators, with less coupling to internals

Composition depends on interfaces of parts, which can be swapped and combined flexibly.

पाठ: Composition Over Inheritance and Delegation

15. In UML, how is composition shown?

  1. A dashed line with a hollow triangle
  2. A solid line with a hollow triangle
  3. A solid line with a filled diamond at the whole's end
  4. A dashed arrow
जवाब देखें

जवाब: A solid line with a filled diamond at the whole's end

Composition uses a filled diamond; aggregation uses a hollow one.

पाठ: UML Class Diagrams

SOLID Design Principles

16. According to the Open/Closed Principle, how should a new payment method usually be added?

  1. Edit a large switch statement in existing code
  2. Copy the whole payment module
  3. Add a new class implementing an existing interface
  4. Make all fields public
जवाब देखें

जवाब: Add a new class implementing an existing interface

OCP favours extension through new implementations rather than modifying tested code.

पाठ: Single Responsibility and Open/Closed

17. Which is a sign of a Liskov Substitution Principle violation?

  1. A subclass adds a new helper method
  2. A subclass throws UnsupportedOperationException for a method the parent supports
  3. A subclass overrides toString
  4. A class implements two interfaces
जवाब देखें

जवाब: A subclass throws UnsupportedOperationException for a method the parent supports

Refusing behaviour the parent promises breaks substitutability.

पाठ: Liskov Substitution and Interface Segregation

18. What does the Dependency Inversion Principle recommend?

  1. Both high-level and low-level modules should depend on abstractions
  2. High-level modules should create low-level modules directly
  3. All classes should be static
  4. Avoid interfaces to reduce code
जवाब देखें

जवाब: Both high-level and low-level modules should depend on abstractions

DIP inverts the dependency so business logic depends on interfaces, not concrete details.

पाठ: Dependency Inversion and Dependency Injection

Object Behaviour: Equality, Lifecycle and Errors

19. If a Java class overrides equals but not hashCode, what typically goes wrong?

  1. Equal objects may land in different hash buckets, so HashSet and HashMap lookups fail
  2. The class no longer compiles
  3. equals stops being called
  4. Objects become immutable
जवाब देखें

जवाब: Equal objects may land in different hash buckets, so HashSet and HashMap lookups fail

The equals-hashCode contract requires equal objects to have equal hash codes.

पाठ: Identity, Equality and Hashing

20. Two Team objects were created by a shallow copy. What happens if one adds a member to its members list?

  1. Only that team changes
  2. A compile error occurs
  3. The garbage collector removes the list
  4. Both teams see the new member, because they share the same list
जवाब देखें

जवाब: Both teams see the new member, because they share the same list

Shallow copies share referenced objects such as the list.

पाठ: Object Lifecycle, Copying and Resources

21. Which exception best signals that a method was called while the object is in the wrong state?

  1. IllegalArgumentException
  2. ArithmeticException
  3. IllegalStateException
  4. ClassNotFoundException
जवाब देखें

जवाब: IllegalStateException

IllegalStateException indicates the call is not valid for the object's current state.

पाठ: Exceptions and Design by Contract

Object-Oriented Design in Practice and Revision

22. In the parking lot design, why is pricing modelled as an interface?

  1. Interfaces are faster than classes
  2. Java requires interfaces for numbers
  3. Pricing rules vary, so an interface lets new strategies be added without changing ParkingLot
  4. To make Vehicle abstract
जवाब देखें

जवाब: Pricing rules vary, so an interface lets new strategies be added without changing ParkingLot

Variation points behind interfaces follow the Open/Closed and Dependency Inversion principles.

पाठ: From Requirements to Classes

23. Which language implements interfaces structurally, so a type satisfies an interface just by having its methods?

  1. Java
  2. Go
  3. C#
  4. C++
जवाब देखें

जवाब: Go

Go interfaces are satisfied implicitly by any type with the required method set.

पाठ: OOP Across Languages

24. What is an anaemic domain model?

  1. Classes holding data with getters and setters while business rules live elsewhere, so objects cannot protect their invariants
  2. A model with too many methods
  3. A database with no tables
  4. A model that uses only static classes for performance
जवाब देखें

जवाब: Classes holding data with getters and setters while business rules live elsewhere, so objects cannot protect their invariants

Separating all behaviour from data loses the main benefit of encapsulation.

पाठ: Common OOP Mistakes

25. Which concept allows a superclass reference to call the subclass's version of a method at run time?

  1. Method overloading
  2. Encapsulation
  3. Static binding of fields
  4. Run-time polymorphism through overriding (dynamic dispatch)
जवाब देखें

जवाब: Run-time polymorphism through overriding (dynamic dispatch)

Dynamic dispatch chooses the overriding method of the actual object at run time.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ