Oops Answers

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

QUESTION BANK - ANSWERS

ECS51004 OBJECT ORIENTED PROGRAMMING USING C++

SL.NO UNIT I CO BTL


PART A
1 Define Encapsulation and Data hiding. 1 1

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.

Data hiding is the instruction of the data from direct access by the
program. It helps the programmer to build secure programs that cannot
be invaded by code on other parts of the program. It protects and
maintains object integrity by preventing intended or unintended changes
and intrusions

2 What are the rules to be followed for identifiers? 1 1

1. Only alphabet characters, digits and underscores are permitted.


2. The name cannot start with a digit.
3. Uppercase and lowercase letters are distinct.
4. A declared keyword cannot be used as a variable name.

3 What is the need of type conversion? 1 1

To make a variable of one type compatible with a variable of another type


to perform an operation.
To reduce the memory in use, relevant to the value of the variable

4 What is the output of the following program, if it is correct? Otherwise 1 3


indicate the mistake:
#include <iostream>
using namespace std;
int main()
{
float a,b;
a = (5 + 3 * 5)/4;
b=a+2;
cout<<"a="<<a<<"b="<<b;
return 0;
}
OUTPUT
a=5 b=7

5 What is abstract Class? 1 1

It is used to express broad concepts from which more concrete classes can
be derived. An abstract class-type object cannot be created. It cannot be
instantiated, but they can be subclassed
6 List out the advantages of new operator over malloc[] 1 1

The execution time of new is less than the malloc() function as new is a
construct, and malloc is a function.
new syntax is more readable and easier to maintain.

7 Define class and objects in OOPS 1 1

Object is a triangle of entity that may be exhibiting some well-defined


behavior. These are the basic run-time entities in an object-oriented
system.

Class is a set of attributes and behavior shared by similar objects (or) in


simple way collection of objects of similar type is called a Class. It holds its
own data members and member functions, which can be accessed and
used by creating an instance of that class.

8 List out the basic concepts of OOPS 1 1

1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing

9 List out four Storage Classes in C++ 1 1

1. auto
2. register
3. extern
4. static
5. mutable

10 What are member functions? 1 1

Member functions are operators and functions that are declared as


members of a class. It can help to improve the modularity of code and
increase code reusability.

PART B (6 MARKS)
1 Give classification of operators available in C++ with the help of neat and 1 2
clean diagram.

Arithmetic Operators
Relational Operators

Logical Operators
Bitwise Operators

Assignment Operator

2 Explain the different features of OOPS in detail 1 2

OBJECT
Object is a triangle of entity that may be exhibiting some well-defined
behavior.
These are the basic run-time entities in an object-oriented system.
They may represent a person, a place, a bank account, a table of data or
any item the program has to handle.
They may be represents userdefined data such as vectors, time and lists.

CLASSES
Class is a set of attributes and behavior shared by similar objects (or) in
simple way collection of objects of similar type is called a Class.
It holds its own data members and member functions, which can be
accessed and used by creating an instance of that class.
The entire set of data and code of an object can be made a user-defined
data type with the help of a class.
POLYMORPHISM:
Polymorphism is the ability to take more than one form.
We can use the same function name again and again with different
signatures
An operation may exhibit different behaviors in different instances.
The behavior depends upon the types of data used in the operation.

ENCAPSULATION:
It is the wrapping up of data and function into a single unit (called class).
The data is not accessible to the outside world, and only those functions
which are wrapped in the class can access it

3 Write a Program to find the entered number is even or odd 1 3

#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter an integer number: ";
cin>>num;

if(num%2==0)
cout<<num<<" is an EVEN number."<<endl;
else
cout<<num<<" is an ODD number."<<endl;

return 0;
}
OUTPUT
Enter an integer number: 6
6 is an EVEN number.

4 Write a C++Program to find the biggest among two numbers 1 2

#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
if(num1>num2)
{
cout<<"First number "<<num1<<" is the largest";
}
else
{
cout<<"Second number "<<num2<<" is the largest";
}
return 0;
}

OUTPUT
Enter first number:5
Enter second number:9
Second number 9 is the largest

5 What is the need of array. Discuss different types of arrays. 1 2

An array is a data structure that contains a group of elements. It helps


maintain large sets of data under a single variable name to avoid confusion
that can occur when using several variables. It is easy to manipulate and
short the array data.

One-Dimensional Array:
In this type of array, it stores elements in a single dimension. And, In this
array, a single specification is required to describe elements of the array.
It arranges all the elements in row wise.

Two-Dimensional Array:
In this type of array, two indexes describe each element, the first index
represents a row, and the second index represents a column.
The elements are arranged row-wise and column-wise and are i number of
rows and j number of columns.

6 Write the C++ program to manipulate the following string handling 1 2


functions
a.Strcpy()
b.strcmp()
c.strlen()

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[10]="Hello";
char str2[10]="World";
char str3[10];
int len;
strcpy(str3,str1);
cout<<"strcpy(str3,str1): "<<str3<<endl;

if(strcmp(str1, str2)==0)
{
cout<<"The strings are equal"<<endl;

}
else {
cout<<"The strings are not equal"<<endl;
}

len=strlen(str1);
cout<<"strlen(str1): "<<len<<endl;
return 0;
}

OUTPUT
strcpy(str3,str1): Hello
The strings are not equal
strlen(str1): 5

PART C (10 MARKS)


1 Write a c++ program to print the average of n numbers 1 2

#include <iostream>
using namespace std;
int main() {
int x,i;
float avg = 0,y;
cout << "Enter the number of elements to calculate the average: ";
cin >> x;
cout << "Enter the elements one by one \n";
for(i = 0; i < x; i++)
{
cin >> y;
avg += y;
}
avg /= x;
cout << "\nThe average of the entered input numbers is = " << avg;
return 0;
}
OUTPUT
Enter the number of elements to calculate the average: 4
Enter the elements one by one
1
2
3
4
The average of the entered input numbers is = 2.5
2 Differentiate between call by value and call by reference. Also explain with 1 2
the suitable program

CALL BY VALUE

 The copy of the value is passed into the function


 Changes made inside the function are not reflected in other
function
 Actual and formal arguments will be created in different memory
locations

#include <iostream>
using namespace std;
void change (int data)
void main()
{
int data=3;
change(data);
cout<<"Value of the data:"<<data<<endl;
return 0;
}
void change(int data)
{
data=5;
}

OUTPUT
Value of the data: 3

CALL BY REFERENCE

 Address of the value is passed to the function


 Changes made inside the function are reflected outside the
function also.
 Actual and formal arguments will be created in same memory
location
#include <iostream>
using namespace std;
void swap(int*x,int*y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
int main()
{
int x=500,y=100;
swap(&x,&y);
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of Y is: "<<y<<endl;
return 0;
}

OUTPUT
Value of X is: 100
Value of Y is: 500
3 Develop the source code to find the most occurring element in an array of 1 3
integers

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,j,most_count=0,count,e;
int arr[15],n;
cout<<"Enter number of array elements:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the array elements:";
cin>>arr[i];
}
for(i=0;i<n;i++)
{
count=1;
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count ++;
}
}
if (count>most_count)
{
most_count=count;
e=arr[i];
}
}
cout<<e<<" occurred "<<most_count<<" times which is maximum";
}
getch();
return 0;

OUTPUT
Enter the number of array elements:4
Enter array elements:6
Enter array elements:6
Enter array elements:5
Enter array elements:3
6 occurred 2 times which is maximum
UNIT –II (PART A)
1 What is the difference between local variable and data member? 2 2

A data member belongs to an object of a class whereas local variable


belongs to its current scope.
Data members are accessible to all member function of the class.

A local variable is declared within the body of a function and can be used
only from the point at which it is declared to the immediately following
closing brace.
Local variable are not accessible in any another function or class.

2 What is a function parameter? Give an example 2 1

A function parameter is a variable that is declared in the function


signature and used to receive values passed into the function when it is
called. Function parameters allow us to pass values to functions and make
the functions more flexible and reusable.

#include <iostream>
using namespace std;
int add(int num1, int num2)
{
int sum= num1 + num2;
return sum;
}

int main() {
int x = 5;
int y = 7;
int result = add(x,y);
cout << "The sum of " << x << " and " << y << " is "<< result << endl;
return 0;

3 What is data hiding 2 1

Data hiding is the instruction of the data from direct access by the
program. It helps the programmer to build secure programs that cannot
be invaded by code on other parts of the program. It protects and
maintains object integrity by preventing intended or unintended changes
and intrusions

4 Define abstraction and Encapsulation 2 1

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.

Abstraction: It focuses on the essential characteristics of objects. It refers


to the act of representing essential features without including the
background details or explanations. Classes use the concept of abstraction
and are defined as a list of abstract attributes such as size, weight and
cost, and functions to operate on these attributes.

5 What is the Need for Static Members? 2 1

Static members can be used to store data that needs to be shared among
all instances of the class. They can help reduce memory usage by avoiding
the creation of duplicate data for each instance of the class. It can also be
used to provide a common behaviour among all instances of the class.

6 Define Polymorphism 2 1

Polymorphism is the ability to take more than one form.


We can use the same function name again and again with different
signatures
An operation may exhibit different behaviors in different instances.
The behavior depends upon the types of data used in the operation.

7 What are the roles of friend class and friend function? 2 1

A friend function is a function declared inside a class that is given access to


the private and protected members of that class. This allows the function
to operate on the private data of the class without having to use public
member functions, which may not be desirable for various reasons.

A friend class allows to access the private and protected members of the
class that it is declared in. Friend classes are often used to implement
tightly coupled classes that need to share private data or methods

8 Differentiate runtime polymorphism and compile time 2 2


polymorphism
9 Examine the term of ‘function overloading’ 2 2

Function overloading is used to define two functions of the same type but
with a different number of parameters with the same names. Function
overloading can also be treated as compile-time polymorphism.

10 Define reference variable. Give its syntax. 2 2

Reference variable is an alternate name of already existing variable. It


refers to the address of another variable.
SYNTAX
datatype variable_name; // variable declaration
datatype& refer_var = variable_name; // reference variable

PART B(6 MARKS)


1 Explain difference between Function Overloading and Operator 2 2
Overloading with example

Function Overloading:
Function overloading is a feature in programming languages that allows
the programmer to define multiple functions with the same name but
different parameters. The compiler determines which function to call
based on the number, types, and order of the parameters passed to the
function.

Example:
#include <iostream>
using namespace std;
void SumNum(int A, int B);
void SumNum(int A, int B, int C);
void SumNum(int A, int B, int C, int D);
int main()
{
SumNum(1,2);
SumNum(1,2,3);
SumNum(1,2,3,4);

return 0;
}
void SumNum(int A, int B)
{
cout<< endl << "SUMNUM is : "<< A+B;
}

void SumNum(int A, int B, int C)


{
cout<< endl << "SUMNUM is : "<< A+B+C;
}

void SumNum(int A, int B, int C, int D)


{
cout<< endl << "SUMNUM is : "<< A+B+C+D;
}
Output:
SumNum is 3
SumNum is 6
SumNum is 10

Operator Overloading:

Operator overloading allows the programmer to redefine the meaning of


an operator for a user-defined type. The operator overloading function is
defined as a member function or a global function that takes one or more
operands of the user-defined type.

Example:
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
Output
12 + i9

2 Write a program to print the area of Triangle having sides (4,5) and 2 3
(5,8) .Use constructor for defining the functions

#include <iostream>
using namespace std;
class Triangle {
private:
double length;
double height;
public:
Triangle(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return (length * height)/2;
}
};
int main() {
Triangle tri1(4, 5);
Triangle tri2(5, 8);
cout << "The Area of Triangle 1: " << tri1.calculateArea() << endl;
cout << "The Area of Triangle 2: " << tri2.calculateArea() << endl;
return 0;
}
OUTPUT
The Area of Rectangle 1: 10
The Area of Rectangle 2: 20

3 Describe the constructor overloading with suitable example 2 2

Constructor overloading have the same name but the different number of
arguments. This allows us to create objects using different initialization
methods depending on the arguments passed to the constructor.

#include <iostream>
using namespace std;
class Person {
private:
int age;

public:
Person() {
age = 20;
}
Person(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
OUTPUT
Person1 Age = 20
Person2 Age = 45

4 What are the different ways to define member functions of a class. What 2 2
is the role of scope resolution operator in the definition of member
function?

Inline member function: The function definition is placed directly in the


class definition using the "inline" keyword. This is typically used for small,
simple functions that are called frequently.

Syntax
class MyClass {
public:
inline void myFunction() {
// function definition here
}
};

Member function defined outside the class definition: The function


definition is placed outside the class definition, typically in a separate
source file. This is used for larger, more complex functions.

Syntax:
class MyClass {
public:
void myFunction(); // declaration
};

void MyClass::myFunction() {
// function definition here
}

The scope resolution operator (::) is used in the definition of a member


function outside of the class to identify which class the function belongs
to.
It is used to qualify hidden names so that you can still use them. Scope
resolution operator can be used to identify a member of a namespace , or
to identify a namespace that nominates the member's namespace in a
using directive.

PART C(10 MARKS)


1 Write down a C++ program to illustrate the default constructor and 2 2
parametrized constructor

Default Constructor:
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
Output:
Default Constructor Invoked
Default Constructor Invoked

Parameterized Constructor

#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object
of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
Output:

101 Sonoo 890000


102 Nakul 59000
2 Build the source code to find the areas of the circle (PI * r * r), rectangle (l 2 3
* 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.

#include <iostream>
using namespace std;
const float PI = 3.14159;
class Shape {
public:
float area(float radius)
{
return PI * radius * radius;
}
float area(float length, float breadth)
{
return length * breadth;
}

float area(float side)


{
return side * side;
}
};
int main() {
float radius, length, breadth, side;
Shape shape;

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


cin >> radius;
cout << "Enter the length and breadth of the rectangle: ";
cin >> length >> breadth;
cout << "Enter the side of the square: ";
cin >> side;

cout << "Area of the circle: " << shape.area(radius) << endl;
cout << "Area of the rectangle: " << shape.area(length, breadth) << endl;
cout << "Area of the square: " << shape.area(side) << endl;

return 0;
}

OUTPUT
Enter the radius of the circle: 4
Enter the length and breadth of the rectangle:4 5
Enter the side of the square: 5
Area of the circle: 50.26548
Area of the rectangle: 20
Area of the square: 25

You might also like