0% found this document useful (0 votes)
47 views

Assignment Week 7

This document contains a programming assignment with 10 multiple choice questions related to C++ concepts like virtual functions, virtual tables, casting, constructors, etc. It provides sample code snippets and asks students to identify outputs, number of virtual tables created, appropriate cast operators, and fill in code with correct casting. The assignment is worth a total of 20 marks and is due on April 5, 2017 from Partha Pratim Das to students of computer science and engineering at IIT Kharagpur.

Uploaded by

ankit kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Assignment Week 7

This document contains a programming assignment with 10 multiple choice questions related to C++ concepts like virtual functions, virtual tables, casting, constructors, etc. It provides sample code snippets and asks students to identify outputs, number of virtual tables created, appropriate cast operators, and fill in code with correct casting. The assignment is worth a total of 20 marks and is due on April 5, 2017 from Partha Pratim Das to students of computer science and engineering at IIT Kharagpur.

Uploaded by

ankit kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming in C++: Assignment Week 7

Total Marks : 20

Partha Pratim Das


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

April 5, 2017

Question 1
Find out the output of the following program? Marks: 1
#include <iostream>
using namespace std;

int fun(int* ptr){


return (*ptr + 10);
}

void fun(int& ptr){


ptr = 100;
}

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

class Faculty : virtual public Person {


public:
Faculty(int x) :Person(x) {
cout << 3 * x << " ";
}
};

class Student : virtual public Person {


public:
Student(int x) :Person(x) {
cout << 3 * x << " ";
}
};

class TA : public Faculty, public Student {


public:
TA(int x) : Student(x), Faculty(x) {
cout << 5 * x << " ";
}
};

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

class D1: public Base


{
public:
virtual void function1() {};
};

class D2: public Base


{
public:
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

a) Base::function1() & D2::function2()

b) Base::function1() & D1::function2()

c) D1::function1()only

d) D1::function1() & D2::function2()

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;

int n = static_cast<int>(f); // stmt-1

int* pn = static_cast<int*>(pf); // stmt-2

void* pv = static_cast<void*>(pf); // stmt-3

return 0;
}

a) stmt-1, stmt-2

b) stmt-2, stmt-3

c) stmt-1, stmt-3

d) stmt-1, stmt-2, 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;

class Base { public: virtual ~Base() {} };


class Derived : public Base { };

int main() {

4
// Fill the blank by appropriate caste operator
Derived *pd = ___________ <Derived*>(new Base);

cout << (pd ? "Success": "Failure");

return 0;
}

Answer: dynamic cast


Solution: dynamic cast is used to cast a base pointer into a derived pointer. If the base
pointer doesn’t point to an object of the type of the derived, it returns NULL pointer.

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;

struct A { virtual ~A() { } };


struct B : A { };

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

Find out the correct statements. Marks: 1

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

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

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:

Bar* bar = dynamic_cast<Bar*>(base);


Foo* foo = dynamic_cast<Foo*>(base);

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

//------------------------- Template Code (Editable) --------------------


_____________________________

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

operator A() { return A(i); }

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;

A(char *s) : str(s) { }


//-------------------------- Template Code (Editable) ---------------------
// Write the appropriate constructor or conversion operator function

____________________________________

//----------------------- Suffixed Fixed Code -----------------------------


};

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:

operator char *()


{ return (str); }

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

void fun() const {


(_______________(this))->roll = 5;
}

int getRoll() { return roll; }


};

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

You might also like