Oops Question Bank

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

OOPS QUESTION BANK

2 MARKS QUESTION

1) What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.

2) What is OOPS?
C++ is an object-oriented programming language developed by Bjarne Stroustrup.
C++ is a cross-platform language that can be used to create high- performance applications.
C++ is a versatile language for handling very large programs.

3) Write basic concepts of OOPS?


Following are the concepts of OOPS:
1. Class and Object
2. Abstraction
3. Encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic Binding

4) What is a class?
A class is simply a representation of a type of object. It is the blueprint/plan/template that
describes the details of an object.

5) What is an Object?
An object is an instance of a class. It has its own state, behavior, and identity.

6) 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.
Levels are Public, Protected, Private, Internal, and Protected Internal.

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

8) 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.
9) What are manipulators?
Manipulators are the functions which can be used in conjunction with the insertion (<<) and
extraction (>>) operators on an object. Examples are endl and setw.

10) Explain the term constructor


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:
 Constructor Name should be the same as a class name.
 A constructor must have no return type.

11) Define Destructor?


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.

12) 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.

13) What is a virtual function?


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.

14) What is a friend function?


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.

15) What is function overloading?


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.

16) 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

17) What is an abstract class?


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.

18) What is a ternary operator?


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.

19) What is the use of finalize method?


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.

20) 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.

21) 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.
It also forwards a call from a constructor, to a constructor in the superclass.

22) What is method overriding?


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.

23) 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.
Java uses Interface to implement multiple inheritances.

24) What is exception handling?


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.

25) What are tokens?


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.

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


Overloading is static Binding, whereas Overriding is dynamic Binding. Overloading is nothing
but the same method with different arguments, and it may or may not return the equal value in
the same class itself.
Overriding is the same method names with the same arguments and return types associated
with the class and its child class.
4 MARKS QUESTION

1) Explain the POP and OOP.

S. Procedural Oriented Programming Object-Oriented Programming


No.

1. In procedural programming, program is In object-oriented programming, program is


divided into small parts called divided into small parts called objects.
functions.
2. Procedural programming follows top Object-oriented programming follows
down approach. bottom up approach.
3. There is no access specifier in Object-oriented programming has access
procedural programming. specifiers like private, public, protected etc.

4. Adding new data and function is not Adding new data and function is easy.
easy.
5. Procedural programming does not have Object-oriented programming provides data
any proper way for hiding data so it is hiding so it is more secure.
less secure.
6. In procedural programming, Overloading is possible in object-oriented
overloading is not possible. programming.
7. In procedural programming, function is In object-oriented programming, data is
more important than data. more important than function.

8. Procedural programming is based on Object-oriented programming is based on


unreal world. real world.
9. Examples : C, FORTRAN, Pascal, Examples : C++, Java, Python, C# etc.
Basic etc.
 OOP Paradigm

The basic idea behind object oriented programming is to combine into a single unit both the
data and the functions that operate on the data.

This unit is called as object. The functions in the object are called member functions and the
data within it is called instance variables

The data can’t be accessed directly, it can be accessed using function. Hence accidental use
of data can be avoided in object oriented programming.

This property is known as data hiding. The data and related functions are enclosed within an
object. This is known as data encapsulation.

 Need of Object-Oriented Programming

Major motivation of object oriented programming is to overcome the limitations of procedural


programming.

The object oriented programming is used in the applications in which –

1. Emphasis is on data rather than procedures.


2. Programs can be divided into known objects.
3. Data needs to be hidden from the outside functions.
4. New data needs to be added frequently for maintaining the code.
5. Objects need to communicate with each other.
6. Some common properties are used by various functions.
Benefits of OOP

Following are some advantages of object oriented programming –

1. Using inheritance the redundant code can be eliminated and the existing classes can be used.

2. The standard working modules can be created using object oriented programming. These
modules can then communicate to each other to accomplish certain task.

3. Due to data hiding property, important data can be kept away from unauthorized access.

4. It is possible to create multiple objects for a given class.

5. For upgrading the system from small scale to large scale is possible due to object oriented
feature.

6. Due to data centered nature of object oriented programming most of the details of the
application model can be captured.

7. Message passing technique in object oriented programming allows the objects to communicate
to the external systems.

8. Partitioning the code for simplicity, understanding and debugging is possible due to object
oriented and modular approach
2) Explain the various features of OOPs

Class:
 A class in C++ is a user-defined type declared with class keyword.
 A class has data and functions as its members.
 Its data & function is accessed by the three access specifiers private, protected or public.
 By default access to members of a C++ class is private.
 A C++ class is like a blueprint for an object.

Declaration of Class

 A class is defined in C++ using keyword class followed by the name of class.
 The body of class is defined inside the curly brackets and terminated by a semicolon at
the end.

Syntax:

Class class_Name
{
private/public/protected : Data_type variable ;
…………………….. ; Return_type fun(arg.);
…………………….. ;
};
Object
 An Object is an instance of a Class.
 When a class is defined, no memory is allocated but when an object is created memory
is allocated.

Declaring Objects:
 When a class is defined, only the specification for the object is defined; no memory or
storage is allocated.
 To use the data and access functions defined in the class, you need to create objects.

Syntax:
Class_Name Object_Name ;

 Write the difference between class and structure.

Class Structure

Classes are of reference types. Structure are of value types.

Class has limitless features. Structure has limited features.

Class is generally used in large programs. Structure are used in small programs.

A Structure is not allowed to inherit from


A Class can inherit from another class. another Structure or class.

The data member of a class can be protected. The data member of Structure can’t be
protected.

Function member of the class can be virtual or Function member of the Structure cannot be
abstract. virtual or abstract.
Abstraction:
Data abstraction means representing only essential features by hiding all the implementation details.
In C++, class is an entity used for data abstraction purpose.

Example
class Student {
int roll;
char name [10];
public;
void input ( );
void display ( );
}:

In main function we can access the functionalities using object. For instance

Student obj;
obj.input ( );
obj.display ( );

Thus only abstract representation can be presented, using class.

Information Hiding:

The data member of member function of a class can be declared as public or private. If particular data
attribute is declared as public then it is accessible to any other class.
But if the data member is declared as private then only the member function of that class can access
the data values. Another class cannot access these data values.
This property is called data hiding.

Advantages of Abstraction

The advantages of abstraction are as follows:

 It enables code reuse by avoiding code duplication.

 It enhances software security by making only necessary information available to the


users and hiding the complex ones.
Encapsulation

Encapsulation means enclosing the data/variables and the methods for manipulating the data
into a single entity called a class.

It helps to hide the internal implementation of the functions and state of the variables,
promoting abstraction.

 Difference between Data Abstraction and Data Encapsulation

Data Encapsulation Data Abstraction

It is a process of binding data members of a It is the process of eliminating unimportant


class to the member functions of that class details of a class. In this process only important
properties are highlighted.
Data encapsulation depends upon object data Data abstraction is independent upon object
type. data type.

It is used in software implementation phase. It is used in software design phase.


Data abstraction is represented by using
Data encapsulation can be achieved by abstract classes.
inheritance.

Polymorphism

The word polymorphism means to have many forms. So, by using polymorphism, you can add
different meanings to a single component.

There are two types of polymorphism:

 Run-time polymorphism

 Compile-time polymorphism

 4 unit me explain krne hai


Inheritance

Inheritance is one of the most important features of object oriented programming. It allows a class to
inherit the properties and methods of another class called the parent class, the base class, or the
super-class.

The class that inherits is called the child class or sub-class.

It helps to avoid duplication of codes by allowing code reuse as you need not define the same
methods and properties present in a super-class in the sub-classes again. The sub-class can simply
inherit them.

Types of inheritance

 Single Inheritance.
 Multiple Inheritance.
 Multilevel Inheritance.
 Hierarchical Inheritance.
 Hybrid Inheritance

 UNIT 4 me krne hai


3) Explain the introduction of c++

 C++ as Object Oriented Programming Language

The language C++ (pronounced as see plus plus) was developed by Bjarne Strousstrup in 1979 at
Bell Labs.

It is popularly known as a object oriented programming language. This language is compiled.


C++ began as an enhancement to C, first by adding classes, virtual functions, inheritance and
many other features.

 Difference between C and C++

Class Structure

C is a procedure oriented language. C++ is an object oriented programming


language.

C makes use of top down approach of problem C++ makes use of bottom up approach of
solving. problem solving.

The input and output is done using scanf and The input and output is done using cin and cout
printf statements. statements.

The I/O operations are supported by iostream.h


The I/O operations are supported by stdio.h header file.
header file.

C does not support inheritance, polymorphism, C++ supports inheritance, polymorphism, class
class and object concepts. and object concepts.

The data type specifier or format specifier (%d, The format specifier is not required in cin and
%f, %c) is required in printf and scanf functions. cout functions.
 Structure of C++

There are four sections in the structure of C++ program –

Include file section

Class declaration section

Function definition section

Main function definition

4) Describe briefly the term namespace, identifiers, variables constants, enum

A. Namespace :
Namespace defines a scope for the identifiers that are used in a program.
For using the identifiers defined in the namespace scope we must include the using directive :
using namespace std;
Here, std is the namespace where standard class libraries are defined.
This directive will bring all the identifiers defined in std to the current global scope.

B. Identifiers :
Identifiers refer to the names of variables, functions, arrays, classes, etc., which are created by the
programmer.
They are the fundamental requirement of any language.
Each language has its own rules for naming these identifiers. Following are the rules for C++ :
Only alphabetic characters, digits and underscores are permitted.
The name cannot start with a digit.
Uppercase and lowercase letters are distinct.
A declared keyword cannot be used as a variable name.

C. Constants :
Constants refer to fixed values that do not change during the execution of a program.
They include integers, characters, floating point numbers and strings.
Literal constant do not have memory locations.
Examples :
123 // decimal integer
12.34 // floating point integer
037 // octal integer
0X2 // hexadecimal integer
“C++” // string constant
‘A’ // character constant
L ‘ab’ // wide-character constant

D. Enumerated :
An enum specifier defines the set of all names that will be permissible values of the type. These
permissible values are called members.
The enum type days_of_week has seven members : Sun, Mon, Tue, and so on, up to Sat.
Enumerated means that all the values are listed.
This is unlike the specification of an int, for example, which is given in terms of a range of possible
values.
In an enum you must give a specific name to every possible value.

E. Variables :
A variable is a symbolic name that can be given a variety of values.
Variables are stored in particular places in the computer’s memory.
When a variable is given a value, that value is actually placed in the memory space occupied by
the variable.
Most popular languages use the same general variable types, such as integers, floating-point
numbers, and characters.

5) What are the different types of operators used in C++ ?

Memory Management Operators :

C++ defines two unary operators new and delete that perform the task of allocating and
freeing the memory in a easier way.
Since these operators manipulate memory on the free store, they are also known as free
store operators.
The new operator can be used to create objects of any type. It takes the following general
form :

pointer-variable = new data-type;

When a data object is no longer needed, it is destroyed using the delete operator.
Manipulators :

Manipulators are operators that are used to format the data display.
The most commonly used manipulators are endl and setw.
The endl manipulator, when used in an output statement, causes a linefeed to be inserted.
It has the same effect as using the newline character “\n”.

Type cast operator :

C++ permits explicit type conversion of variables or expressions using the type cast
operator.

The following two versions are equivalent :

(type-name) expression // C notation


type-name (expression) // C++ notation
A type-name behaves as if it is a function for converting values to a designated type.
The function-call notation usually leads to simplest expressions.
However, it can be used only if the type is an identifier.

For example, p = int * (q);

is illegal. In such cases, we must use C type notation.


p = (int *) q;
6) Describe various control statements used in C++ ?

1) The if statement : The if statement is implemented in two forms :


 Simple if statement
 if...else statement
2) The switch statement :
This is a multiple-branching statement where, based on a condition, the control is
transferred to one of the many possible points.
This is implemented as follows : switch (expression)
{
case1 :
{
action1;
}
case2 :
{
action2;
}
case3 :
{
action3;
}
default :
{
action4;
}
}
action5;
3) The do-while statement :
The do-while is an exit-controlled loop. Based on a condition, the control is transferred
back to a particular point in the program.
The syntax is as follows : do
{
action;
}
while (condition is true); action2;

4) The while statement :


This is an entry-controlled loop structure.
The syntax is as follows : while (condition is true)
{
action1;
}
action2;

5) The for statement :


The for is an entry-controlled loop and is used when an action is to be repeated for a
predetermined number of times.
The syntax is as follows :
for (initial value; test; increment)
{
action1;
}
action2;
7) What is class access specifiers

Class Access Specifiers


Data hiding is one of the important features of Object Oriented Programming
which allows preventing the functions of a program to access directly the internal
representation of a class type. The access restriction to the class members is
specified by public, private, and protected sections within the class body. The
keywords public, private, and protected are called access specifiers. The default
access specifier for members is private.

The Public Members

in child classes which are called derived classes


A public member is accessible from anywhere outside the class but within a
program. You can set and get the value of public data members even without
using any member function.

The Private Members

A private member cannot be accessed from outside the class. Only the class
member functions can access private members. By default all the members of a
class would be private.

The Protected Members

A protected member is very similar to a private member but it Provides one


additional benefit that they can be accessed (inherited classes).
Example

 Difference b/w private, public, and protected


8) Define the Scope Resolution Operator.

The Scope Resolution Operator (::) can be used to uncover the hidden variable and function.
9) Define the Static Data member and Static Function member .

 STATIC DATA MEMBER

It is a variable which is declared with static keyword. when a data member is defined as static,
only one variable is created in the memory even if there are many object of that class.
A state data item is useful when all object of the same class mush shore a common item of
information.
All static data is initialized to zero when the first object is created, if no initialization present there.
Any change in the static data member through one member function will reflect in all member
function and state member function.
Declaration :
Static data_type member_name;
Example

#include<iostream>
using namespace std;
class ABC{
static int x; // Declaration of static data member
public:
static void fun() // Declaration of static member function
{
Cout<<”value of x : “<<x;
}
};

Int ABC::x=10; // Define static data member

int main()
{
ABC obj; // // Access without static member function
x.fun();
cout<<ABC::x; // Access without static member function

return 0;
}
 STATIC FUNCTION MEMBER

A static member function is a special member function which is used to acess only static data
member we can use a static member function to determine. where object of the class have been
created or not.

Example

#Include<lostream>

Using namespace std;

Class item{
Static string name;
Static Int a:

public:

Static void item_details(){

cout<<"Enter the name of item";


cin>>name;
cout<<" enter the quantity of team";
cin>>a;
}
};

int main(){

item obj;
Obj.item_detaila();
Cout<<item:: item-details();
Return 0;
}
 FRIEND FUNCTION

when a function is declared as a friend then it can access the private and protected data of the
class.
Friend function is not a member of class

Syntax:
friend return type name of function (argument);

Ex: friend void fun();

A friend function declared inside the class using friend keyword. But definition is also outside the
class . it defined as a nor mal function.

Ex:
Void printdata(test &p){
Cout<<”a=”<<p.a<<endl;
Cout<<”b=’<<p.b<<endl;
}
It cann’t be called using the object because it is not part of member function .
We cann’t access directly data member . we need to use object name and membership operator.

Ex:

You might also like