Vous êtes sur la page 1sur 9

Programming in C++: Explanation Assignment Week 4

Total Marks : 20
Right hand side of each question shows its Type (MCQ/MSQ/SA/Programming)

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology
Kharagpur-721302
ppd@cse.iitkgp.ernet.in

September 9, 2016

Question 1
Marks: 1
Identify the incorrect statement(s) about static data members of a class. [MSQ]

a. It needs to be defined to avoid linker error

b. Static data member must be initialized in a source file

c. It is associated with object not with class

d. It can be accessed as a member of any object of the class

Answer: c)
Explanation:
It is associated with the class not with the object. There exist a single copy of a static data
member of a given class which is shared by all the objects of that class.

Question 2
Marks: 1
Objects of the same class share the values of variables but they maintain separate
copies for variables. [MCQ]
Choose the correct pair to fill the blanks.

a. static, non-static

b. non-static, static

c. global, static

d. static, global

Answer: a)
Explanation: It is by definition

1
Question 3
Marks: 1
By using which type of function, a private static data member can be initialized safely,
even before any object has been created? [MCQ]
a. non-static member function

b. static member function

c. constructor function

d. global function
Answer: b)
Explanation: It is by definition

Question 4
Marks: 1
Which of the following are true about a static member function? [MSQ]
a. It can access global functions and data

b. It has this pointer

c. It cannot be declared as const or volatile

d. There may be a non-static function in the same class with the same name
Answer: a), c)
Explanation: Const-ness of a member function is about the const-ness of the object and the
type of this pointer (which now becomes const * const this). Static member functions are
not concerned with the objects they just deal with the class as a whole. Hence, const-ness
has to semantics for static member functions.

Question 5
Marks: 1
Consider the code segment below: [MCQ]
// .......
class A {
static int a;
public:
static void init(int);
// .......
};

int A::a;

void A::init(int i) { a = i; }
// .......

int main() {
int x = 10;

2
A::a = x; // stmt-1

A::a = 10 // stmt-2

A::init(10); // stmt-3

// .......
}
Identify the correct statement(s) inside the main() that may be used to set 10 to A::a.
a. stmt-1
b. stmt-1 or stmt-2
c. stmt-3
d. stmt-2
Answer: c
Explanation:
The static member a is private cannot be accessed from outside.

Question 6
Marks: 1
A friend function violates which feature(s) of OOPs? [MSQ]
a. Data hiding
b. Data abstraction
c. Polymorphism
d. Data encapsulation
Answer: a), d)
Explanation:
friend is not a member of a class, it is an outsider. By using it we can access private data
member of a class.

Question 7
Marks: 1
Identify the correct statements(s) about operator overloading in a class using global func-
tions. [MSQ]
a. It is very difficult to access private data members inside the function
b. It cannot be used for unary operators
c. It does not allow multiple overloads for the same operator
d. It violates encapsulation
Answer: a, d
Explanation: The global function takes an object as argument. To access the private data
member, we have to call a public member function through this object , which makes code
very complected.

3
Question 8
Marks: 1
Why is it necessary to overload some operators in a class by a global function (rather than
a member function)? [MCQ]

a. Because member functions are slower than global functions

b. Because built-in types cannot have member functions

c. Because the left hand operand may not be an object of the class

d. Because the right hand operand may not be an object of the class

Answer: c
Explanation:
For example, consider the class Complex. Construct a complex number c1 as Complex c1(3,4).
If we try to perform 23 + c1 we cannot do it through member function because it require a
call like: 23.operator+ (Complex &c1). This operator+ must be in int type. And no such
operator can exist in int because it is built-in type.

Question 9
Marks: 2

class Node {
private:
int key;
Node *next;
/* Other members of Node Class */

_________ int LinkedList::search();


};

Fill the blanks so that search() method of LinkedList class can access private data of Node
class.

a) static

b) friend

c) blank (Spaces)

d) unsigned

Answer: b)
Explanation: By making it friend, LinkedList class can access private data of Node class.

4
Question 10
Marks: 2
Fill in the gaps below to complete the program:

class IDGenerator {
private:
static int s_nextID;
public:
__________ int getNextID(); // Fill in the keyword
};

___________________ // Fill in the statement

int IDGenerator::getNextID() { return s_nextID++; }

int main() {
int add = 0, count = 0;
cin >> count;
for (int i = 0; i < count; ++i){
add = add + IDGenerator::getNextID();
}
cout << add;
return 0;
}

The inputs and the desired output are given below.

Public set 1

Input: 5

Output: 15

Public set 2

Input: 104

Output: 5460

Private set

Input: 296

Output: 43956

Answer:
static int getNextID();
int IDGenerator::s nextID = 1; (It is the definition of the static data member)

5
Question 11
Marks: 3
Fill in the gaps below to complete the program:

#include <iostream>
using namespace std;

class Box
{
private:
double length, breadth, height;
public:

Box(double a = 0, double b = 0, double c = 0) :


length(a), breadth(b), height(c) {};

double getDimension(){
return length + breadth + height;
}

Box ___________(const Box& x){ // Fill the name of the function


Box box;
___________________ // Fill the statement
___________________ // Fill the statement
___________________ // Fill the statement
return box;
}
};

int main(){
double l = 0, b = 0, h = 0;
cin >> l; cin >> b; cin >> h;
Box Box1(5.00, 6.00, 7.00), Box2(l, b, h), Box3;
Box3 = Box1 + Box2;
double dim = Box3.getDimension();
cout << dim;
return 0;
}

The inputs and the desired output are given below.

Public set

Input: 12.90, 23.26, 3.43

Output: 57.59

Private set

Input: 2.00, 5.00, 3.00

Output: 28.00

6
Answer:

operator+
box.length = this->length + x.length;
box.breadth = this->breadth + x.breadth;
box.height = this->height + x.height;

Explanation: It is an example of operator+ overloading for a user-defined data type Box.

Question 12
Marks: 3
Fill in the gaps below to complete the program:

#include <iostream>
using namespace std;

class Date
{
int da; // 2 digit day
int mo; // 2 digit month
int yr; // 4 digit year
public:
Date(int d, int m, int y): da(d), mo(m), yr(y) { }

friend ________ operator<<(ostream& os, const Date& dt); // Fill the return type
};

________ operator<<(ostream& os, const Date& dt) // Fill the return type
{
_______________________ // Fill the implementation

return os;
}

int main()
{
int day = 0, month = 0, year = 0;

cin >> day;


cin >> month;
cin >> year;

Date dt(day, month, year);

cout << dt;


return 0;
}

The inputs and the desired output are given below.

7
Public set

Input: 10, 25, 2015


Output: 10/25/2015

Private set

Input: 12, 10, 1998


Output: 12/10/1098

Answer:

ostream&
ostream&
os << dt.mo << / << dt.da << / << dt.yr;

Explanation:
It is overloading of output stream. It must return an reference of ostream type. Only then we
shall be able to stream the outputs like:
int a, b, c;
cout << a << b << c;
Putting parentheses, we get:
(((cout << a)) << b) << c);
The value of every parenthesized expression is cout (of course, with the respective value
streamed already) so that the next variable can be streamed to it.

Question 13
Marks: 2
Fill in the gaps below to complete the program:

#include <iostream>
using namespace std;

namespace first
{
int x = 17;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 19.4635;
}

int main() {
______ ______::x; // Fill the gap
______ ______::y; // Fill the gap

8
cin >> x;
cin >> y;

cout << x << \n;


cout << y << \n;

cout << first::y << \n;


cout << second::x << \n;
return 0;
}

The inputs and the desired output are given below.

Public set

Input: 5 2.7183

Output: 5 2.7183 10 3.1416

Private set

Input: 66 4.21

Output: 66 4.21 10 3.1416

Answer: using first // using second


Explanation:
The statement using first::x, qualify the x which belong to namespace first in the scope of
the main(). Similar the statement using second::y makes y available in main. So cin >> x
refers the first::x and cin >> y refers second::y;

Vous aimerez peut-être aussi