Vous êtes sur la page 1sur 16

C++ Test Paper (APT Software Avenues Pvt. Ltd.

)
1. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. class Base
4. {
5. public:
6. virtual void print() const = 0;
7. };
8. class DerivedOne : virtual public Base
9. {
10. public:
11. void print() const
12. {
13. cout << "1";
14. }
15. };
16. class DerivedTwo : virtual public Base
17. {
18. public:
19. void print() const
20. {
21. cout << "2";
22. }
23. };
24. class Multiple : public DerivedOne, DerivedTwo
25. {
26. public:
27. void print() const
28. {
29. DerivedTwo::print();
30. }
31. };
32. int main()
33. {
34. Multiple both;
35. DerivedOne one;
36. DerivedTwo two;
37. Base *array[ 3 ];
38. array[ 0 ] = &both;
39. array[ 1 ] = &one;
40. array[ 2 ] = &two;
41. for ( int i = 0; i < 3; i++ )
42. array[ i ] -> print();
43. return 0;
44. }
a) 121
b) 212
c) 12
d) None of the mentioned
2. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. class sample
4. {
5. public:
6. virtual void example() = 0;
7. };
8. class Ex1:public sample
9. {
10. public:
11. void example()
12. {
13. cout << "ubuntu";
14. }
15. };
16. class Ex2:public sample
17. {
18. public:
19. void example()
20. {
21. cout << " is awesome";
22. }
23. };
24. int main()
25. {
26. sample* arra[2];
27. Ex1 e1;
28. Ex2 e2;
29. arra[0]=&e1;
30. arra[1]=&e2;
31. arra[0]->example();
32. arra[1]->example();
33. }
a) ubuntu
b) is awesome
c) ubuntu is awesome
d) None of the mentioned

3. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. class p
4. {
5. protected:
6. int width, height;
7. public:
8. void set_values (int a, int b)
9. {
10. width = a; height = b;
11. }
12. virtual int area (void) = 0;wa
13. };
14. class r: public p
15. {
16. public:
17. int area (void)
18. {
19. return (width * height);
20. }
21. };
22. class t: public p
23. {
24. public:
25. int area (void)
26. {
27. return (width * height / 2);
28. }
29. };
30. int main ()
31. {
32. r rect;
33. t trgl;
34. p * ppoly1 = &rect;
35. p * ppoly2 = &trgl;
36. ppoly1->set_values (4, 5);
37. ppoly2->set_values (4, 5);
38. cout << ppoly1 -> area() ;
39. cout << ppoly2 -> area();
40. return 0;
41. }
a) 1020
b) 20
c) 10
d) 2010
4. Which operator is used to signify the namespace?
a) conditional operator
b) ternary operator
c) scope operator
d) none of the mentioned

5. Identify the correct statement.
a) Namespace is used to group class, objects and functions.
b) Namespace is used to mark the beginning of the program.
c) Namespace is used to seperate the class, objects.
d) None of the above

6. What is the output of these program?
1. #include <iostream>
2. using namespace std;
3. namespace first
4. {
5. int x = 5;
6. int y = 10;
7. }
8. namespace second
9. {
10. double x = 3.1416;
11. double y = 2.7183;
12. }
13. int main ()
14. {
15. using first::x;
16. using second::y;
17. bool a, b;
18. a = x > y;
19. b = first::y < second::x;
20. cout << a << b;
21. return 0;
22. }
a) 11
b) 01
c) 00
d) 10
7. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. namespace extra
4. {
5. int i;
6. }
7. void i()
8. {
9. using namespace extra;
10. int i;
11. i = 9;
12. cout << i;
13. }
14. int main()
15. {
16. enum letter { i, j};
17. class i { letter j; };
18. ::i();
19. return 0;
20. }
a) 9
b) 10
c) compile time error
d) none of the mentioned

8. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. int func (int a, int b)
4. {
5. cout << a;
6. cout << b;
7. return 0;
8. }
9. int main(void)
10. {
11. int(*ptr)(char, int);
12. ptr = func;
13. func(2, 3);
14. ptr(2, 3);
15. return 0;
16. }
a) 2323
b) 232
c) 23
d) compile time error
9. Which of the following permits function overloading on c++?
a) type
b) number of arguments
c) both of the mentioned
d) none of the mentioned

10. Copy constructors are generally written to accept the parameters by reference? Explain why.

11. Overloaded operators always return references. Explain why.

12. What is the signature of the 'this' pointer in a const member function of a class?

13. Exceptions are always preferred to be thrown by value and not by reference , why?

14. We have written two functions like:
a) void f( X* x);
b) void f(X*& x);
Explain the situations when we might need the two versions and why one version cannot be
a substitute for the other.

15. Why are destructors always declared as virtual?

16. What is the output of the program?
#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
virtual void f( int );
virtual void f( double );
virtual void g( int i = 10 );
};
void Base::f( int )
{
cout << "Base::f(int)" << endl;
}
void Base::f( double )
{
cout << "Base::f(double)" << endl;
}
void Base::g( int i )
{
cout << i << endl;
}
class Derived: public Base
{
public:
void f( complex<double> );
void g( int i = 20 );
};
void Derived::f( complex<double> )
{
cout << "Derived::f(complex)" << endl;
}
void Derived::g( int i )
{
cout << "Derived::g() " << i << endl;
}
void main()
{
Base b;
Derived d;
Base* pb = new Derived;
b.f(1.0);
d.f(1.0);
pb->f(1.0);
b.g();
d.g();
pb->g();
delete pb;
}

17. The following code shows classes that perform their own memory management. Point out as
many memory-related errors as possible, and answer the additional questions.
Consider the following code:
class B
{
public:
virtual ~B();
void operator delete ( void*, size_t ) throw();
void operator delete[]( void*, size_t ) throw();
void f( void*, size_t ) throw();
};
class D : public B
{
public:
void operator delete ( void* ) throw();
void operator delete[]( void* ) throw();
};
Why do B's operators delete have a second parameter, whereas D's do not? Do you see any
way to improve the function declarations?
Continuing with the same piece of code: Which operator delete() is called for each of the
following delete expressions? Why, and with what parameters?
D* pd1 = new D;
delete pd1;
B* pb1 = new D;
delete pb1;
D* pd2 = new D[10];
delete[] pd2;
B* pb2 = new D[10];
delete[] pb2;

Are the following two assignments legal?
typedef void (B::*PMF)(void*, size_t);
PMF p1 = &B::f;
PMF p2 = &B::operator delete;

Are there any memory-related errors or issues in the following code?
class X
{
public:
void* operator new( size_t s, int )
throw( bad_alloc )
{
return ::operator new( s );
}
};
class SharedMemory
{
public:
static void* Allocate( size_t s )
{
return OsSpecificSharedMemAllocation( s );
}
static void Deallocate( void* p, int i = 0 )
{
OsSpecificSharedMemDeallocation( p, i );
}
};
class Y
{
public:
void* operator new( size_t s,
SharedMemory& m ) throw( bad_alloc )
{
return m.Allocate( s );
}
void operator delete( void* p,
SharedMemory& m,
int i ) throw()
{
m.Deallocate( p, i );
}
};
void operator delete( void* p ) throw()
{
SharedMemory::Deallocate( p );
}
void operator delete( void* p,
std::nothrow_t& ) throw()
{
SharedMemory::Deallocate( p );
}

18. What is the difference, if any, between the following? (T stands for any class type.). Explain
clearly.
T t;
T t();
T t(u);
T t = u;

19. You are doing a code review. A programmer has written the following function, which uses
unnecessary temporary objects in at least three places. How many can you identify, and how should
the programmer fix them?
string FindAddr( list<Employee> emps, string name )
{
for( list<Employee>::iterator i = emps.begin();
i != emps.end();
i++ )
{
if( *i == name )
{
return i->addr;
}
}
return "";
}
Do not change the operational semantics of this function, even though they could be improved.

20. Here is the declaration of the Stack template. Your mission: Make Stack exception-safe and
exception-neutral. That is, Stack objects should always be in a correct and consistent state,
regardless of any exceptions that might be thrown in the course of executing Stack's member
functions. If any exceptions are thrown, they should be propagated seamlessly through to the caller,
who can deal with them as he pleases, because he knows the context of T and we don't.
template <class T> class Stack
{
public:
Stack();
~Stack();

/*...*/

private:
T* v_; // ptr to a memory area big
size_t vsize_; // enough for 'vsize_' T's
size_t vused_; // # of T's actually in use
};
Write the Stack default constructor and destructor in a way that is demonstrably exception-safe
(works properly in the presence of exceptions) and exception-neutral (propagates all exceptions to
the caller, without causing integrity problems in a Stack object).

21. You are doing a code review. A programmer has written the following class, which shows some
poor style and has some real errors. How many can you find, and how would you fix them?
class Complex
{
public:
Complex( double real, double imaginary = 0 )
: _real(real), _imaginary(imaginary)
{
}
void operator+ ( Complex other )
{
_real = _real + other._real;
_imaginary = _imaginary + other._imaginary;
}
void operator<<( ostream os )
{
os << "(" << _real << "," << _imaginary << ")";
}
Complex operator++()
{
++_real;
return *this;
}
Complex operator++( int )
{
Complex temp = *this;
++_real;
return temp;
}
private:
double _real, _imaginary;
};


22. What's in a (type) name?" Here's an exercise that demonstrates why and how to use typename,
using an idiom that's common in the standard library.
1. What is typename, and what does it do?
2. What, if anything, is wrong with the code below?

template<typename T>
class X_base
{
public:
typedef T instantiated_type;
};

template<typename A, typename B>
class X : public X_base<B>
{
public:
bool operator()( const instantiated_type& i ) const
{
return i != instantiated_type();
}

23. In the following code, which functions are called? Why?
namespace A
{
struct X;
struct Y;
void f( int );
void g( X );
}

namespace B
{
void f( int i )
{
f( i ); // which f()?
}
void g( A::X x )
{
g( x ); // which g()?
}
void h( A::Y y )
{
h( y ); // which h()?
}
}


24. Analyze the following code fragment.
void f()
{
T t(1);
T& rt = t;
//--- #1: do something with t or rt ---
t.~T();
new (&t) T(2);
//--- #2: do something with t or rt ---
}// t is destroyed again
Is the code in block #2 safe and/or legal? Explain.

25. The new-style casts in standard C++ offer more power and safety than the old-style (C-style)
casts. How well do you know them? The rest of this problem uses the following classes and
global variables:
class A { public: virtual ~A(); /*...*/ };
A::~A() { }

class B : private virtual A { /*...*/ };

class C : public A { /*...*/ };

class D : public B, public C { /*...*/ };

A a1; B b1; C c1; D d1;
const A a2;
const A& ra1 = a1;
const A& ra2 = a2;
char c;
This snippet presents four questions.
1. Which of the following new-style casts are not equivalent to a C-style cast?
const_cast
dynamic_cast
reinterpret_cast
static_cast
2. For each of the following C-style casts, write the equivalent new-style cast. Which are
incorrect if not written as a new-style cast?
void f()
{
A* pa; B* pb; C* pc;
pa = (A*)&ra1;
pa = (A*)&a2;
pb = (B*)&c1;
pc = (C*)&d1;
}
3. Analyze each of the following C++ casts for style and correctness.
void g()
{
unsigned char* puc = static_cast<unsigned char*>(&c);
signed char* psc = static_cast<signed char*>(&c);

void* pv = static_cast<void*>(&b1);
B* pb1 = static_cast<B*>(pv);

B* pb2 = static_cast<B*>(&b1);

A* pa1 = const_cast<A*>(&ra1);
A* pa2 = const_cast<A*>(&ra2);

B* pb3 = dynamic_cast<B*>(&c1);

A* pa3 = dynamic_cast<A*>(&b1);

B* pb4 = static_cast<B*>(&d1);

D* pd = static_cast<D*>(pb4);

pa1 = dynamic_cast<A*>(pb2);
pa1 = dynamic_cast<A*>(pb4);

C* pc1 = dynamic_cast<C*>(pb4);
C& rc1 = dynamic_cast<C&>(*pb2);
}
4. Why is it typically useless to const_cast from non-const to const? Demonstrate a
valid example in which it can be useful to const_cast from non-const to const.

26. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. class X
4. {
5. public:
6. int a;
7. void f(int b)
8. {
9. cout<< b << endl;
10. }
11. };
12. int main()
13. {
14. int X :: *ptiptr = &X :: a;
15. void (X :: * ptfptr) (int) = &X :: f;
16. X xobject;
17. xobject.*ptiptr = 10;
18. cout << xobject.*ptiptr << endl;
19. (xobject.*ptfptr) (20);
20. }
a) 10, 20
b) 20, 10
c) 20
d) 10
27. The "this != &other" test (illustrated below) is a common coding practice intended to prevent self-
assignment. Is the condition necessary and/or sufficient to accomplish this? Why or why not? If not,
how would you fix it?
T& T::operator=( const T& other )
{
if( this != &other ) // the test in question
{
// ...
}
return *this;
}


28. What is the output of this program?
1. #include <iostream>
2. #include <iomanip>
3. using namespace std;
4. void showDate(int m, int d, int y)
5. {
6. cout << setfill('0');
7. cout << setw(2) << m << '/'
8. << setw(2) << d << '/'
9. << setw(4) << y << endl;
10. }
11. int main()
12. {
13. showDate(1, 1, 2013);
14. return 0;
15. }


29. What is the output of this program?
1. #include <iostream>
2. using namespace std;
3. void PrintSequence(int StopNum)
4. {
5. int Num;
6. Num = 1;
7. while (true) {
8. if (Num >= StopNum)
9. throw Num;
10. cout << Num;
11. Num++;
12. }
}
13. int main(void)
14. {
15. try {
16. PrintSequence(20);
17. }
18. catch(int ExNum)
19. {
20. cout << "Caught an exception with value: " << ExNum;
21. }
22. return 0;
23. }
a) compile time error
b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) none of the mentioned

Vous aimerez peut-être aussi