oop-merged (1)
oop-merged (1)
com/
1) What is OOPS?
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
3) What is a class?
4) What is an Object?
An object is an instance of a class. It has its own state, behavior, and identity.
5) What is Encapsulation?
Encapsulation is an attribute of an object, and it contains all data which is hidden. That hidden
data can be restricted to the members of that class.
1/8
https://career.guru99.com/
6) 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.
7) What is Inheritance?
Inheritance is a concept where one class shares the structure and behavior defined in another
class. If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance.
Manipulators are the functions which can be used in conjunction with the insertion () operators
on an object. Examples are endl and setw.
A constructor is a method used to initialize the state of an object, and it gets invoked at the time
of object creation. Rules for constructor are:
A destructor is a method which is automatically called when the object is made of scope or
destroyed. Destructor name is also same as class name but with the tilde symbol before the
name.
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.
A virtual function is a member function of a class, and its functionality can be overridden in its
derived class. This function can be implemented by using a keyword called virtual, and it can be
given during function declaration.
A virtual function can be declared using a token(virtual) in C++. It can be achieved in C/Python
Language by using function pointers or pointers to function.
2/8
https://career.guru99.com/
A friend function is a friend of a class that is allowed to access to Public, private, or protected
data in that same class. If the function is defined outside the class cannot access such
information.
A friend can be declared anywhere in the class declaration, and it cannot be affected by access
control keywords like private, public, or protected.
Function overloading is a regular function, but it can perform different tasks. It allows the
creation of several methods with the same name which differ from each other by the type of
input and output of the function.
Example
[crayon-5f2cf66ff2e66074243655/]
15) What is operator overloading?
Operator overloading is a function where different operators are applied and depends on the
arguments. Operator,-,* can be used to pass through the function, and it has its own
precedence to execute
An abstract class is a class which cannot be instantiated. Creation of an object is not possible
with an abstract class, but it can be inherited. An abstract class can contain only an Abstract
method. Java allows only abstract method in abstract class while other languages allow non-
abstract method as well.
The ternary operator is said to be an operator which takes three arguments. Arguments and
results are of different data types, and it depends on the function. The ternary operator is also
called a conditional operator.
Finalize method helps to perform cleanup operations on the resources which are not currently
used. Finalize method is protected, and it is accessible only through this class or by a derived
class.
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.
3/8
https://career.guru99.com/
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.
The super keyword is used to invoke the overridden method, which overrides one of its
superclass methods. This keyword allows to access overridden methods and also to access
hidden members of the superclass.
Method overriding is a feature that allows a subclass to provide the implementation of a method
that overrides in the main class. It will override the implementation in the superclass by
providing the same method name, same parameter, and same return type.
An exception is an event that occurs during the execution of a program. Exceptions can be of
any type – Runtime exception, Error exceptions. Those exceptions are adequately handled
through exception handling mechanism like try, catch, and throw keywords.
A compiler recognizes a token, and it cannot be broken down into component elements.
Keywords, identifiers, constants, string literals, and operators are examples of tokens.
Even punctuation characters are also considered as tokens. Example: Brackets, Commas,
Braces, and Parentheses.
4/8
https://career.guru99.com/
Overriding is the same method names with the same arguments and return types associated
with the class and its child class.
An object is an instance of a class. Objects hold multiple information, but classes don’t have
any information. Definition of properties and functions can be done in class and can be used by
the object.
Abstraction is a useful feature of OOPS, and it shows only the necessary details to the client of
an object. Meaning, it shows only required details for an object, not the inner constructors, of an
object. Example – When you want to switch on the television, it is not necessary to know the
inner circuitry/mechanism needed to switch on the TV. Whatever is required to switch on TV will
be shown by using an abstract class.
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
Sealed modifiers are the access modifiers where the methods can not inherit it. Sealed
modifiers can also be applied to properties, events, and methods. This modifier cannot be used
to static members.
30) How can we call the base method without creating an instance?
Yes, it is possible to call the base method without creating an instance. And that method should
be “Static method."
Doing Inheritance from that class.-Use Base Keyword from a derived class.
5/8
https://career.guru99.com/
The new modifier instructs the compiler to use the new implementation instead of the base class
function. Whereas, Override modifier helps to override the base class function.
– Parametric Constructor – With Parameters. Create a new instance of a class and also
passing arguments simultaneously.
Early binding refers to the assignment of values to variables during design time, whereas late
Binding refers to the assignment of values to variables during run time.
THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which
differentiates between the current object with the global object. It refers to the current object.
The default access type of a Structure is public, but class access type is private. A structure is
used for grouping data, whereas a class can be used for grouping data and methods. Structures
are exclusively used for data, and it doesn't require strict validation, but classes are used to
encapsulate and inherent data, which requires strict validation.
A pure virtual function is a function which can be overridden in the derived class but cannot be
defined. A virtual function can be declared as Pure by using the operator =0.
Example -
[crayon-5f2cf66ff2e6f715458999/]
38) What are all the operators that cannot be overloaded?
6/8
https://career.guru99.com/
Dynamic or Run time polymorphism is also known as method overriding in which call to an
overridden function is resolved during run time, not at the compile time. It means having two or
more methods with the same name, same signature but with different implementation.
This is a special constructor for creating a new object as a copy of an existing object. There will
always be only one copy constructor that can be either defined by the user or the system.
42) What does the keyword virtual represented in the method definition?
False.
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.
7/8
https://career.guru99.com/
Zero instances will be created for an abstract class. In other words, you cannot create an
instance of an Abstract Class.
50) Which OOPS concept exposes only the necessary information to the calling
functions?
Encapsulation
8/8
Powered by TCPDF (www.tcpdf.org)
Free Mock Coding Interview
Looking to crack SDE Interviews?
Get a personalised Learning Plan for FREE!
In simple language, a class can be considered as the blueprint or template, based on which
objects can be created. So the Objects are considered the instance of a class, and are therefore
sometimes called “instances”. The term “characteristics” refers to the “what” about the
Object, and the term “behavior” refers to the “how” about the Object.
Class = A specific car model, such as Audi A4, BMW I8, Maruti Suzuki Vitara Brezza, etc.
Object = A specific car of any model, like the car you own
Characteristics = What is the color of your car? What is the Chassis number of your car?
etc
Behavior = How to start the car? How to change the gear of the car? etc.
Characteristics are also known as data, attributes, or properties, and Behaviours are also known
as the functions, procedures or methods, in the programming language.
The concept of “objects” allows the OOPs model to easily access, use and modify the instance
data and methods, interact with other objects, and define methods in runtime (during the
execution of the program). This gives the OOPs model significance and makes it diverse in its
implementation.
In fact, the OOPs model is so popular, that many of the most widely used programming
languages support and use this Object Oriented Programming or OOPs model, such as Java,
C++, Python, C#, etc.
OOPs helps users to understand the software easily, although they don’t know the actual
implementation.
With OOPs, the readability, understandability, and maintainability of the code increase
multifold.
Even very big software can be easily written and managed easily using OOPs.
3. What are some major Object Oriented Programming languages?
The programming languages that use and follow the Object-Oriented Programming paradigm
or OOPs, are known as Object-Oriented Programming languages. Some of the major Object-
Oriented Programming languages include:
Java
C++
Javascript
Python
PHP
Nearly all programming paradigms include Structured programming, including the OOPs
model.
Inheritance
Encapsulation
Polymorphism
Data Abstraction
7. What are some advantages of using OOPs?
OOPs is very helpful in solving very complex level of problems.
Highly complex programs can be created, handled, and maintained easily using object-
oriented programming.
OOPs, promote code reuse, thereby reducing redundancy.
OOPs also helps to hide the unnecessary details with the help of Data Abstraction.
OOPs, are based on a bottom-up approach, unlike the Structural programming paradigm,
which uses a top-down approach.
Polymorphism offers a lot of flexibility in OOPs.
9. What is a class?
A class can be understood as a template or a blueprint, which contains some values, known as
member data or member, and some set of rules, known as behaviors or functions. So when an
object is created, it automatically takes the data and functions that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can create as many
objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created based on
that template.
One can visualize Encapsulation as the method of putting everything that is required to do the
job, inside a capsule and presenting that capsule to the user. What it means is that by
Encapsulation, all the necessary data and methods are bind together and all the unnecessary
details are hidden to the normal user. So Encapsulation is the process of binding data
members and methods of a program together to do a specific job, without revealing
unnecessary details.
In OOPs, Polymorphism refers to the process by which some code, data, method, or object
behaves differently under different circumstances or contexts. Compile-time polymorphism
and Run time polymorphism are the two types of polymorphisms in OOPs languages.
Example:
// In this program, we will see how multiple functions are created with the same nam
// but the compiler decides which function to call easily at the compile time itself
class CompileTimePolymorphism{
// 1st method with name add
public int add(int x, int y){
return x+y;
}
// 2nd method with name add
public int add(int x, int y, int z){
return x+y+z;
}
// 3rd method with name add
public int add(double x, int y){
return (int)x+y;
}
// 4th method with name add
public int add(int x, double y){
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
CompileTimePolymorphism demo=new CompileTimePolymorphism();
// In the below statement, the Compiler looks at the argument types and decides t
System.out.println(demo.add(2,3));
// Similarly, in the below statement, the compiler calls method 2
System.out.println(demo.add(2,3,4));
// Similarly, in the below statement, the compiler calls method 4
System.out.println(demo.add(2,3.4));
// Similarly, in the below statement, the compiler calls method 3
System.out.println(demo.add(2.5,3));
}
}
In the above example, there are four versions of add methods. The first method takes two
parameters while the second one takes three. For the third and fourth methods, there is a
change of order of parameters. The compiler looks at the method signature and decides which
method to invoke for a particular method call at compile time.
Example:
class AnyVehicle{
public void move(){
System.out.println(“Any vehicle should move!!”);
}
}
class Bike extends AnyVehicle{
public void move(){
System.out.println(“Bike can move too!!”);
}
}
class Test{
public static void main(String[] args){
AnyVehicle vehicle = new Bike();
// In the above statement, as you can see, the object vehicle is of type AnyVehi
// But the output of the below statement will be “Bike can move too!!”,
// because the actual implementation of object ‘vehicle’ is decided during runtim
vehicle = new AnyVehicle();
// Now, the output of the below statement will be “Any vehicle should move!!”,
vehicle.move();
}
}
As the method to call is determined at runtime, as shown in the above code, this is called
runtime polymorphism.
Compile Time Polymorphism: C++ supports compile-time polymorphism with the help of
features like templates, function overloading, and default arguments.
Runtime Polymorphism: C++ supports Runtime polymorphism with the help of features
like virtual functions. Virtual functions take the shape of the functions based on the type
of object in reference and are resolved at runtime.
Now here, the method called after “new” keyword - MyClass(), is the constructor of this class.
This will help to instantiate the member data and methods and assign them to the object
myClassObject.
20. What are the various types of constructors in C++?
The most common classification of constructors includes:
Default constructor: The default constructor is the constructor which doesn’t take any
argument. It has no parameters.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known as
parameterized constructors.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
Copy constructor: A copy constructor is a member function that initializes an object using
another object of the same class.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
23. Are class and structure the same? If not, what's the difference
between a class and a structure?
No, class and structure are not the same. Though they appear to be similar, they have
differences that make them apart. For example, the structure is saved in the stack memory,
whereas the class is saved in the heap memory. Also, Data Abstraction cannot be achieved with
the help of structure, but with class, Abstraction is majorly used.
Let me explain to you with a common example. Let's take three different vehicles - a car, truck,
or bus. These three are entirely different from one another with their own specific
characteristics and behavior. But. in all three, you will find some common elements, like
steering wheel, accelerator, clutch, brakes, etc. Though these elements are used in different
vehicles, still they have their own features which are common among all vehicles. This is
achieved with inheritance. The car, the truck, and the bus have all inherited the features like
steering wheel, accelerator, clutch, brakes, etc, and used them as their own. Due to this, they
did not have to create these components from scratch, thereby facilitating code reuse.
Single inheritance
Multiple inheritances
Multi-level inheritance
Hierarchical inheritance
Hybrid inheritance
Whereas Overriding is a runtime polymorphism feature in which an entity has the same name,
but its implementation changes during execution. For example, Method overriding.
Image
Also, an abstract class can contain abstract methods as well as non-abstract methods.
Garbage collection refers to this mechanism of handling the memory in the program. Through
garbage collection, the unwanted memory is freed up by removing the objects that are no
longer needed.
However, on the other hand, C++ can be implemented without OOPs, as it also supports the
C-like structural programming model.
class BaseClass2 {
public:
BaseClass2()
{ cout << "BaseClass2 constructor called" << endl; }
};
int main()
{
DerivedClass derived_class;
return 0;
}
Output:
Reason:
The above program demonstrates Multiple inheritances. So when the Derived class’s
constructor is called, it automatically calls the Base class's constructors from left to right order
of inheritance.
static
{
System.out.println(“a”);
i = 100;
}
}
System.out.println(Scaler.i);
}
}
Output:
b
c
a
100
Reason:
Firstly the static block inside the main-method calling class will be implemented. Hence ‘b’ will
be printed first. Then the main method is called, and now the sequence is kept as expected.
class ClassA {
public:
ClassA(int ii = 0) : i(ii) {}
void show() { cout << "i = " << i << endl;}
private:
int i;
};
class ClassB {
public:
ClassB(int xx) : x(xx) {}
operator ClassA() const { return ClassA(x); }
private:
int x;
};
void g(ClassA a)
{ a.show(); }
int main() {
ClassB b(10);
g(b);
g(20);
getchar();
return 0;
}
Output:
i = 10
i = 20
Reason:
ClassA contains a conversion constructor. Due to this, the objects of ClassA can have integer
values. So the statement g(20) works. Also, ClassB has a conversion operator overloaded. So
the statement g(b) also works.
Output:
Main1
Reason:
Here the main() method is overloaded. But JVM only understands the main method which has
a String[] argument in its definition. Hence Main1 is printed and the overloaded main method
is ignored.
#include<iostream>
using namespace std;
class BaseClass{
int arr[10];
};
int main(void)
{
cout<<sizeof(DerivedClass);
return 0;
}
Output:
If the size of the integer is 4 bytes, then the output will be 80.
Reason:
Since DerivedBaseClass1 and DerivedBaseClass1 both inherit from class BaseClass,
DerivedClass contains two copies of BaseClass. Hence it results in wastage of space and a large
size output. It can be reduced with the help of a virtual base class.
#include<iostream>
class B : public A {
public:
void print()
{ cout <<" Inside B"; }
};
class C: public B {
};
int main(void)
{
C c;
c.print();
return 0;
}
Output:
Inside B
Reason:
The above program implements a Multi-level hierarchy. So the program is linearly searched up
until a matching function is found. Here, it is present in both classes A and B. So class B’s
print() method is called.
Smalltalk
C++
PHP
Java
Adele Goldberg
Dennis Ritchie
Alan Kay
Andrea Ferro
Encapsulation
Polymorphism
Exception
Abstraction
5. Which language among the following supports classes, but does not support the concept of
Polymorphism?
C++ programming language
C# programming language
7. _______ is the feature of the Object-oriented programming model which allows one class to
derive features from another class?
Inheritance
Data hiding
Encapsulation
Polymorphism
8. ________ is an object-oriented programming language but does not support all inheritance
types.
Smalltalk
Kotlin
Java
C++
9. Which among the following operators can be used to show Polymorphism in CPP?
10. Which among the following does not show or cannot be used, to show Polymorphism?
11. Which among the following functions represent the concept of Polymorphism?
Virtual function
Inline function
Undefined function
True
False
13. _ is the feature of OOPs which is responsible for binding data with their implementation as
a single entity?
Polymorphism
Inheritance
Abstraction
Encapsulation
Public member
Protected member
Private member
15. State true or false: A Java application can be created without implementing the OOPs
concept.
True
False