0% found this document useful (0 votes)
67 views10 pages

100+ OOP Interview Questions

The document contains over 100 object-oriented programming (OOP) interview questions categorized into basic, intermediate, and advanced levels. Key concepts covered include OOP principles, design patterns, and important distinctions such as encapsulation, inheritance, and polymorphism. Additionally, it provides tips for interview success, emphasizing the importance of understanding fundamentals and practicing coding.

Uploaded by

gargprakhar2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views10 pages

100+ OOP Interview Questions

The document contains over 100 object-oriented programming (OOP) interview questions categorized into basic, intermediate, and advanced levels. Key concepts covered include OOP principles, design patterns, and important distinctions such as encapsulation, inheritance, and polymorphism. Additionally, it provides tips for interview success, emphasizing the importance of understanding fundamentals and practicing coding.

Uploaded by

gargprakhar2016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

100+ Object-Oriented Programming Interview Questions

Basic Level Questions (1-30)

1. What is Object-Oriented Programming (OOP)?


Object-Oriented Programming is a programming paradigm that organizes software design around data
(objects) rather than functions and logic. It's based on the concept of "objects" that contain data and
code.

2. What are the four main principles of OOP?


Encapsulation: Bundling data and methods together

Inheritance: Creating new classes based on existing ones

Polymorphism: Using one interface for different underlying forms

Abstraction: Hiding complex implementation details

3. What is a class?
A class is a blueprint or template for creating objects. It defines the attributes (data) and methods
(functions) that objects of that type will have.

4. What is an object?
An object is an instance of a class. It's a concrete entity that has state (attributes) and behavior (methods).

5. What is the difference between a class and an object?


Class: A template or blueprint

Object: An actual instance created from the class

6. What is encapsulation?
Encapsulation is the bundling of data and methods that operate on that data within a single unit (class),
and restricting access to the internal state of an object.

7. What are access modifiers?


Access modifiers control the visibility and accessibility of class members:

Public: Accessible from anywhere

Private: Accessible only within the same class


Protected: Accessible within the same class and its subclasses

8. What is inheritance?
Inheritance is a mechanism where a new class inherits properties and methods from an existing class,
promoting code reusability.

9. What is a constructor?
A constructor is a special method that is automatically called when an object is created. It's used to
initialize the object's state.

10. What is a destructor?


A destructor is a special method that is automatically called when an object is destroyed or goes out of
scope. It's used for cleanup operations.
11. What is method overloading?
Method overloading is defining multiple methods with the same name but different parameters (different
number or types of parameters).

12. What is method overriding?


Method overriding is redefining a method in a derived class that was already defined in the base class.

13. What is polymorphism?


Polymorphism allows objects of different classes to be treated as objects of a common base class,
enabling a single interface to represent different underlying forms.

14. What is abstraction?


Abstraction is the process of hiding complex implementation details while showing only the essential
features of an object.

15. What is an abstract class?


An abstract class is a class that cannot be instantiated directly and typically contains one or more abstract
methods that must be implemented by subclasses.

16. What is an interface?


An interface is a contract that defines a set of method signatures that implementing classes must provide.

17. What is the difference between abstract class and interface?


Abstract Class: Can have implemented methods, constructors, and instance variables
Interface: Only method signatures (traditionally), no implementation

18. What is composition?


Composition is a design principle where a class contains objects of other classes as part of its state,
representing a "has-a" relationship.

19. What is aggregation?


Aggregation is a special form of association where objects have their own lifecycle but there's ownership,
representing a "has-a" relationship with weaker coupling than composition.

20. What is association?


Association represents a relationship between two or more objects where objects have their own lifecycle
and there's no ownership.

21. What is the difference between composition and inheritance?


Composition: "Has-a" relationship, more flexible
Inheritance: "Is-a" relationship, creates tight coupling

22. What is a static method?


A static method belongs to the class rather than any instance and can be called without creating an
object of the class.

23. What is a static variable?


A static variable belongs to the class and is shared among all instances of the class.
24. What is method chaining?
Method chaining is a technique where multiple method calls are chained together in a single statement,
with each method returning an object.

25. What is the difference between early binding and late binding?
Early Binding: Method calls are resolved at compile time

Late Binding: Method calls are resolved at runtime (dynamic binding)

26. What is a virtual function?


A virtual function is a function that can be overridden in derived classes and supports runtime
polymorphism.

27. What is multiple inheritance?


Multiple inheritance is a feature where a class can inherit from more than one base class.

28. What is the diamond problem?


The diamond problem occurs in multiple inheritance when a class inherits from two classes that have a
common base class, leading to ambiguity.

29. What is a friend function?


A friend function is a function that has access to private and protected members of a class, even though
it's not a member of that class.

30. What is the difference between public, private, and protected inheritance?
Public: Public and protected members remain public and protected

Private: Public and protected members become private


Protected: Public and protected members become protected

Intermediate Level Questions (31-70)

31. Explain the SOLID principles.


S: Single Responsibility Principle
O: Open/Closed Principle

L: Liskov Substitution Principle

I: Interface Segregation Principle

D: Dependency Inversion Principle

32. What is the Single Responsibility Principle?


A class should have only one reason to change, meaning it should have only one job or responsibility.

33. What is the Open/Closed Principle?


Software entities should be open for extension but closed for modification.

34. What is the Liskov Substitution Principle?


Objects of a superclass should be replaceable with objects of its subclasses without breaking the
application.
35. What is the Interface Segregation Principle?

Clients should not be forced to depend on interfaces they don't use.

36. What is the Dependency Inversion Principle?


High-level modules should not depend on low-level modules. Both should depend on abstractions.

37. What is a design pattern?


A design pattern is a reusable solution to a commonly occurring problem in software design.

38. Explain the Singleton pattern.


Singleton ensures that a class has only one instance and provides global access to that instance.

39. Explain the Factory pattern.


Factory pattern creates objects without specifying the exact class to create, using a factory method.

40. Explain the Observer pattern.


Observer pattern defines a one-to-many dependency between objects so that when one object changes
state, all dependents are notified.

41. What is the Strategy pattern?


Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

42. What is the Decorator pattern?


Decorator pattern allows behavior to be added to objects dynamically without altering their structure.

43. What is the difference between Association, Aggregation, and Composition?


Association: Loose relationship, independent lifecycles

Aggregation: "Has-a" relationship, independent lifecycles

Composition: "Part-of" relationship, dependent lifecycles

44. What is coupling and cohesion?


Coupling: Degree of interdependence between modules (should be low)

Cohesion: Degree to which elements within a module work together (should be high)

45. What is the difference between overloading and overriding?


Overloading: Same method name, different parameters, compile-time
Overriding: Same method signature, runtime polymorphism

46. What is dynamic binding?


Dynamic binding is the process of linking method calls to method definitions at runtime rather than
compile time.

47. What is a pure virtual function?


A pure virtual function is a virtual function that has no implementation in the base class and must be
implemented by derived classes.

48. What is an abstract method?


An abstract method is a method declared without implementation that must be implemented by
subclasses.

49. Can you override a static method?


No, static methods cannot be overridden because they belong to the class, not instances.

50. What is method hiding?


Method hiding occurs when a subclass defines a static method with the same signature as a static
method in the parent class.

51. What is the difference between interface and abstract class in Java?
Interface: Multiple inheritance, only method signatures (before Java 8)

Abstract Class: Single inheritance, can have implemented methods

52. What is runtime polymorphism?


Runtime polymorphism is achieved through method overriding and virtual functions, where the actual
method called is determined at runtime.

53. What is compile-time polymorphism?


Compile-time polymorphism is achieved through method overloading and operator overloading,
resolved at compile time.

54. What is the difference between shallow copy and deep copy?
Shallow Copy: Copies object references, not the actual objects

Deep Copy: Creates new objects for all referenced objects

55. What is object cloning?


Object cloning is the process of creating an exact copy of an object.

56. What is the difference between aggregation and composition?


Aggregation: Objects can exist independently

Composition: Objects cannot exist without the parent object

57. What is a nested class?


A nested class is a class defined inside another class.

58. What is an inner class?


An inner class is a non-static nested class that has access to instance variables of the outer class.

59. What is the difference between method overloading and method overriding?
Overloading: Same name, different parameters, compile-time

Overriding: Same signature, different implementation, runtime

60. What is the purpose of the 'this' keyword?


The 'this' keyword refers to the current instance of the class and is used to access instance variables and
methods.

61. What is the purpose of the 'super' keyword?


The 'super' keyword refers to the parent class and is used to access parent class methods and
constructors.

62. What is constructor chaining?


Constructor chaining is the process of calling one constructor from another constructor in the same class
or parent class.

63. Can constructors be overloaded?


Yes, constructors can be overloaded by having different parameter lists.

64. Can constructors be inherited?


No, constructors are not inherited, but they can be called using the super() keyword.

65. What is a copy constructor?


A copy constructor is a constructor that creates an object by copying another object of the same class.

66. What is operator overloading?


Operator overloading allows operators to be redefined for user-defined types.

67. What is function overloading?


Function overloading is defining multiple functions with the same name but different parameters.

68. What is the difference between public and private inheritance?


Public: IS-A relationship maintained

Private: HAS-A relationship, inheritance details hidden

69. What is virtual inheritance?


Virtual inheritance is used to solve the diamond problem in multiple inheritance.

70. What is the difference between virtual and pure virtual functions?
Virtual: Can have implementation, can be overridden

Pure Virtual: No implementation, must be overridden

Advanced Level Questions (71-110)

71. Explain the Template Method pattern.


Template Method defines the skeleton of an algorithm in a base class, letting subclasses override specific
steps without changing the algorithm's structure.

72. What is the Command pattern?


Command pattern encapsulates a request as an object, allowing you to parameterize clients with different
requests and queue operations.

73. Explain the State pattern.


State pattern allows an object to alter its behavior when its internal state changes, appearing as if the
object changed its class.

74. What is the Proxy pattern?


Proxy pattern provides a placeholder or surrogate for another object to control access to it.
75. Explain the Adapter pattern.
Adapter pattern allows incompatible interfaces to work together by providing a wrapper that translates
one interface to another.

76. What is the Bridge pattern?


Bridge pattern separates abstraction from implementation, allowing both to vary independently.

77. What is the Composite pattern?


Composite pattern composes objects into tree structures to represent part-whole hierarchies, treating
individual objects and compositions uniformly.

78. What is the Facade pattern?


Facade pattern provides a simplified interface to a complex subsystem.

79. What is the Flyweight pattern?


Flyweight pattern minimizes memory usage by sharing common parts of state between multiple objects.

80. What is the Chain of Responsibility pattern?


Chain of Responsibility pattern passes requests along a chain of handlers until one handles the request.

81. What is the Interpreter pattern?


Interpreter pattern defines a representation for a language's grammar and an interpreter to interpret
sentences in the language.

82. What is the Iterator pattern?


Iterator pattern provides a way to access elements of a collection sequentially without exposing its
underlying representation.

83. What is the Mediator pattern?


Mediator pattern defines how objects interact with each other, promoting loose coupling by keeping
objects from referring to each other explicitly.

84. What is the Memento pattern?


Memento pattern captures and externalizes an object's internal state so it can be restored later without
violating encapsulation.

85. What is the Prototype pattern?


Prototype pattern creates objects by cloning existing instances rather than creating new ones from
scratch.

86. What is the Visitor pattern?


Visitor pattern represents operations to be performed on elements of an object structure, allowing new
operations without changing the classes.

87. What is dependency injection?


Dependency injection is a design pattern where dependencies are provided to an object rather than the
object creating them itself.
88. What is inversion of control?

Inversion of control is a principle where the control of object creation and management is transferred
from the application code to a framework or container.

89. What is the difference between abstract factory and factory method?
Factory Method: Creates objects through inheritance
Abstract Factory: Creates families of related objects through composition

90. What is the Law of Demeter?


The Law of Demeter states that an object should only communicate with its immediate friends and not
with strangers.

91. What is the difference between composition and aggregation in terms of memory
management?
Composition: Parent manages child object lifecycle

Aggregation: Child objects manage their own lifecycle

92. How does garbage collection work in OOP languages?


Garbage collection automatically manages memory by identifying and deallocating objects that are no
longer reachable or referenced.

93. What is the difference between stack and heap memory in OOP?
Stack: Stores local variables and method calls, managed automatically

Heap: Stores objects, managed by garbage collector

94. What is object pooling?


Object pooling is a design pattern that reuses objects instead of creating new ones, improving
performance and memory usage.

95. What is the difference between tight coupling and loose coupling?
Tight Coupling: High dependency between modules, hard to maintain
Loose Coupling: Low dependency, easier to maintain and test

96. What is aspect-oriented programming?


Aspect-oriented programming is a paradigm that increases modularity by separating cross-cutting
concerns like logging, security, and transactions.

97. What is reflection in OOP?


Reflection is the ability of a program to examine and modify its own structure and behavior at runtime.

98. What is serialization?


Serialization is the process of converting an object into a format that can be stored or transmitted and
later reconstructed.

99. What is the difference between composition and delegation?


Composition: Object contains other objects

Delegation: Object forwards method calls to another object


100. What is the principle of least privilege in OOP?

The principle of least privilege states that objects should only have the minimum access rights needed to
perform their function.

101. What is the difference between static and dynamic loading?


Static Loading: Classes loaded at compile time
Dynamic Loading: Classes loaded at runtime as needed

102. What is method dispatch?


Method dispatch is the mechanism by which the correct method implementation is selected and called at
runtime.

103. What is the difference between early binding and late binding in method calls?
Early Binding: Method calls resolved at compile time

Late Binding: Method calls resolved at runtime

104. What is the role of interfaces in achieving multiple inheritance?


Interfaces allow a class to inherit method signatures from multiple sources, providing a form of multiple
inheritance.

105. What is the difference between inheritance and interface implementation?


Inheritance: Inherits both interface and implementation

Interface Implementation: Only inherits the interface contract

106. How do you handle the fragile base class problem?


Use composition over inheritance, design stable interfaces, and follow the Liskov Substitution Principle.

107. What is the difference between abstract methods and virtual methods?
Abstract Methods: Must be implemented in derived classes

Virtual Methods: Can be overridden but have default implementation

108. What is the role of constructors in inheritance?


Constructors are not inherited but can be called from derived class constructors using super() or similar
mechanisms.

109. How do you implement the Singleton pattern in a thread-safe manner?


Use synchronized blocks, double-checked locking, or enum-based implementation to ensure thread
safety.

110. What is the difference between method signature and method prototype?
Method Signature: Method name and parameter types

Method Prototype: Method signature plus return type

Tips for Interview Success


1. Understand the Fundamentals: Master the four pillars of OOP

2. Practice Coding: Implement design patterns and OOP concepts


3. Real-world Examples: Relate concepts to practical scenarios

4. Code Quality: Focus on clean, maintainable code

5. Problem-Solving: Think through design decisions logically


6. Stay Updated: Learn modern OOP practices and patterns

Remember to explain your reasoning and provide examples when answering these questions during
interviews!

You might also like