CT-2 QP - Set D - Answer
CT-2 QP - Set D - Answer
Sl.No Course
PO PO PO PO PO PO PO PO PO P1 P1 P1
. Outcome
1 2 3 4 5 6 7 8 9 0 1 2
s
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 Mark BL CO PO PI
No s Code
1 In hierarchical inheritance, do members of base class 1 4 3 2 2.4.3
get divided among all of its child classes?
a. Yes, equally
b. Yes, depending on type of inheritance
c. No, it’s doesn’t get divided
d. No, it may or may not get divided
2 In hierarchical inheritance, each class can inherit the 1 2 3 1 1.3.1
base class________________
a. Independently using any inheritance
b. Independently with private inheritance only
c. With same type of inheritance
d. With each class using different inheritance only
3 A friend function declaration appears_________ 1 1 3 1 1.3.1
a. Outside of the class
b. Inside the class declaration to which it is a
friend
c. As a separate declaration inside main()
d. Anywhere prior to the function invocation
4 What is the correct output of given code snippets in 1 3 3 2 2.4.1
C++?
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
class compare
{
int n=100;
char c='a';
friend int larger(compare,compare);
};
int larger(compare c1,compare c2)
{
if (c1.n>c2.c)
return c1.n;
else
return c2.c;
}
int main() {
// Write C++ code here
compare c1,c2;
cout<<larger(c1,c2);
return 0;
}
a. 100
b. 97
c. Run time error
d. ‘a’
5 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
6 If hierarchical inheritance requires to inherit more 1 1 3 2 2.1.2
than one class to single class, which syntax is correct?
(A, B, C are class names)
a. hierarchical class A: public B, public C
b. multiple class A: public B, public C
c. many class A: public B, public C
d. class A: public B, public C
7 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
8 Which is the correct syntax of defining a pure virtual 1 1 3 5 5.2.1
function?
a. pure virtual return_typefunc();
b. virtual return_typefunc() pure;
c. virtual return_typefunc() = 0;
d. virtual return_typefunc();
9 1 2 3 2 2.1.3
What happens to a function defined inside a class
without any complex operations (like looping, a large
number of lines, etc)?
a. It becomes virtual function
b. It becomes friend function
c. It becomes inline function
d. It gives error while compiling
10 .A ________ is a behavior that specifies the sequence 1 1 3 5 5.1.1
of states an object goes through during its lifetime in
response to events.
a. class
b. state machine
c. use case
d. activity
11 How Exception handling is implemented in the C++ 1 2 4 2 1.3.1
program?
a. Using Exception keyword
b. Using try-catch block
c. Using Exception block
d. Using Error handling schedules
12 What id the syntax for catching any type of 1 2 4 2 1.3.1
exceptions?
a. catch(Exception e)
b. catch(…)
c. catch(Exception ALL)
d. catch(ALL)
13 Templates are initiated at_______________________ 1 1 4 2 2.1.2
a. Runtime
b. Compile time
c. At the time of Object creation
d. None of the above
#include <iostream>
using namespace std;
// Forward declaration
class ABC;
class XYZ {
int x;
public:
void set_data(int a)
{
x = a;
}
class ABC {
int y;
public:
void set_data(int a)
{
y = a;
}
// Driver code
int main()
{
ABC _abc;
XYZ _xyz;
_xyz.set_data(20);
_abc.set_data(35);
Answer:
A derived class can be derived from another derived
class. A child class can be the parent of another class.
Syntax:
class A // base class
{
..........
};
class B
{
..........
}
class C : access_specifier B
// derived class
{
...........
};
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle";
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are
vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels”;
}
};
// main function
int main()
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;
}
OR
22 Global group requires a system to be developed for 10 2 4 5 5.1.1
B maintenance of leaves in their organization. There are
different types of leaves: Casual leaves, Medical
leaves, Earned leaves and special leaves. These leaves
vary from employee to employee. If the employee is
regular he can avail all types of leaves. An adhoc
employee can only avail casual leaves. The medical
and earned leaves can be credited in the employees
account. The maximum limit of the credit is 240.
Further if an employee has availed all the leaves his
salary may be deducted. Draw an activity diagram for
the scenario.
PART – C (1*10=10 Marks)
SCENARIO BASED/HOTs
23 Design a simple Calculator performing the four basic 10 3 3 3 3.2.2
A arithmetic operations in C++ using a class template.
The template of the class will consist of two variables
whose values are passed at the time of object creation.
The constructor of this class takes two arguments of
generic datatypes. Further, this Calculator class
template consists of five main functions – show(),
addition(), subtraction(), multiplication(), and
division(). The show() function is responsible for
calling the rest of the four generic functions. Created
two instances from the template of Calculator class
and performed the basic calculations using its class
functions
public:
Calculator(Temp num1, Temp num2)
{
n1 = num1;
n2 = num2;
}
void show()
{
cout<< "Addition is: " << n1 << "+" << n2 << "="
<<addition() <<endl;
cout<< "Subtraction is: " <<n1 << "-" << n2 << "="
<<subtraction() <<endl;
cout<< "Product is: " << n1 << "*" << n2 << "="
<<multiplication() <<endl;
cout<< "Division is: " << n1 << "/" << n2 << "="
<<division() <<endl;
}
int main()
{
Calculator<int> Calc1(25, 12);
Calculator<float> Calc2(13.6, 5.9);
return 0;
}
Output
OR
23 Construct a class named Fruit with a data member to 10 2 4 5 5.1.1
B calculate the number of fruits in a basket. Create two
other class named Apples and Mangoes to calculate
the number of apples and mangoes in the basket. Print
the number of fruits of each type and the total number
of fruits in the basket.
#include <iostream>
using namespace std;
class Fruit
{
public:
static int fruitCounter;
Fruit()
{
fruitCounter++;
}
~Fruit()
{
fruitCounter--;
}
};
class Apples: public Fruit
{
public:
static int applesCounter;
Apples():Fruit()
{
applesCounter++;
}
~Apples()
{
applesCounter--;
}
};
class Mangoes: public Fruit
{
public:
static int mangoesCounter;
Mangoes():Fruit()
{
mangoesCounter++;
}
~Mangoes()
{
mangoesCounter--;
}
};
int Fruit::fruitCounter = 0;
int Apples::applesCounter = 0;
int Mangoes::mangoesCounter = 0;
int main()
{
Apples firstApple,secondApple,thirdApple;
Mangoes
firstMango,secondMango,thirdMango,fourthMango;
cout<< "Total number of fruits: "
<<Fruit::fruitCounter<<endl;
cout<< "Number of apples: "
<<Apples::applesCounter<<endl;
cout<< "Number of mangoes: "
<<Mangoes::mangoesCounter<<endl;
return 0;