Assignment Week 7
Assignment Week 7
Total Marks : 20
April 5, 2017
Question 1
Find out the output of the following program? Marks: 1
#include <iostream>
using namespace std;
int main(void){
const int val = 10;
const int *ptr = &val;
int *ptr1 = const_cast <int *>(ptr);
*ptr1 = fun(ptr1);
fun(*ptr1);
cout << *ptr;
return 0;
}
a) 10
b) 100
c) 20
d) Compilation error
Answer: b)
Solution: Function fun() expects a pointer to an int, not a const int. The statement
int *ptr1 = const_cast <int *>(ptr)}
returns a pointer ptr1 that refers to a without the const qualification of val.
1
Question 2
What will be the output of the following program? Marks: 1
#include<iostream>
using namespace std;
class Person {
public:
Person(int x) { cout << 2 * x << " "; }
Person() { cout << 3 << " "; }
};
int main() {
TA ta1(30);
}
a) 90 90 150
b) 150 90 90
c) 3 90 90 150
d) 60 90 90 150
Answer: c)
Solution: constructor and Destructor of Person will also be called two times when object ta1
is constructed and destructed. So object ta1 has two copies of all members of Person, this
causes ambiguities. The solution to this problem is virtual keyword. We make the classes
Faculty and Student as virtual base classes to avoid two copies of Person in TA class.
Question 3
How many virtual table will be set up by the compiler for the following code ? Marks: 1
2
class Base {
public:
virtual void function1() {};
virtual void function2() {};
};
a) 0
b) 1
c) 2
d) 3
Answer: d)
Solution: Because there are 3 classes here, the compiler will set up 3 virtual tables: one for
Base, one for D1, and one for D2.
Question 4
For the above code (Question 3) what will be the virtual function table entry for function1 &
function2 in class D2? Marks: 1
c) D1::function1()only
Answer: a)
Solution: VFT for Class base– Base::fucntion1() & Base::function2()
VFT for Class D1– D1::fucntion1() & Base::function2()
VFT for Class D2– Base::fucntion1() & D2::function2()
Question 5
In the above code(Ref:Question 3), in which classes the compiler automatically add a hidden
pointer to the virtual function table?
a) Base, D1, D2
3
b) Base only
c) D1, D2
d) Base, D1
Answer: a)
Solution: Whenever a class defines a virtual function a hidden member variable is added to
the class which points to an array of pointers to (virtual) functions called the Virtual Function
Table (VFT)
Question 6
Look at the code given below. Identify the statements where correct use of staic cast operator
has been done. MCQ, Mark 1
#include <iostream>
using namespace std;
int main(){
float f = 12.3;
float* pf = &f;
return 0;
}
a) stmt-1, stmt-2
b) stmt-2, stmt-3
c) stmt-1, stmt-3
Answer: c)
Solution: In stmt-2, types pointed to are unrelated.
Question 7
Fill the blank by an appropriate cast operator given that the output of the program is:Failure
SA, Marks: 1
#include <iostream>
using namespace std;
int main() {
4
// Fill the blank by appropriate caste operator
Derived *pd = ___________ <Derived*>(new Base);
return 0;
}
Question 8
Look at the following code snippet. If typeid(*ap).name() and typeid(ar).name() will be called,
then it will refer to which structure/s. Marks: 1
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
B obj;
A* ap = &obj;
A& ar = obj;
return 0;
}
a) struct A struct B
b) struct A struct A
c) struct B struct B
d) struct B struct A
Answer: c)
Solution: If the expression points to a base class type, yet the object is actually of a type
derived from that base class, a type info reference for the derived class is the result.
Question 9
Consider the following code snippet. What will be output of the code below? Marks: 1
class Base {
protected:
int marker;
public:
Base(int m = 4) : marker(m) {}
5
virtual ~Base() {};
virtual void Action() { ++marker; }
};
class Derived : public Base {
public:
void Action() {
static_cast<Base>(*this).Action();
marker *= 2;
cout << marker << endl;
}
};
int main() {
Base *p = new Derived;
p->Action();
return 0;
}
a) 8
b) 10
c) 4
d) 32
Answer: a)
Solution: static cast<Base>(*this).Action() explicitly call a single-argument construc-
tor. Hence, value of marker is becoming 4 again after the
static cast<Base>(*this).Action() call.
Question 10
static cast can be used for
1. Downcast
2. Upcast
3. Implicit conversions
4. User-defined conversions
a) 1 2 3
b) 1 3 4
c) 3 4 2
d) 1 2 3 4
Answer: d)
Solution: By the definition of static cast
6
Programming Questions
Question 1
Fill the bank with appropriate casting Marks: 3
#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 1
Input: 1
Output: 12,13,
Public 2
Input: 2
Output: 12,13,14,15:
Public 3
Input: 3
Output: 12,13,14,15:12,13,
Private
Input: 4
Output: 12,13,14,15:12,13,14,15:
Answer:
Solution: The casts execute at runtime, and work by querying the object (no need to worry
about how for now), asking it if it the type we’re looking for. If it is, dynamic cast<Type*>
returns a pointer; otherwise it returns NULL.
Question 2
Problem statement: Consider the following program. Fill the blank With proper constructor
/ conversion operator for the given casting to match the outputs of test cases. Marks: 2
#include <iostream>
using namespace std;
class A {
int i;
public:
A(int ai) : i(ai) {}
int get() const { return i; }
void update() { ++i; }
};
class B {
int i;
public:
B(int ai) : i(ai) {}
int get() const { return i; }
8
_____________________________
//-------------------------- Suffix Fixed Code --------------------------
void update() { ++i; }
};
int main() {
int i;
cin >> i;
A a(i++);
B b(i);
B &r = static_cast<B>(a);
a.update();
cout << a.get() << ":";
cout << r.get() << ":";
A &s = static_cast<A>(b);
b.update();
cout << b.get() << ":";
cout << s.get() << ":";
return 0;
}
//------------------------------------------------------------------
Public 1
Input:
1
Output: 2:1:3:2:
Public 2
Input:
15
Output: 16:15:17:16:
Private
Input:
5
Output: 6:5:7:6:
Answer:
B(A& a) : i(a.get()) {}
Question 3
Problem Statement: Write the appropriate constructor / operator function in the following
program to cast a class A object to a char * object. Marks: 2
9
//------------------------- Prefixed Fixed Code --------------------------
#include <iostream>
#include <cstring>
using namespace std;
class A {
public: char *str;
____________________________________
int main() {
char input[20];
cin >> input;
A a(input);
// A ==> char *
char *s = static_cast<char*>(a);
strcat(s, "-success");
cout << s;
return 0;
}
//--------------------------------------------------------------------
Public 1
Input: string
Output: string-success
Public 2
Input: what
Output: what-success
Private
Input: My
Output: My-success
Answer:
10
Question 4
Consider the following code snippet and fill the bank with appropriate code. Marks: 3
#include <iostream>
using namespace std;
class student {
private:
int roll;
public:
student(int r) :roll(r) {}
int main(void) {
int old_roll_no = 0;
cin >> old_roll_no;
student s(old_roll_no);
cout << s.getRoll() << " ";
s.fun();
cout << s.getRoll() ;
return 0;
}
Public 1
Input: 3
Output: 3 5
Public 2
Input: 12
Output: 12 5
Private
Input: 1
Output: 1 5
Answer:
(const_cast <student*>(this))->roll = 5;
Solution: const cast can be used to change non-const class members inside a const member
function.
11