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
}
Most helpful rated by users: