Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CPP IMP Questions by Vishal Sir

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

VJTech Academy

OBJECT ORIENTED PROGRAMMING


LANGUAGE IMPORTANT
QUESTIONS LIST WITH ANSWERS

Author:

“Prof. Vishal Jadhav”


(BE in Computer Engineering and Having 8.5 years of IT industry experience.
VJTech Academy, Maharashtra Contact No: +91-9730087674,
Email id: vjtechacademy@gmail.com)

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
VJTech Academy

 UNIT-I : Principles of Object Oriented Programming: Total Marks: 14

1. Explain with suitable example, syntax of for loop in C++.


- For loop is one of the types of looping statement.
- It is used to execute the block of statements N no of times.
- for is a predefined keyword.
- For loop in an entry-controlled loop.
- In this loop, first we test the condition and then executes the body. If condition is false first time
then body executes 0 times.
- Syntax:

for(initialization ; condition ; increment/decrement)


{
//body of for loop
}

- Example:
for(i=1;i<=5;i++)
{
cout<<”\nHello World”;

- Output
Hello World
Hello World
Hello World
Hello World
Hello World

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
VJTech Academy

2. State any four object-oriented programming language.


Ans=> Object oriented programming language:
 C++
 Smalltalk
 Object Pascal
 Java
 Simula
 Ada
 Turbo Pascal
 Eiffel
 C#
 Python

3. State any four applications of object-oriented programming.


Ans=> Applications of object-oriented programming:
1) Real time systems
2) Simulation and modeling
3) Object-oriented databases
4) Hypertext, hypermedia and expertext
5) AI and expert systems
6) Neural networks and parallel programming
7) Decision support and office automation systems
8) CIM/CAM/CAD systems

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
VJTech Academy

4. State the use of scope resolution operator and its use in C++.
Ans:
1) It is used to identify a hidden variable. Scope resolution operator allows access to the global
version of a variable. The scope resolution operator is used to refer variable of class anywhere in
program.
:: Variable_name
2) Scope resolution operator is also used in classes to identify the class to which a member function
belongs. Scope resolution operator is used to define function outside of class.
Return_type class_name :: function_name( ) { }

5. State use of new operator.


Ans: The new operator in C++ is used for dynamic storage allocation. This operator can be used to
create object of any type.
The general syntax of new operator in C++ is as follows:
pointer_variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
int *p=new int[5];

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
VJTech Academy

6. Write a C++ program that replaces the string “Computer” in the String “Diploma in Computer
Engineering” with string “Information Technology”.

#include <iostream>
using namespace std;
int main()
{
string str1="Diploma in Computer Engineering";
string str2="Information Technology";

cout<<"\nBefore replace value of str1:"<<str1;


str1.replace(11,8,str2);
cout<<"\nAfter replace value of str2:"<<str1;

return 0;
}

7. Write a C++ program to display decimal number in reverse order.

#include <iostream>
using namespace std;
int main()
{
int no,rev=0,rem;
cout<<"\nEnter any integer number:";
cin>>no;
while(no>0)
{
rem=no%10;
rev=rev*10+rem;
no=no/10;
}
cout<<"\nReverse Number="<<rev;
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
VJTech Academy

8. Differentiate between do-while and while loops on the basis of syntax.


Ans:

9. Explain syntax of any two String functions.


- Following are the list of string functions present in C++

strlen()

- This function is used to calculate the length of given string.


- Syntax:
int len=strlen(string);
- Example:
char str[10]=”VJTech”;
int len=strlen(str);
len=6

strcpy()

- This function is used to copy one string to another string.


- Syntax:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
VJTech Academy

strcpy(str2, str1); //str1 copied into str2


- Example:
char str1[10]=”VJTech”,str2[10];
strcpy(str2,str1);

strrev()

- This function is used to make the reverse string of given string.


- Syntax:
strrev(string);
- Example:
char str[10]=”VJTech”;
strrev(str);
str=”hceTJV”

strcat()

- This function is used to concate two strings.


- Syntax:
strcat(str1, str2);
- Example:
char str1[20]=”VJTech”, str2[10]=”Academy”;
strcat(str1, str2);
str1=”VJTechAcademy”;

strcmp()

- This function is used to compare two strings.


- If both strings are equal then it will return 0.
- If first string is greater than second string then it will return >0 value.
- If first string is less than second string then it will return <0 value.
- Syntax:
strcmp(str1,str2);
- Example:
char str1[10]=”VJTech”,str2[10]=”VJTech”;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
VJTech Academy

int x=strcmp(str1, str2);


if(x==0)
cout<<”\nBoth string are equal”;
else
cout<<”\nBoth strings are not equal”;

10. Explain working of insertion and extraction operators in C++ with the help of suitable
example.
Ans: Insertion operators: The operator << is called the put to operator. It inserts (or sends) the
contents of the variable on its right to the object on its left.
Example: cout<< string;

Extraction operators: The operator >> is known as extraction or get from operator. It extracts (or
takes) the value from the keyboard and assigns it to the variable on its right.
Example: cin>> number1;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
VJTech Academy

11. Write a C++ program that displays first 10 odd numbers.

#include <iostream>
using namespace std;
int main()
{
int i;
cout<<"\nFirst 10 Odd Numbers:";
for(i=1;i<20;i++)
{
if(i%2!=0)
{
cout<<"\t"<<i;
}
}
return 0;
}

12. List features of procedure-oriented programming.


Ans: Features of procedure-oriented programming:
1. Emphasis is on doing things (algorithms).
2. Large program are divided into smaller programs known as functions.
3. Most of the functions share global data.
4. Data moves openly from one function to another function.
5. Functions transform data from one form to another.
6. Use top –down approach in program design.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
VJTech Academy

13. Explain data encapsulation and data abstraction.


Ans:
Data encapsulation: The wrapping up of data and function into a single unit (called class) is known
as encapsulation. The data is not accessible to the outside world, and only those functions which
are wrapped in the class can access it. These functions provide the interface between the object‟s
data and the program. Encapsulation is a mechanism that keeps the data and code safe from
external interference and misuse. This insulation of the data from direct access by the program is
called data hiding or information hiding.
Data abstraction: Abstraction refers to the act of representing essential features without including
the background details or explanation. Data abstraction is the process of defining a data type, often
called abstract data type (ADT), together with the principle of data hiding. Classes use the concept
of abstraction. They encapsulate all the essential properties of the object that are to be created.
The attributes are called as data members as they hold information. The functions that operate on
these data are called as member functions.

14. Write a program to swap two integer values by using call by reference.

#include <iostream>
using namespace std;
void swap(int *x,int *y);
int main()
{
int a=100,b=200;

cout<<"\n***Before Swapping***";
cout<<"\na="<<a<<"\tb="<<b;
swap(&a,&b);
cout<<"\n***After Swapping***";
cout<<"\na="<<a<<"\tb="<<b;

return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
VJTech Academy

15. Differentiate between call by value and call by reference method.


Ans:

16. Give four differences between object-oriented programming and procedure oriented
programming.
Ans:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
VJTech Academy

17. Explain scope resolution operator and memory management operator in C++.
Ans:
(i) Scope resolution operator:
1) It is used to identify a hidden variable. Scope resolution operator allows access to the global
version of a variable. The scope resolution operator is used to refer variable of class anywhere in
program.
:: Variable_name
2) Scope resolution operator is also used in classes to identify the class to which a member
function belongs. Scope resolution variable is used to define function outside of class.
Return_type class_name :: function_name( ) { }
(ii) Memory management operator:
There are two types of memory management operators in C++:
 new
 delete
These two memory management operators are used for allocating and freeing memory block in
efficient and convenient ways.
New operator: The new operator in C++ is used for dynamic storage allocation. This operator can
be used to create object of any type.
Example: int *p=new int;
Delete operator: The delete operator in C++ is used for releasing memory space when the object is
no longer needed. Once a new operator is used, it is efficient to use the corresponding delete
operator for release of memory
Example: delete p;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
VJTech Academy

18. Write a program to insert an element at location of array.

#include<iostream.h>
using namespace std;
int main()
{
int a[10],N,x,Loc,i;

cout<<"\nEnter Size of Array:";


cin>>N;
cout<<"\nEnter Array Elements:";
for(i=0;i<N;i++)
{
cin>>a[i];
}
cout<<"\nEnter element for insertion:";
cin>>x;
cout<<"\nEnter location of array for insertion:";
cin>>Loc;
for(i=N-1;i>=Loc-1;i--)
{
a[i+1]=a[i];
}
a[Loc-1]=x;
N++;
cout<<"\nArray Elements After Insertion:";
for(i=0;i<N;i++)
{
cout<<"\t"<<a[i];
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
VJTech Academy

19. Write a program to display largest element from entered array.

#include <iostream>
using namespace std;
int main()
{
int a[10],N,i,largest;

cout<<"\nEnter Size of Array:";


cin>>N;
cout<<"\nEnter Array Elements:";
for(i=0;i<N;i++)
{
cin>>a[i];
}
largest=a[0];
for(i=1;i<N;i++)
{
if(a[i]>largest)
{
largest=a[i];
}
}
cout<<"\nLargest Number="<<largest;
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
VJTech Academy

20. Write structure of C++ program.


Ans:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
VJTech Academy

21. List two memory management operators available in C++ and state its use in one line.
Ans: There are two types of memory management operators in C++:
 new
 delete
These two memory management operators are used for allocating and freeing memory block in
efficient and convenient ways.
New operator: The new operator in C++ is used for dynamic storage allocation. This operator can
be used to create object of any type. General syntax of new operator in C++ is as follows:
datatype pointer_variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;

Delete operator: The delete operator in C++ is used for releasing memory space when the object is
no longer needed. Once a new operator is used, it is efficient to use the corresponding delete
operator for release of memory.

General syntax of delete operator in C++ is as follows:

delete pointer_variable;

#include<iostream.h>
#include<conio.h>
void main()
{

//Allocates using new operator memory space in memory for storing a integer datatype
int *a= new int;
clrscr();
*a=100;
cout << " The Output is:a= " << *a;

//Memory Released using delete operator


delete a;
getch();
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
VJTech Academy

22. Write a C++ program to add two 3 x 3 matrices and display addition

#include<iostream>
using namespace std;
int main()
{

int a[3][3],b[3][3],c[3][3],i,j;
cout<<"\nEnter first 3*3 matrix elements:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter second 3*3 matrix elements:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

cout<<"\nAddition of two 3*3 matrix elements:";


for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<c[i][j]<<"\t";
}
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions

OBJECT ORIENTED PROGRAMMING


LANGUAGE IMPORTANT
QUESTIONS LIST WITH ANSWERS

Author:

“Prof. Vishal Jadhav”


[BE in Computer Engineering and Having 8.5 years of IT industry experience.
VJTech Academy, Maharashtra, Contact No: +91-9730087674,
Email id: vjtechacademy@gmail.com)]

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions

 UNIT-II : Classes and Objects: Total Marks: 18

1. Explain use of friend function with the help of suitable example.


Friend function:
 Private members of a class cannot be accessed from outside the class.
 A non-member function cannot have an access to the private data of a class.
 Sometimes, two classes may need to share data in a common function.
 To access private data of more than one class in one common function, friend function
is required.
 The common function is declared as a friend function of all those classes from which the
function wants to share data
Syntax for declaring friend function: -
friend return_type function_type(Paramater list);
Syntax for calling friend function: -
function_name(Argument list);
Characteristics of friend function:
1. It is not in the scope of the class to which it has been declared as friend.
2. As it is not in the scope of the class, it cannot be called using the object of that class.
3. It can be invoked like a normal function without the help of any object.
4. It cannot access the member names directly and has to use an object name and dot
membership operator with each member name.
5. It can be declared either in the public or the private part of a class without affecting its
meaning.
6. It has the objects as arguments.
Program:
#include <iostream>
using namespace std;
class Distance
{
private:
int z;
public:
void getdata()
{
cout<<"\nEnter Distance:";
cin>>z;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions

}
friend void calc_distance(Distance m,Distance n)
{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions

2. Explain the concept of memory allocation for object/ explain how memory is allocated to
objects of a class.
Ans:
 The memory space for object is allocated when object is declared & not when the class
is declared.
 The member functions are created & placed in memory space only once when they are
defined as a part of a class definition.
 Since all the objects belonging to that class use the same member functions, no
separate space is allocated for member functions.
 When the objects are created only space for (data) member variables are allocated
separately for each object.
 Separate memory locations for the objects are essential because the (data) member
variables will hold different data values for different objects

 In the above diagram, member functions 1 and 2 are stored in the common memory
space as they require access by all objects.
 Each object (object 1, object 2, object 3) has its own separate memory space for its
member variables.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions

3. What is parameterized constructor? Explain with example.


Definition:
 Constructor which accepts one or more arguments are called as parameterized
constructor.
 When constructor taken some arguments then it is called as parameterized constructor.
 It is used to initialize data members of object.
 There is no need to call the constructor by using object name and dot operator. It will
called automatically when object is created.
Syntax:
ClassName(list of parameters)
{
//constructor body

To call parameterized constructor using two different ways:

1) Implicit constructor call:


ClassName ObjectName(Argument list);

2) Explicit constructor call:


ClassName ObjectName=ClassName(Argument list);

Program:

#include <iostream>
using namespace std;
class Addition
{
int a,b;
public:
Addition(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"Addition="<<a+b;
}
};
int main()
{
Addition a1(100,200);
a1.display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions

4. Explain the concept of overload constructor in class with example.


Overloaded constructor:
 When more than one constructor function is defined in a class then it is called as
overloaded constructor.
 All constructors are defined with the same name as the class name they belong to.
 Each of the constructors contains different number of arguments. Depending upon the
number of arguments and their data type, the compiler executes appropriate
constructor.
 When constructor names are same but its arguments are different then it is called as
constructor overloading.
Program:
#include <iostream>
using namespace std;
class Addition
{
int a,b;
public:
Addition(int m)
{
a=m;
b=500;
}
Addition(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nAddition="<<a+b;
}
};
int main()
{
Addition a1(200);
Addition a2(200,300);
a1.display();
a2.display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions

5. Why friend function is required? Give four characteristic of friend function.


Ans:
Friend function:
 Private members of a class cannot be accessed from outside the class.
 A non-member function cannot have an access to the private data of a class.
 Sometimes, two classes may need to share data in a common function.
 To access private data of more than one class in one common function, friend function
is required.
 The common function is declared as a friend function of all those classes from which the
function wants to share data.
Characteristics of friend function:
1. It is not in the scope of the class to which it has been declared as friend.
2. It cannot be called by using the object of that class.
3. It can be invoked like a normal function without the help of any object.
4. It cannot access the member names directly and has to use an object name and dot
membership operator with each member name.
5. It can be declared either in the public or the private part of a class without affecting its
meaning.
6. It has the objects as arguments.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions

6. Explain friend function with suitable example.


Friend function:
 Private members of a class cannot be accessed from outside the class.
 A non-member function cannot have an access to the private data of a class.
 Sometimes, two classes may need to share data in a common function.
 To access private data of more than one class in one common function, friend function
is required.
 The common function is declared as a friend function of all those classes from which the
function wants to share data
Syntax for declaring friend function: -
friend return_type function_type(Paramater list);
Syntax for calling friend function: -
function_name(Argument list);
Characteristics of friend function:
7. It is not in the scope of the class to which it has been declared as friend.
8. As it is not in the scope of the class, it cannot be called using the object of that class.
9. It can be invoked like a normal function without the help of any object.
10. It cannot access the member names directly and has to use an object name and dot
membership operator with each member name.
11. It can be declared either in the public or the private part of a class without affecting its
meaning.
12. It has the objects as arguments.
Program:
#include <iostream>
using namespace std;
class Distance
{
private:
int z;
public:
void getdata()
{
cout<<"\nEnter Distance:";
cin>>z;
}
friend void calc_distance(Distance m,Distance n)

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions

{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}

7. Calculate the size of object B1 defined in following class :

Ans:
 In above given class data member B_name is declared as character array of 15
size then it will require 15 bytes of memory.
 Data member B_id declared as integer then it required 2 bytes of memory
 Data member Price declared as integer then it required 2 bytes of memory.
 So total memory required for B1 object is 19 bytes.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions

8. Define a class named ‘Train’ representing following members:


Data members:-
- Train Number
- Train Name
- Source
- Destination
- Journey Date
- Capacity
Member Functions:
- Initialize Members
- Input Train Data
- Display Data

Write a C++ program to test the train class.

#include <iostream>
#include<string.h>
using namespace std;
class Train
{
private:
int train_no;
char train_name[20];
char source[20];
char destination[20];
char journey_date[10];
int capacity;
public:
Train()
{
train_no=0;
strcpy(train_name,"");
strcpy(source,"");
strcpy(destination,"");
strcpy(journey_date,"");
capacity=0;
}
void get_train_info()
{
cout<<"\nEnter Train No:";
cin>>train_no;
cout<<"\nEnter Train Name:";
cin>>train_name;
cout<<"\nEnter Train Source City:";
cin>>source;
cout<<"\nEnter Train Destination city:";
cin>>destination;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions

cout<<"\nEnter Train Journey Date:";


cin>>journey_date;
cout<<"\nEnter Train Capacity:";
cin>>capacity;
}
void disp_train_info()
{
cout<<"\nTrain No:"<<train_no;
cout<<"\nTrain Name:"<<train_name;
cout<<"\nSource City:"<<source;
cout<<"\nDestination city:"<<destination;
cout<<"\nJourney Date:"<<journey_date;
cout<<"\nTrain Capacity:"<<capacity;
}
};
int main()
{
Train t1;
t1.get_train_info();
t1.disp_train_info();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions

9. Write a C++ program to calculate sum of distance and display the results using friend function.

#include <iostream>
using namespace std;
class Distance
{
private:
int z;
public:
void getdata()
{
cout<<"\nEnter Distance:";
cin>>z;
}
friend void calc_distance(Distance m,Distance n)
{
int sum;
sum=m.z+n.z;
cout<<"\nSum of distance="<<sum;
}
};
int main()
{
Distance d1,d2;
d1.getdata();
d2.getdata();
calc_distance(d1,d2);
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions

10. Write a C++ program to display number of objects created using static member.
#include <iostream>
using namespace std;
class Item
{
private:
static int count;
public:
Item()
{
count++;
cout<<"\nObject created successfully";
}
static void display()
{
cout<<"\nNo of Objects created="<<count;
}
};
int Item::count;
int main()
{
Item i1,i2,i3;
Item::display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions

11. Define a class named ‘Bank Account’ to represent following members:


Data members:-
- Account Number
- Name of Depositor
- Account Type
- Balance Amount

Member functions:
- Initialize members
- Deposit Amount
- Withdraw Amount
- Display Balance
Write a C++ program to test the Bank Account class for 10 customers.
#include <iostream>
using namespace std;
class Bank
{
private:
int acc_no;
char cust_name[20];
char acc_type[10];
float available_amt;
public:
Bank()
{
cout<<"\nEnter Account Number:";
cin>>acc_no;
cout<<"\nEnter Customer Name:";
cin>>cust_name;
cout<<"\nEnter Account Type:";
cin>>acc_type;
cout<<"\nEnter account opening balance:";
cin>>available_amt;
}
void deposit_amount()
{
int deposit_amt;
cout<<"\nHow much amount do you wants to deposit:";
cin>>deposit_amt;
available_amt=available_amt+deposit_amt;
cout<<"\nAmount deposit successfully";
}
void withdraw_amount()
{
int withdraw_amt;
cout<<"\nHow much amount do you wants to withdraw:";

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions

cin>>withdraw_amt;
available_amt=available_amt-withdraw_amt;
cout<<"\nAmount withdraw successfully";
}
void display_acc_info()
{
cout<<"\n****BANK ACCOUNT DETAILS****";
cout<<"\nAccount No:"<<acc_no;
cout<<"\nCustomer Name:"<<cust_name;
cout<<"\nAccount Type:"<<acc_type;
cout<<"\nAvailable Balance:"<<available_amt;
}
};
int main()
{
Bank b[10];
int i;
for(i=0;i<10;i++)
{
b[i].deposit_amount();
b[i].withdraw_amount();
b[i].display_acc_info();
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions

12. Fix the compilation errors and find the output of following program
#include class Test
{
private: int x;
public:
Test(int x = 0)
{
this->x = x;
}
void change(Test *t)
{
this = t;
}
void print()
{
cout << "x = " << x << endl;
}
};
void main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
}
Ans:
- Missing iostream.h header file so include header file iostream.h in this program.
- Pointer object t assigning on normal object obj (this=t) which is incorrect assignment.
- The statement this=t is incorrect so we have modified it to this->x=t->x.
- After fixing all above errors, output of above program is x=10;

13. State the use of static data member of a class. List properties of static member function.
Characteristics of static data members:
1. It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
2. Only one copy of that member is created for the entire class.
3. Created copy is shared by all the objects of that class, no matter how many objects are
created.
4. It is visible only within the class, but its lifetime is the entire program.

- Since objects are created anywhere in a program, and all objects shares the value of static
member it is necessary to make static members global and re-declared outside of the class.

====================================================================

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions

14. Describe various places at which member functions can define using suitable example.
1) Member function definition inside the class.
2) Member function definition outside the class.
Note: take help from our class notebook
15. Define a structure that represents Fruit with properties fruit name, fruit type, fruit color.
Write a program that accepts data of four fruits and displays the results.

#include <iostream>
#include<string.h>
using namespace std;
struct Fruit
{
char fruit_name[20];
char fruit_type[20];
char fruit_color[20];
};
int main()
{
struct Fruit f[4];
int i;
for(i=0;i<4;i++)
{
cout<<"\nEnter Fruit Name:";
cin>>f[i].fruit_name;
cout<<"\nEnter Fruit Type:";
cin>>f[i].fruit_type;
cout<<"\nEnter Fruit Color:";
cin>>f[i].fruit_color;
}
for(i=0;i<4;i++)
{
cout<<"\nFruit Name:"<<f[i].fruit_name;
cout<<"\nFruit Type:"<<f[i].fruit_type;
cout<<"\nFruit Color:"<<f[i].fruit_color;
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions

16. Explain different access specifiers used in C++.


Access specifiers:
1. private
2. protected
3. public
Private access specifier: Class members declared as private can be accessed only from within
the class. Outside class access is not allowed for private members of class. By default, class
members are private.
Protected access specifier: Class members declared as protected can be accessed by the
member functions within its class and any class immediately derived from it. These members
cannot be accessed by the functions outside these two classes.
Public access specifier: Class members declared as public can be accessed from outside the
class.
Program:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions

17. State any one use of this pointer with suitable example.
 “this‟ pointer is used to represent an object that invokes a member function.
 It points to the object for which the function is called.
 It is also used to access members of object inside function definition.
 Example: this->rollno=1;
 Normally, we are not using this keyword regularly because it makes code complex.
 “this” pointer always keeps track of current calling object of that member function.
Program:
#include <iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
void getdata()
{
this->a=100;
this->b=200;
}
void putdata()
{
cout<<"\nAddition="<<(this->a+this->b);
}
};
int main()
{
Addition a1;
a1.getdata();
a1.putdata();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions

18. Correct the syntactical and logical errors in the following and explain program:

Ans:
1) T is a normal object but while calling getdata() and display() (->) operator is used instead of
dot operator(.). This is the mistake.
2) p is pointer object but while calling getdata() used dot operator instead of (->).This is the
mistake.

19. What is the importance of constructor?


 A constructor is important to initialize the objects of its class.
 It is called constructor because it constructs the value of the data members inside object
of the class.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions

20. How do we invoke a constructor function?


Ans:

21. Write a program to declare class Time having data member as hrs, mins, secs. Write
constructor to assign values and destructor to destroy values. Accept & display data for one
object.

#include <iostream>
using namespace std;
class Time
{
private:
int hrs,mins,secs;
public:
Time()
{
cout<<"\nEnter Hours:";
cin>>hrs;
cout<<"\nEnter Minues:";
cin>>mins;
cout<<"\nEnter Seconds:";
cin>>secs;
}
void display()
{
cout<<"\nTime="<<hrs<<":"<<mins<<":"<<secs;
}
~Time()

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions

{
cout<<"\nObject destroyed successfully";
}
};
int main()
{
Time t1;
t1.display();
return 0;
}

22. Give four differences between structure and class.


Ans:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions

23. Write a program to declare class student having data members name and percentage. Write
constructor to initialize these data members. Accept and display this data for one object.
#include <iostream>
using namespace std;
class Student
{
private:
char name[20];
float percentage;
public:
Student()
{
cout<<"\nEnter Name of student:";
cin>>name;
cout<<"\nEnter Student Percentage:";
cin>>percentage;
}
void display()
{
cout<<"\nStudent Name:"<<name;
cout<<"\nStudent percentage:"<<percentage;
}
};
int main()
{
Student s1;
s1.display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
OOP Important Questions

24. Write a program to show object as function argument.


#include <iostream>
using namespace std;
class Sample
{
private:
int a;
public:
void getdata()
{
cout<<"\nEnter Value of a:";
cin>>a;
}
void display(Sample m)
{
cout<<"\nAddition="<<(a+m.a);
}
};
int main()
{
Sample s1,s2;

s1.getdata();
s2.getdata();

s1.display(s2);

return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
OOP Important Questions

25. What do you mean by default argument? Illustrate concept of constructor with default
argument using suitable example.

#include <iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
Addition(int x,int y=0)
{
a=x;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
OOP Important Questions

b=y;
}
void display()
{
cout<<"Addition="<<(a+b);
}
};
int main()
{
Addition a1(100);
a1.display();
return 0;
}

26. What are the rules governing the declaration of a destructor member function?
Ans:
Rules for declaration of destructor member function:
1. Destructor name is same as class name but is preceded by a tilde(~).
2. Destructor is declared in public area of a class.
3. Destructor never takes any argument.
4. Destructor never returns any value.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
OOP Important Questions

27. Write a program to declare class Account having data member as acc_no and balance. Accept
and display data for five object using pointer to array of object.
#include<iostream>
using namespace std;
class Bank
{
private:
int acc_no;
char name[20];
float balance;
public:
void get_bank_info()
{
cout<<"\nEnter Account Number:";
cin>>acc_no;
cout<<"\nEnter Customer Name:";
cin>>name;
cout<<"\nEnter Account Balance:";
cin>>balance;
}
void disp_bank_info()
{
cout<<"\n"<<acc_no<<"\t"<<name<<"\t"<<balance;
}
};
int main()
{
Bank *b[5];
int i;
for(i=0;i<5;i++)
{
b[i]->get_bank_info();
}
cout<<"\n************************************";
cout<<"\n*********VJTECH BANK***************";
cout<<"\n************************************";
cout<<"\nACCNO\tNAME\tBALANCE";
cout<<"\n===================================";
for(i=0;i<5;i++)
{
b[i]->disp_bank_info();
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
OOP Important Questions

28. Explain multiple constructors in class. Give suitable example.


Program:
#include <iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
Addition()
{
a=100;
b=200;
}
Addition(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nAddition="<<(a+b);
}
};
int main()
{
Addition a1,a2(500,600);
a1.display();
a2.display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
OOP Important Questions

29. What is copy constructor? Give the syntax and example for copy constructor.
Ans:

Program:
#include <iostream>
using namespace std;
class Code
{
private:
int id;
public:
Code() //default Constructor
{
id=100;
}
Code(Code &m) //Copy Constructor
{
id=m.id;
}
void display()
{
cout<<"\nValue of ID="<<id;
}
};
int main()
{
Code c1;
Code c2(c1);
c1.display();
c2.display();
return 0;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
OOP Important Questions

30. What do you mean by inline function? Write its syntax and example.
 To eliminate the cost of calls to small functions, c++ proposes a new feature called inline
function.
 An inline function is a function that is expanded in line when it is invoked. That is the
compiler replaces the function call with the corresponding function code.
 If we make a function as inline, then the compiler replaces the calling function location with
the definition of the inline function at compile time.
 Any changes made to an inline function will require the inline function to be recompiled
again.
 Because the compiler would need to replace all the code with a new code, otherwise, it will
execute the old code.
 The main use of the inline function in C++ is to save memory space.
 When we call the function then it takes some time to jump over function definition and also
It consume some memory for function call. But it will save time and memory due to inline
function.

Syntax:
-------
inline return_type function_name(parameters)
{
// function code?
}
Example:

#include<iostream>
using namespace std;
class Addition
{
private:
int a,b;
public:
inline void getdata()
{
a=100;
b=200;
}
inline void putdata()

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
OOP Important Questions

{
cout<<"\nAddition="<<(a+b);
}
};
int main()
{
Addition a1;
a1.getdata();
a1.putdata();
return 0;
}

31. Write a program to declare class ‘city’ with data member’s cityname and state. Create array of
object of size 5. Read and print data for array using pointer to object.
#include<iostream>
using namespace std;
class City
{
private:
char city_name[20];
char state_name[20];
public:
void get_city_info()
{
cout<<"\nEnter city name:";
cin>>city_name;
cout<<"\nEnter state name:";
cin>>state_name;
}
void disp_city_info()
{
cout<<"\n"<<city_name<<"\t"<<state_name;
}
};
int main()
{
City *c[5];
int i;
for(i=0;i<5;i++)
{
c[i]->get_city_info();
}
cout<<"\nCITYNAME\tSTATENAME";
cout<<"\n=====================";
for(i=0;i<5;i++)

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
OOP Important Questions

{
c[i]->disp_city_info();
}
return 0;
}

32. Differentiate between constructor and destructor


Ans:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
OOP Important Questions

33. Write a program to declare class ‘staff’ having data members as name and post. Accept this
data for 5 staffs and display name of staff who are HOD.
#include <iostream>
#include <string.h>
using namespace std;
class staff
{
private:
char name[20];
char post[20];
public:
void get_staff_info()
{
cout<<"\nEnter Staff Name:";
cin>>name;
cout<<"\nEnter Staff Post:";
cin>>post;
}
void disp_staff_info()
{
if(strcmp(post,"HOD")==0)
{
cout<<"\n"<<name<<"\t"<<post;
}
}
};
int main()
{
staff s[5];
int i;
for(i=0;i<5;i++)
{
s[i].get_staff_info();
}
cout<<"\nNAME\tPOST";
cout<<"\n===================";
for(i=0;i<5;i++)
{
s[i].disp_staff_info();
}
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
OOP Important Questions

34. Write a program to define a structure ‘Tender’ having data members’ tender-no., cost and
company-name. Accept & display this data for two variables of this structure.
#include <iostream>
using namespace std;
struct Tender
{
int tender_no;
float tender_cost;
char company_name[20];
};
int main()
{
struct Tender t[2];
int i;
for(i=0;i<2;i++)
{
cout<<"\nEnter Tender Number:";
cin>>t[i].tender_no;
cout<<"\nEnter Tender Cost:";
cin>>t[i].tender_cost;
cout<<"\nEnter Tender Company Name:";
cin>>t[i].company_name;
}
for(i=0;i<2;i++)
{
cout<<"\nTender No:"<<t[i].tender_no;
cout<<"\nTender Cost:"<<t[i].tender_cost;
cout<<"\nCompany Name:"<<t[i].company_name;
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
OOP Important Questions

35. Write a program to declare a class ‘Journal’ having data members as journal-name, price & no-
of-pages. Accept this data for two objects & display the name of journal having greater price.
#include <iostream>
using namespace std;
class Journal
{
public:
char journal_name[20];
float price;
int no_of_pages;

void get_Journal_details()
{
cout<<"\nEnter Journal Name:";
cin>>journal_name;
cout<<"\nEnter journal price:";
cin>>price;
cout<<"\nEnter no of pages:";
cin>>no_of_pages;
}
void disp_journal_details()
{
cout<<"\n****Journal Information****";
cout<<"\nJournal Name:"<<journal_name;
cout<<"\nJournal Price:"<<price;
cout<<"\nNo of Pages:"<<no_of_pages;
}
};
int main()
{
Journal j1,j2;
j1.get_Journal_details();
j2.get_Journal_details();
if(j1.price>j2.price)
{
j1.disp_journal_details();
}
else
{
j2.disp_journal_details();
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35
OOP Important Questions

36. Write a program to declare a class birthday having data members day, month, year. Accept
this information for five objects using pointer to the array of objects.
#include <iostream>
using namespace std;
class Birthday
{
private:
int day,month,year;
public:
void get_birthday_info()
{
cout<<"\nEnter Day:";
cin>>day;
cout<<"\nEnter Month:";
cin>>month;
cout<<"\nEnter Year:";
cin>>year;
}
void disp_birthday_info()
{
cout<<"\nBirth date:"<<day<<"/"<<month<<"/"<<year;
}
};
int main()
{
Birthday *b[5];
int i;
for(i=0;i<5;i++)
{
b[i]->get_birthday_info();
}
for(i=0;i<5;i++)
{
b[i]->disp_birthday_info();
}
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 36
OOP Important Questions

37. Write a C++ program to find smallest number from two numbers using friend function.

#include <iostream>
using namespace std;
class Number
{
private:
int a;
public:
void getdata()
{
cout<<"\nEnter any number:";
cin>>a;
}
friend void Calc_Smallest(Number m,Number n)
{
if(m.a<n.a)
{
cout<<"\nSmallest Number="<<m.a;
}
else
{
cout<<"\nSmallest Number="<<n.a;
}
}
};
int main()
{
Number n1,n2;
n1.getdata();
n2.getdata();
Calc_Smallest(n1,n2);
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 37
OOP Important Questions

OBJECT ORIENTED PROGRAMMING


LANGUAGE IMPORTANT
QUESTIONS LIST

Author:

“Prof. Vishal Jadhav”


[BE in Computer Engineering and Having 8.5 years of IT industry experience.
VJTech Academy, Maharashtra Contact No: +91-9730087674,
Email id: vjtechacademy@gmail.com)]

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions

 UNIT-III Inheritance Total Marks: 14

1. Write a program to implement single inheritance. Declare base class ‘Employee’ with emp_no
and emp_name. Declare derived class ‘Fitness’ with height and weight. Accept and display
data for one employee.
#include <iostream>
using namespace std;
class Employee
{
public:
int emp_no;
char emp_name[20];
void get_emp_info()
{
cout<<"\nEnter Employee No:";
cin>>emp_no;
cout<<"\nEnter Employee Name:";
cin>>emp_name;
}
void disp_emp_info()
{
cout<<"\nEmployee No:"<<emp_no;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Fitness:public Employee
{
public:
int height,weight;
void get_fitness_info()
{
cout<<"\nEnter Employee Height:";
cin>>height;
cout<<"\nEnter Employee Weight:";
cin>>weight;
}
void disp_fitness_info()
{
cout<<"\nEmployee Height:"<<height;
cout<<"\nEmployee Weight:"<<weight;
}
};
int main()
{
Fitness f1;
f1.get_emp_info();

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions

f1.get_fitness_info();
f1.disp_emp_info();
f1.disp_fitness_info();

return 0;
}
OUTPUT:
Enter Employee No:1010
Enter Employee Name:Dennis
Enter Employee Height:5
Enter Employee Weight:26

Employee No:1010
Employee Name:Dennis
Employee Height:5
Employee Weight:26

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions

2. Define classes to appropriately represent class hierarchy as shown in above figure. Use
constructors for both classes and display Salary for a particular employee.

#include <iostream>
using namespace std;
class Employee
{
public:
int emp_no;
char emp_name[20];
Employee()
{
cout<<"\nEnter Employee No:";
cin>>emp_no;
cout<<"\nEnter Employee Name:";
cin>>emp_name;
}
void disp_emp_info()
{
cout<<"\nEmployee No:"<<emp_no;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Salary:private Employee
{
public:
int basic_pay,HRA,DA,CLA;
Salary():Employee()
{

cout<<"\nEnter basic pay:";

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions

cin>>basic_pay;
cout<<"\nEnter HRA:";
cin>>HRA;
cout<<"\nEnter DA:";
cin>>DA;
cout<<"\nEnter CLA:";
cin>>CLA;
}
void Calculate_Salary()
{
disp_emp_info();
int gross_salary;
gross_salary=(basic_pay+HRA+DA+CLA);
cout<<"\nBasic Salary:"<<basic_pay;
cout<<"\nHRA:"<<HRA;
cout<<"\nDA:"<<DA;
cout<<"\nCLA:"<<CLA;
cout<<"\nGross Salary:"<<gross_salary;
}
};
int main()
{
Salary s1;
s1.Calculate_Salary();
return 0;
}
OUTPUT
Enter Employee No:1010
Enter Employee Name:James
Enter basic pay:12000
Enter HRA:1200
Enter DA:500
Enter CLA:800

Employee No:1010
Employee Name:James
Basic Salary:12000
HRA:1200
DA:500
CLA:800
Gross Salary:14500

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions

3. Write a program to implement inheritance as shown in Fig. 1. Assume suitable member


function

#include <iostream>
using namespace std;
class Staff
{
public:
int code;
void get_staff_info()
{
cout<<"\nEnter Staff Code:";
cin>>code;
}
void disp_staff_info()
{
cout<<"\nStaff Code:"<<code;
}
};
class Teacher:public Staff
{
public:
char subject[20];
void get_teacher_info()
{
cout<<"\nEnter Teacher Subject:";
cin>>subject;
}
void disp_teacher_info()
{
cout<<"\nTeacher Subject:"<<subject;
}
};
class Officer:public Staff
{
public:
char grade;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions

void get_officer_info()
{
cout<<"\nEnter Officer Grade:";
cin>>grade;
}
void disp_officer_info()
{
cout<<"\nofficer Grade:"<<grade;
}
};
int main()
{
cout<<"\n****Teacher Class****";
Teacher t1;
t1.get_staff_info();
t1.get_teacher_info();
t1.disp_staff_info();
t1.disp_teacher_info();
cout<<"\n****Officer Class****";
Officer s1;
s1.get_staff_info();
s1.get_officer_info();
s1.disp_staff_info();
s1.disp_officer_info();
return 0;
}

OUTPUT:

****Teacher Class****
Enter Staff Code:1010
Enter Teacher Subject:C++
Staff Code:1010
Teacher Subject:C++
****Officer Class****
Enter Staff Code:2020
Enter Officer Grade:A
Staff Code:2020
officer Grade:A

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions

4. Write a program to implement inheritance as shown in Fig.2 Assume suitable member


function.

#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student rollno:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent RollNo:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions

cout<<"\nEnter Class Test-2 Marks:";


cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Mark:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Sport
{
protected:
int sport_wt;
public:
void get_sport_info()
{
cout<<"\nEnter Sport Weightage:";
cin>>sport_wt;
}
void disp_sport_info()
{
cout<<"\nSport Weightage:"<<sport_wt;
}
};
class Result:public Test,public Sport
{
protected:
int total;
public:
void disp_total_marks()
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.get_sport_info();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
r1.disp_sport_info();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions

OUTPUT:

Enter Student rollno:1010


Enter Student Name:James
Enter Class Test-1 Marks:78
Enter Class Test-2 Marks:45
Enter Sport Weightage:7
Student RollNo:1010
Student Name:James
Class Test-1 Mark:78
Class Test-2 Marks:45
Total Marks:123
Sport Weightage:7

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions

5. Q-1: Define concept of Virtual Base class with an example


Q-2: State the reason for making a class virtual with the help of example

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions

#include <iostream>
using namespace std;
class A
{
public:
void display_A()
{
cout<<"\nDisplay Method of class A";
}
};
class B:virtual public A
{
public:
void display_B()
{
cout<<"\nDisplay Method of class B";
}
};
class C:virtual public A
{
public:
void display_C()
{
cout<<"\nDisplay Method of class C";
}
};
class D:public B,public C
{
public:
void display_D()
{
cout<<"\nDisplay Method of class D";
}
};
int main()
{
D d1;
d1.display_A();
d1.display_B();
d1.display_C();
d1.display_D();
return 0;
}
OUTPUT
Display Method of class A
Display Method of class B
Display Method of class C
Display Method of class D

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions

6. Q-1 Explain constructors in derived class using one example.


Q-2 Write a program to demonstrate constructor in derived class with respect to order of
calling constructor and passing parameters to base class constructor

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions

Program:

#include<iostream>
using namespace std;
class Alpha
{
private:
int x;
public:
Alpha(int i)
{
x=i;
}
void show_x()
{
cout<<"\nValue of x="<<x;
}
};
class Beta
{
private:
int y;
public:
Beta(int j)
{
y=j;
}
void show_y()
{
cout<<"\nValue of y="<<y;
}
};
class Gamma:public Alpha,public Beta
{
private:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions

int z;
public:
Gamma(int m,int n,int p):Alpha(m),Beta(n)
{
z=p;
}
void show_z()
{
cout<<"\nValue of z="<<z;
}
};
int main()
{
Gamma g1(100,200,300);
g1.show_x();
g1.show_y();
g1.show_z();
return 0;
}
OUTPUT
Value of x=100
Value of y=200
Value of z=300

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions

7. Demonstrate hybrid inheritance with the help of suitable example

Program

#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions

cout<<"\nEnter Student rollno:";


cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent RollNo:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Class Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Mark:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Sport
{
protected:
int sport_wt;
public:
void get_sport_info()
{
cout<<"\nEnter Sport Weightage:";
cin>>sport_wt;
}
void disp_sport_info()
{
cout<<"\nSport Weightage:"<<sport_wt;
}
};
class Result:public Test,public Sport
{
protected:
int total;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions

public:
void disp_total_marks()
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.get_sport_info();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
r1.disp_sport_info();
return 0;
}

OUTPUT:

Enter Student rollno:1010


Enter Student Name:James
Enter Class Test-1 Marks:78
Enter Class Test-2 Marks:45
Enter Sport Weightage:7
Student RollNo:1010
Student Name:James
Class Test-1 Mark:78
Class Test-2 Marks:45
Total Marks:123
Sport Weightage:7

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions

8. Write a program that illustrates multilevel inheritance.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions

Program

#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student roll no:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll NO:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Class Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Test
{
protected:
int total;
public:
void disp_total_marks()

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions

{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions

9. Explain different visibility modes used in inheritance

There are three types of Visibility modes:

Public Visibility mode:


- If we derive a subclass from a public base class. Then the public member of the base class
will become public in the derived class and protected members of the base class will
become protected in the derived class. Private members are not inherited in subclass.

Protected Visibility mode:


- If we derive a subclass from a protected base class. Then the public member of the base
class will become protected in the derived class and protected members of the base class
will become protected in the derived class. Private members are not inherited in subclass.

Private Visibility mode:


- If we derive a subclass from a private base class. Then the public member of the base class
will become private in the derived class and protected members of the base class will
become private in the derived class. Private members are not inherited in subclass.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
OOP Important Questions

10. Write a program for multiple inheritances.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
OOP Important Questions

Program

#include <iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"\nEnter Student roll no:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll NO:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Class Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Class Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Student,public Test
{
protected:
int total;
public:
void disp_total_marks()

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
OOP Important Questions

{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
int main()
{
Result r1;
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total_marks();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
OOP Important Questions

OBJECT ORIENTED PROGRAMMING


LANGUAGE IMPORTANT
QUESTIONS LIST

Author:

“Prof. Vishal Jadhav”


[BE in Computer Engineering and Having 8.5 years of IT industry experience.
VJTech Academy, Maharashtra Contact No: +91-9730087674,
Email id: vjtechacademy@gmail.com)]

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions

 UNIT-IV : Pointers and Polymorphism in C++: Total Marks: 14

1. Define polymorphism. List types of polymorphism.


Definition:-
- Polymorphism is the ability to take more than one form.
- Polymorphism is a Greek word.
- Poly means ‘many’ and morphism means ‘forms’ so meaning of polymorphism is ‘Many
Forms’.
Types –
1. Compile time polymorphism
2. Run time polymorphism

2. Give significance of ‘&’ and ‘*’ operators.


Ans:
Address operator (&) : It is used to retrieve address of a variable. With address operator
address of a variable can be stored in pointer variable.
Pointer operator (*) : It is used to declare a pointer variable. Also used as “value at” operator to
read value stored inside the address pointed by pointer.
Example: int a=100,*p;
p=&a;
3. Write advantages of pointer.
Ans:
The pointer has following advantages:
1. Pointers reduce the length and complexity of a program.
2. They increase execution speed.
3. Pointer saves the memory.
4. A pointer enables us to access a variable that is defined outside the function
5. The use of a pointer array of character strings results in saving of data storage space in
memory.
6. Pointer supports dynamic memory management.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions

4. Write a C++ program to swap two numbers using pointer


#include <iostream>
using namespace std;
int main()
{
int a=100,b=200,temp;
int *p1,*p2;
p1=&a;
p2=&b;
cout<<"\n***Before Swapping***";
cout<<"\na="<<a<<"\tb="<<b;
temp=*p1;
*p1=*p2;
*p2=temp;
cout<<"\n***After Swapping***";
cout<<"\na="<<a<<"\tb="<<b;
return 0;
}

5. Define pointer. Give syntax for declaration of pointer.


Ans:
Definition: Pointer is a variable that holds memory address of another variable of similar data
type.
Syntax for declaration:
data_type *pointer_variable_name;

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions

6. Difference between Function Overloading and Function Overriding.

Function Overloading Function Overriding


Function names are same but its arguments Parent class function name and child class
are different is known as function overloading. function name both are exactly same.
Without inheritance function overloading can Function overriding implemented in
be implemented.
inheritance.
It is an example of compile-time It is an example of run-time polymorphism.
polymorphism.
Overloaded functions are always present in Functions are always in different scope.
same scope.
Constructor can be overloaded. Constructor cannot be overridden.

7. State rule of operator overloading.


Rules for overloading operators:
1. Only existing operators can be overloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user defined data type.
3. We can’t change the basic meaning of an operator.
4. Overloaded operators follow the syntax rules of the original operators. They can’t be
overridden.
5. There are some operators that can’t be overloaded.
6. We can’t use friend functions to overload certain operators.
7. If we overload Unary operators with member function then it takes no arguments and friend
function take one argument.
8. If we overload Binary operators with member function then it takes one arguments and friend
function take two arguments.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions

8. Write a program to search a character in a string using pointer.

#include <iostream>
using namespace std;
int main()
{
char str[10],*p,key;
int flag=0;
cout<<"\nEnter String:";
cin>>str;
cout<<"\nEnter character for searching:";
cin>>key;

p=&str[0];

while(*p!='\0')
{
if(key==*p)
{
flag=1;
break;
}
p++;
}
if(flag==1)
cout<<"\nCharacter is found!!!";
else
cout<<"\nCharacter is not found!!!";
return 0;
}

OUTPUT
Enter String:VJTech
Enter character for searching:k
Character is not found!!!

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions

9. Write a C++ program to add two complex numbers overloading “+” operator.
#include <iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
void getdata(int r,int i)
{
real = r;
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
Complex operator +(Complex m)
{
Complex temp;
temp.real = real + m.real;
temp.imag = imag + m.imag;
return temp;
}
};
int main()
{
Complex c1,c2,c3;
c1.getdata(10,-20);
c2.getdata(2,4);
c3=c1+c2;
cout<<"\nObject c1:";
c1.display();
cout<<"\nObject c2:";
c2.display();
cout<<"\nObject c3:";
c3.display();
return 0;
}
OUTPUT
Object c1:10+i-20
Object c2:2+i4
Object c3:12+i-16

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
OOP Important Questions

10. State any four points of differentiation between compile time polymorphism and run time
polymorphism

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
OOP Important Questions

11. Write a C++ program to overload area() function to calculate area of shapes like triangle
,square, circle.
#include<iostream>
using namespace std;
void area(int s);
void area(float r);
void area(float base,float height);
int main()
{
int side;
float radius,bs,ht;
cout<<"Enter side of a square:";
cin>>side;
cout<<"Enter radius of circle:";
cin>>radius;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
area(side);
area(radius);
area(bs,ht);
return 0;
}
void area(int s)
{
cout<<"\nArea of square="<<(s*s);
}
void area(float r)
{
cout<<"\nArea of Circle="<<(3.14*r*r);
}
void area(float base,float height)
{
cout<<"\nArea of triangle="<<((base*height)/2);
}
OUTPUT
Enter side of a square:2
Enter radius of circle:3
Enter base and height of triangle:12 9
Area of square=4
Area of Circle=28.26
Area of triangle=54

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
OOP Important Questions

12. Explain concept of function overriding with example.


Function Overriding: -
- When base class function name & its arguments and derived class function name & its
arguments are same then it is called as function overriding.
- In this case, base class function overridden by derived class function.
- If we tried to call that function using derived class object then only derived class function
body got executed.
- Program:

#include <iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"\ndisplay method of Base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of Derived class";
}
};

int main()
{
Derived d1;
d1.display();
return 0;
}

- In the above example, base class and derived class both contains a same name function
“display”. The derived class overrides the “display‟ function of base class.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
OOP Important Questions

13. Write a program for overloading of ++ unary operator for inch to feet conversion. 12 inches =
1 feet.

#include <iostream>
using namespace std;
class UnayOPOverload
{
private:
int inch;
public:
void getdata()
{
cout<<"\nEnter value of inch:";
cin>>inch;
}
void operator ++()
{
float feet=(float)inch/12;
cout<<"\nInches="<<inch;
cout<<"\nFeet="<<feet;
}
};
int main()
{
UnayOPOverload v1;
v1.getdata();
++v1;
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
OOP Important Questions

14. Write a program to copy content of one string to another string using pointer to string.

#include <iostream>
using namespace std;
int main()
{
char str1[10],str2[10],*p1,*p2;
cout<<"\nEnter Any String:";
cin>>str1;
p1=&str1[0];
p2=&str2[0];
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
cout<<"\nCopied String="<<str2;

return 0;
}

OUTPUT:
Enter Any String:Pune
Copied String=Pune

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
OOP Important Questions

15. Write a program to display string in reverse order by using pointer.

#include <iostream>
using namespace std;
int main()
{
char str1[10],str2[10],*p1,*p2;
int len=0;
cout<<"\nEnter any string:";
cin>>str1;
p1=&str1[0];
p2=&str2[0];
while(*p1!='\0')
{
len++;
p1++;
}
p1--;
while(len>0)
{
*p2=*p1;
p1--;
p2++;
len--;
}
*p2='\0';
cout<<"\nReverse String : "<<str2;
return 0;
}

OUTPUT

Enter any string:Pune


Reverse String : enuP

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
OOP Important Questions

16. Write programs which perform arithmetic operation using pointer.

#include <iostream>
using namespace std;
int main()
{
int a=120,*p;
p=&a;
cout<<"\nAddress stored in p="<<p; //p=1000
p=p+2;
cout<<"\nAddress stored in p="<<p; //p=1004
p=p-3;
cout<<"\nAddress stored in p="<<p; //p=998
p++;
cout<<"\nAddress stored in p="<<p; //p=1000
p--;
cout<<"\nAddress stored in p="<<p; //p=998
return 0;
}
OUTPUT:
Address stored in p=1000
Address stored in p=1004
Address stored in p=998
Address stored in p=1000
Address stored in p=998

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
OOP Important Questions

17. Explain Virtual Function with an example:

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
OOP Important Questions

#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout<<"\ndisplay method of base class";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of derived class";
}
};
int main()
{
Base *bptr;
Base b1;
Derived d1;
cout<<"\nBase class pointer points to Base class";
bptr=&b1;
bptr->display();
cout<<"\nBase class pointer points to Derived class";
bptr=&d1;
bptr->display();

return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
OOP Important Questions

18. Rules for virtual functions.

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
OOP Important Questions

19. Explain pure virtual function with an example?

#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()=0;
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of derived class";
}
};
int main()
{
Base *bptr;
Derived d1;
bptr=&d1;
bptr->display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
OOP Important Questions

20. Explain Abstract class in C++?


- In C++ class is made abstract by declaring at least one of its functions as pure virtual function.
- We use predefined keyword ‘virtual’ for declaring pure virtual function.
- A pure virtual function is declared by using notation "=0".
- This function definition written in derived class.
- Pure virtual function is declared in base class which has no body.
- Syntax:
virtual returntype FunctionName(Parameter List)=0;
- Program:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void display()=0;
};
class Derived:public Base
{
public:
void display()
{
cout<<"\ndisplay method of derived class";
}
};
int main()
{
Derived d1;
d1.display();
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
OOP Important Questions

21. Write a program to overload the ‘–’ operator to negate value of variable.

#include<iostream>
using namespace std;
class Space
{
private:
int x,y,z;
public:
void getdata(int m,int n,int p)
{
x=m;
y=n;
z=p;
}
void display()
{
cout<<"\nx="<<x<<"\ty="<<y<<"\tz="<<z;
}
void operator -()
{
x=-x;
y=-y;
z=-z;
}
};
int main()
{
Space s;
s.getdata(10,-20,30);
cout<<"\n***Before calling Operator overloaded function***";
s.display();
-s; //calling operator overloaded function
cout<<"\n***After calling Operator overloaded function***";
s.display();
return 0;
}

OUTPUT:
***Before calling Operator overloaded function***
x=10 y=-20 z=30
***After calling Operator overloaded function***
x=-10 y=20 z=-30

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
OOP Important Questions

22. Write a program to overload ‘– –‘ unary operator to decrement the data members of object by
one. Assume suitable data.
#include <iostream>
using namespace std;
class UnayOPOverload
{
private:
int a,b;
public:
void getdata(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nValue of a="<<a;
cout<<"\nValue of b="<<b;
}
void operator --()
{
a--;
b--;
}
};
int main()
{
UnayOPOverload v1;
v1.getdata(100,200);
cout<<"\n***Before calling Operator Overload function***";
v1.display();
--v1;
cout<<"\n***After calling Operator Overload function***";
v1.display();
return 0;
}

OUTPUT
***Before calling Operator Overload function***
Value of a=100
Value of b=200
***After calling Operator Overload function***
Value of a=99
Value of b=199

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
OOP Important Questions

23. Write a program to search an element in an array using a pointer


#include <iostream>
using namespace std;
int main()
{
int a[5],i,key,flag=0;
int *ptr;
cout<<"\nEnter Five Array Elements:";
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
cout<<"\nEnter key element for searching:";
cin>>key;

ptr=&a[0];
for(i=0;i<5;i++)
{
if(key==*ptr)
{
flag=1;
break;
}
ptr++;
}
if(flag==1)
{
cout<<"\nElement is Found!!!";
}
else
{
cout<<"\nElement is not Found!!!";
}
return 0;
}
OUTPUT
Enter Five Array Elements:10 20 30 40 50
Enter key element for searching:40
Element is Found!!!

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
OOP Important Questions

24. Write a program to find the length of string using pointer.

#include <iostream>
using namespace std;
int main()
{
char str[10],*ptr;
int len=0;
cout<<"\nEnter Any String:";
cin>>str;
ptr=&str[0];
while(*ptr!='\0')
{
len++;
ptr++;
}
cout<<"\nLength of string="<<len;

return 0;
}

OUTPUT
Enter Any String:Latur
Length of string=5

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
OOP Important Questions

OBJECT ORIENTED PROGRAMMING


LANGUAGE IMPORTANT
QUESTIONS LIST WITH ANSWERS

Author:

“Prof. Vishal Jadhav”


[BE in Computer Engineering and Having 8.5 years of IT industry experience.
VJTech Academy, Maharashtra Contact No: +91-9730087674,
Email id: vjtechacademy@gmail.com)]

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
OOP Important Questions

 UNIT-V: File Operations: Total Marks:08

1. List and explain use of any four file mode parameters


1)ios::in =>open file in read mode.
2)ios::out =>open file in write mode.
3)ios::app =>Append contents to the file
4)ios::ate =>Move the pointer to the end of file.
5)ios::binary =>File open in binary Mode.
6)ios::nocreate =>This will cause to fail the open function if file doesn’t exists.
7)ios::noreplace =>This Will cause to fail open function if file is exists.
8)ios::trunc =>This Will truncate the file .

2. Write a program to count no of occurrence of particular character in text file.


Ans:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
int count=0;
char ch,m;
ifstream fin;
clrscr();
fin.open("msbte.txt",ios::in);
cout<<"Enter a word to count:";
cin>>m;
while(!fin.eof())
{
fin.get(ch);
if(ch==m)
{
count++;
}
}
cout<<"\nOccurrence of character="<<count;
fin.close();
getch();
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
OOP Important Questions

3. Write a program to copy contents of ABC.txt file to XYZ.txt file.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char ch;
ifstream fin;
ofstream fout;
clrscr();
fin.open("ABC.txt",ios::in);
fout.open("XYZ.txt",ios::out);

while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
cout<<"\nContents of ABC.txt file copied into XYZ.txt file successfully";
fin.close();
fout.close();
getch();
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
OOP Important Questions

4. Write a program that will create data file containing the list of telephone numbers as:
John 34567
Hari 56788
……
Use a class object to store each set of data.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

class Telephone
{
private:
char cust_name[20];
char mobile_no[10];
public:
void getdata()
{
cout<<"\nEnter Customer Name:";
cin>>cust_name;
cout<<"\nEnter Mobile No:";
cin>>mobile_no;
}
void putdata()
{
ofstream fout;
fout.open("telphone.txt",ios::app);
fout<<"\n"<<cust_name<<"\t"<<mobile_no;
cout<<"\nNew customer entry added";
}
};
void main()
{
Telephone t1;
clrscr();
t1.getdata();
t1.putdata();
getch();
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
OOP Important Questions

5. Give syntax of and explain various functions related to ifstream and ofstream classes: seekp(),
getline(),
Seekp
 The seekp method of ofstream in C++ is used to set the position of the file pointer in
the output sequence with the specified position.
 This method takes the new position to be set.
 Syntax: seekp(position);
getline:

 The cin is an object which is used to take input from the user but does not allow to take
the input in multiple lines.
 To accept the multiple lines input, we use the getline() function.
 It is a pre-defined function defined in a <string.h> header file used to accept a line or a
string from the input stream until the delimiting character is encountered.
 Syntax of getline() function:
 There are two ways of representing a function:
 The first way of declaring is to pass three parameters.

getline( istream cin, string str, char delim );

 The second way of declaring is to pass two parameters.

getline( istream cin, string str );


 Example
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name;
cout <<"Enter your name :";
getline(cin,name);
cout<<"\nHello "<<name;
return 0;
}

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
OOP Important Questions

6. Draw hierarchy of stream classes for file operations.

7. Explain file management functions seekg(), seekp(), tellg() and tellp();


1) Seekg() Function:
- It is used to set the position of get pointer.
- We use get pointer for reading the contents of the file.
- Syntax: seekg(position);

2)Seekp() Function:

- It is used to set the position of put pointer.


- We use put pointer for writing files.
- Syntax: seekp(position);

3)tellg() Function:

- It is used to get current position of get pointer.


- Syntax: int tellg();

4)tellp() Function:

- It is used to get current position of put pointer.


- Syntax: int tellp();

OOP IMP Questions by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6

You might also like