OOPs Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm that uses objects to organize code. It involves concepts like encapsulation, inheritance, and polymorphism.
Ques 2. What is inheritance in OOP?
Inheritance is a mechanism where a new class inherits properties and behaviors from an existing class. It promotes code reusability and establishes a relationship between classes.
Ques 3. What is a constructor?
A constructor is a special method in a class that is automatically called when an object of the class is created. It is used for initializing object properties.
Example:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
}
Ques 4. What is the 'super' keyword used for?
The 'super' keyword is used to call the constructor or methods of the parent class in a subclass. It is often used to access the superclass's methods or properties.
Example:
class Child extends Parent {
constructor() {
super();
}
}
Ques 5. What is the 'this' keyword in OOP?
The 'this' keyword refers to the current instance of the class. It is used to access the current object's properties and methods within that class.
Example:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log('Hello, ' + this.name + '!');
}
}
Ques 6. What is a static method in a class?
A static method is a method that belongs to the class rather than an instance of the class. It is called on the class itself, not on an object created from the class.
Example:
class MathUtils {
static add(a, b) {
return a + b;
}
}
Ques 7. What is an interface in Java?
In Java, an interface is a collection of abstract methods. A class implements an interface, providing concrete implementations for all its methods. It allows for achieving multiple inheritance in Java.
Example:
interface Printable {
void print();
}
Ques 8. What is the role of the 'super()' statement in a constructor?
The 'super()' statement is used in a subclass constructor to call the constructor of its superclass. It initializes the inherited properties and ensures that the superclass's constructor is executed before the subclass's constructor.
Example:
class Subclass extends Superclass {
constructor() {
super();
}
}
Ques 9. What is the purpose of the 'sealed' keyword in C#?
In C#, the 'sealed' keyword is used to prevent a class from being inherited. It ensures that the class cannot be used as a base class for other classes.
Example:
sealed class MySealedClass {
// class content
}
Intermediate / 1 to 5 years experienced level questions & answers
Ques 10. Explain the concept of encapsulation.
Encapsulation is the bundling of data and methods that operate on that data into a single unit known as a class. It restricts access to some of the object's components.
Ques 11. Explain polymorphism.
Polymorphism allows objects of different types to be treated as objects of a common type. It can take the form of method overloading or method overriding.
Ques 12. Explain the concept of method overloading.
Method overloading is a feature in OOP where a class can have multiple methods with the same name but with different parameter types or a different number of parameters.
Example:
class MathOperations {
add(a, b) {
return a + b;
}
add(a, b, c) {
return a + b + c;
}
}
Ques 13. What is an interface in OOP?
An interface in OOP defines a contract for classes, specifying a set of methods that a class must implement. It allows for achieving multiple inheritance in languages that don't support it directly.
Example:
interface Printable {
print();
}
Ques 14. Explain the concept of method overriding.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows a subclass to provide a specialized version of a method.
Example:
class Animal {
speak() {
console.log('Animal speaks');
}
}
class Dog extends Animal {
speak() {
console.log('Dog barks');
}
}
Ques 15. What is a destructor?
A destructor is a method that is called when an object is about to be destroyed. In many programming languages, like C++, it is used to release resources held by the object.
Example:
class MyClass {
destructor() {
// clean up resources
}
}
Ques 16. What is a virtual function?
A virtual function is a function in a base class that is declared using the 'virtual' keyword and can be overridden by a derived class. It enables dynamic method binding and polymorphism.
Example:
class Shape {
virtual calculateArea() = 0;
}
Ques 17. What is an abstract method?
An abstract method is a method declared in an abstract class or an interface that has no implementation in the base class. Subclasses must provide an implementation for abstract methods.
Example:
abstract class Printer {
abstract print();
}
Ques 18. Explain the term 'constructor chaining'.
Constructor chaining refers to the process of calling one constructor from another in the same class or a superclass. It allows for reusing code and initializing objects more efficiently.
Example:
class Vehicle {
constructor(make) {
this.make = make;
}
}
class Car extends Vehicle {
constructor(make, model) {
super(make);
this.model = model;
}
}
Ques 19. What is the purpose of the 'final' keyword?
In OOP, the 'final' keyword can be applied to classes, methods, or variables. It indicates that a class cannot be extended, a method cannot be overridden, or a variable cannot be reassigned.
Example:
final class MyFinalClass {
// class content
}
Ques 20. Explain the concept of method hiding.
Method hiding occurs when a subclass provides a static method with the same signature as a static method in its superclass. It does not override the method but hides it.
Example:
class Parent {
static show() {
console.log('Static method in Parent');
}
}
class Child extends Parent {
static show() {
console.log('Static method in Child');
}
}
Ques 21. What is an abstract class in C#?
In C#, an abstract class is a class that cannot be instantiated and may contain abstract methods. It is used to provide a common base for related classes and enforce a certain structure on derived classes.
Example:
abstract class Shape {
public abstract void Draw();
}
Ques 22. Explain the 'is-a' and 'has-a' relationships in OOP.
'is-a' relationship represents inheritance, where a class is a specialized version of another class. 'has-a' relationship represents composition, where a class contains an instance of another class as a member.
Experienced / Expert level questions & answers
Ques 23. What is the difference between abstraction and encapsulation?
Abstraction involves hiding the unnecessary details while encapsulation involves bundling the data and methods that operate on the data into a single unit.
Ques 24. Explain the concept of abstract classes.
Abstract classes are classes that cannot be instantiated and may contain abstract methods. Subclasses must implement these abstract methods.
Example:
abstract class Shape {
abstract calculateArea();
}
Ques 25. What is the difference between composition and inheritance?
Composition involves combining objects to create more complex ones, while inheritance involves creating a new class by inheriting properties and behaviors from an existing class.
Ques 26. Explain the concept of multiple inheritance.
Multiple inheritance allows a class to inherit properties and behaviors from more than one superclass. Some languages support it directly, while others provide alternatives like interfaces.
Ques 27. What is a friend class in C++?
In C++, a friend class is a class that is not a part of the class hierarchy but is granted access to the private and protected members of another class.
Ques 28. What is the SOLID principle in OOP?
SOLID is an acronym for five design principles in OOP: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. These principles aim to make software designs more understandable, flexible, and maintainable.
Ques 29. What is the diamond problem in the context of multiple inheritance?
The diamond problem occurs when a class inherits from two classes that have a common ancestor. It can lead to ambiguity in the inheritance hierarchy, especially if the common ancestor has members.
Ques 30. Explain the term 'covariant return type' in OOP.
Covariant return type allows a method in a subclass to return a more derived type than the method in the base class. It is a feature supported by some programming languages, including C++ and Java.
Most helpful rated by users: