odesk c++ programming test




Odesk c++ programming test answers :

Consider the following code:
#include
using namespace std;

int main()
{
cout << "The value of __LINE__ is " <<__line__;>
What will be the result when the above code is compiled and executed?
Correct answer is:
c. The code will compile and run without errors

-----------

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

Correct answer is:
protected access specifier

----------------

Which of the following is not a standard STL header?

Correct answer is:
a.

---------------

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

Correct answer is:
In a class all members are private by default, whereas in struct all members are public by default

------------

Consider the following code:
#include
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?

Correct answer is:
Constructor of A Constructor of B Destructor of B Destructor of A

-----------

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:
Correct answer is:
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;

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)
{
}
};
Correct answer is:
a. Yes, it is valid

-------------

Which of the following STL classes is deprecated (ie should no longer be used)
ostrstream
ostringstream
ostream
wostream
Correct answer is:
ostrstream

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
Correct answer is:
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
e. vector::size returns the number of elements in the vector

---------

Consider the following code:
class BaseException
{
public:
virtual void Output()
{
cout << "Base Exception" <<>
Invoking Exception Test will result in which output?
Correct answer is:
c. Unknown Exception Thrown

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();
}
Correct answer is:
b. Base Derived

---------

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 <<>
Correct answer is:
b. In second 1 In first 2

------------

Consider the following class hierarchy:
class Base
{
}

class Derived : private Base
{
}
Which of the following are true?
Correct answer is:
d. Derived can access public and protected member functions of Base

---------------

Consider the sample code given below and answer the question that follows.
class A
{
public:
A() {}
~A()
{
cout << "in destructor" <<>
How many times will "in destructor" be output when the above code is compiled and executed?
Correct answer is:

d. A compile time error will be generated because destructors cannot be called directly

--------------

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?
Correct answer is:
e. The code will compile without errors

---------------------


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
Correct answer is:
b. equating it to 0

-------------------


Consider the sample code given below and answer the question that follows:
char **foo;
/* Missing code goes here */
for(int i = 0; i <>
Referring to the sample code above, what is the missing line of code?
Correct answer is:
d. foo = new char*[200];

-------------------

Consider the following code:
class BaseException
{
public:
virtual void Output()
{ cout << "Base Exception" <<>
Invoking Exception Test will result in which output?
Correct answer is:
b. Derived Exception

--------------------

Which of the following is a function that returns a non zero value to indicate an I/O stream error?
a. bad

b. good

c. fail

d. eof

e. err

f. error

g. filerror

h. None of the above
Correct answer is:

h. None of the above

---------------------

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?
Correct answer is:
e. None of the above
Consider the code given below and answer to the question.
#include
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();
}
Correct answer is:
In Base In Base In Derived

---------------


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
Correct answer is:
c. Constructors can take parameters, but destructors cannot
e. It is illegal to define a constructor as virtual

---------------


Consider the following class hierarchy:
class Base
{
}

class Derived : public Base
{
}
Which of the following are true?
Correct answer is:
d. Derived can access public and protected member functions of Base
e. The following line of code is valid: Base *object = new Derived();

--------------

Consider the sample code given below and answer the question that follows.
template Run;
Which one of the following is an example of the sample code given above?
Correct answer is:
c. A template function declaration

-------------

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

Correct answer is:
a. c
b. f

------------------

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
Correct answer is:
c. Only 1 and 2 are true

Which of the following operators cannot be overloaded?
a. +=

b. >>

c. <>
Correct answer is:
d. .
e. ::
h. ?:

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?
Correct answer is:
d. Person(const Person &p);

State which of the following is true.
a. Function templates in C++ are used to create a set of functions that apply the same algorithm to different data types

b. Classes in C++ are used to develop a set of type-safe classes

c. C++ is useful for developing collection classes

d. C++ is useful for developing smart pointers

e. All of the above
Correct answer is:
e. All of the above
What is true about Function?
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
Correct answer is:
d. You can create arrays of functions

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
Correct answer is:
b. Throw an exception from the constructor

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
Correct answer is:
b. protected

Consider the following code:
#include

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?
Correct answer is:
d. 7

Which of the following are NOT valid C++ casts?
a. dynamic_cast

b. reinterpret_cast

c. static_cast

d. const_cast

e. void_cast
Correct answer is:
e. void_cast
Consider the code given below and refer to the question.
class BaseException
{
public:
virtual void Output()
{
cout << "Base Exception" <<>
Invoking Exception Test will result in which output?
Correct answer is:
a. Base Exception

Which of the following is NOT a standard sorting algorithm:
a. std::sort

b. std::qsort

c. std::stable_sort

d. std::partial_sort
Correct answer is:
b. std::qsort

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

b. 3

c. 2

d. 1

e. 0
Correct answer is:
d. 1

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
Correct answer is:
d. std::cerr

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 no perform type checking
e. Inline functions should be preferred over macros because inline functions perform type checking
Correct answer is:
b. Macro usage should be avoided because they are error prone
d. Macro usage should be avoided because macros do no perform type checking
e. Inline functions should be preferred over macros because inline functions perform type checking

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?
Correct answer is:
d. There will be an error because in the declaration of innerObject the type Inner must be qualified by Outer

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:" << style="font-weight: bold;"> Which of the following lines from the sample code above are examples of data member definition?

Correct answer is:
e. 19

What is the output of the following code segment?
int n = 9;
int *p;
p=&n;
n++;
cout << *p+2 << "," <<>
Correct answer is:
c. 12,10

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
Correct answer is:
a. A derived class can call the destructor of the parent class explicitly

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
Correct answer is:
c. terminate

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?
Correct answer is:
e. monthTable winter,spring={"March","April","May"};

What happened when the following code is compiled and executed?
#include
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<

return 0;
}
Correct answer is:
b. The printed output will be 5

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
Correct answer is:
a. add

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
Correct answer is:
c. Write the error to a log file

Which of the following are NOT valid C++ casts?
a. dynamic_cast

b. reinterpret_cast

c. static_cast

d. const_cast

e. void_cast
Correct answer is:
e. void_cast

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
Correct answer is:
c. Declaration of a local variable

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
Correct answer is:
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

Unary operator, overloaded by means of a friend function take one reference argument.
Correct answer is:
a. True

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
Correct answer is:
a. fstream