CT-2 QP - Set B - Answer
CT-2 QP - Set B - Answer
CT-2 QP - Set B - Answer
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
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;
}
void show()
{
Box b(10);
cout<<"Value of capacity is: "<<b.capacity<<endl;
}
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
Answer:
Recursive functions.
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.
class Box {
double width;
public:
double length;
};
#include <iostream>
class Distance {
private:
int meter;
// friend function
public:
Distance() : meter(0) {}
};
int addFive(Distance d) {
d.meter += 5;
return d.meter;
int main() {
Distance 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”;
}
}
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;
return 0;
}
Output:
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