oDesk Programming With C++ Test Answers can help you to get more job in Web designing and web programming.
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);
You can find out more test answers
oDesk Programming With C++ Test Answers.
Which of
the following is a predefined object in C++ and used to insert to the standard
error output?
Answer:
std::cerr
If a
matching catch handler (or ellipsis catch handler) cannot be found for the
current exception, then the following predefined runtime function is called
______.
Answer:
terminate
Which of
the following statements regarding functions are false?
Answer:
Functions can return the type void
What
access specifier allows only the class or a derived class to access a data
member
Answer: protected
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?
class A
{
private:
int x;
float y;
public:
friend class B;
};
class B
{
};
Which of the following is true?
Answer:
B can access all private data members of A
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?
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?
Answer:
foo = new char*[200];
Which of
the following statements are true for operator overloading in C++?
Answer:
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?
typedef char *monthTable[3];
Referring to the code above, which of the following choices creates two monthTable arrays and initializes one of the two?
Answer:
monthTable winter,spring={"March","April","May"};
A pure
virtual function can be declared by _______.
Answer:
equating it to 0
State
whether True or False.
Unary operator overloaded by means of a friend function takes one reference argument.
Unary operator overloaded by means of a friend function takes one reference argument.
Answer: a.
True
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?
template <class T> Run(T process);
Which one of the following is an example of the sample code given above?
Answer:
A template function 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?
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?
Answer:
Person(const Person &p);
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?
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?
Answer:
There will be an error because in the declaration of innerObject the type Inner
must be qualified by Outer
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?
class screen;
Which of the following statements are true about the class declaration above?
Answer:
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)
{
}
};
class someclass
{
int var1, var2;
public:
someclass(int num1, int num2) : var1(num1), var2(num2)
{
}
};
Answer:
Yes, it is valid
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?
Answer: The code will compile without errors
Which of
the following member functions can be used to add an element in an std::vector?
Answer:
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?
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?
Answer:
19
What is the output of the following code segment?
int n = 9;
int *p;
p=&n;
n++;
cout << *p+2 << "," << n;
int *p;
p=&n;
n++;
cout << *p+2 << "," << n;
Answer:
12,10
Base
class members are made accessible to a derived class and inaccessible to rest
of the program by _____.
Answer:
protected 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
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
Answer:
Only 1 and 2 are true
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?
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?
Answer:
Code will Crash or Throw and Exception
What
linkage specifier do you use in order to cause your C++ functions to have C
linkage
Answer:
extern "C"
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
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
Answer:
f
Which of
the following are true about class and struct in C++:
Answer:
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?
#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?
Answer:
The code will compile and run without errors
Which of
the following techniques should you use to handle a destructor that fails?
Answer:
Write the error to a log file
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?
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?
Answer:
Derived Exception
Which of
the following statements about function overloading, is true?
Answer:Overloaded
functions may not be declared as "inline"
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();
}
#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();
}
Answer:
In Base In Base In Derived
What
does ADT stand for?
Answer:
Abstract 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?
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?
Answer:
Unknown Exception Thrown
Consider
the following code:
#define SQ(a) (a*a)
int answer = SQ(2 + 3);
#define SQ(a) (a*a)
int answer = SQ(2 + 3);
What
will be the value of answer after the above code executes?
Answer:
11
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;
}
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;
}
Answer:
In second 1 In first 2
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?
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?
Answer:
None of the above
Which of
the following is NOT a standard sorting algorithm:
Answer:
std::partial_sort
In C++,
the keyword auto can be used for:
Answer:
Declaration of a local variable
Which of
the following STL classes is deprecated (ie should no longer be used)
Answer.
ostrstream
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?
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?
Answer:
Derived Exception
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?
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?
Answer:
2
How
many arguments can be passed to an overloaded binary operator?
Answer: 2
Which of
the following is not a standard STL header?
Answer. <queue>
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;
}
#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;
}
Answer:
The printed output will be 5
Which of the
following statements about constructors and destructors are true?
Answer:
Constructors can take parameters, but destructors cannot
Answer:
It is illegal to define a constructor as virtual
Which of
the following are NOT valid C++ casts
Answer. void_cast
Which of
the following techniques should you use to handle a constructor that fails?
Answer:
Throw an exception from the constructor
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?
#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?
Answer: 7
You can find out more test answers
oDesk Programming With C++ Test Answers.