oDesk Programming with C++ Test


Which of the following is a predefined object in C++ and used to insert to the standard error output?

a. std::err
b. std::error
c. std::cerror
d. std::cerr
e. std::cin
f. std::clog

If a matching catch handler (or ellipsis catch handler) cannot be found for the current exception, then the following predefined runtime function is called ______.

a. abort
b. set_terminate
c. terminate
d. close

Which of the following statements regarding functions are false?

a. Functions can be overloaded
b. Functions can return the type void
c. Inline functions are expanded during compile time to avoid invocation overhead
d. You can create arrays of functions
e. You can pass values to functions by reference arguments
f. You can return values from functions by reference arguments
g. A function can return a pointer

What access specifier allows only the class or a derived class to access a data member

a. private
b. protected
c. default
d. virtual
e. public

Consider two classes A and B:

class A
{
private:
    int x;
    float y;
public:
friend class B;
};
class B
{
};

Which of the following is true?

a. A can access all private data members of B
b. B can access all private data members of A
c. A cannot access the private members of B
d. B cannot access the private members of A
e. Both A and B can access each other's private data members

Consider the sample code given below and answer the question that follows:

char **foo; 
/* Missing code goes here */ 
for(int i = 0; i < 200; i++) 
{ 
foo[i] = new char[100]; 
} 

Referring to the sample code above, what is the missing line of code?

a. foo = new *char[200];
b. foo = new char[200];
c. foo = new char[200]*;
d. foo = new char*[200];
e. foo = new char[][200];

Consider the sample code given below and answer the question that follows.

class Grandpa 
{ 
} ; 
class Ma : virtual public Grandpa 
{ 
} ; 
class Pa : virtual public Grandpa 
{ 
} ; 
class Me : public Ma, public Pa, virtual public Grandpa 
{ 
} ; 

How many instances of Grandpa will each instance of Me contain?

a. 1
b. 2
c. 3
d. 4

Which of the following statements are true for operator overloading in C++?

a. The * operator can be overloaded to perform division
b. The * operator can be overloaded to perform assignment
c. ** can be overloaded to perform "to the power of"
d. Operators can be overloaded only in inside classes
e. Operators can be overloaded globally

Sample Code

typedef char *monthTable[3];

Referring to the code above, which of the following choices creates two monthTable arrays and initializes one of the two?

a. monthTable(winter,spring={"March","April","May"});
b. monthTable winter, spring;
c. monthTable, winter, spring;
d. monthTable, winter,spring={"March","April","May"};
e. monthTable winter,spring={"March","April","May"};

A pure virtual function can be declared by _______.

a. equating it to 1
b. equating it to 0
c. equating it to NULL
d. the 'pure' keyword
e. the 'abstract' keyword

State whether True or False.

Unary operator overloaded by means of a friend function takes one reference argument.

a. True
b. False

Consider the sample code given below and answer the question that follows.

template <class T> Run(T process);

Which one of the following is an example of the sample code given above?

a. A non-template member function
b. A template function definition
c. A template function declaration
d. A template class definition
e. A template class declaration

Consider the sample code given below and answer the question that follows.

class Person 
{
    string name;
    int age;
    Person *spouse;
public:
    Person(string sName);
    Person(string sName, int nAge);
    Person(const Person& p);
    Copy(Person *p);
    Copy(const Person &p);
    SetSpouse(Person *s);
};

Which one of the following are declarations for a copy constructor?

a. Person(string sName);
b. Person(string sName, int nAge);
c. Copy(Person *p);
d. Person(const Person &p);
e. Copy(const Person &p)?

Suppose MyClass is a class that defines a copy constructor and overloads the assignment operator. In which of the following cases will the copy constructor of MyClass be called?

a. When an object of MyClass is passed by value to a function
b. When an object of MyClass is returned by value from a function
c. MyClass object1;   MyClass object2;   object2 = object1;
d. MyClass object1;   MyClass *object2  = new MyClass(object1);
e. MyClass object1;   MyClass object2 = object1;

Consider the sample code given below and answer the question that follows.

class Outer
{
  public:
    class Inner
      {
        int Count;
        public:
        Inner(){};
      };
};
int main()
  {
Inner innerObject;
Outer outObject;
return 0;
  } 

What will be the result when the above code is compiled?

a. The code will compile fine
b. There will be errors because classes cannot be defined inside other classes
c. There will be an error because Outer does not define a constructor
d. There will be an error because in the declaration of innerObject the type Inner must be qualified by Outer
e. There will be no errors but a warning that Inner and Outer do not have destructors

Consider the line of code given below and answer the question that follows.

class screen; 

Which of the following statements are true about the class declaration above?

a. Incorrect syntax. The body of the class declaration is missing
b. Incorrect syntax. {}; is missing
c. The syntax is correct
d. Incorrect syntax. {} is missing
e. Incorrect syntax. Requires a *

In the given sample Code, is the constructor definition valid?

class someclass
{
   int var1, var2;
   public:
      someclass(int num1, int num2) : var1(num1), var2(num2)
      {
      }
};

a. Yes, it is valid
b. No, we cannot assign values like this
c. No, the parenthesis cannot be empty
d. No, var1 and var2 are not functions but are variables

Consider the sample code given below and answer the question that follows.

class Person 
{
public:
   Person();
      virtual ~Person();
};
class Student : public Person
{
public:
   Student();
   ~Student();
};
main()
{
   Person *p = new Student();
   delete p;
}

Why is the keyword "virtual" added before the Person destructor?

a. To make it impossible for this particular destructor to be overloaded
b. To ensure that correct destructor is called when p is deleted
c. To ensure that the destructors are called in proper order
d. To improve the speed of class Person's destruction
e. To prevent the Person class from being instantiated directly making it an abstract base class

Consider the sample code given below and answer the question that follows.

class SomeClass
{
int x;
public:
SomeClass (int xx) : x(xx) {}
};
SomeClass x(10);
SomeClass y(x);

What is wrong with the sample code above?

a. SomeClass y(x); will generate an error because SomeClass has no copy constructor
b. SomeClass y(x); will generate an error because SomeClass has no default constructor
c. SomeClass y(x); will generate an error because SomeClass has no public copy constructor
d. x(xx) will generate an error because it is illegal to initialize an int with that syntax
e. The code will compile without errors

Which of the following member functions can be used to add an element in an std::vector?

a. add
b. front
c. push
d. push_back

Consider the sample code given below and answer the question that follows.

1 class Car
2 {
3 private:
4 int Wheels;
5 
6 public:
7 Car(int wheels = 0)
8 : Wheels(wheels)
9 {
10 }
11
12 int GetWheels()
13 {
14 return Wheels;
15 }
16 };
17 main()
18 {
19 Car c(4);
20 cout << "No of wheels:" << c.GetWheels();
21 }

Which of the following lines from the sample code above are examples of data member definition?

a. 4
b. 7
c. 8
d. 14
e. 19

What is the output of the following code segment?


int n = 9;
int *p;
p=&n;
n++;
cout << *p+2 << "," << n;

a. 11,9
b. 9,10
c. 12,10
d. 11,10

Base class members are made accessible to a derived class and inaccessible to rest of the program by _____.

a. public access specifier
b. private access specifier
c. protected access specifier
d. friend access specifier

Consider the following statements relating to static member functions and choose the appropriate options:

1. They have external linkage
2. They do not have 'this' pointers
3. They can be declared as virtual
4. They can have the same name as a non-static function that has the same argument types

a. All are true
b. Only 1, 2 and 4 are true
c. Only 1 and 2 are true
d. Only 1,3 and 4 are true
e. All are false

Consider the following code:

template<class T> void Kill(T *& objPtr)
{
   delete objPtr;
   objPtr = NULL;
}
class MyClass
{
};
void Test()
{
   MyClass *ptr = new MyClass();
   Kill(ptr);
   Kill(ptr);
} 

Invoking Test() will cause which of the following?

a. Code will Crash or Throw and Exception
b. Code will Execute, but there will be a memory leak
c. Code will execute properly
d. Code will exhibit undefined behavior

What linkage specifier do you use in order to cause your C++ functions to have C linkage

a. extern "C"
b. extern C
c. _stdcall
d. _cdecl
e. _fastcall?

Consider the sample code given below and answer the question that follows.

class X { 
  int   i; 
protected: 
  float f; 
public: 
  char  c; 
}; 
class Y : private X { };

Referring to the sample code above, which of the following data members of X are accessible from class Y   

a. c
b. f
c. i
d. None of the above

Consider the following code:

        class A {
              typedef int I;      // private member
              I f();
              friend I g(I);
              static I x;
          };

Which of the following are valid:

a. A::I A::f() { return 0; }
b. A::I g(A::I p = A::x);
c. A::I g(A::I p) { return 0; }
d. A::I A::x = 0;

Which of the following are true about class and struct in C++:

a. A class can have destructor but a struct cannot
b. A class can have inheritance but a struct cannot
c. In a class all members are public by default, whereas in struct all members are private by default
d. In a class all members are private by default, whereas in struct all members are public by default

Consider the following code:

#include<iostream>
using namespace std;
int main()
{
cout << "The value of __LINE__ is " <<__LINE__;
return 0;
}

What will be the result when the above code is compiled and executed?

a. The compilation will fail with the error - '__LINE__' : undeclared identifier
b. The compilation will fail with the error - '__LINE__' unresolved identifier
c. The code will compile and run without errors
d. The code will crash at runtime

Which of the following techniques should you use to handle a destructor that fails?

a. Return an error code from the destructor
b. Throw an exception from the destructor
c. Write the error to a log file
d. Use "delete this;" in the destructor
e. None of the above

Consider the following code:

class BaseException
{
public:
   virtual void Output()
   {
      cout << "Base Exception" << endl;
   }
};
class DerivedException : public BaseException
{
public:
   virtual void Output()
   {
      cout << "Derived Exception" << endl;
   }
};

void ExceptionTest()
 
{
   try
   {
      throw DerivedException();
   }
   catch (BaseException& ex)
   {
      ex.Output();
   }
   catch (...)
   {
      cout << "Unknown Exception Thrown!" << endl;
   }
}

Invoking Exception Test will result in which output?

a. Base Exception
b. Derived Exception
c. Unknown Exception Thrown
d. No Output will be generated

Which of the following statements about function overloading, is true?

a. C++ and namespaces should be used to replace occurrences of function overloading
b. Overloaded functions may not be declared as "inline"
c. Although the return types and parameter types of overloaded functions can be different, the actual number of parameters cannot change
d. Function overloading is possible in both C and C++
e. The parameter lists and const keyword are used to distinguish functions of the same name declared in the same scope

What will be the output of the following code?

#include<iostream>
using namespace std;
class b
{
int i;
public:
void vfoo()
{ cout <<"In Base "; }
};
class d : public b
{
int j;
public:
void vfoo()
{
cout<<"In Derived ";
}
};
void main()
{
b *p, ob;
d ob2;
p = &ob;
p->vfoo();
p = &ob2;
p->vfoo();
ob2.vfoo();
}

a. In Base In Base In Derived
b. In Base In Derived In Derived
c. In Derived In Derived In Derived
d. In Derived In Base In Derived
e. In Base In Base In Base

What does ADT stand for?

a. Accessible derived type
b. Access to derived type
c. Abstract data type
d. Abstract derived type
e. Accessible data type

Consider the following code:

class BaseException
{
public:
    virtual void Output()
    {
    cout << "Base Exception" << endl;
    }
};
class DerivedException : public BaseException
{
public:
    virtual void Output()
    {
    cout << "Derived Exception" << endl;
    }
};
void ExceptionTest()
{
    try
    {
          throw DerivedException();
    }
    catch (BaseException ex)
    {
          ex.Output();
    }
    catch (...)
    {
          cout << "Unknown Exception Thrown!" << endl;
    }
}

Invoking Exception Test will result in which output?

a. Base Exception
b. Derived Exception
c. Unknown Exception Thrown
d. No Output will be generated

Consider the following code:

#define SQ(a) (a*a)
int  answer = SQ(2 + 3);

What will be the value of answer after the above code executes?

a. 10
b. 11
c. 25
d. 13
e. None of the above

What will be the output of the following code?

class A
{
public:
      A():pData(0){}
      ~A(){}
      int operator ++()
      {
            pData++;
            cout << "In first ";
            return pData;
      }
      int operator ++(int)
      {
            pData++;
            cout << "In second ";
            return pData;
      }
private:
      int pData;
};
void main()
{
     A a;
     cout << a++;
     cout << ++a;
}

a. In first 1 In second 2
b. In second 1 In first 2
c. In first 0 In second 2
d. In second 0 In first 2

You want the data member of a class to be accessed only by itself and by the class derived from it. Which access specifier will you give to the data member?

a. Public
b. Private
c. Protected
d. Friend
e. Either Public or Friend

Consider the sample code given below and answer the question that follows.

class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle: public Shape
{
public:
void draw()
{
// Code to draw rectangle
}
//Some more member functions.....
};
class Circle : public Shape
{
public:
void draw()
{
// Code to draw circle
}
//Some more member functions.....
};
int main()
{
Shape objShape;
objShape.draw();
}

What happens if the above program is compiled and executed?

a. Object objShape of Shape class will be created
b. A compile time error will be generated because you cannot declare Shape objects
c. A compile time error will be generated because you cannot call draw function of class 'Shape'
d. A compile time error will be generated because the derived class's draw() function cannot override the base class draw() function
e. None of the above

Which of the following is NOT a standard sorting algorithm:

a. std::sort
b. std::qsort
c. std::stable_sort
d. std::partial_sort

Which of the following statements are true?

a. Inline functions should be preferred over macros because inline functions have better performance
b. Macro usage should be avoided because they are error prone
c. Normal functions should be preferred over macros because normal functions have better performance
d. Macro usage should be avoided because macros do not perform type checking
e. Inline functions should be preferred over macros because inline functions perform type checking

In C++, the keyword auto can be used for:

a. Automatic assignment of data to objects during instantiation
b. Automatic call of a function
c. Declaration of a local variable
d. Automatically erasing an object when it is no longer needed
e. Automatic handling of run-time errors in the program
f. Automatic termination of a program in case the user does not respond within a given time period
g. Automatic creation of variables

Which of the following STL classes is deprecated (ie should no longer be used)

a. ostrstream
b. ostringstream
c. ostream
d. wostream

What will be the output of the following code?


class b
{
    int i;
    public:
    virtual void vfoo()
  {
    cout <<"Base ";
  }
};
class d1 : public b
{
    int j;
    public:
    void vfoo()
  {
    j++;
    cout <<"Derived";
  }
};
class d2 : public d1
{
    int k;
};
void main()
{
    b *p, ob;
    d2 ob2;
    p = &ob;
    p->vfoo();
    p = &ob2;
    p->vfoo();
}

a. Base Base
b. Base Derived
c. Derived Base
d. Derived Derived

Consider the following code:

class BaseException
{
   public:
   virtual void Output()
   {
      cout << "Base Exception" << endl;
   }
};
class DerivedException : public BaseException
{
   public:
   virtual void Output()
   {
      cout << "Derived Exception" << endl;
   }
};
void ExceptionTest()
{
   try
   {
      throw new DerivedException();
   }
   catch (DerivedException ex)
   {
      ex.Output();
   }
   catch (...)
   {
      cout << "Unknown Exception Thrown!" << endl;
   }
}

Invoking Exception Test will result in which output?

a. Base Exception
b. Derived Exception
c. Unknown Exception Thrown
d. No Output will be generated

Consider the sample code given below and answer the question that follows.

class A
{
public:
A() {}
~A()
{
cout << "in destructor" << endl;
}
};
void main()
{
A a;
a.~A();
}

How many times will "in destructor" be output when the above code is compiled and executed?

a. 0
b. 1
c. 2
d. A compile time error will be generated because destructors cannot be called directly

If input and output operations have to be performed on a file, an object of the __________ class should be created.

a. fstream
b. iostream
c. ostream
d. istream
e. None

Which of the following sets of functions do not qualify as overloaded functions?

a. void fun(int, char *) 
    void fun(char *,int)
b. void x(int,char) 
    int *x(int,char)
c. int get(int) 
    int get(int,int)
d. void F(int *) 
    void F(float *)
e. All of the above are overloaded functions

Which of the following statements are FALSE with regard to destructors

a. A derived class can call the destructor of the parent class explicitly
b. A class may have only one destructor
c. Destructors cannot be invoked directly
d. The return type for a destructor is void
e. Destructors cannot accept arguments

Consider the following code:

#include<iostream>
using namespace std;
class A
{
public :
        A()
        {
                cout << "Constructor of A\n";
        };
        ~A()
        {
                cout << "Destructor of A\n";
        };
};
class B : public A
{
public :
        B()
        {
                cout << "Constructor of B\n";
        };
        ~B()
        {
                cout << "Destructor of B\n";
        };
};

int main()
 
{
        B        *pB;
        pB = new B();
        delete pB;
        return 0;
}

What will be the printed output?

a. Constructor of B
    Constructor of A
    Destructor of A
    Destructor of B
b. Constructor of A
    Constructor of B
    Destructor of B
    Destructor of A
c. Constructor of B
    Constructor of A
    Destructor of B
    Destructor of A
d. Constructor of A
    Constructor of B
    Destructor of A
    Destructor of B
e. The sequence of construction and destruction of A and B will be compiler specific

Consider the sample code given below and answer the question that follows.

class Person 
{
    string name;
    int age;
    Person *spouse;
public:
    Person(string sName);
    Person(string sName, int nAge);
    Person(const Person& p);
    Copy(Person *p);
    Copy(const Person &p);
    SetSpouse(Person *s);
};

Which one of the following are declarations for a copy constructor?

a. Person(string sName);
b. Person(string sName, int nAge);
c. Copy(Person *p);
d. Person(const Person &p);
e. Copy(const Person &p)?

Which of the following statements are true about C++ vector class?

a. vector::empty deletes all elements of the vector
b. vector::erase can be used to delete a single element and a range of elements of the vector
c. After calling, vector::erase causes some of the iterators referencing the vector to become invalid
d. vector::count returns the number of elements in the vector
e. vector::size returns the number of elements in the vector
f. vector::capacity returns the number of elements in the vector

How many arguments can be passed to an overloaded binary operator?

a. 4
b. 3
c. 2
d. 1
e. 0

Which of the following is not a standard STL header?

a. <array>
b. <deque>
c. <queue>
d. <list>

What will happen when the following code is compiled and executed?

#include<iostream>
using namespace std;
class myclass
{
private:
    int number;
public:
    myclass()
    {
        number = 2;
    }
    int &a()
    {
        return number;
    }
};

int main()
 
{
    myclass m1,m2;
    m1.a() = 5; 
    m2.a() = m1.a();
    cout << m2.a();
    return 0;
}

a. Compile time errors will be generated because right hand side of expressions cannot be functions
b. The printed output will be 5
c. The printed output will be 2
d. The printed output will be undefined

Which of the following statements about constructors and destructors are true?

a. In a given class, constructors are always required to be defined, but destructors are not
b. Neither constructors nor destructors can take parameters
c. Constructors can take parameters, but destructors cannot
d. It is illegal to define a destructor as virtual
e. It is illegal to define a constructor as virtual
f. Both explicitly declared constructors and explicitly declared destructors are required in a class

Which of the following are NOT valid C++ casts

a. dynamic_cast
b. reinterpret_cast
c. static_cast
d. const_cast
e. void_cast

Suppose MyClass is a class that defines a copy constructor and overloads the assignment operator. In which of the following cases will the copy constructor of MyClass be called?

a. When an object of MyClass is passed by value to a function
b. When an object of MyClass is returned by value from a function
c. MyClass object1;
    MyClass object2;
    object2 = object1;
d. MyClass object1;
    MyClass *object2  = new MyClass(object1);
e. MyClass object1;
    MyClass object2 = object1;

Consider the following code:

class Animal
{
private:
    int weight;
public:
    Animal()
    {
    }
    virtual void Speak()
    {
        cout << "Animal speaking";
    }
};
class Snake : public Animal
{
private:
    int length;
public:
    Snake()
    {
    }
    void Speak()
    {
        cout << "Snake speaking\r\n";
    }
};
int main()
{
    Animal *array = new Snake[10];
    for (int index= 0; index < 10; index++)
    {
        array->Speak();
        array++;
    }
     return 0;
}

What happens when the above code is compiled and executed?

a. The code will generate compilation errors
b. The code will compile and run fine. "Animal speaking" will be printed to the output
c. The code will compile and run fine. "Snake speaking" will be printed to the output
d. The code will crash at runtime

Which of the following are true about class member functions and constructors?

a. A constructor can return values but a member function cannot
b. A member function can declare local variables but a constructor cannot
c. A member function can return values but a constructor cannot
d. A constructor can declare local variables but a member function cannot
e. A member function can throw exceptions but a constructor cannot

Which of the following techniques should you use to handle a constructor that fails?

a. Return an error code from the constructor
b. Throw an exception from the constructor
c. Write the error to a log file
d. Use "delete this;" in the constructor
e. None of the above

Consider the following code:

#include<iostream>
using namespace std;
class A
{
public:
  A()
  {
    cout << "Constructor of A\n";
  };
  ~A()
  {
    cout << "Destructor of A\n";
  };
};
class B
{
public:
  B()
  {
    cout << "Constructor of B\n";
  };
  ~B()
  {
    cout << "Destructor of B\n";
  };
};
class C
{
public:
  A objA;
  B objB;
};
int main()
{
  C *pC;
  pC = new C();
  delete pC;
  return 0;
}

What will be the printed output?

a. Constructor of B
    Constructor of A
    Destructor of A
    Destructor of B
b. Constructor of A
    Constructor of B
    Destructor of B 
    Destructor of A
c. Constructor of B
    Constructor of A
    Destructor of B
    Destructor of A
d. Constructor of A
    Constructor of B
    Destructor of A 
    Destructor of B
e. The sequence of construction and destruction of A and B will be compiler specific

Consider the following code:

#include<stdio.h>
int main(int argc, char* argv[])
{
        enum Colors 
        {
                red,
                blue,
                white = 5,
                yellow,
                green,
                pink
        };
        Colors color = green;
        printf("%d", color);
        return 0;
}

What will be the output when the above code is compiled and executed?

a. 4
b. 5
c. 6
d. 7
e. 8
f. 9
g. The code will have compile time errors

Consider the following class hierarchy:

class Base
{
}
class Derived : private Base
{
}

Which of the following are true?

a. The relation between Base and Derived can be described as: Base is a Derived
b. The relation between Base and Derived can be described as: Base has a Derived
c. Derived can access private member functions of Base
d. Derived can access public and protected member functions of Base

Consider the following class hierarchy:

class Base
{
}

class Derived : public Base
 
{
}

Which of the following are true?

a. The relationship between the Base and Derived can be described as: Base is a Derived
b. The relationship between the Base and Derived can be described as: Base has a Derived
c. Derived can access only public member functions of Base
d. Derived can access public and protected member functions of Base
e. The following line of code is valid:

Which of the following operators cannot be overloaded?

a. +=
b. >>
c. <
d. .
e. ::
f. &&
g. =
h. ?: