Programming Test Set-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Programming in C++: Programming Test-1

Total Marks : 20

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology
Kharagpur – 721302
partha.p.das@gmail.com

April 18, 2017

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;
}

Public test case-1


Input: 4
Output: 4

Public test case-2


Input: 8
Output: 8

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;
};

class Car : private Engine


{ // Car has-a Engine
public:
// Define the constructor. Name of the parameter should be ’nc’
________________________
void start() {
cout << "car with " << Engine::getCylinder() <<
" cylinder engine started" << endl;

________________________ // Call the start function of Engine


}
};

int main()
{
int cylin;
cin >> cylin;
Car c(cylin);
c.start();
return 0;
}

Public Test case-1


Input: 8
Output: car with 8 cylinder engine started
8 cylinder engine started

3
Public Test case-2
Input:10
Output: car with 10 cylinder engine started
10 cylinder engine started

Private Test case-1


Input:4
Output: car with 4 cylinder engine started
4 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) {}
};

class Clerk :public Employee {


public:
Clerk(string fName, double sal) : Employee(fName, sal) {}
};

void congratulate(Employee* emp) {


emp->addBonus(200);
emp->show();
cout << " ";
};
int main() {
Employee* emp;
int sal_m, sal_c;
cin >> sal_c >> sal_m;
Manager m1("Steve", sal_m);
Clerk c1("Kevin", sal_c);

// Call the proper function/s to congratulate the Manager and the Clerk

return 0;
}

Public Test case-1


Input:

5
2000
1000
Output: Kevin 2200 Steve 1200

Public Test case-2


Input:
4000
1000

Output: Kevin 4200 Steve 1200

Private Test case


Input:
6200
2500
Output: Kevin 6400 Steve 2700

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() {};
};

class Foo : public Base {


public:
virtual void DoIt() { cout << "12,"; };
void FooIt() { cout << "13,"; }
};

class Bar : public Base {


public:
virtual void DoIt() { cout << "14,"; }
void BarIt() { cout << "15:"; }
};

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) {

Base* base = CreateRandom(n);


base->DoIt();
Bar* bar = __________________(base);
Foo* foo = __________________(base);
if (bar)
bar->BarIt();
if (foo)
foo->FooIt();
}
return 0;
}

Public Test case-1


Input: 1
Output: 12,13,

7
Public Test case-2
Input: 2
Output: 12,13,14,15:

Public Test case-3


Input: 3
Output: 12,13,14,15:12,13,

Private Test case


Input: 4
Output: 12,13,14,15: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;
}

Public Test case-1


Input:
6
5
Output:
1 2 3 4 5 Caught 5

Public Test case-2


Input:
8
6

9
Output:
1 2 3 4 5 6 Caught 6

Private Test case


Input:
3
3
Output:
1 2 3 Caught 3

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++;

__________delete(___); // Release the memory allocated above

return 0;
}

Public Test case-1


Input: -7
Output: -13

Public Test case-2


Input: 11
Output: 23

private Test case-1


Input: 15
Output: 31

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;

// Array out of Bounds


inline int arraybound(int total, int bounds) {
if (bounds > 40)
____________________("Array Out of Bounds"); // throw exception here

return total * bounds;


}

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

std::cout << e.what() ; // Print the exception


}

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; }
};

/* Rectangle class is derived from classes Area and Perimeter. */


class Rectangle: public Area, public Perimeter { // Inherit the required base classes

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;

Rectangle r(l, b);


r.print();
return 0;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy