0% found this document useful (0 votes)
2 views5 pages

Oops Interview Questions

The document outlines key concepts of Object-Oriented Programming (OOP), including the four pillars: encapsulation, abstraction, inheritance, and polymorphism. It explains differences between classes and objects, method overloading and overriding, and various OOP concepts such as access modifiers, constructors, and the 'this' keyword. Additionally, it discusses dynamic method dispatch, the significance of the 'final' keyword, and the distinction between static and non-static methods.

Uploaded by

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

Oops Interview Questions

The document outlines key concepts of Object-Oriented Programming (OOP), including the four pillars: encapsulation, abstraction, inheritance, and polymorphism. It explains differences between classes and objects, method overloading and overriding, and various OOP concepts such as access modifiers, constructors, and the 'this' keyword. Additionally, it discusses dynamic method dispatch, the significance of the 'final' keyword, and the distinction between static and non-static methods.

Uploaded by

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

1. What are the four pillars of OOP?

 Answer: The four pillars of Object-Oriented Programming (OOP) are:


 Encapsulation: Wrapping data and methods that operate on the data together
as a single unit.
 Abstraction: Hiding the complex implementation details and exposing only the
necessary features.
 Inheritance: Deriving new classes from existing ones to reuse code.
 Polymorphism: The ability to use a single interface to represent different
underlying forms (object types).
 Textual Example:
 Encapsulation: A Person class where attributes like name and age are private,
and methods like setName() and getAge() are public.
 Abstraction: A Vehicle class with an abstract method start(), which subclasses
implement differently (e.g., Car or Truck).
 Inheritance: A Dog class inherits from a Animal class, so it gains general
behaviors (like eat()) from Animal.
 Polymorphism: A draw() method in a Shape class that can be overridden in
subclasses like Circle and Rectangle, so each has its own implementation.

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


 Answer:
o Class: A blueprint or prototype that defines properties and methods for objects.
It's an abstract template for creating objects.
o Object: An instance of a class with real values assigned to its attributes.

 Textual Example:
o Class: A Car class defines attributes like make, model, and speed, and methods
like accelerate() and brake().
o Object: An object of the Car class could be car1, where make = "Toyota", model
= "Camry", and speed = 60.

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


 Answer:
o Method Overloading: Defining multiple methods with the same name but
different parameter types.
o Method Overriding: Redefining a method in a subclass that already exists in
the superclass with the same method signature.
 Textual Example:
o Method Overloading: A Calculator class with add(int a, int b) and add(double a,
double b) methods. Both methods have the same name but different parameter
types.
o Method Overriding: A Dog class overriding the makeSound() method of the
Animal class to return "Woof!" instead of the general sound.

4. What is polymorphism and how is it implemented in OOP?


 Answer:
Polymorphism allows objects of different classes to be treated as objects of a
common superclass. It can be implemented through method overriding (runtime
polymorphism) or interface implementation.
 Textual Example:
o Method Overriding: In a Shape class, the method draw() is overridden by Circle
and Rectangle classes to provide specific drawing implementations. Calling
draw() on different shapes invokes the corresponding implementation.
o Interface Implementation: A Movable interface is implemented by both Car
and Person classes, each providing its own version of the move() method.

5. Explain the concept of inheritance.


 Answer:
Inheritance allows a class to inherit properties and methods from another class,
enabling code reusability and establishing relationships between classes.
 Textual Example:
o If you have a Vehicle class with methods like start() and stop(), you can create a
Car class that inherits from Vehicle and automatically has access to those
methods. The Car class can also add its own specific methods, like playMusic().

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


 Answer:
o Interface: Defines a contract that a class must follow, only containing method
signatures (no implementation).
o Abstract Class: Can have both abstract methods (without implementation) and
concrete methods (with implementation).
 Textual Example:
o Interface: A Flyable interface might define the method fly(). Any class that
implements Flyable, such as Bird or Airplane, must provide its own
implementation of fly().
o Abstract Class: A Shape class may have an abstract method draw() (without
implementation) but also a concrete method resize(). Subclasses like Circle and
Rectangle will implement draw() but can inherit resize().
7. What is constructor overloading?
 Answer:
Constructor overloading allows a class to have multiple constructors with different
parameters. The appropriate constructor is called based on the arguments passed
during object creation.
 Textual Example:
o A Rectangle class may have a constructor Rectangle(int length, int width) for
custom dimensions and a constructor Rectangle() that sets the dimensions to
default values (e.g., 1x1).

8. What is the difference between a shallow copy and a deep copy?


 Answer:
o Shallow Copy: Copies the reference of an object, meaning changes to the
copied object can affect the original.
o Deep Copy: Creates a completely independent copy of an object, including any
objects contained within it.
 Textual Example:
o Shallow Copy: If you copy an array of friends from person1 to person2 using
shallow copy, both will point to the same array. Modifying person2’s array will
also affect person1.
o Deep Copy: Using deep copy, person2 gets an entirely new array of friends, and
changes to person2’s array won’t affect person1.

9. What is an access modifier in OOP and what are the different types?
 Answer:
Access modifiers define the visibility and accessibility of classes, methods, variables.
The main types are:
o Public: Accessible from any class.

o Private: Accessible only within the same class.

o Protected: Accessible within the same class and subclasses.

 Textual Example:
o Public: A name variable declared as public in a class can be accessed directly
from any class.
o Private: A balance variable in a BankAccount class is private and can’t be
accessed directly outside the class.
o Protected: A protected method in a class can be accessed within the class and
subclasses, but not from outside the class.

10. Explain the concept of "this" keyword.


 Answer:
The this keyword refers to the current instance of the class. It is used to differentiate
between instance variables and method parameters when they have the same name.
 Textual Example:
o In a Person class, if the constructor parameter is named name and there is an
instance variable called name, you can use this.name to refer to the instance
variable and differentiate it from the parameter.

11. What is encapsulation and how is it implemented?


 Answer:
Encapsulation is the bundling of data and methods that operate on that data into a
single unit, restricting direct access to some of the object’s components. It is typically
implemented using private variables and public getter/setter methods.
 Textual Example:
o In a BankAccount class, the balance is private and can only be modified through
the public methods deposit() and withdraw().

12. Can you explain the concept of dynamic method dispatch?


 Answer:
Dynamic method dispatch refers to the process of resolving method calls at runtime
instead of compile-time, which enables polymorphism in object-oriented languages.
 Textual Example:
o A Shape class has a method draw(). If you call draw() on a reference variable of
type Shape, it may refer to a Circle object at runtime, and the draw() method of
Circle is executed, not the one in Shape.

13. What is the significance of the 'final' keyword in OOP?


 Answer:
The final keyword in OOP is used in three contexts:
o Variables: Makes a variable constant.

o Methods: Prevents method overriding.

o Classes: Prevents inheritance.

 Textual Example:
o Final Variable: final int MAX_SPEED = 120; makes MAX_SPEED a constant.

o Final Method: public final void stop() cannot be overridden by subclasses.

o Final Class: public final class Car {} cannot be subclassed.

14. What is the difference between static and non-static methods?


 Answer:
o Static Methods: Belong to the class and can be called without creating an
instance of the class.
o Non-static Methods: Belong to instances of the class and require an object to
be called.
 Textual Example:
o Static Method: Math.max(5, 10) can be called without an object.

o Non-static Method: A Car object must be created to call a non-static method


like car1.start().

You might also like