0% found this document useful (0 votes)
61 views

Question Bank & Solutions-Module-1

The document contains 11 questions related to object-oriented programming concepts like procedure-oriented programming vs object-oriented programming, features of OOP like encapsulation, polymorphism, inheritance etc. It also includes questions on function overloading and reference variables in C++. Sample programs are provided as part of the answers to demonstrate concepts like function overloading to calculate areas of different shapes and reference variable to swap two integer values.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Question Bank & Solutions-Module-1

The document contains 11 questions related to object-oriented programming concepts like procedure-oriented programming vs object-oriented programming, features of OOP like encapsulation, polymorphism, inheritance etc. It also includes questions on function overloading and reference variables in C++. Sample programs are provided as part of the answers to demonstrate concepts like function overloading to calculate areas of different shapes and reference variable to swap two integer values.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Question Bank

Module-1

1. List out the difference between a procedure-oriented program and an object-oriented


program. Jan.-18, 19, Marks 5

OP OOP

Emphasis is given more to


Emphasis is given more to data in OOP
procedures (functions)

The programming task is divided The programming task is divided into objects
into a collection of data structures (consisting of data members and associated
and methods or functions. member functions)

Procedures are not separated from data,


Procedures are being separated
instead, procedures and data are combined
from data being manipulated
together and put into a class

A piece of code uses the data to The data uses the piece of code to perform the
perform the specific task specific task

Data is moved freely from one Data is hidden (using private and protected
function to another function using access specifiers) and can be accessed only by
function parameters. member functions, not by an external function.

Data is secure OOP due to the use of access


Data is not secure in POP
specifiers
POP follows the top-down OOP follows the bottom-up approach to solve
approach to solve the problem the problem

As the size of the program


Irrespective of the size of the code the
increases the debugging becomes
debugging is simple and easier in OOP.
difficult

2. Give the difference between procedure-oriented programming and object-oriented


programming. July-19, Marks 4

Basic concepts and features of object oriented programming language like C++.

Following are the features or basic concepts of C++ programming language.

1. Objects and Classes

2. Data abstraction

3. Data encapsulation

4. Inheritance

5. Polymorphism

6. Binding

7. Message passing

1. Objects and Classes:

Classes are user-defined data types on which objects are created. Objects with similar
properties and methods are grouped together to form a class. So class is a collection of
objects. The object is an instance of a class.
2. Data abstraction

Abstraction refers to the act of representing essential features without including the
background details or explanation.

Example: Let’s take one real life example of a TV, which you can turn on and off, change
the channel, adjust the volume, and add external components such as speakers, VCRs,
and DVD players, BUT you do not know its internal details, that is, you do not know how
it receives signals over the air or through a cable, how it translates them, and finally
displays them on the screen.

Example Data abstraction:

#include <iostream>

Using namespace std;

int main( )

cout << “Hello C++” <<endl;

return 0;

Here, you don’t need to understand how cout displays the text on the user’s screen. You
need to only know the public interface and the underlying implementation of cout is free
to change.

3. Data encapsulation

Information hiding, Wrapping (combining) of data and functions into a single unit (class)
is known as data encapsulation.
Data is not accessible to the outside world, only those functions which are wrapped in
the class can access it.

4. Inheritance

Inheritance is the process of deriving a new class from an existing class. Existing class
is known as base, parent or super class. The new class that is formed is called derived
class, child or sub class. Derived class has all the features of the base class plus it has
some extra features also.

Writing reusable code. Objects can inherit characteristics from other objects.

5. Polymorphism

The dictionary meaning of polymorphism is “having multiple forms” or Ability to take


more than one form.

A single name can have multiple meanings depending on its context. It includes
function overloading, operator overloading.

6. Binding

Binding means connecting the function call to the function code to be executed in
response to the call.

Static binding means that the code associated with the function call is linked at compile
time. Also known as early binding or compile time polymorphism.

Dynamic binding means that the code associated with the function call is linked at
runtime. Also known as late binding or runtime polymorphism.

7. Message passing

Objects communicate with one another by sending and receiving information.


3. Explain the concept of object-oriented program i) Encapsulation ii) Polymorphism iii)
Inheritance iv) Data initialization Jan.-18, Marks 8

4. List and explain any four features of an object-oriented program. Jan.-19, Marks 5

5. Explain the various features of OOC. July-19, Marks 8

6. State the important features of the Object-Oriented programming paradigm. July-18,


Marks 8

7. What is a reference variable? Explain. Write a C++ program to swap two integer
values and display the values before and after swapping. Jul.-17, Marks 5

What is a reference variable?

Difference between Reference and Pointer Variable.

How to Create and use Reference Variable in C++ programs

Write a C++ program to swap two integers values and display the values before and
after swapping using call by reference.

Definition of Reference variable in C++

A reference variable is just another name to an already existing variable.

Difference between Reference and Pointer Variable


How to Creating Reference Variable

The reference variable is created using the & symbol.

For example, let a is a variable and we can create a reference variable of a as x as


follows,

int a =10;

int &x=a;

Here variable x is a reference variable for variable a. Both a and x are pointing at the
same memory location. Hence if the value of a is changed then the value of x also
changes automatically.

Use of Reference

The reference variables are used to pass the parameter to the functions pass by
reference.

Points to Remember

1. Reference variables should not be initilized with a constant value. For example int
&a=100 is not allowed.

2. Never return the reference variable from a function as a memory address.

3. Avoid assigning the reference variables to the variables whose memory is


dynamically allocated.

The following program demonstrates the C++ program to swap two integers values
and display the values before and after swapping using call by reference.

#include<iostream>

using namespace std;


void swap(int &x, int &y)

int temp;

temp = x;

x = y;

y = temp;

int main()

int a, b;

cout<<"Enter the value of a: ";

cin>>a;

cout<<"Enter the value of b: ";

cin>>b;

cout<<endl<<"Before swapping: ";

cout<<"a= "<<a<<" and b= "<<b;

swap(a, b);

cout<<endl<<"After swapping: ";

cout<<"a= "<<a<<" and b= "<<b;


}

OUTPUT:

Enter the value of a: 10

Enter the value of b: 20

Before swapping: a= 10 and b= 20

After swapping: a= 20 and b= 10

8. Explain function prototyping with an example. Jan.-18, Jul-18 Marks 5

What is function overloading in C++?

How to write a C++ program to find the areas of the circle (PI * r * r), rectangle (l * b),
and square (x * x) by getting the r, l, b, and x through keyboard and printing the areas on
the console using the method area() by applying the concept of function overloading.

Function overloading means, two or more functions have the same names but different
argument lists.

The arguments may differ in the type of arguments or number of arguments, or both.

However, the return types of overloaded methods can be the same or different is called
function overloading.

Function overloading conceptshelps us to use the same function names multiple time in
the same program.

Following program demonstrates the overloaded function to find the area of the circle,
rectangle, and square.

#include<iostream>

using namespace std;


int area(int x)

return x*x;

int area(int l, int b)

return l*b;

double area(double r)

return 3.142*r*r;

int main()

int x, l, b;
double r;

cout<<"Enter the length of a square: ";

cin>>x;

cout<<"Enter the length of rectangle: ";

cin>>l;

cout<<"Enter the width of rectangle: ";

cin>>b;

cout<<"Enter the radius of circle: ";

cin>>r;

cout<<endl<<"The area of square is "<<area(x)<<endl;

cout<<endl<<"The area of rectangle is "<<area(l, b)<<endl;

cout<<endl<<"The area of circle is "<<area(r)<<endl;

OUTPUT:

Enter the length of a square: 10

Enter the length of rectangle: 10

Enter the width of rectangle: 15

Enter the radius of circle: 5.5

The area of square is 100


The area of rectangle is 150

The area of circle is 95.0455

9. What is polymorphism? Write a C++ program using the overloaded function area to
find the area of a square, circle, and rectangle. Jul.-17, Marks 6

10. Define function overloading and Write a C++ program to find the areas of the circle
(PI * r * r), rectangle (l * b), and square (x * x) by getting the r, l, b, and x through the
keyboard and printing the areas on the console using the method area() by applying the
concept of function overloading. July 18, Marks 8.

11. What is Function Overloading? Write a C++ program to define three overloaded
functions to find the sum of two integers, the sum of two floating-point numbers, and
the sum of three integers. Jan.-19, Marks 7

Function overloading means, two or more functions have the same names but different
argument lists.

The arguments may differ in the type of arguments or number of arguments, or both.

However, the return types of overloaded methods can be the same or different is called
function overloading.

Function overloading conceptshelps us to use the same function names multiple time in
the same program.

Following program demonstrates the overloaded function to find the sum of two
integers, sum of two floating-point numbers, and the sum of three integers.

#include<iostream>

using namespace std;


int sum(int x, int y)

return x+y;

double sum(double x, double y)

return x+y;

int sum (int x, int y, int z)

return x+y+z;

int main()

cout <<"The Sum of two integers: "<<sum(10, 20)<<endl;

cout <<"The Sum of two floats: "<<sum(10.5, 20.7)<<endl;


cout <<"The Sum of three integers: "<<sum(10, 20,
30)<<endl;

OUTPUT:

The Sum of two integers: 30

The Sum of two floats: 31.2

The Sum of three integers: 60

12. Explain function overloading with an example. Jan.-18, Marks 5

13. Write a short note on function overloading. July-19, Marks 4

14. Explain how one can bridge two classes using the friend function. Write a C++
program to find the sum of two numbers using the bridge friend function add().Jul.-17,
Marks 6

What is friend function in C++ with Programming example?

Explain how one can bridge two classes using the friend function. Write a C++ program
to find the sum of two numbers using bridge function add()

A class can have global non-member functions and member functions of other classes
as friends.

The friend function is a function that is not a member function of the class but it can
access the private and protected members of the class.

The friend function is given by a keyword friend.

These are special functions which are declared anywhere in the class but have given
special permission to access the private members of the class.
Following program demonstrates the C++ program to find the sum of two numbers
using bridge function add().

#include<iostream>

using namespace std;

class addtwonumbers

int x, y;

public:

addtwonumbers()

x = 0;

y = 0;

addtwonumbers(int a, int b)

x = a;

y = b;

}
friend int add(addtwonumbers &obj);

};

int add(addtwonumbers &obj)

return (obj.x+ obj.y);

int main()

addtwonumbers a1;

cout<<"Sum is: "<<add(a1)<<endl;

addtwonumbers a2(10, 20);

cout<<"Sum is: "<<add(a2)<<endl;

OUTPUT:

Sum is: 0

Sum is: 30
15. What are static members of a class? Write a C++ program to count the number of
objects created. Jul.-17, Marks 5

What are static members in C++ with Programming example?

Write a C++ program to count the number of objects created.

A data member prefixed with a static keyword becomes the static member of a class.
Irrespective of the number of objects of the class are created, there exists only one copy
of the static member.

A static member is shared by all objects of the class.

Static data members hold global data that is common to all objects of the class.
Examples of such global data are

count of objects currently present, common data accessed by all objects, etc.

A static data member has certain special characteristics:

It is initialized to zero when first object is created. No other initialization is permitted.

Only one copy of the data member is created for the entire class and is shared by all the
objects of class, no matter how many objects are created.

Static variables are normally used to maintain values common to entire class objects.

The following program demonstrates the C++ program to count the number of objects
created.

#include<iostream>

using namespace std;

class counter

{
private:

static int count;

public:

counter()

count++;

void display()

cout<<"The number of objects created are: "<<count;

};

int counter::count=0;

int main()

counter c1;

counter c2;

counter c3;

c2.display();
}

OUTPUT:

The number of objects created are: 2

16. Define a friend function. Illustrate with an example. Jan-19, Marks 5

17. What is the inline function? Write a C++ program to find the maximum of two
numbers using the inline function. July-19, Marks 8

What is an inline function in C++ with Programming example?

Write a C++ program to find the maximum of two numbers using inline function.

In normal function, First control will move from calling to called function.

Then arguments will be pushed on to the stack, then control will move back to the
calling from called function.

This process takes extra time in executing. To avoid this, we use inline function.

An inline function is expanded whenever it is called.

That is, when we call the inline function the compiler replaces the call with the
corresponding function definition, thus saving time of context switching between the
calling and called function.

The following program demonstrates the C++ program to find the maximum of two
numbers using the inline function.

#include<iostream>

using namespace std;

inline int max(int x, int y)


{

if (x>y)

return x;

else

return y;

int main()

int a, b;

cout<<"Enter the first number: ";

cin>>a;

cout<<"Enter the second number: ";

cin>>b;

cout<<"The maximum number is "<<max(a,b);

OUTPUT:

Enter the first number: 30

Enter the second number: 25

The maximum number is 30


18. Define a Student class with the following measures: Data Members: RollNo, Name,
average marks Member function: to read the data, to print the data. Write a C++
program to read the data of 10 students and print the 10 students’ information. July-17,
Marks 5

Write a C++ program to define a Student class with following Members:

Data Members: Roll No, Name, Average Marks

Member Functions: to read a data and to print the data.

Program should read 3 student information and print the 3 student information.

#include<iostream>

using namespace std;

class Student

private:

int rno;

char name[20];

float avgmarks;

public:

void read_data();

void print_data();
};

void Student::read_data()

cout<<"Enter the roll no. ";

cin>>rno;

cout<<"Enter name. ";

cin>>name;

cout<<"Enter Average Marks. ";

cin>>avgmarks;

void Student::print_data()

cout<<rno<<"\t"<<name<<"\t"<<avgmarks<<endl;

int main()

Student std[5];

for (int i=0;i<3;i++)

{
std[i].read_data();

cout<<"Student Information is"<<endl;

cout<<"RNo\tName\tAvg Marks"<<endl;

for (int i=0;i<3;i++)

std[i].print_data();

return 0;

Output:

Enter the roll no. 1

Enter name. Mahesh

Enter Average Marks. 95.98

Enter the roll no. 2

Enter name. Rahul

Enter Average Marks. 99.99

Enter the roll no. 3

Enter name. Prabhas


Enter Average Marks. 100

Student Information is

RNo Name Avg Marks

1 Mahesh 95.98

2 Rahul 99.99

3 Prabhas 100

4 Sachin 97

5 Virat 96

19. How does namespace help in preventing pollution of the global namespace?
July-17, Jan.-18, Marks 4

What are namesapces in C++ programming language?

What is std namespace in C++ programming language?

How to create our own namespace to avoid global name conflicts?

Namespace is a new concept introduced by the ANSI C++ standards committee.

A namespace is introduced to avoid name clashes (global name conflicts) like


re-declaration of variables, method names, class, and structure names.

Syntax for using standard namespace:

using namespace std;

In the above syntax “std” is the namespace where ANSI C++ standard class libraries are
defined.
If one wants to use functions like cout, cin, endl etc, he has to include std namespace in
his program. Otherwise compiler flag an compile time error.

Even own namespaces can be defined.

Syntax:

namespace namespace_name

//Declaration of variables, functions, classes, etc.

Programming exaple to demonstrate the use of namespace:

#include<iostream>

using namespace std;

namespace ns1

int a = 10;

namespace ns2

float a = 20.5;

}
int main()

cout<<"Namespace example"<<endl;

cout<<"The value of a in ns1 is "<<ns1::a<<endl;

cout<<"The value of a in ns2 is "<<ns2::a<<endl;

return 0;

Output:

Namespace example

The value of a in ns1 is 10

The value of a in ns2 is 20.5

In the above program we can use cout, endl as we have used the standard namespace
that is std in our program.

The ns1 and ns2 are the programmer generated namespaces. To access the value of a
from ns1 and ns2 we need to use scope resolution operator.

How to write a C++ program to get the employee details (emp no, emp name, basic
salary (initialized to 1000 by the constructor) and allowance) of the Employee class
through the keyboard using the method GetData() and display them using the method
DispData() on the console in the format emp no, emp name, basic salary, allowance.

Program Description:

1. First we create a class named Employee.


2. Data members such as empid, emp name, basic salary and allowances are added as
private members of the class.

3. Next, we add a default constructor to initialize the basic salary of all employees to
1000. That is, whenever an object of class employee is created, the basic salary of the
employee is initialized to 1000.

4. GetData() and DispData() are the two-member functions. Which are declared in class
employee and are defined outside the class using scope resolution operator.

5. In main method an array of employee object is created to store multiple employee


information.

6. Finally, GetData() is called three times to store the 3 employee information and
DispDat() is called 3 times to display the employee information.

The following program demonstrates the C++ program to get and display employee
details using an array of objects concept

#include<iostream>

using namespace std;

class Employee

private:

int empid;

char empname[20];

int bsalary;

int allowance;
public:

Employee()

bsalary = 1000;

void GetData();

void DispData();

};

void Employee::GetData()

cout<<"Enter the employee id: ";

cin>>empid;

cout<<"Enter the employee name: ";

cin>>empname;

cout<<"Enter the employee allowace: ";

cin>>allowance;

void Employee::DispData()

{
cout<<endl<<empid<<"\t"<<empname<<"\t"<<bsalary<<"\t"<<allowance
;

int main()

Employee e[3];

cout<<"Enter the employee information:"<<endl;

for(int i=0;i<3;i++)

e[i].GetData();

cout<<endl<<"The employee information is:";

cout<<endl<<"EmpID \t Name \t Bsalary \t Allowance";

for(int i=0; i<3;i++)

e[i].DispData();

OUTPUT:
Enter the employee information:

Enter the employee id: 123

Enter the employee name: Mahesh

Enter the employee allowace: 100

Enter the employee id: 234

Enter the employee name: Vidya

Enter the employee allowace: 300

Enter the employee id: 345

Enter the employee name: Prabhas

Enter the employee allowace: 500

The employee information is:

EmpID Name Bsalary Allowance

123 Mahesh 1000 100

234 Vidya 1000 300

345 Prabhas 1000 50

20. Explain namespace, with an example. July-18, Marks 4

21. Write a C++ program to get the employee details (empno, empname, bsalary
(initialized to 1000 by constructor) and allowance) of the Employee class through the
keyboard using the method GetData() and display them using the method DispData() on
the console in the format empno, empname, bsalary,allowance. VTU : July-18, Marks 8
22. What is a constructor? List the different types of constructors and explain the
default constructor with an example. Jan.-18, Marks 6

What are Constructors?

What are the types of constructors with simple programming examples?

Constructors are special class functions which performs initialization of every object.

The Compiler calls the Constructor whenever an object is created.

Constructor’s initialize values to data members after storage is allocated to the object.

While defining a constructor you must remember that the name of constructor will be
same as the name of the class, and constructors never have return type.

Constructors can be defined either inside the class definition or outside class definition
using class name and scope resolution :: operator.

Constructors are of four types :

1. Default Constructor

2. Parameterized Constructor

3. Copy Constructor

4. Explicit Constructor

1. Default Constructor in C++

Default constructor is the constructor which doesn’t take any argument. It has no
parameters in default constructors

Syntax :
class_name ()

Constructor Definition

Following program demonstrates the default constructor:

#include<iostream>

using namespace std;

class areaRect

private:

int h, w;

public:

areaRect()

h = 0;

w = 0;

int area()

{
cout<<"Enter the height of rectangle: ";

cin>>h;

cout<<"Enter the width of the rectangle: ";

cin>>w;

return h*w;

};

int main()

int result;

areaRect a1;

result = a1.area();

cout <<endl<<"Area of Rectangle is: "<<result;

Output:

Area of Rectangle is: 0

2. Parameterized Constructor in C++

These are the constructors with parameters. Using this Constructor you can provide
different values to data members of different objects, by passing the appropriate values
as an argument.
Syntax :

class_name ()

Constructor Definition

Following program demonstrates the parameterized constructor:

#include<iostream>

using namespace std;

class areaRect

private: int h, w;

public:

areaRect()

h = 0;

w = 0;

areaRect(int x, int y)

{
h = x;

w = y;

int area()

return h*w;

};

int main()

int result;

areaRect a1;

result = a1.area();

cout <<endl<<"Area of Rectangle is: "<<result;

areaRect a2(10, 20);

result = a2.area();

cout <<endl<<"Area of Rectangle is: "<<result;

Output:
Area of Rectangle is: 0

Area of Rectangle is: 200

3. Copy Constructor in C++

Copy constructor is a special type of constructor in which new object is created as a


copy of an existing object.

The copy constructor is called whenever a new variable is created from an object.

Syntax :

class_name (class_name &obj)

Constructor Definition

Following program demonstrates the copy constructor:

#include<iostream>

using namespace std;

class areaRect

private: int h, w;

public:

areaRect()
{

h = 10;

w = 10;

areaRect(areaRect &obj)

h = obj.h;

w = obj.w;

int area()

return h*w;

};

int main()

int result;

areaRect a1;

result = a1.area();
cout <<endl<<"Area of Rectangle is: "<<result;

areaRect a2(a1);

result = a2.area();

cout <<endl<<"Area of Rectangle is: "<<result;

Output:

Area of Rectangle is: 100

Area of Rectangle is: 100

4. Explicit Constructor in C++

In C++, compiler is allowed to make one time type conversion to resolve the parameters
to functions.

In C++, a constructor with only one required parameter is considered as an implicit


conversion function. It converts the parameter type to the class type.

Prefixing explicit keyword prevents the constructor from using the implicit conversion.

Syntax :

explicit class_name (parameters)

Constructor Definition

}
Following program demonstrates the explicit constructor:

#include<iostream>

using namespace std;

class test

private:

int val;

public: explicit test(int x)

val = x;

void display()

cout <<"The value of val is "<<val;

};

int main()

test t1=10;
t1.display();

Output:

The value of val is 10.

How to use constructors in C++ programming with examples?

23. What is a constructor? Mention its types. Explain parameterized constructor with an
example. Jan.-19, Marks 6

24. What is a constructor? Mention its type. Explain the parameterized constructor with
a suitable code. July-19, Marks 8

25. Write a C++ program to define an Employee class with the following Members: Data
Members: empid, emp salary Member Functions: to read a data and to print the data.
The program should use an array within the class to read 3 employee information and
print the 3 employee information.

Write a C++ program to define a Employee class with the following Members:

Data Members: empid, emp salary

Member Functions: to read a data and to print the data.

Program should use array within class to read 3 employee information and print the 3
employee information.

Source Code for C++ program to read and print Employee information

#include<iostream>
using namespace std;
class Employee
{
private:
int empid[3];
int empsal[3];
public:
void read_data();
void print_data();
};
void Employee::read_data()
{
for (int i=0; i<3;i++)
{
cout<<"Enter the Employee ID. ";
cin>>empid[i];
cout<<"Enter the Employee salary. ";
cin>>empsal[i];
}
}
void Employee::print_data()
{
for (int i=0; i<3;i++)
{
cout<<empid[i]<<"\t"<<empsal[i]<<endl;
}
}
int main()
{
Employee emp;
emp.read_data();
cout<<endl<<"Employee Information is: "<<endl;
cout<<"EmpID\tEMP Salary"<<endl;
emp.print_data();
}

Output:

Enter the Employee ID. 123

Enter the Employee salary. 10000

Enter the Employee ID. 234


Enter the Employee salary. 20000

Enter the Employee ID. 345

Enter the Employee salary. 30000

Employee Information is:

EmpID EMP Salary

123 10000

234 20000

345 30000

You might also like