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

MidQPaper_OOP_Solution

This document is a sample solution for an Object Oriented Programming internal examination. It includes questions and answers on topics such as access specifiers, memory management, data hiding, polymorphism, inheritance, function overloading, and examples of C++ functions. The document also contains code snippets illustrating concepts like call by value and call by reference, as well as class creation and member functions.

Uploaded by

senona6037
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)
9 views

MidQPaper_OOP_Solution

This document is a sample solution for an Object Oriented Programming internal examination. It includes questions and answers on topics such as access specifiers, memory management, data hiding, polymorphism, inheritance, function overloading, and examples of C++ functions. The document also contains code snippets illustrating concepts like call by value and call by reference, as well as class creation and member functions.

Uploaded by

senona6037
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/ 5

202000212: Object Oriented Programming

Semester - 2 Internal Examination – March 2024


SAMPLE Solution

Date: 18/03/2024 Time: 2:00 pm To 3:00 pm Maximum


Instructions:
1. Figures on the right indicate full marks.
2. Make the suitable assumption if required, do specify the same.

Q. 1 (A) Answer the following. [04]

(i) State the difference between private and public access specifiers.
Private members can be accessed only within the class
Public members can be accessed from outside the class as well

(ii) List memory management operators in C++.


New, delete

(iii) Explain reference variable in C++.


A reference variable is an alias of an existing variable.
Both variable refer to the same data in memory.
or
int a;
int &b = a;
b is a reference variable

(iv) Mention the use of scope resolution operator.


Scope resolution operator is used to define the member function outside the class
Scope resolution operator is used to access the global version of a variable.

Q. 2 (A) Explain the following terms: [04]


1. Data hiding
Data hiding is an object-oriented programming (OOP) technique
specifically used to hide internal object details (i.e., data members).
Data hiding guarantees exclusive data access to class members only and
protects and maintains object integrity by preventing intended or
unintended changes and intrusions
2. Polymorphism
The term polymorphism refers to the presence of multiple forms.
Polymorphism usually occurs when there is a hierarchy of classes that
are linked by inheritance. C++ polymorphism means that depending on
the type of object that invokes the function, a different function will be
executed.
Functions with same name, function overloading is an example of
polymorphism
3. Inheritance
It is possible to inherit attributes and methods from one class to
another. This feature provides reusability and a hierarchical
structure in the program
4. Inline function
An inline function is a function that is expanded in line when it is
called. When the inline function is called whole code of the inline
function gets inserted or substituted at the point of the inline function
call.
Inline is a request, which may be ignored by the compiler.

Q. 2 (B) a. List 4 differences between C and C++. [04]


C C++
Data and functions are separated Data and functions are
in C because it is a procedural encapsulated together in form of
programming language. an object in C++.
C does not support information Data is hidden by the
hiding. Encapsulation to ensure that data
structures and operators are used
as intended.
C does not have reference C++ supports reference variables
variables
C has malloc and free for dynamic C++ has new and delete operators
memory allocation for dynamic memory allocation

b. Find the errors in the following C code. Remove the errors and find the
output.

#include<stdio.h>
int main()
{
int i=3, *j, k;
j = i;
print("%d\n", i**j*i+*j);
return 0;
}
#include<stdio.h>
int main()
{
int i=3, *j, k;
j = &i;//error remove
print("%d\n", i**j*i+*j);
return 0;
}
Output 30

OR
(B) What are the different types of functions in C++? Explain pass by [04]
value and pass by reference with respect to functions in C++ with a
suitable example.

Inline functions
Friend functions
Overloaded functions
Virtual functions
Functions with arguments and return values
Functions with arguments and without return values
Functions without arguments and with return values
Functions without arguments and without return values

Call by Value
In call by value, the actual value that is passed as argument is not changed after
performing some operation on it. When call by value is used, it creates a copy
of that variable into the stack section in memory. When the value is changed, it
changes the value of that copy, the actual value remains the same.

#include<iostream>
using namespace std;

void my_function(int x) {
x = 50;
cout << "Value of x from my_function: " << x << endl;
}

void main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 10

Call by Reference
In call by reference the actual value that is passed as argument is changed after
performing some operation on it. When call by reference is used, it creates a
copy of the reference of that variable into the stack section in memory. Is uses
a reference to get the value. So when the value is changed using the reference it
changes the value of the actual variable.

#include<iostream>
using namespace std;

void my_function(int &x) {


x = 50;
cout << "Value of x from my_function: " << x << endl;
}

main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output:
Value of x from my_function: 10
Value of x from main function: 50
Q-3 (A) Create a class time with members hours and minutes. Write a member function
display() to print the time. Write a main program and create 2 objects of class
time and display the time.

#include<iostream>
Using namespace std;
class time
{
int hours, minutes;
public:
void getdata()
{
cout<<”Enter time in hours and minutes form”;
cin>>hours;
cin>>minutes;
}
Void display()
{
Cout<<”Time is”<<hours<<”Hours and”<<minutes<<”Minutes”;
}
};
Int main()
{
time t1, t2;
t1.getdata();
t2.getdata();
t1.display();
t2.display();
return 0;
}

(B) What do you mean by function overloading? List the rules and explain with a [04]
suitable example.
 C++ provides function overloading which allows to use multiple
functions sharing the same name. Function overloading is an example
of compile time polymorphism.

 However, the two functions with the same name must differ in at least
one of the following,
a) The number of arguments
b) The data type of arguments
c) The order of appearance of arguments
d) Function overloading does not depend on return type.

Example:
void display(int var)
{
cout << "Integer number: " << var << endl;
}
void display(float var)
{
cout << "Float number: " << var << endl;
}
void display(int var1, float var2) {
cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}
int main()
{
int a = 5; float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}

OR
(B) Create a Class shape with members length and breadth. Write member [04]
functions to initialize the members and display the members. Write a function
area() to find the area of a rectangle using functions with default arguments.

class shape
{
public:
int length, breadth;
void input()
{
cout<<”Enter length and breadth of a Rectangle:”
cin>>length>>breadth;
}
void display()
{
cout<<”The length and Breadth of a Rectangle are:”;
cout<<length<<breadth;
}
int area(length=10, breadth=5)
{
return length*breadth;
}
};
int main(){
shape s;
s.area();
s.area(4);
s.area(5,8);
return 0;
}

You might also like