Basic Interview Questions On OOPS
Basic Interview Questions On OOPS
1) What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection
of objects.
There are various OOP languages but the most widely used are:
o Python
o Java
o Go
o Dart
o C++
o C#
o Ruby
The aim of OOP is to implement real-world entities like inheritance, hiding, polymorphism in programming. The
main purpose of OOP is to bind together the data and the functions that operate on them so that no other part of
the code can access this data except that function.
o Inheritance
o Encapsulation
o Polymorphism
o Data Abstraction
OOPs, programming paradigm is considered as a better style of programming. Not only it helps in writing a
complex piece of code easily, but it also allows users to handle and maintain them easily as well. Not only that,
the main pillar of OOPs - Data Abstraction, Encapsulation, Inheritance, and Polymorphism, makes it easy for
programmers to solve complex scenarios. As a result of these, OOPs is so popular.
Advantages of OOP
Disadvantages of OOP
Code can be reused thereby reducing redundancy Does not support code reusability
9) What do you understand by pure object-oriented language? Why Java is not a pure object-oriented
programming language?
The programming language is called pure object-oriented language that treats everything inside the program as
an object. The primitive types are not supported by the pure OOPs language. There are some other features that
must satisfy by a pure object-oriented language:
o Encapsulation
o Inheritance
o Polymorphism
o Abstraction
o All predefined types are objects
o All user-defined types are objects
o All operations performed on objects must be only through methods exposed to the objects.
Java is not a pure object-oriented programming language because pre-defined data types in Java are not
treated as objects. Hence, it is not an object-oriented language.
10) What do you understand by class and object? Also, give example.
Class: A class is a blueprint or template of an object. It is a user-defined data type. Inside a class, we define
variables, constants, member functions, and other functionality. It does not consume memory at run time. Note
that classes are not considered as a data structure. It is a logical entity. It is the best example of data binding.
Object: An object is a real-world entity that has attributes, behavior, and properties. It is referred to as an
instance of the class. It contains member functions, variables that we have defined in the class. It occupies space
in the memory. Different objects have different states or attributes, and behaviors.
Class Object
It is conceptual. It is real.
It binds data and methods together into a single It is just like a variable of a class.
unit.
It does not occupy space in the memory. It occupies space in the memory.
It uses the keyword class when declared. It uses the new keyword to create an object.
A class can exist without any object. Objects cannot exist without a class
12) What are the key differences between class and structure?
Class Structure
Class is a group of common objects that shares The structure is a collection of different
common properties. data types.
It deals with data members and member functions. It deals with data members only.
It's members are private by default. It's members are public by default.
The keyword class defines a class. The keyword struct defines a structure.
Useful while dealing with the complex data Useful while dealing with the small data
structure. structure.
8. Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if:
It is a static method
The base class is inherited by some other subclass
Access modifiers determine the scope of the method or variables that can be accessed from other various objects
or classes. There are five types of access modifiers, and they are as follows:
Private
Protected
Public
Friend
Protected Friend
Manipulators are helping functions. It is used to manipulate or modify the input or output stream. The
modification is possible by using the insertion (<<) and extraction (>>) operators
The example of manipulators that do not have arguments is endl, ws, flush, etc. Manipulators with arguments
are setw(val), setfill(c), setbase(val), setiosflags(flag).
36. What is a constructor?
A constructor is a special type of method that has the same name as the class and is used to initialize objects of
that class.
Default constructor
Parameterized constructor
Copy constructor
Static constructor
Private constructor
Copy Constructor- The copy constructor in C++ is used to copy data of one object to another.
40. What is the use of ‘finalize’?
Finalize as an object method used to free up unmanaged resources and cleanup before Garbage
Collection(GC). It performs memory management tasks.
Inheritance is a feature of OOPs which allows classes inherit common properties from other classes. This
property helps you get rid of redundant code thereby reducing the overall size of the code.
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example,
the vehicle class can have ‘car’, ‘bike’, etc as its subclasses.
Increases the time and effort required to execute a program as it requires jumping back and forth between
different classes
The parent class and the child class get tightly coupled
Any modifications to the program would require changes both in the parent as well as the child class
Needs careful implementation else would lead to incorrect results
What is Polymorphism?
Polymorphism is nothing but assigning behavior or value in a subclass to something that was already
declared in the main class. Simply, polymorphism takes more than one form.
Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of
compile-time polymorphism is method overloading.
If a class has multiple methods having same name but different in parameters, it is known as Method
Overloading.
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding
Operator overloading refers to implementing operators using user-defined types based on the arguments passed
along with it.
24. Differentiate between overloading and overriding.
Overloading Overriding
Child class redefining methods present in the
Two or more methods having the same name
base class with the same parameters/
but different parameters or signature
signature
Resolved during compile-time Resolved during runtime
25. What is encapsulation?
Encapsulation refers to binding the data and the code that works on that together in a single unit. For example, a
class. Encapsulation also allows data-hiding as the data specified in one class is hidden from other classes.
Data abstraction is a very important feature of OOPs that allows displaying only the important information and
hiding the implementation details.
Abstract class
Abstract method
An abstract class is a class that consists of abstract methods. These methods are basically declared but not
defined. If these methods are to be used in some subclass, they need to be exclusively defined in the subclass.
No. Instances of an abstract class cannot be created because it does not have a complete implementation.
However, instances of subclass inheriting the abstract class can be created
Virtual functions are functions that are present in the parent class and are overridden by the subclass. These
functions are used to achieve runtime polymorphism.
Pure virtual functions or abstract functions are functions that are only declared in the base class. This means that
they do not contain any definition in the base class and need to be redefined in the subclass.
An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the
abstract methods of an interface
friend Function
Virtual Functions: A virtual function is a member function which is declared within a base class and is re-
Exception handling in Object-Oriented Programming is a very important concept that is used to manage errors.
An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve
them.
Error Exception
Errors are problems that should not be encountered by
Conditions that an application might try to catch
applications
48. What is a try/ catch block?
A try/ catch block is used to handle exceptions. The try block defines a set of statements that may lead to an
error. The catch block basically catches the exception.
A finally block consists of code that is used to execute important code such as closing a connection, etc. This
block executes when the try block exits. It also makes sure that finally block executes even in case some
unexpected exception is encountered.
A parameter is a variable used during the declaration of the function or subroutine, and arguments are passed to
the function body, and it should match with the parameter defined. There are two types of Arguments.
Call by Value – Value passed will get modified only inside the function, and it returns the same value
whatever it is passed into the function.
Call by Reference – Value passed will get modified in both inside and outside the functions and it returns
the same or different value.
Tokens
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as
follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
The base class is the most generalized class, and it is said to be a root class.
The superclass is the parent class from which another class inherits.
Binding is nothing but the association of a name with the class. Static Binding is a binding in which name
can be associated with the class during compilation time, and it is also called as early Binding.
Dynamic Binding is a binding in which name can be associated with the class during execution time, and
it is also called as Late Binding.
An inline function is a technique used by the compilers and instructs to insert complete body of the
function wherever that function is used in the program source code.
11) What is the difference between call by value and call by reference in C?
Description When a copy of the value is passed to When a copy of the value is passed to
the function, then the original value is the function, then the original value is
not modified. modified.
Safety In this case, actual arguments remain In this case, actual arguments are not
safe as they cannot be modified. reliable, as they are modified.
Arguments The copies of the actual arguments are The addresses of actual arguments are
passed to the formal arguments. passed to their respective formal
arguments.
calloc() malloc()
Initialization It initializes the content of the It does not initialize the content of
memory to zero. memory, so it carries the garbage
value.
Return value It returns a pointer pointing to the It returns a pointer pointing to the
allocated memory. allocated memory.
C C++
C language was developed by Dennis C++ language was developed by Bjarne
Ritchie. Stroustrup.
In C language, data and functions are the In the C++ language, both data and functions
free entities. are encapsulated together in the form of a
project.
C does not support the data hiding. C++ supports data hiding. Therefore, the data
Therefore, the data can be used by the cannot be accessed by the outside world.
outside world.
C supports neither function nor operator C++ supports both function and operator
overloading. overloading.
In C, the function cannot be implemented In the C++, the function can be implemented
inside the structures. inside the structures.
Reference variables are not supported in C C++ supports the reference variables.
language.
C language does not support the virtual and C++ supports both virtual and friend functions.
friend functions.
In C, scanf() and printf() are mainly used C++ mainly uses stream cin and cout to
for input/output. perform input and output operations.
1 stdio.h
Input/Output functions
2 conio.h
Console Input/Output functions
3 stdlib.h
General utility functions
4 math.h
Mathematics functions
5 string.h
String functions
6 ctype.h
Sr.No. Header Files & Description
Why is C still so popular?One of the very strong reasons why C programming language is so popular and used
so widely is the flexibility of its use for memory management. Programmers have opportunities to control how,
when, and where to allocate and deallocate memory. Memory is allocated statically, automatically, or
dynamically in C programming with the help of malloc and calloc functions.
Bitwise Operators
Operator Description
| Bitwise OR Operator
AND Operator
00001100
& 00011001
OR Operator
00001100
00011001
00001100
^ 00011001
_________
~ 00100011
__________
11011100
2's Complement
2's Complement :
11011011
+ 1
_________
11011100
Shift Operators
The right shift operator shifts all bits towards the right by a certain number of specified bits. It is
denoted by >>.
When we shift any number to the right, the least significant bits are discarded, while the most
significant bits are replaced by zeroes.
The left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted
by <<.
C is a mid-level and procedural programming language. The Procedural programming language is also known
as the structured programming language is a technique in which large programs are broken down into smaller
modules, and each module uses structured code. This technique minimizes error and misinterpretation.
C is known as a mother language because most of the compilers and JVMs are written in C language. Most of
the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust,
javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these
languages.
C is called a mid-level programming language because it binds the low level and high -level programming
language.
o Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into
parts
o Portable: C is highly portable means that once the program is written can be run on any machine with
little or no modifications.
o Mid Level: C is a mid-level programming language as it combines the low- level language with the
features of the high-level language.
o Structured: C is a structured language as the C program is broken into parts.
o Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
o Memory Management: C provides an inbuilt memory function that saves the memory and improves the
efficiency of our program.
o Extensible: C is an extensible language as it can adopt new features in the future.
printf(): The printf() function is used to print the integer, character, float and string values on to the screen.
scanf(): The scanf() function is used to take input from the user.
8) What is the difference between the local variable and global variable in C?
Following are the differences between a local variable and global variable:
Life Life of a variable is created when the Life of a variable exists until the
function block is entered and program is executing.
destroyed on its exit.
Storage Variables are stored in a stack unless The compiler decides the storage
specified. location of a variable.
o C functions are used to avoid the rewriting the same code again and again in our program.
o C functions can be called any number of times from any place of our program.
o When a program is divided into functions, then any part of our program can easily be tracked.
o C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes
the C program more understandable.
11) What is the difference between call by value and call by reference in C?
Following are the differences between a call by value and call by reference are:
Description When a copy of the value is passed to When a copy of the value is passed to
the function, then the original value is the function, then the original value is
not modified. modified.
Memory Actual arguments and formal arguments Actual arguments and formal
location are created in separate memory arguments are created in the same
locations. memory location.
Safety In this case, actual arguments remain In this case, actual arguments are not
safe as they cannot be modified. reliable, as they are modified.
Arguments The copies of the actual arguments are The addresses of actual arguments are
passed to the formal arguments. passed to their respective formal
arguments.
When a function calls itself, and this process is known as recursion. The function that calls itself is known as a
recursive function.
An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code
optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.
o One-dimensional array: One-dimensional array is an array that stores the elements one after the
another.
o Multidimensional array: Multidimensional array is an array that contains more than one array.
o A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the
performance fast. Whenever a variable is declared inside a program, then the system allocates some
memory to a variable. The memory contains some address number. The variables that hold this address
number is known as the pointer variable.
o Accessing array elements: Pointers are used in traversing through an array of integers and strings. The
string is an array of characters which is terminated by a null character '\0'.
o Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the
execution of a program.
o Call by Reference: The pointers are used to pass a reference of a variable to other function.
o Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data
structures like tree, graph, linked list, etc.
A pointer that doesn't refer to any address of value but NULL is known as a NULL pointer. When we assign a '0'
value to a pointer of any type, then it becomes a Null pointer.
A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far
pointer is a 32-bit pointer that obtains information outside the memory in a given section.
18) What is dangling pointer in C?
o If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory
occupied by the first pointer while the first pointer still points to that memory location, the first pointer
will be known as a dangling pointer. This problem is known as a dangling pointer problem.
o Dangling pointer arises when an object is deleted without modifying the value of the pointer. The
pointer points to the deallocated memory.
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. }
In the above example, initially memory is allocated to the pointer variable ptr, and then the memory is
deallocated from the pointer variable. Now, pointer variable, i.e., ptr becomes a dangling pointer.
The problem of a dangling pointer can be overcome by assigning a NULL value to the dangling pointer. Let's
understand this through an example:
1. #include<stdio.h>
2. void main()
3. {
4. int *ptr = malloc(constant value); //allocating a memory space.
5. free(ptr); //ptr becomes a dangling pointer.
6. ptr=NULL; //Now, ptr is no longer a dangling pointer.
7. }
In the above example, after deallocating the memory from a pointer variable, ptr is assigned to a NULL value.
This means that ptr does not point to any memory location. Therefore, it is no longer a dangling pointer.
In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to
pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer
contains the address of a first pointer. Let's understand this concept through an example:
1. #include <stdio.h>
2. int main()
3. {
4. int a=10;
5. int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.
6. ptr=&a;
7. pptr=&ptr;
8. printf("value of a is:%d",a);
9. printf("\n");
10. printf("value of *ptr is : %d",*ptr);
11. printf("\n");
12. printf("value of **pptr is : %d",**pptr);
13. return 0;
14. }
In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the
address of 'a' variable.
o In case of static memory allocation, memory is allocated at compile time, and memory can't be increased
while executing the program. It is used in the array.
o The lifetime of a variable in static memory is the lifetime of a program.
o The static memory is allocated using static keyword.
o The static memory is implemented using stacks or heap.
o The pointer is required to access the variable present in the static memory.
o The static memory is faster than dynamic memory.
o In static memory, more memory space is required to store the variable.
1. For example:
2. int a[10];
The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.
o In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased
while executing the program. It is used in the linked list.
o The malloc() or calloc() function is required to allocate the memory at the runtime.
o An allocation or deallocation of memory is done at the execution time of a program.
o No dynamic pointers are required to access the memory.
o The dynamic memory is implemented using data segments.
o Less memory space is required to store the variable.
1. For example
2. int *p= malloc(sizeof(int)*10);
1. malloc()
o The malloc() function is used to allocate the memory during the execution of the program.
o It does not initialize the memory but carries the garbage value.
o It returns a null pointer if it could not be able to allocate the requested space.
Syntax
1. ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.
2. calloc()
1. The calloc() is same as malloc() function, but the difference only is that it initializes the memory
with zero value.
Syntax
1. ptr = (cast-type*)calloc(n, element-size);// allocating the memory using calloc() function.
2. realloc()
1. The realloc() function is used to reallocate the memory to the new size.
2. If sufficient space is not available in the memory, then the new block is allocated to accommodate
the existing data.
Syntax
1. ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.
2. free():The free() function releases the memory allocated by either calloc() or malloc() function.
Syntax
1. free(ptr); // memory is released using free() function.
The above syntax releases the memory from a pointer variable ptr.
More details.
calloc() malloc()
Return value It returns a pointer pointing to the It returns a pointer pointing to the
allocated memory. allocated memory.
o The structure is a user-defined data type that allows storing multiple types of data in a single unit. It
occupies the sum of the memory of all members.
o The structure members can be accessed only through structure variables.
o Structure variables accessing the same structure but the memory allocated for each variable will be
different.
o The union is a user-defined data type that allows storing multiple types of data in a single unit. However,
it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member
only.
o In union, we can access only one variable at a time as it allocates one common space for all the members
of a union.
The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in
a program. C has the following tokens:
6. The argument passed to the main() function while executing the program is known as command line
argument. For example:
Syntax
(type_name) expression;
Yes, by holding the base address of array into a pointer, we can access the array using a pointer.
A loop running continuously for an indefinite number of times is called the infinite loop.
1. for(;;){
2. //code to be executed
3. }
1. while(1){
2. //code to be executed
3. }
1. do{
2. //code to be executed
3. }while(1);