Assignment - Week6 - C++ - 2nd - Run - Solution
Assignment - Week6 - C++ - 2nd - Run - Solution
Total Marks : 20
April 6, 2017
Question 1
Look at the code snippet below and find out What will be the correct syntax for up-casting ?
Mark 1
class Parent {
public:
void sleep() {}
};
int main(){
Parent parent;
Child child;
return 0;
}
Answer: c)
Explanation: By definition of upcasting (Base Class to Derive class)
Question 2
What will be the correct syntax for down-casting in above code (QS No-1)?
1
b) Child *pChild = &parent;
Answer: a)
Explanation: By definition of downcasting (Derive class to Base Class)
Question 3
Look at the following code snippet and Find out the static binding
from the option below Mark 1
class Base {
public:
void first() {}
virtual void second() {}
};
class Derived : public Base {
public:
void f() {}
virtual void g() {}
};
int main() {
Base b;
Derived d;
Base *pb = &b;
Base *pd = &d;
Base &rb = b;
Base &rd = d;
pb->first(); // 1
pd->first(); // 2
rb.first(); // 3
rd.first(); // 4
pb->second(); // 5
pd->second(); // 6
rb.second(); // 7
rd.second(); // 8
}
a) 1 2 5 6
b) 3 4 7 8
c) 1 2 3 4
d) 5 6 7 8
Answer: c)
Explanation:Direct function calls can be resolved using a process known as early binding or
static binding.
2
Question 4
Consider the code of question 3 and find out the dynamic binding from the option below
a) 1 2 5 6
b) 3 4 7 8
c) 1 2 3 4
d) 5 6 7 8
Answer: d)
Explanation: Virtual functions get resolve at run time.
Question 5
Consider the following code snippet. Find out the correct sequence of function calling. Marks:
1
class X {
public:
virtual void f() { } //1
void g() { } //2
};
class Y : public X {
public:
void f() { } //3
virtual void g() { }//4
};
int main() {
X x; Y y;
X& rx = y;
x.f();
x.g();
rx.f();
rx.g();
return 0;
}
a) 1 2 3 2
b) 1 2 3 4
c) 3 2 1 2
d) 3 2 1 4
Answer: a)
Explanation: rx.f() call Y::f as f is virtual in the base class.
3
Question 6
What will be the output/Error of the bellow code snippet?
class Base {
public:
virtual void show() = 0;
};
int main()
{
Base obj;
Base *b;
Derived d;
b = &d;
b->show();
}
d) Virtual Function
Answer: a)
Explanation: Cant create of object of a abstract base class
Question 7
What will be the output of the following program? Marks: 1
#include<iostream>
using namespace std;
class base {
public:
base() { cout << "c"; }
virtual ~base() { cout << "~c"; }
};
int main(void)
4
{
{
derived *d = new derived();
base *b = d;
delete b;
}
return 0;
}
a) d c c d
b) d c d c
c) c d c d
d) c d d c
Answer: d)
Explanation: Deleting a derived class object using a pointer to a base class that has a non-
virtual destructor results in undefined
Question 8
Find the abstract classes from the bellow code?
class Instrument {
public:
virtual void play() = 0
{ cout << "Instrument: Init Brush" << endl; }
};
class Wind : public Instrument {
void play() { cout << "Polygon: play" << endl; }
};
class Percussion : public Instrument {
};
class Woodwind : public Wind {
public:
void play() { cout << "Woodwind: play" << endl; }
};
class Brass : public Wind {
public:
void play() { cout << "Brass: play" << endl; }
};
class Drum : public Percussion {
public:
void play() { cout << "Drum: play" << endl; }
};
class Tambourine : public Percussion {
public:
void play() { cout << "Tambourine: play" << endl; }
};
a) Woodwind, Brass
5
b) Instrument, Percussion
c) Instrument, Tambourine
d) Percussion, Drum
Answer: b)
Explanation: pure virtual function play() is there in Instrument and Percussion class
Question 9
What will be the output of the following Code Snippet ? Marks: 1
class Instrument {
public:
virtual void play()
{ cout << "1 "; }
};
class Wind : public Instrument {
void draw() { Instrument::play(); cout << "2 "; }
};
class Percussion : public Instrument {
};
class Woodwind : public Wind {
public:
void play() { Instrument::play(); cout << "3 "; }
};
class Brass : public Wind {
public:
void play() { Instrument::play(); cout << "4 "; }
};
class Drum : public Percussion {
public:
void play() { Instrument::play(); cout << "5 "; }
};
class Tambourine : public Percussion {
public:
void play() { Instrument::play(); cout << "6 "; }
};
int main() {
Instrument *arr[] = { new Woodwind, new Brass, new Drum, new Tambourine };
for (int i = 0; i < sizeof(arr) / sizeof(Instrument *); ++i) arr[i]->play();
return 0;
}
a) 3 4 5 6
b) 1 3 1 4 1 5 1 6
c) 1 3 4 5 6
d) Compile Time Error: Virtual function cant have body
Answer: b)
Explanation:: Virtual function may have body
6
Question 10
What will be the output of the following code snippet? Marks: 1
A a;
B b;
a.i = 8;
b.d = 9.7;
A *p = &a;
B *q = &b;
p = (A*)&b;
q = (B*)&a;
cout << p->i << endl;
cout << q->d << endl;
a) 9.7 8
b) 8 9.7
c) GARBAGE
Answer: c)
Programming Questions
Question 1
Problem Statement: Marks: 2
Consider the following code. Modify the code in editable section to match the public test cases.
#include <iostream>
using namespace std;
class B {
public:
B() { cout << "98 "; } // dont modify the "cout"
};
class D : public B {
7
int n;
public:
D(int p):n(p) { cout << n << " "; }
~D() { cout << n*2 << " "; }
};
int main() {
int n ; cin >> n ;
delete basePtr;
return 0;
}
//---------------------------------------------------------------------
Public-1
Input:
2
Output:
98 2 4 56
Public-2
Input:
8
Output:
98 8 16 56
Private
Input:
3
Output:
98 3 6 56
Answer: virtual
Question 2
Consider the skeletal code below. When the program is executed, the member functions
(excluding the constructors and destructors) are called in the order: A::g B::g B::f A::g
B::g C::g C::f. Fill up the blanks to match the test cases. Marks: 2
#include <iostream>
using namespace std;
8
};
class B : public A { protected: int bi;
public:
B(int i) : A(i), bi(i) {}
_______ void f() // Fill the blank or Remove blank
{ cout << ai << bi; } // DO NOT EDIT THIS LINE
Public 1
Input: 2
Output: 43322
Public 2
Input: 23
Output: 43242323
Private
Input: 4
Output: 43544
Question 3
Consider the following code. write the proper definition of getArea() in the editable section
so that the test cases would pass . Marks: 2
9
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Rect.setWidth(x);
Rect.setHeight(y);
Tri.setWidth(x);
Tri.setHeight(y);
while (*pShape)
cout << (*pShape++)->getArea() << " ";
return 0;
}
10
Public-1
Input:
3
7
Output:
21 10
Public-2
Input:
35
23
Output:
805 402
Private
Input:
6
9
Output:
54 27
Answer:
Question 4
Consider the following code. Fill up the code in editable section to congratulate the Manager
and the Clerk, so that outputs will be matched as given in the test cases. 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) {}
};
11
class Clerk :public Employee {
public:
Clerk(string fName, double sal) : Employee(fName, sal) {}
};
// Call the proper function to congratulate the Manager and the Clerk
return 0;
}
Public-1
Input:
2000
1000
Output:
Kevin 2200 Steve 1200
Public-2
Input:
4000
1000
Output:
Kevin 4200 Steve 1200
Private
Input:
6200
2500
Output:
Kevin 6400 Steve 2700
Answer:
congratulate(&c1);
congratulate(&m1);
12
Question 5
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
// Fill the blank to run the code
__________ void area() {
cout << "Parent class area :" << endl;
}
};
13
shape->area();
return 0;
}
Public-1
Input:
10 5
Output:
50 25
Public-2
Input:
10 25
Output:
250 125
Private
Input:
60 30
Output:
1800 900
Answer:
virtual
shape = &rec;
shape = &tri;
14