0% found this document useful (0 votes)
11 views

OOP

The document provides a comprehensive overview of Object-Oriented Programming (OOP), detailing its key principles such as encapsulation, abstraction, inheritance, and polymorphism. It explains fundamental concepts like classes, objects, constructors, and various types of relationships between objects, including association, aggregation, and composition. Additionally, it covers advanced topics like design patterns, SOLID principles, and the differences between various OOP features, making it a valuable resource for understanding OOP fundamentals.

Uploaded by

misojek222
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)
11 views

OOP

The document provides a comprehensive overview of Object-Oriented Programming (OOP), detailing its key principles such as encapsulation, abstraction, inheritance, and polymorphism. It explains fundamental concepts like classes, objects, constructors, and various types of relationships between objects, including association, aggregation, and composition. Additionally, it covers advanced topics like design patterns, SOLID principles, and the differences between various OOP features, making it a valuable resource for understanding OOP fundamentals.

Uploaded by

misojek222
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/ 13

1. What is OOP?

Answer:
OOP (Object-Oriented Programming) is a programming paradigm based on objects that
contain data (attributes) and methods (functions) to operate on the data. It helps organize
code into reusable and manageable components.

2. What are the main principles of OOP?

Answer:
The four main principles of OOP are:

1. Encapsulation – Hiding internal details and exposing only the necessary parts.

2. Abstraction – Showing essential features and hiding unnecessary details.

3. Inheritance – Reusing existing code by deriving new classes from base classes.

4. Polymorphism – Using a single interface to represent multiple types.

3. What is a class?

Answer:
A class is a blueprint for creating objects. It defines attributes (data) and behaviors
(methods) that the object will have.

4. What is an object?

Answer:
An object is an instance of a class that contains actual values for the attributes and can
perform actions defined by the methods.

5. What is encapsulation?

Answer:
Encapsulation is the practice of wrapping data (variables) and methods (functions) into a
single unit (class) and restricting direct access to them to protect data integrity.

6. What is abstraction?
Answer:
Abstraction means hiding complex implementation details and showing only the necessary
features to the user.

7. What is inheritance?

Answer:
Inheritance allows a class to inherit properties and behaviors from another class, promoting
code reuse.

8. What is polymorphism?

Answer:
Polymorphism allows a function or method to have multiple forms, such as method
overloading (same function name with different parameters) and method overriding
(redefining a function in a subclass).

9. What are the types of inheritance?

Answer:

1. Single Inheritance – A child inherits from a single parent.

2. Multiple Inheritance – A child inherits from multiple parents.

3. Multilevel Inheritance – A chain of inheritance.

4. Hierarchical Inheritance – One parent, multiple children.

5. Hybrid Inheritance – A combination of multiple inheritance types.

10. What is method overloading?

Answer:
Method overloading allows multiple methods with the same name but different parameters
within the same class.

11. What is method overriding?


Answer:
Method overriding allows a subclass to provide a different implementation of a method that
is already defined in its parent class.

12. What is the difference between method overloading and method overriding?

Answer:

Method Overloading Method Overriding

Happens in the same class Happens in different (parent-child) classes

Different parameter list Same parameter list

Compile-time polymorphism Runtime polymorphism

13. What is the difference between class and object?

Answer:

Class Object

Blueprint/template Instance of class

Defines structure Holds actual data

14. What are constructors?

Answer:
Constructors are special methods that initialize an object when it is created.

15. What are destructors?

Answer:
Destructors are methods that clean up resources when an object is destroyed.

16. What is an abstract class?

Answer:
An abstract class cannot be instantiated and may contain abstract methods (without
implementation).
17. What is an interface?

Answer:
An interface is a contract that defines a set of methods that must be implemented by any
class that adopts it.

18. What is the difference between an abstract class and an interface?

Answer:

Abstract Class Interface

Can have implemented methods Only method signatures

Supports constructors No constructors

Can contain variables Only constants

19. What is multiple inheritance?

Answer:
Multiple inheritance allows a class to inherit from more than one base class.

20. What is a static method?

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

21. What is a static variable?

Answer:
A static variable belongs to the class and is shared among all objects.

22. What is the "this" keyword?

Answer:
The "this" keyword refers to the current instance of the class.
23. What is the "super" keyword?

Answer:
The "super" keyword is used to call a parent class constructor or method.

24. What is dynamic method dispatch?

Answer:
Dynamic method dispatch (runtime polymorphism) allows calling overridden methods based
on the object's actual type at runtime.

25. What is coupling in OOP?

Answer:
Coupling refers to the degree of dependency between classes. Low coupling is preferred for
better maintainability.

26. What is cohesion in OOP?

Answer:
Cohesion refers to how well a class focuses on a single responsibility.

27. What is association in OOP?

Answer:
Association represents a relationship between objects where they can exist independently
but work together.

28. What is aggregation?

Answer:
Aggregation is a "has-a" relationship where an object contains another object.

29. What is composition?

Answer:
Composition is a stronger form of aggregation where the contained object cannot exist
without the parent object.
30. What is dependency injection?

Answer:
Dependency Injection is a design pattern where dependencies are provided from outside the
class rather than being created inside.

31. What is a singleton class?

Answer:
A singleton class allows only one instance of the class to exist.

32. What are access modifiers?

Answer:
Access modifiers define the visibility of class members (public, private, protected).

33. What is an inner class?

Answer:
An inner class is a class defined within another class.

34. What is an immutable object?

Answer:
An immutable object cannot be changed after it is created.

35. What is a shallow copy?

Answer:
A shallow copy copies references of objects, not the actual objects.

36. What is a deep copy?

Answer:
A deep copy creates a new copy of the actual objects.
37. What is the difference between "==" and "equals()" in Java?

Answer:
"==" compares memory addresses, while "equals()" compares content.

38. What is a design pattern?

Answer:
A design pattern is a general solution to a common problem in software design.

39. What is the SOLID principle?

Answer:
SOLID stands for:

1. Single Responsibility

2. Open/Closed Principle

3. Liskov Substitution

4. Interface Segregation

5. Dependency Inversion

Here are more OOP interview questions along with simple answers:

40. What is the difference between composition and inheritance?

Answer:

• Inheritance: "Is-a" relationship (e.g., a Dog is-a Animal).

• Composition: "Has-a" relationship (e.g., a Car has-a Engine).


Composition is preferred over inheritance to reduce code coupling.

41. What is a friend function in C++?

Answer:
A friend function allows access to private and protected members of a class without being a
member of that class.

42. What is the difference between public, private, and protected access specifiers?
Answer:

Modifier Accessible in same class Subclasses Outside class

Public Yes Yes Yes

Private Yes No No

Protected Yes Yes No

43. What is operator overloading?

Answer:
Operator overloading allows you to redefine the behavior of operators (e.g., +, -) for user-
defined types in languages like C++.

44. What is the difference between early binding and late binding?

Answer:

• Early Binding: Method call is resolved at compile time (e.g., method overloading).

• Late Binding: Method call is resolved at runtime (e.g., method overriding using
virtual functions).

45. What is the difference between deep copy and shallow copy?

Answer:

• Shallow Copy: Copies only references (changes affect original object).

• Deep Copy: Creates a completely new copy of objects.

46. What is the difference between composition and aggregation?

Answer:

• Aggregation: Weak relationship; objects can exist independently.

• Composition: Strong relationship; objects depend on each other for existence.

47. What is the significance of the "final" keyword in OOP?


Answer:
The final keyword (in Java, C#) is used to:

1. Prevent class inheritance.

2. Prevent method overriding.

3. Make variables constant.

48. What is the Liskov Substitution Principle (L in SOLID)?

Answer:
A subclass should be able to replace a parent class without affecting the program's
correctness.

49. What is the difference between an interface and an abstract class in Java?

Answer:

• Interface: 100% abstraction, only method declarations, no constructors.

• Abstract Class: Partial abstraction, can have implemented methods and constructors.

50. What is an association relationship in OOP?

Answer:
Association is a relationship between two independent classes, where objects can interact
with each other but are not dependent.

51. What is duck typing in OOP?

Answer:
Duck typing is a concept in dynamically typed languages (like Python), where an object's
methods determine its type rather than inheritance.

52. What is multiple dispatch?

Answer:
Multiple dispatch means a method can be selected based on the runtime types of multiple
arguments (common in Python, CLOS).
53. What is the difference between a constructor and a method?

Answer:

• Constructor: Used to initialize objects and is called automatically.

• Method: Defines behavior and is called explicitly.

54. Can a constructor be private?

Answer:
Yes, a private constructor is used in singleton design patterns to restrict object creation.

55. What is function overriding?

Answer:
Function overriding allows a subclass to provide its own implementation of a method
already defined in its parent class.

56. What are virtual functions in C++?

Answer:
Virtual functions allow dynamic binding, meaning the function call is resolved at runtime
based on the actual object type.

57. What is an abstract method?

Answer:
An abstract method is a method without implementation, meant to be overridden by
subclasses.

58. What is an object's lifetime in OOP?

Answer:
An object's lifetime refers to the period between its creation (instantiation) and its
destruction (garbage collection or manual deallocation).

59. What is the difference between "has-a" and "is-a" relationships?

Answer:
• "Is-a": Represents inheritance (e.g., Dog is an Animal).

• "Has-a": Represents composition (e.g., Car has an Engine).

60. What is method hiding in OOP?

Answer:
If a subclass defines a static method with the same signature as in the parent class, it hides
the parent method rather than overriding it.

61. What is a namespace in OOP?

Answer:
A namespace is used to group related classes, functions, and variables to avoid name
conflicts.

62. What are the advantages of OOP?

Answer:

1. Code reusability.

2. Improved maintainability.

3. Enhanced security with encapsulation.

4. Scalability.

63. What is the difference between composition and dependency injection?

Answer:

• Composition: One object contains another.

• Dependency Injection: An object's dependencies are provided externally.

64. What is a pure virtual function?

Answer:
A function declared in an abstract class with = 0 in C++ is a pure virtual function, forcing
derived classes to implement it.
65. What is a virtual destructor in C++?

Answer:
A virtual destructor ensures proper cleanup of resources in an inheritance hierarchy.

66. What is the diamond problem in OOP?

Answer:
It occurs in multiple inheritance when two parent classes share a common base class,
leading to ambiguity.

67. What is the difference between delegation and inheritance?

Answer:

• Delegation: Object passes responsibility to another.

• Inheritance: Object derives behavior from a parent class.

68. What are getters and setters?

Answer:
Methods used to access (get) and modify (set) private variables of a class.

69. What is an adapter pattern in OOP?

Answer:
The adapter pattern allows incompatible interfaces to work together by acting as a bridge.

70. What is object cloning?

Answer:
Object cloning creates a duplicate object with the same properties.

71. What is the open-closed principle in OOP?

Answer:
A class should be open for extension but closed for modification.
72. What is cohesion and why is it important?

Answer:
Cohesion measures how well a class focuses on a single responsibility. High cohesion
improves code maintainability.

73. What is loose coupling in OOP?

Answer:
Loose coupling means reducing dependencies between classes to make the system easier to
change.

74. Can a constructor return a value?

Answer:
No, constructors do not return values; they only initialize objects.

75. What is garbage collection?

Answer:

Garbage collection automatically reclaims memory occupied by unused objects.

You might also like