50 Object-Oriented Programming (OOP) Theory
Questions with Answers for Cognizant Tech
Interview
Key Takeaway: Mastering these core OOP concepts—encapsulation, inheritance, polymorphism,
and abstraction—will strengthen your theoretical understanding and prepare you for Cognizant’s
technical rounds.
1. What is a Class and an Object?
A class is a blueprint or template defining data members (attributes) and methods (behaviors);
an object is an instance of a class created at runtime with specific state values. [1]
2. What are the four pillars of OOP?
Encapsulation: Bundling data and methods within a class
Inheritance: Deriving new classes from existing ones
Polymorphism: One interface, multiple implementations
Abstraction: Hiding internal details and exposing only essentials. [2]
3. Define Encapsulation.
Encapsulation hides data implementation by restricting direct access to class fields via access
modifiers and providing public getter/setter methods. [2]
4. What is Inheritance?
Inheritance allows a subclass to inherit attributes and methods from a superclass, promoting
code reuse and hierarchical classification. [2]
5. What is Polymorphism?
Polymorphism enables a single method or operator to operate in different contexts:
Compile-time (static) polymorphism: Method overloading
Runtime (dynamic) polymorphism: Method overriding. [2]
6. Define Abstraction.
Abstraction simplifies complexity by exposing only necessary interfaces and hiding
implementation details, achieved via abstract classes and interfaces. [1]
7. What is Method Overloading?
Method overloading is having multiple methods in the same class with the same name but
different parameter lists (type, number, or order). [2]
8. What is Method Overriding?
Method overriding allows a subclass to provide a specific implementation of a method already
defined in its superclass, using the same signature. [2]
9. Difference between Abstract Class and Interface.
Abstract class: Can have both abstract and concrete methods, constructors, and state;
supports single inheritance
Interface: Contains only abstract methods (pre–Java 8), static/final constants, supports
multiple inheritance; Java 8+ allows default and static methods. [2]
10. When to use Interface vs Abstract Class?
Use interface to define a contract for multiple unrelated classes; use abstract class to share
code among closely related classes with common state. [2]
11. What is a Constructor?
A constructor is a special method invoked when an object is created to initialize its state. It has
the same name as the class and no return type. [1]
12. What is Constructor Overloading?
Defining multiple constructors in a class with different parameter lists to initialize objects in
different ways. [1]
13. What is the default constructor?
If no constructor is defined, the compiler provides a no-argument default constructor that
initializes members to default values. [1]
14. What is super()?
super() calls the parent class’s constructor. It must be the first statement in a subclass
constructor if used [2] .
15. What is this()?
this() calls anotherconstructor within the same class. It must be the first statement in the
constructor and helps avoid duplication [1] .
16. What are Access Modifiers?
Define visibility for classes, methods, and fields:
private: class-only
default (package-private): package-only
protected: package and subclasses
public: accessible everywhere. [2]
17. What is Association?
A relationship where objects of one class use objects of another class, implemented via instance
variables. [1]
18. What is Aggregation?
A specialized form of association representing a “has-a” relationship with independent lifecycles
(weak ownership). [1]
19. What is Composition?
A form of aggregation with strong ownership where the contained object’s lifecycle is bound to
the container’s lifecycle. [1]
20. What is Coupling in OOP?
Coupling measures interdependence between classes; low coupling (weak association) is
desirable for maintainability. [1]
21. What is Cohesion?
Cohesion measures how closely related the responsibilities of a single class are; high cohesion
within a class indicates focused functionality. [1]
22. What is a Final Class, Method, and Variable?
final class: cannot be subclassed
final method: cannot be overridden
final variable: value cannot change after initialization. [2]
23. What is the difference between abstract and final?
abstract: class/method must be subclassed or implemented
final: class/method cannot be subclassed or overridden. [2]
24. Explain Liskov Substitution Principle (LSP).
Subclasses should be substitutable for their base classes without altering correct program
behavior, ensuring derived classes extend base classes without changing their behavior. [1]
25. Explain Open/Closed Principle (OCP).
Software entities (classes, modules, functions) should be open for extension but closed for
modification, enabling new functionality via inheritance or composition. [1]
26. Explain Single Responsibility Principle (SRP).
A class should have only one reason to change, i.e., it should have only one job or responsibility.
[1]
27. Explain Dependency Inversion Principle (DIP).
High-level modules should not depend on low-level modules; both should depend on
abstractions. Abstractions should not depend on details, but details on abstractions. [1]
28. What is Design by Contract?
A methodology where software designers define precise, verifiable interface specifications—
preconditions, postconditions, and invariants—for software components. [1]
29. What is an Immutable Class?
An immutable class’s state cannot change after creation. To design one: declare class final,
mark fields private final, and avoid setters. [2]
30. Why use Immutable Objects?
They are thread-safe, easier to design and maintain, and help avoid unexpected side effects. [2]
31. What is Object Cloning?
Creating a copy of an existing object via clone() method defined in Cloneable interface. Deep
vs. shallow cloning depends on whether referenced objects are cloned. [1]
32. What is the Prototype Design Pattern?
A creational pattern where new objects are created by copying a prototype instance, improving
performance when object creation is expensive. [1]
33. What is the Factory Method Design Pattern?
Defines an interface for creating objects but lets subclasses decide which class to instantiate,
promoting loose coupling. [1]
34. What is the Singleton Design Pattern?
Ensures a class has only one instance and provides a global access point, typically implemented
with a private constructor and a static getInstance() method. [1]
35. What is the Adapter Design Pattern?
Allows incompatible interfaces to work together by wrapping an existing class with a new
interface. [1]
36. What is the Observer Design Pattern?
Defines a one-to-many dependency: when one object’s state changes, all its dependents are
notified and updated automatically. [1]
37. What is MVC (Model-View-Controller)?
An architectural pattern separating data (Model), UI (View), and business logic (Controller),
improving modularity and testability. [1]
38. How is OOP implemented in Java?
Through classes, objects, inheritance (extends), interfaces (implements), access modifiers, and
polymorphism via method overriding and overloading. [2]
39. What is Dynamic Binding?
Method call resolution occurs at runtime based on the object’s actual type, enabling runtime
polymorphism. [2]
40. What is Static Binding?
Method call resolution occurs at compile time based on reference type, applicable to static,
private, and final methods. [2]
41. What is a Nested Class?
A class declared within another class. Types include static nested classes and inner classes
(non-static), local classes, and anonymous classes. [1]
42. What is an Anonymous Inner Class?
A one-time use class without a name defined and instantiated in a single statement, often used
to override methods or implement interfaces on the fly. [1]
43. How does Java achieve multiple inheritance of type?
Via interfaces: a class can implement multiple interfaces to inherit method signatures from each,
avoiding the diamond problem. [2]
44. What is Diamond Problem and how does Java handle it?
The diamond problem occurs when multiple inheritance paths define the same method. Java
resolves it via interfaces with default methods requiring explicit override in the implementing
class. [2]
45. What is Method Hiding?
When a subclass defines a static method with the same signature as a static method in its
superclass, the subclass method hides the superclass’s method. [1]
46. What is Covariant Return Type?
Allows an overriding method to return a subtype of the return type declared in the overridden
method, increasing flexibility. [2]
47. What is the difference between Association, Aggregation, and Composition?
Association: General “uses-a” relationship
Aggregation: Weak “has-a” with independent lifecycles
Composition: Strong “has-a” with dependent lifecycles. [1]
48. Explain Polymorphic Behavior with an Example.
Animal a = new Dog();
a.sound(); // Calls Dog’s implementation at runtime
Though reference type is Animal, Dog’s sound() executes due to dynamic binding. [2]
49. What is Interface Segregation Principle (ISP)?
Clients should not depend on interfaces they do not use. Split large interfaces into smaller, role-
specific ones. [1]
50. How do you prevent a class from being subclassed?
Declare the class with the final keyword; no class can extend a final class. [2]
These 50 OOP theory questions and answers cover fundamental concepts, relationships, design
principles, and patterns essential for Cognizant’s technical interviews. Focus on understanding
definitions, recognizing use cases, and illustrating concepts with concise examples to excel.
⁂
1. https://www.geeksforgeeks.org/interview-prep/oops-interview-questions/
2. https://www.hirist.tech/blog/top-50-java-oops-interview-questions-and-answers/