Part I: Multiple-Choice / True-False Questions
Part I: Multiple-Choice / True-False Questions
Part I: Multiple-Choice / True-False Questions
Page 1 of 8
Dr R.K. Singla
-c
-g
-pg
-Wall
none of the above
2. A function void hcf(Object& obj) is given. Which of the following statements about
what happens when hcf is called is true?
a.
b.
c.
d.
e.
overload
override
redefine
overshadow
shadow
d = d;
b = d;
d = b;
*pb = d;
all are valid
stack
list
deque
vector
none of the above
forward iterator
bi-directional iterator
random-access iterator
all of the above
none of the above
C++ Programming
Page 2 of 8
Dr R.K. Singla
~MyStr();
friend ostream& operator << (ostream& ostr, const MyStr& mystr)
{ ostr << mystr.data; return ostr; }
private:
char *data;
};
Questions 8-14 refer to the use of the MyStr class given above. The variables s, s1, and s2 are
all objects of class MyStr.
Answer whether the following relational comparisons are valid or not (T is valid, F is invalid):
8.
s == "Test"
9.
"Test" == s
10. s1 > s2
11. s1 >= s2
12. s == 5
C++ Programming
Page 3 of 8
Dr R.K. Singla
Answer: __________________________________________________________________
___1. Destructors need to be declared virtual to avoid memory leak. _______________
___2. Cannot declare an object of class Pet because Pet is an abstract class.________
___3. The pet member variable m_breed cannot be accessed in the derived class.____
_________________________________________________________________________
2. Give a declaration of an array (named arr) of three function pointers, each
pointing to a function that takes one double argument and returns a character pointer.
Answer: ____ char* (*arr[3])(double); __________________________________________
C++ Programming
Page 4 of 8
Dr R.K. Singla
Answer: 16 64_____________________________________________________________
4.
Answer: __The program prints out the size of a file (in bytes), which name is given as
a command line argument.__________________________________________________
5.
Answer: 5 3 1 ________________________________________________________
C++ Programming
Page 5 of 8
Dr R.K. Singla
of a given element within an array. If the element is not found the array size (number of
elements) is instead returned. For example, the following program outputs: 1 4 2 .
#include <iostream>
#include "date.h"
int main()
{
int array_int[]
= { 5, 7, 4, 7 };
double array_double[] = { 4.5, 8.0, 7.5, 2.5 };
Date
array_date[]
= {Date(6,8,2002),Date(3,9,2002),Date(31,4,2002)};
a. Implement the findLocation function such that it works for both int and double data types
(as well as other types). Hint: use templates.
template<class T>
int findLocation(T elem, T arr[], int n)
{
for (int i = 0 ; i < n ; i++ )
{
if ( arr[i] == elem )
{
return i;
}
return n;
}
b. Modify the Date class declaration/definitions shown below such that an array of Date
objects also works with your findLocation function.
class Date {
public:
Date(int dd, int mm, int yyyy) : day(dd), month(mm), year(yyyy) {}
int getDay()
const { return day;
};
int getMonth() const { return month; };
int getYear() const { return year; };
bool operator==(const Date& D) const
{ return (D.day==day) && (D.month==month) && (D.year==year); }
private:
int day, month, year;
};
C++ Programming
Page 6 of 8
Dr R.K. Singla
{ delete [] m_data; }
a. Make Array a template class capable of storing other basic data types (and objects). Write
your modifications directly into the above code (possibly changing the existing code).
b. In a program using the Array template class, how does one declare a char Array of size 5?
Answer: _Array<char> a(5)_________________________________________________
c. Implement the assignment operator for the Array class. Array assignment should be allowed
only if the capacity of the two arrays is the same; otherwise the program exits with an error
message (don't worry about templates here, assume the array elements are of type int ).
Array& Array :: operator=(const Array& A)
{
if ( this != &A ) {
if ( m_capacity == A.m_capacity ) {
// no need to delete m_data, same capacity.
m_capacity = A.m_capacity;
for ( int i=0 ; i < A.m_capacity ; i++ ) {
m_data[i] = A.m_data[i];
}
}
else {
cerr << "Array capacity mismatch in assignment!" << endl;
exit(-1); // exit program
}
}
return *this;
}
C++ Programming
Page 7 of 8
Dr R.K. Singla
delete expr;
The declaration of the base class is provided (not to be modified). Your task is to write the
declarations / definitions of the derived classes.
////////////////// Abstract base class, do not modify ////////////////////
class ExprNode
{
public:
virtual bool eval() const = 0;
virtual ~ExprNode() { }
};
////////////////// VALUE node declaration / definitions //////////////////
class ValueNode : public ExprNode
{
public:
ValueNode(bool value) : m_value(value) { /* empty */ }
virtual bool eval() const { return m_value; }
virtual ~ValueNode() { /* empty */ };
private:
bool m_value;
};
C++ Programming
Page 8 of 8
Dr R.K. Singla