You are on page 1of 18

OOPs

1) What is OOPS?

OOPS is abbreviated as Object Oriented Programming system in which programs are


considered as a collection of objects.

2) Name any seven widely used OOP languages.

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

3) What is the purpose of using OOPs concepts?

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.

4) What are the four main features of OOPs?

The OOP has the following four features:

o Inheritance
o Encapsulation
o Polymorphism
o Data Abstraction

5) Why OOP is so popular?

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.
6) What are the advantages and disadvantages of OOP?

Advantages of OOP

o It follows a bottom-up approach.


o It models the real word well.
o It allows us the reusability of code.
o Avoids unnecessary data exposure to the user by using the abstraction.
o OOP forces the designers to have a long and extensive design phase that results in
better design and fewer flaws.
o Decompose a complex problem into smaller chunks.
o Programmer are able to reach their goals faster.
o Minimizes the complexity.
o Easy redesign and extension of code that does not affect the other functionality.

Disadvantages of OOP

o Proper planning is required.


o Program design is tricky.
o Programmer should be well skilled.
o Classes tend to be overly generalized.

7) What are the limitations of OOPs?


o Requires intensive testing processes.
o Solving problems takes more time as compared to Procedure Oriented Programming.
o The size of the programs created using this approach may become larger than the
programs written using the procedure-oriented programming approach.
o Software developed using this approach requires a substantial amount of pre-work
and planning.
o OOP code is difficult to understand if you do not have the corresponding class
documentation.
o In certain scenarios, these programs can consume a large amount of memory.
o Not suitable for small problems.
o Takes more time to solve problems.

 What is the difference between OOP and SOP?


Object-Oriented Programming Structural Programming

Object-Oriented Programming is a type of


Provides logical structure to a program where
programming which is based on objects
programs are divided functions
rather than just functions and procedures

Bottom-up approach Top-down approach

Provides data hiding Does not provide data hiding

Can solve problems of any complexity Can solve moderate problems

Code can be reused thereby reducing


Does not support code reusability
redundancy

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.

11) What are the differences between class and object?

Class Object

It is a logical entity. It is a real-world entity.

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 is a data type that represents the blueprint of It is an instance of the class.


an object.

It is declared once. Multiple objects can be declared as and when


required.

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 supports inheritance. It does not support inheritance.

Member variables cannot be initialized directly. Member variables can be initialized


directly.

It is of type reference. It is of a type value.

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.

An instance of a class is an object. An instance of a structure is a structure


variable.
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

28) What are the access modifiers?

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

Accessibility from Accessibility from Accessibility from


Name
own class derived class world
Public Yes Yes Yes
Private Yes No No
Protected Yes Yes No

14) What are the manipulators in OOP and how it works?

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

There are two types of manipulators with arguments or without arguments.

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.
Rules for constructor are:

 Constructor Name should be the same as a class name.


 A constructor must have no return type.

37. What is a destructor?

A destructor is a method that is automatically invoked when an object is destroyed.

38. Types of constructors

 Default constructor
 Parameterized constructor
 Copy constructor
 Static constructor
 Private constructor

 Default Constructor- A constructor with no parameters is known as a default


constructor.

 Parameterized Constructor- In C++, a constructor with parameters is known as a

parameterized constructor. This is the preferred method to initialize member data.

 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.

 41. What is Garbage Collection(GC)?


GC is an implementation of automatic memory management. The Garbage collector
frees up space occupied by objects that are no longer in existence.

10. What is inheritance?

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.

11. What are the different types of inheritance?

 Single inheritance
 Multiple inheritance
 Multilevel inheritance
 Hierarchical inheritance
 Hybrid inheritance
12. What is the difference between multiple and multilevel inheritance?

Multiple Inheritance Multilevel Inheritance


Multilevel inheritance means a class inherits
Multiple inheritance comes into picture when
from another class which itself is a subclass
a class inherits more than one base class
of some other base class
Example: A class describing a sports car will
Example: A class defining a child inherits
inherit from a base class Car which inturn
from two base classes Mother and Father
inherits another class Vehicle
13. What is hybrid inheritance?

Hybrid inheritance is a combination of multiple and multi-level inheritance.

14. What is hierarchical 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.

15. What are the limitations of inheritance?

 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

16. What is a superclass?


 A superclass or base class is a class that acts as a parent to some other class or classes.
For example, the Vehicle class is a superclass of class Car.

17. What is a subclass?


 A class that inherits from another class is called the subclass. For example, the class
Car is a subclass or a derived of Vehicle class.

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.

What is static polymorphism?

Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time.
An example of compile-time polymorphism is method overloading.

20. What is dynamic polymorphism?


Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of
polymorphism which is resolved during runtime. An example of runtime polymorphism is
method overriding.

 What is method overloading?

If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.

 What is method overriding

If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding

23. What is operator overloading?

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.

28. What is data abstraction?

Data abstraction is a very important feature of OOPs that allows displaying only the
important information and hiding the implementation details.

29. How to achieve data abstraction?

Data abstraction can be achieved through:

 Abstract class
 Abstract method

30. What is an abstract class?

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.
31. Can you create an instance of an abstract class?

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

33. Differentiate between data abstraction and encapsulation.

Data abstraction Encapsulation


Solves the problem at the implementation
Solves the problem at the design level
level
Allows showing important aspects while Binds code and data together into a single
hiding implementation details unit and hides it from the world

34. What are virtual functions?

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.

35. What are pure virtual functions?

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.

20) What is the super keyword?

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

22) What is an interface?

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

A friend function can access the private and protected data of a class. We declare a friend
function using the friend keyword inside the body of the class.

Virtual Functions

A virtual function is a member function which is declared within a base class and is re-
defined(Overriden) by a derived class.
46. What is exception handling?

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.

47. What is the difference between an error and an exception?

Error Exception
Errors are problems that should not be Conditions that an application might try to
encountered by applications catch
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.

49. What is a finally block?

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.

19) What are the different types of arguments?

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.

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

Following are the operators that cannot be overloaded -.

1. Scope Resolution (::)


2. Member Selection (.)
3. Member selection through a pointer to function (.*)

What is ‘this’ pointer?


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.

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

What are a base class, subclass, and superclass?

The base class is the most generalized class, and it is said to be a root class.

A Subclass is a class that inherits from one or more base classes.

The superclass is the parent class from which another class inherits.

What is static and dynamic Binding?

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 Bindin

What is an Inline function?

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?

Call by value Call by reference

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 Actual arguments and formal


location arguments are created in separate arguments are created in the same
memory 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.

What is the difference between malloc() and calloc()?

calloc() malloc()

Description The malloc() function allocates a The calloc() function allocates


single block of requested memory. multiple blocks of requested memory.

Initialization It initializes the content of the It does not initialize the content of
memory to zero. memory, so it carries the garbage
value.

Number of It consists of two arguments. It consists of only one argument.


arguments

Return value It returns a pointer pointing to the It returns a pointer pointing to the
allocated memory. allocated memory.

Why is C known as a mother language?


It introduces new core concepts like arrays, functions, file handling which are used in these
languages.

C++ Data Types


3) What is the difference between C and C++?

C C++

C language was developed by Dennis C++ language was developed by Bjarne


Ritchie. Stroustrup.

C is a structured programming language. C++ supports both structural and object-


oriented programming language.

C is a subset of C++. C++ is a superset of C.

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.

Sr.No. Header Files & Description

1 stdio.h
Input/Output functions
Sr.No. Header Files & Description

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
Character handling functions

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 AND Operator

| Bitwise OR Operator

^ Bitwise XOR Operator

~ Bitwise Complement Operator


<< Bitwise Shift Left Operator

>> Bitwise Shift Right Operator

AND Operator

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

//Bitwise AND Operation of 12 and 25

00001100

& 00011001

_________

00001000 = 8 (In decimal)

OR Operator

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25

00001100

| 00011001

_________

00011101 = 29 (In decimal)


Bitwise XOR Operator

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25

00001100

^ 00011001

_________

00010101 = 21 (In decimal)

Bitwise Complement Operator

35 = 00100011 (In Binary)

// Using bitwise complement operator

~ 00100011

__________

11011100

2's Complement

36 = 00100100 (In Binary)

1's Complement = 11011011

2's Complement :
11011011

+ 1

_________

11011100

Shift Operators

There are two shift operators in C++ programming:

 Right shift operator >>


 Left shift operator <<
Right Shift Operator

 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.

One bit Right Shif

Left Shift Operator

 The left shift operator shifts all bits towards the left by a certain number of specified
bits. It is denoted by <<.

One bit Left Shift

You might also like