Assignment - Week5 - C++ - 2nd - Run
Assignment - Week5 - C++ - 2nd - Run
Total Marks : 20
April 3, 2017
Question 1
Look at the code snippet bellow. Find out the sequence in which the function area() associated
with Rectangle & Triangle class will be called. Mark 1
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
int main () {
Rectangle rect;
Triangle trgl;
rect.set_values (6,5);
trgl.set_values (6,5);
rect.area() ;
trgl.area() ;
return 0;
}
a) Area-2, Area-1
1
b) Area-1, Area-1
c) Area-1, Area-2
d) Area-2, Area-2
Answer: c)
Solution: By the order of calling functions
Question 2
Which of the following function will be invoked by d.func(1)? Mark 1
#include <iostream>
using namespace std;
int main() {
Derived d;
d.func(1);
return 0;
}
a) Base::func(int)
b) Derived::func(int)
c) Compilation Error
Question 3
Find out the out put of the following Program.
#include <iostream>
using namespace std;
class Animal {
public:
int legs = 4;
};
2
class Dog : public Animal {
public:
int tail = 1;
};
int main() {
Dog d;
cout << d.legs;
cout << d.tail;
return 0;
}
a) 1 4
d) 4 1
Question 4
Look at the code snippet bellow. Find out, which of the show() function will be called by
calling b->show(). Mark 1
class Base {
public:
void show() { }
};
class Derived :public Base {
public:
void show() { }
};
int main() {
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show();
return 0;
}
Answer: a)
Solution: Early Binding Occurs
3
Question 5
Look at the code snippet bellow. Find out, which of the show() function will be called by
calling b->show() ? Mark 1
class Base {
public:
virtual void show() { cout << "Base class"; }
};
class Derived :public Base {
public:
void show() { cout << "Derived Class"; }
};
int main() {
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show();
return 0;
}
Answer: b)
Solution: Late Binding Occurs as show() is virtual
Question 6
What will be the output of the following Code snippet? Mark 1
class B {
public:
B() { cout << "B "; }
~B() { cout << "~B "; }
};
class C : public B {
public:
C() { cout << "C "; }
~C() { cout << "~C "; }
};
class D : private C {
B data_;
public:
D() { cout << "D " << endl; }
~D() { cout << "~D "; }
};
int main() {
4
{D d; }
return 0;
}
a) B C B C D D C B C B
b) B C B D D B C B
c) B D B C C B D B
d) B C B C C B C B
Question 7
Identify the correct statements? Mark 1
class base {
public:
int x;
protected:
int y;
private:
int z;
};
a) 1, 3 & 5
b) 2, 4 & 6
c) 6, 8 & 9
5
d) 2, 6 & 7
Answer: d)
Solution: By definition of public, private and protected inheritance
Question 8
Which lines of the following program will not compile? Mark 1
d.var_ = 1; // ---14
d.varD_ = 1; // ---15
return 0; // ---17
} // ---18
a) 6, 10, 14, 15
b) 6, 15
c) 4, 14, 16
d) 6, 14, 16
Answer: c)
Solution: Cant access protected data member
Question 9
Consider the following code snippet. Which of the following statement is true, when t.getX()
is called ? Mark 1
class Test {
int x;
public:
Test(int a):x(a){}
6
virtual void show() = 0;
int getX() { /* Function definition */ }
};
int main(void){
Test t(5);
t.getX();
return 0;
}
d) b & c
Answer: b)
Solution: Cant create object of a abstract class
Question 10
What will be the output of the following program? Marks 1
#include<iostream>
using namespace std;
class Shape {
public:
int x, y;
void draw()
{ Shape::draw(); cout << w << " " << h ; }
};
int main() {
Rectangle *r = new Rectangle(1,2);
r-> draw();
return 0;
}
7
a) 0 0 1 2
b) 7 8 1 2
c) 7 8 5 6
d) 0 0 5 6
Answer: b)
Solution: Shape(7, 8) initialize x and y as 7 and 8. Rectangle(1,2) initialize w and h as 1 and
2.
Programming Questions
Question 1
Consider the skeletal code given below and Fill the blank or remove the blank in the header
of the functions to match the output for the given input. Marks 2
#include <iostream>
using namespace std;
class Shape {
protected:
float l;
public:
void getData(){ cin >> l; }
return l*l;
}
};
int main()
{
Shape * b; //Base class pointer
Circle d; //Derived class object
b = &d;
b->getData();
cout << b->calculateArea();
return 0;
}
8
a. Input: 5
Output: 78.5
b. Input: 28
Output: 2461.76
c. Input: 45
Output: 6358.5
Answer: virtual
Solution: Derived class function is called using a base class pointer. virtual function is resolved
late, at runtime.
Question 2
Consider the skeletal code given below.Fill up the blank by following the instructions associated
with each blank and complete the code, So that the output of the test cases would match Marks:
3
#include <iostream>
using namespace std;
class Area {
public:
int calc(int l, int b) { return l*b; }
};
class Perimeter {
public:
int calc(int l, int b) { return 2 * (l + b); }
};
int area_calc() {
/* Calls calc() of class Area and returns it. */
________________
}
int peri_calc() {
/* Calls calc() function of class Perimeter and returns it. */
________________
}
};
int main() {
9
int l, b;
cin >> l >> b;
return 0;
}
10
Public-1
1. Input:
4
6
Output:
24
20
2. Input:
2
3
Output:
6
10
3. Input:
65
34
Output:
2210
198
Answer
public Area, public Perimeter
Question 3
Consider the skeletal code given below. Marks: 3
Fill up the blanks by following the instructions associated with it and complete the code, so
that the output of the test cases should match. Do not change any other part of the code.
#include<iostream>
using namespace std;
class Polygon {
protected: int width, height;
public:
Polygon(int a, int b) : width(a), height(b) {}
11
public:
Rectangle(int a, int b) : Polygon(a, b) {}
int area() { return width*height; }
};
int main() {
int h, w;
cin >> h >> w;
delete ppoly1;
delete ppoly2;
return 0;
}
12
Public 1
Input:
4
5
Output: 20:10:
Public 2
Input:
30
15
Output: 450:225:
Private
Input:
8
6
Output: 48:24:
Answer
virtual int area(void) = 0; or virtual int area(void) { return 0; }
Question 4
Fill the blank by defining the proper constructor where name of the parameter should be nc
Marks 2
#include <iostream>
class Engine {
public:
Engine(int nc) :cylinder(nc){}
void start() {
cout << getCylinder() << " cylinder engine started" ;
};
int getCylinder() {
return cylinder;
}
private:
int cylinder;
};
13
class Car : private Engine
{ // Car has-a Engine
public:
//Define the constructor to run the code. Name of the parameter should be nc
________________________
void start() {
cout << "car with " << Engine::getCylinder() <<
" cylinder engine started" << endl;
int main()
{
int cylin;
cin >> cylin;
Car c(cylin);
c.start();
return 0;
}
a. Input: 8
Output: car with 8 cylinder engine started
8 cylinder engine started
b. Input:10
Output: car with 10 cylinder engine started
10 cylinder engine started
c. Input:4
Output: car with 4 cylinder engine started
4 cylinder engine started
Answer
Car(int nc) : Engine(nc) { }
Engine::start();
14