Programming Test Set-1
Programming Test Set-1
Programming Test Set-1
Total Marks : 20
Question 1
Here display() is a non member function which should display the data member of Myclass.
Apply the proper concept to fill the blank so that the given test cases will pass. Marks 2
#include<iostream>
using namespace std;
class MyClass { int x_;
public:
MyClass(int i) : x_(i) {}
________________________; // Declare the display function.
};
void display(__________________) {
cout << " " << a.x_;
}
int main(){
int x;
cin >> x;
MyClass obj(x);
display(obj);
return 0;
}
1
Private test case-1
Input: 0
Output: 0
Solution:
friend void display(const MyClass &a); // const MyClass &a
2
Question 2
Fill in the blanks below. The name of the parameter of the constructor should be nc. Marks 2
#include <iostream>
using namespace std;
class Engine {
public:
Engine(int nc) :cylinder(nc){}
void start() {
cout << getCylinder() << " cylinder engine started" ;
};
int getCylinder() {
return cylinder;
}
private:
int cylinder;
};
int main()
{
int cylin;
cin >> cylin;
Car c(cylin);
c.start();
return 0;
}
3
Public Test case-2
Input:10
Output: car with 10 cylinder engine started
10 cylinder engine started
Solution
Car(int nc) : Engine(nc) { }
Engine::start();
4
Question 3
Consider the following code. Fill up the code in editable section to congratulate the Manager
and the Clerk, so that outputs for the test cases will match. Marks: 2
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
string Name;
double salary;
Employee(string fName, double sal) : Name(fName), salary(sal) {}
void show() {
cout << Name << " " << salary;
}
void addBonus(double bonus) {
salary += bonus;
}
};
class Manager :public Employee {
public:
Manager(string fName, double sal) : Employee(fName, sal) {}
};
// Call the proper function/s to congratulate the Manager and the Clerk
return 0;
}
5
2000
1000
Output: Kevin 2200 Steve 1200
Solution
congratulate(&c1);
congratulate(&m1);
6
Question 4
Fill in the blanks with appropriate casting. Marks: 2
#include <iostream>
using namespace std;
class Base {
public:
virtual void DoIt() = 0; // pure virtual
virtual ~Base() {};
};
Base* CreateRandom(int x) {
if ((x % 2) == 0)
return new Foo;
else
return new Bar;
}
int main() {
int lim = 0;
cin >> lim;
for (int n = 0; n < lim; ++n) {
7
Public Test case-2
Input: 2
Output: 12,13,14,15:
Solution
Bar* bar = dynamic_cast<Bar*>(base);
Foo* foo = dynamic_cast<Foo*>(base);
8
Question 5
Modify the code in editable section to match the public test cases. Marks: 2
#include <iostream>
#include <vector>
using namespace std;
class Test {
static int count;
int id;
public:
Test(int id) {
count++;
cout << count << ’ ’;
if (count == id)
throw id;
}
~Test() {}
};
int Test::count = 0;
int main() {
int n, m = 0;
cin >> n >> m;
_______________________ // Using STL vector, declare testArray
try {
for (int i = 0; i < n; ++i) {
testArray.push_back(Test(m));
}
}
_______________________ // Write the catch clause
{
cout << "Caught " << i << endl;
}
return 0;
}
9
Output:
1 2 3 4 5 6 Caught 6
Solution
vector<Test> testArray;
catch (int i)
10
Question 6
Fill up the blanks to get the desired output according to the test cases in the perspective of
dynamic memory allocation and de-allocation. Marks 2
#include <iostream>
using namespace std;
int main(){
int d;
____________________ // Declare variable ’p’ and use operator new to
// allocate memory to it
cin >> d ;
*p = d ;
cout << ++*p + d++;
return 0;
}
solution
int *p = (int *)operator new(sizeof(int)); // operator // p
11
Question 7
Consider the code given below. Fill up the marked lines to complete the code to match the
output of the test cases. Marks: 2
Public
#include<iostream>
using namespace std;
class MyClass {
public:
static int count;
MyClass(){ count++; }
~MyClass() { _________________ } // Write the destructor
};
int MyClass::count = 1;
int main() {
cin >> MyClass::count;
MyClass *pt;
pt = new MyClass[2];
delete[] pt;
return 0;
}
Public-1
Input:
20
Output: 21,20,
Public-2
Input:
2
Output: 3,2,
Private
Input:
42
Output: 41,42,
Solution
count--; cout << MyClass::count << ",";
12
Question 8
Consider the code given below. Fill up the marked lines to complete the code to match the
output of the test cases. Marks: 2
#include <iostream>
#include <stdexcept>
using namespace std;
int main(void) {
int i = 0, total, bound;
cin >> total >> bound;
try {
i = arraybound(total, bound);
std::cout << i ;
}
catch (std::overflow_error& e){ // catch the exception here
return 0;
}
Public 1
Input:
50
50
Output: Array Out of Bounds
Public 2
Input:
10
5
Output: 50
Private 1
Input:
2
54
Output: Array Out of Bounds
13
Solution
throw std::overflow_error("Array Out of Bounds")
14
Question 9
Consider the code given below. Fill up the marked lines to complete the code to match the
output of the test cases. Marks: 2
#include <iostream>
using namespace std;
class Area {
public:
int area;
int calc(int l, int b) { area = 4 *l*b; return area; }
};
class Perimeter {
public:
int peri;
int calc(int l, int b) {peri = 8 * (l + b); return peri; }
};
private:
int length, breadth;
int area() {
/* Calls area_calc() of class Area and returns it. */
______________________________;
}
int peri() {
/* Calls peri_calc() function of class Perimeter and returns it. */
__________________________________;
}
public:
Rectangle(int l, int b) : length(l), breadth(b) {}
int print() {
cout << area() << " " << peri();
}
};
int main() {
int l, b;
cin >> l >> b;
15
Public 1
Input:
3
4
Output:
48
56
Public 2
Input:
4
5
Output:
80
72
Private 1
Input:
1
2
Output:
8
24
Solution
return Area::calc(length, breadth)
return Perimeter::calc(length, breadth)
16
Question 10
Consider the code given below. Fill up the marked lines to complete the code to match the
output of the test cases. Marks: 2
#include <iostream>
using namespace std;
class A {
int n;
protected:
A(int i) : n(i) { }
virtual void print() = 0;
virtual int get(){ return n; }
};
class B : private A {
public:
B(int i) : A(i) {}
int get() {
_______________; // The get function body
}
};
class C : public B {
public:
C(int i) : B(i) {}
void print() {
cout << __________ << endl; // display the result of the get function
}
};
int main(){
int n;
cin >> n;
C *p = new C(n);
p->print();
return 0;
}
17
Public 1
Input:
9
Output:
9
Public 2
Input:
0
Output:
0
Private 1
Input:
1
Output:
1
Solution
return A::get()
get()
18