0% found this document useful (0 votes)
34 views10 pages

Top 50 OOPs Interview Questions

The document provides a comprehensive list of the top 50 interview questions and answers related to Object-Oriented Programming (OOP). Key concepts covered include definitions of OOP, classes, objects, encapsulation, inheritance, polymorphism, and various types of constructors and methods. It also explains important distinctions such as method overloading vs. overriding, access modifiers, and the differences between classes and structures.
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)
34 views10 pages

Top 50 OOPs Interview Questions

The document provides a comprehensive list of the top 50 interview questions and answers related to Object-Oriented Programming (OOP). Key concepts covered include definitions of OOP, classes, objects, encapsulation, inheritance, polymorphism, and various types of constructors and methods. It also explains important distinctions such as method overloading vs. overriding, access modifiers, and the differences between classes and structures.
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/ 10

Top 50 OOPs Interview Questions & Answers

1) What is OOPS?

Programs are collection of objects. Each obj is representation of class


2) Write basic concepts of OOPS?

Polymorphism

Encapsulation

Inheritance

Abstraction

3) What is a class?

Rep type of obj. blueprint/template, desc details of obj

4) What is an Object?

Instance of class. Own state, behaviour and identity

5) What is Encapsulation?

Contains all data which is hidden. Hidden data is restricted to members of class

Levels: public, protected, private, internal

6) What is Polymorphism?

Assign behaviour or value in subclass to something that was already declared in the main class.
Take more than one form

7) What is Inheritance?

One class share struct and behaviour defined in another class. I

If inheritance applied to one class – single inheritance

If on multiple classes – multiple inheritance

8) What are manipulators?

9) Explain the term constructor

Initialize state of obj.

Rules:

Contructore name = class name

No return type

10) Define Destructor?

Automatically called when object is made of scope or destroyed

Destructore name = ~ class name


11) What is an Inline function?

12) What is a virtual function?

Member function. Its functionality can be overridden in its derived class. Implemented using
keyword called virtual and given during declaration

13) What is a friend function?

14) What is function overloading?

Function overloading is a feature in some programming languages that allows you to define multiple
functions with the same name but with different parameter lists. The correct function to call is
determined by the number and types of arguments passed to the function
15) What is operator overloading?

Operator overloading is a feature in some programming languages that allows developers to define
or change the behavior of operators (like +, -, *, ==, etc.) for user-defined types (like classes or
structs). This makes it possible to use operators with custom objects in a way that is intuitive and
similar to how they are used with built-in types.
16) What is an abstract class?

Abstract classes are used to define common interfaces or functionality that derived classes must
implement or extend. Abstract classes often contain abstract methods, which are declared without
an implementation. Derived classes must provide implementations for these abstract methods.

You cannot create an object of an abstract class. It is meant to serve as a template for other classes.
Abstract classes are intended to be inherited by other classes. The derived class must implement all
abstract methods of the abstract class unless it is also an abstract class.
17) What is a ternary operator?

The ternary operator is a concise way to perform a simple conditional check in many programming
languages. It’s also known as the conditional operator. The ternary operator evaluates a condition
and returns one of two values based on whether the condition is true or false.

condition ? value_if_true : value_if_false

18) What is the use of finalize method?

The finalize method in Java is a special method that is called by the garbage collector before an
object is removed from memory. It was intended to give an object the opportunity to clean up
resources, such as closing files or releasing memory, before the object is destroyed.
19) What are the different types of arguments?

Positional arguments

Keyword arguments

Variable length
Pass by value/reference

Required arguments

20) What is the super keyword?

It is used to refer to the parent class (or superclass) of the current object. It allows a subclass to
access methods, constructors, or properties of its superclass. This is particularly useful when you
want to extend or modify the behavior of the inherited methods.
21) What is method overriding?

Method overriding is a feature in object-oriented programming where a subclass provides a specific


implementation of a method that is already defined in its superclass. When the method in the
subclass has the same name, return type, and parameters as a method in the superclass, the method
in the subclass overrides the method in the superclass.

class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("The cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.sound(); // Output: The animal makes a sound
myDog.sound(); // Output: The dog barks
myCat.sound(); // Output: The cat meows
}
}
22) What is an interface?

An interface in object-oriented programming is a reference type that can contain method


signatures, constants, default methods, static methods, and nested types. Interfaces are used to
achieve abstraction and to define a common protocol for classes to follow.

Interfaces can contain method declarations but not method implementations

Interfaces provide a way to achieve multiple inheritance


interface Animal {

void makeSound(); // Abstract method (no body)

int getAge(); // Abstract method (no body)

23) What is exception handling?

24) What are tokens?

25) What is the main difference between overloading and overriding?

Definition: Method overloading occurs when multiple methods have the same name but different
parameters (different type, number, or both) within the same class.

 Same Method Name: Methods share the same name but differ in their parameter lists.
 Compile-Time Polymorphism: Overloading is resolved at compile-time. The correct
method is selected based on the method signature (method name and parameters).
 Return Type: Overloading does not consider the return type alone; methods must differ
in parameter lists to be considered overloaded.
Definition: Method overriding occurs when a subclass provides a specific implementation of a
method that is already defined in its superclass. The method in the subclass must have the same
name, return type, and parameters as the method in the superclass.

 Same Method Signature: Methods must have the same name, return type, and
parameters.
 Runtime Polymorphism: Overriding is resolved at runtime. The method to be executed is
determined based on the object's runtime type.
 Access Modifiers: The overriding method should have the same or more accessible
visibility than the method in the superclass.
26) What is the main difference between a class and an object?

Definition: A class is a blueprint or template for creating objects. It defines a set of attributes (data)
and methods (functions) that the objects created from the class can use.

Definition: An object is an instance of a class. It represents a specific entity with state (attributes)
and behavior (methods) defined by its class.

Feature Class Object


A blueprint or template for creating
Definition An instance of a class with specific data.
objects.
Defines the structure (attributes) Represents a specific entity with its own
Purpose
and behavior (methods) for objects. state and behavior.
Created using the class constructor (e.g.,
Syntax Defined using the class keyword. new ClassName() in Java,
ClassName() in Python).
Instantiation Not instantiated. Created from a class definition.
Has a state, represented by attribute
State Does not have a state.
values.
Defines methods that objects of the Can call methods defined by its class to
Behavior
class can use. perform actions.
Has a unique identity; each object is
Identity No unique identity; it’s a definition.
distinct.
Contains attribute definitions that
Holds actual values for the attributes
Attributes are common to all objects of the
defined by its class.
class.
Contains method definitions that Can invoke methods to perform actions
Methods
provide behaviors for objects. or modify its state.
Example public class Car { String Car myCar = new Car("Red",
(Java) color; String model; ... } "Toyota");
class Car: def
Example __init__(self, color, my_car = Car("Red", "Toyota")
(Python) model): ...

27) What is an abstraction?

Abstraction is the process of hiding the complex implementation details and showing only the
necessary features of an object or system.

focusing on the high-level functionalities while concealing the underlying implementation.


28) What are the access modifiers?

keywords used in object-oriented programming to set the visibility and accessibility of classes,
methods, and variables. They help control how different parts of a program interact with each other
and enforce encapsulation.

 Public: Open access to all; used to expose class members.


 Private: Restricted access; used to hide implementation details.
 Protected: Limited access; used to allow access within the same package or by
subclasses.
 Default: Package-private in Java; no modifier in Python, defaulting to public.
29) What are sealed modifiers?

Sealed modifiers (also known as sealed classes or sealed interfaces) are used in some programming
languages to restrict how classes or interfaces can be extended or implemented. They provide a way
to control inheritance and ensure that the class or interface hierarchy remains under control.
30) How can we call the base method without creating an instance?

To call a base class method without creating an instance of the class, you typically use static
methods(java) or class methods(python)
31) What is the difference between new and override?

new and override are keywords used to manage and control method inheritance and
polymorphism in classes.

Feature newKeyword override Keyword


- Used to hide a base class method
- Used to override a base class method in a
in a derived class.
derived class.
Purpose - The derived class method has the
- Ensures that the derived class method
same name as the base class method
replaces the base class method.
but is not considered an override.
- Hides the base class method rather
- Replaces the base class method and ensures
than replacing it.
the derived class method is called when the
Visibility - Base class method is still
method is invoked on an instance of the
accessible through the base class
derived class.
reference.

32) What are the various types of constructors?

 Default Constructor: Initializes objects with default values; automatically provided if no


other constructors are defined.
 Parameterized Constructor: Initializes objects with specific values passed as
parameters.
 Copy Constructor: Creates a new object as a copy of an existing object.
 Static Constructor: Initializes static members of a class; automatically called before any
static members are accessed.
 Private Constructor: Prevents instantiation from outside the class; often used in design
patterns like Singleton.
33) What is early and late Binding?

Feature Early Binding Late Binding


- Also known as static binding or - Also known as dynamic binding or
compile-time binding. run-time binding.
Definition
- Method or member resolution is done at - Method or member resolution is done
compile time. at runtime.
Timing - Resolved during compilation. - Resolved during program execution.

Early Binding (Static Binding):


 Definition: Early binding resolves method calls or member accesses at compile time.
The compiler knows exactly which method or member to call based on the static type
of the reference.
 Characteristics:
o The exact method or member is determined before runtime.
o Offers faster performance because method calls are resolved during
compilation.
o Limited flexibility as it requires knowledge of method signatures and types at
compile time.

Late Binding (Dynamic Binding):

 Definition: Late binding resolves method calls or member accesses at runtime. The
actual method or member to be called is determined based on the runtime type of the
object.
 Characteristics:
o The exact method or member is determined during execution based on the
object's actual type.
o Provides more flexibility, especially in polymorphic scenarios where the type
of the object may not be known until runtime.
o Typically incurs a runtime performance overhead due to dynamic method
resolution.

34) What is ‘this’ pointer?

The this pointer is a special keyword used in object-oriented programming to refer to the current
instance of the class in which it is used. It provides a way to access instance variables and methods
from within a class's member functions.
35) What is the difference between structure and a class?

Feature Structure Class


- A data structure that groups related - A blueprint for creating objects that
variables (fields) together. encapsulate data and methods.
Definition
- Typically used for simple data - Supports full object-oriented
grouping. principles.
- Members are private by default in
- By default, all members are public
C++.
Access in C++.
- In C#, members are private by
Modifiers - In C#, all members are private by
default unless explicitly specified
default, but this is often overridden.
otherwise.
- Does not support inheritance in C+ - Supports inheritance (both single and
+ (only composition is used). multiple in C++).
Inheritance
- In C#, structures cannot be - Supports inheritance in C# (single
inherited. inheritance with interfaces)

36) What is the default access modifier in a class?


The default access modifier defines the visibility and accessibility of class members (fields, methods,
properties) when no explicit access modifier is specified

Language Default Access Modifier for Classes Default Access Modifier for Members
C++ private private
C# internal (for classes) private
Java package-private package-private

37) What is a pure virtual function?

A pure virtual function is a concept in object-oriented programming that is used to define an


interface for derived classes without providing an implementation in the base class. It makes a class
abstract, meaning you cannot instantiate it directly, and it forces derived classes to implement the
pure virtual function to become concrete (i.e., instantiable) classes.

- A pure virtual function is a virtual function declared in a base class with = 0 at


Definition
the end of its declaration.
- To create an abstract base class that cannot be instantiated.
Purpose
- To define a common interface for derived classes.
- In C++, use = 0 in the function declaration to indicate a pure virtual function.
Syntax - In other languages like Java, similar functionality is achieved using abstract
methods.

38) What are all the operators that cannot be overloaded?

Operator Description
:: (Scope Resolution Used to define the scope of a name or access a global variable.
Operator) Cannot be overloaded.
. (Member Access Used to access members of an object directly. Cannot be
Operator) overloaded.
.* (Pointer-to-Member Used to access members of an object via a pointer-to-member.
Operator) Cannot be overloaded.
sizeof
Used to determine the size of a data type or object. The result is
evaluated at compile-time. Cannot be overloaded.
typeid
Used to obtain type information at runtime. Cannot be
overloaded.
const_cast
Used to cast away const or volatile qualifiers. Cannot be
overloaded.
dynamic_cast
Used for runtime type identification and casting. Cannot be
overloaded.
static_cast Used for explicit type conversions. Cannot be overloaded.
reinterpret_cast Used for low-level casting between types. Cannot be overloaded.

39) What is dynamic or run time polymorphism?

Refer above

40) Do we require a parameter for constructors?


No, constructors do not require parameters. There are two main types of constructors in
object-oriented programming: parameterized constructors and default (or no-argument)
constructors.

1. Default Constructor (No-Argument Constructor)

 A default constructor is a constructor that takes no parameters. It is used to create an


object with default values.
 If no constructor is defined in a class, most programming languages like C++, Java,
and C# automatically provide a default constructor.
 If a class has no constructor at all, the compiler will provide a default constructor that
initializes the object with default values (e.g., zeros, nulls).

2. Parameterized Constructor

 A parameterized constructor is a constructor that takes one or more parameters. It is used


when you want to initialize an object with specific values.
 If a class only has parameterized constructors and no default constructor, you must pass the
required arguments when creating an object.

41) What is a copy constructor?

create a new object as a copy of an existing object. The copy constructor is typically called when an
object is passed by value, returned from a function, or explicitly copied.

Feature Description
Purpose Creates a new object as an exact copy of an existing object.
Called when an object is passed by value, returned from a function, or
Usage
explicitly copied.
Takes a reference to an object of the same class as its parameter (usually a
Parameter
const reference).
Default vs. If not defined by the user, the compiler provides a default copy constructor
Custom that performs a shallow copy.
Custom copy constructors can be defined to perform a deep copy if the
Deep Copy
class contains pointers or dynamically allocated memory.

42) What does the keyword virtual represented in the method definition?

43) Whether static method can use non static members?

44) What are a base class, subclass, and superclass?

45) What is static and dynamic Binding?

46) How many instances can be created for an abstract class?


47) Which keyword can be used for overloading?

48) What is the default access specified in a class definition?

49) Which OOPS concept is used as a reuse mechanism?

50) Which OOPS concept exposes only the necessary information to the calling
functions?

What is the difference between this() and super() in Java?


this( ) super( )

It represents the current instance of It represents the current instance of


the class. the parent class.

Calls the default constructor of the Calls the default constructor of the
same class. base class.

Access the methods of the same Access the methods of the parent
class. class.

Points current class instance. Points the superclass instance.

109. Can we override the private methods?


It is not possible to override the private methods in Java. Method
overriding is where the method in the subclass is implemented instead of
the method from the parent class. The private methods are accessible
only within the class in which it is declared. Since this method is not
visible to other classes and cannot be accessed, it cannot be overridden.
105. Can we override the static method?
No, as static methods are part of the class rather than the object so we
can’t override them.
106. Can we override the overloaded method?
Yes, since the overloaded method is a completely different method in the
eyes of the compiler. Overriding isn’t the same thing at all. The decision
as to which method to call is deferred to runtime.

You might also like