CT-2 QP - Set B - Answer

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

SRM Institute of Science and Technology

Faculty of Engineering and Technology


School of Computing
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamilnadu
Academic Year:2022 -2023EVEN
Test: CLA T-2 Date: 18-04-2023
Course Code &Title:21CSC101T – Object Oriented Design and Programming Duration: 2 periods
Year &Semester:I / II Max. Marks: 50
SET B

Course Articulation Matrix:

Sl.No. Course
Outcomes PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 P10 P11 P12

1 CO1 - 2 2 - 2 - - - - - - 3
2 CO2 - 2 2 - 2 - - - - - - 3
3 CO3 - 2 2 - 2 - - - - - - 3
4 CO4 - 2 2 - 2 - - - - - - 3
5 CO5 - 2 2 - 2 - - - - - - 3

PART– A (20*1 =20 Marks)

Q. MCQ Marks BL CO PO PI
No Code
1 Consider that A is a parent class; B and C are the sub 1 4 3 2 2.4.3
classes. Which of the following is incorrect?
a. Object of B can access properties of A
b. Object of C can access properties of A
c. Object of C can access properties of B
d. Both options (a) and (b) are correct
2 What is the output of this program? 1 4 3 2 2.4.3
Note: Includes all required header files
using namespace std;
class Base
{
public:
Base(){}
~Base(){}
protected:
private:
};
class Derived:public Base
{
public:
Derived(){}
Derived(){}
private:
protected:
};
int main()
{
cout<< "Executed" <<endl;
}
A. Executed
B. Error
C. Underflow
D. Overflow
3 How is a static member function similar to a friend 1 2 3 2 1.3.1
function?
a. A static member function is like a friend
function that doesn't need to be public
b. A friend function is similar to binary operator
overloading.
c. Friend function does not have access to class’s
private members
d. Friend function have implicit this operator
4 #include <iostream> 1 1 3 1 1.3.1
#include <string>
using namespace std;
class Box
{
int capacity;
public:
Box(int cap){
capacity = cap;
}

friend void show();


};

void show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}

int main(int argc, char const *argv[])


{
show();
return 0;
}
a) Value of capacity is: 10
b) Value of capacity is: 100
c) Error
d) Segmentation fault
If __________________ inheritance is done 1 1 3 1 1.4.1
5 continuously, it is similar to tree structure.
a. Hierarchical
b. Multiple
c. Multilevel
d. Hierarchical and Multiple
6 Which amongst the following is true for hybrid 1 1 3 2 2.1.2
inheritance?
a. Constructor calls are in reverse
b. Constructor calls are priority based
c. Constructor of only derived class is called
d. Constructor calls are usual
7 In late binding, function call is resolved during? 1 2 3 2 3.3.1
a. runtime
b. Compile time
c. Infinite time
d. Before compile time
8 Show the correct statement 1 3 3 5 3.3.2
a. Pure virtual functions and virtual functions are
the same
b. Both Pure virtual function and virtual function
have an implementation in the base class
c. Pure virtual function has no
implementation in the base class whereas
virtual function may have an
implementation in the base class
d. The base class has no pure virtual function
9 ________ is used to represent concurrent flows in an 1 1 3 5 5.1.2
Activity Diagram
a. slide bar
b. synchronization bar
c. swim lane
d. branch
10 What is the correct output of given code snippets in 1 3 3 3 3.4.3
C++?
#include <iostream>
using namespace std;
inline void myfun();
int main()
{
myfun();
return 0;
}
inline void myfun()
{
cout<< "Hello from inline function";
}
a. Hello from inline function
b. Syntax error
c. Run time error
d. No output
11 Template concept allows classes to operate with ______ 1 1 4 3 2.4.1
types.
a) same
b) generic
c) special
d) classified
12 Which of the following is the correct way to use a function 1 1 4 3 2.4.1
template to swap two values?
template <typename T>
void swap(T a, T b)
{ T temp = a; a = b; b = temp; }
int main()
{ int x = 5, y = 10;
swap(x, y);
return 0; }
(a) No error
(b) Type mismatch error
(c) No matching function error
(d) Undefined reference error
13 Which of the following statements about exception 1 3 4 5 3.3.2
handling in C++ is incorrect?
(a) A try block must be followed by at least one catch
block. (b) Multiple catch blocks can be used to catch
different types of exceptions. (c) The throw statement is
used to raise an exception. (d) The try block must always
be followed by a finally block.

14 How do you create an instance of a class template in C++? 1 1 4 3 2.4.1


(a) MyClassmyObj; (b) MyClass<int>myObj; (c)
MyClass(T) myObj; (d) MyClass<T>myObj;
15 Which of the following statements about multilevel 1 3 4 2 3.3.2
exception handling in C++ is correct?
(a) A function can only throw exceptions that are defined
in its own try-catch block. (b) A function can only catch
exceptions that are thrown in its own try-catch block. (c) A
function can throw exceptions that are caught in a
parent try-catch block.(d) A function can only catch
exceptions that are defined in a parent try-catch block.
16 Return type of uncaught_exception() is ___________. 1 3 4 3 3.3.2

A. int

B. bool

C. char *

D. double
17 In nested try block, if inner catch handler gets executed, 1 1 4 3 2.4.1
then __________?
a. Program executes infinitely.
b. Outer catch handler will also get executed.
c. Compiler will jump to the outer catch handler and then
executes remaining executable statements of main().
d. Compiler will execute remaining executable
statements of outer try block and then the main().
18 Component Diagrams are considered as? 1 3 4 2 2.1.3
(A). Behavioural Diagrams
(B). Structure Diagrams
(C). Activity Diagrams
(D). Sequence Diagrams

19 Which of the following diagram is used to illustrate 1 3 4 3 5.1.2


the structure of arbitrarily complex systems?
(A). Component Diagram
(B). Class diagram
(C). Sequence diagram
(D). Data flow diagram

20 A package diagram consists of the following? 1 3 4 3 3.3.2


a) Package symbols, Components
b) Groupings of Use cases, classes, components
c) Interface and components
d) Package symbols, Groupings of Use cases,
classes & components
SET B
PART- B (2 * 10=20 Marks)
CONCEPT UNDERSTANDING
21 (i) "The inline keyword tells the compiler that 3+7 6 3 12 12.1.1
A inline expansion is preferred. However, the
compiler can create a separate instance of the
function (instantiate) and create standard
calling linkages instead of inserting the code
inline". Explain the cases where this behavior
can happen.

Answer:

 The inline specifier instructs the


compiler to replace function calls with
the code of the function body. This
substitution is “inline expansion”
(sometimes called “inlining”).
 Inline expansion alleviates the
function-call overhead at the potential
cost of larger code size.

The inline keyword tells the compiler that


inline expansion is preferred. However, the
compiler can create a separate instance of the
function (instantiate) and create standard
calling linkages instead of inserting the code
inline. Two cases where this can happen are:

Recursive functions.

Functions that are referred to through a pointer


elsewhere in the translation unit.
Note that for a function to be considered as a
candidate for inlining, it must use the new-
style function definition. Functions that are
declared as inline and that are not class
member functions have internal linkage unless
otherwise specified.

The function is virtual.

The program takes the address of the function.


If the compiler cannot inline a function, it
generates a level 1 warning.

As with normal functions, there is no defined


order of evaluation of the arguments to an
inline function. In fact, it could be different
from the order in which the arguments are
evaluated when passed using normal function
call protocol.
(ii) Discuss Friend functions and its benefits of
using appropriate examples.

Answer:
 The friend function in C++ is defined
outside the scope of the class. It has
the authority to access all protected
members and private members of the
class.

 Friends are not member functions, but


the prototypes for friend functions
appear in the class function only.

Syntax of Friend Function in C++


declare the friend function using the keyword
“friend” inside the body of the class.

class Box {

double width;

public:

double length;

friend void printWidth ( Box box ) ;

void setWidth ( double wid ) ;

};

The friend function is invoked like a regular


function without using the object and is
declared in the public or private part.
It is not in the scope of the class that declares
it as a friend.
It has to use the object name and dot
membership operator with the member name
to access the member names.

Benefits of Friend Function in C++


 The friend function allows the
programmer to generate more efficient
codes.
 It allows the sharing of private class
information by a non-member
function.
 It accesses the non-public members of
a class easily.
 It is widely used in cases when two or
more classes contain the interrelated
members relative to other parts of the
program.
 It allows additional functionality that is
not used by the class commonly.
Example:

#include <iostream>

using namespace std;

class Distance {

private:

int meter;

// friend function

friend int addFive(Distance);

public:

Distance() : meter(0) {}

};

// friend function definition

int addFive(Distance d) {

//accessing private members from


the friend function

d.meter += 5;

return d.meter;

int main() {

Distance D;

cout << "Distance: " << addFive(D);

return 0;

Output:
Distance: 5
OR
21 Write a C++ program to read two integers from the 10 4 3 12 12.2.1
B user and divide them, but handle exceptions for
division by zero and invalid inputs. The program
should use a try-catch block to catch any exceptions
that may occur.

#include<iostream>
using namespace std;
int main()
{
int a,b;
cin >> a>> b;
try
{
if (b!=0)
{
cout<<“result (a/b)=”<<a/b;
}
else
{
throw(b);
}
}
catch(int i)
{
cout <<“exception caught”;
}
}

22 Draw an Activity diagram with swim lane for the 10 4 3 12 12.2.1


A front office system in a 5- star hotel described below.
Front office of any hotel is responsible for all room
reservations, room allocations and final settlement of
bills. Any company or person can reserve rooms for
their future stay. They have to indicate from what date
to what date they need the room. They also have to
indicate how many rooms are required. Sometimes
the reservations could be cancelled or dates or number
of rooms changed. For reservation, cancellation or
modification of rooms, customer receives an
acknowledgement from the hotel. Explain the
components in detail.
OR
22 For an Online Hospital Management System, draw 10 4 3 12 12.2.1
B the UML activity diagram without swimlanes and the
corresponding Component diagram.
PART – C (1*10=10 Marks)
SCENARIO BASED/HOTs
23 Create a class with classname as Shape and use the data 10 6 3 12 12.1.2
A members as dimension which should be accessed by all
members of the class and outside of the class also. Derive
a new class as Square which inherits from Shape and
calculate the area using the member function
calculateArea().Derive a new class as Circle which
inherits from the base class Shape and calculate the area
using the member function calculateArea().Use Pure
virtual function as virtual float calculateArea() = 0 in
base class Shape.
Calculate the area of Square and rectangle and print the
same.

class Shape {
protected:
float dimension;

public:
void getDimension() {
cin>> dimension;
}
// pure virtual Function
virtual float calculateArea() = 0;
};

// Derived class
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};

// Derived class
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};

int main() {
Square square;
Circle circle;

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


square.getDimension();
cout<< "Area of square: " <<square.calculateArea() <<endl;

cout<< "\nEnter radius of the circle: ";


circle.getDimension();
cout<< "Area of circle: " <<circle.calculateArea() <<endl;

return 0;
}

Output:

Enter the length of the square: 4


Area of square: 16

Enter radius of the circle: 5


Area of circle: 78.5

OR
23 Ram and shiva are working as accountants in bank. They 10 3 4 2 3.3.2
B need to know all the arithmetic operations to verify the
accounts. Since they are weak in mathematics, they found
difficulty in doing such arithmetic operations. Help them
to check accounts by applying arithmetic operations
including add, subtract, multiply and divide using class
template.

#include <iostream>
using namespace std;
template <class T>
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void displayResult()
{
cout<< "Numbers are: " << num1 << " and " <<
num2 << "." <<endl;
cout<< "Addition is: " << add() <<endl;
cout<< "Subtraction is: " << subtract() <<endl;
cout<< "Product is: " << multiply() <<endl;
cout<< "Division is: " << divide() <<endl;
}
T add() { return num1 + num2; }
T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};
int main()
{
Calculator<int>intCalc(2, 1);
Calculator<float>floatCalc(2.4, 1.2);
cout<< "Int results:" <<endl;
intCalc.displayResult();
cout<<endl<< "Float results:" <<endl;
floatCalc.displayResult();

return 0;
}

OUTPUT:
Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

You might also like