Upwork Test Answer C++ Skill Test

Question: 01
Consider the following code:


#include
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?

Ans: c. The code will compile and run without errors


Question: 02
Base class members are made accessible to a derived class and inaccessible to rest of the program by --------------?


Ans: protected access specifier


Question: 03
Which of the following is not a standard STL header?


Ans: a.


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


Ans: In a class all members are private by default, whereas in struct all members are public by default


Question: 05
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?

Ans: Constructor of A Constructor of B Destructor of B Destructor of A


Question: 06
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;

Ans: a. A::I A::f() { return 0; }


Question: 07
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)
{
}
};

Ans: a. Yes, it is valid


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

ostrstream
ostringstream
ostream
wostream

Ans: ostrstream


Question: 09
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

Ans: b. vector::erase can be used to delete a single element and a range of elements of the vector


Question: 10
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?

Ans: c. Unknown Exception Thrown


Question: 11
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();
}

Ans: b. Base Derived


Question: 12
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;
}

Ans: b. In second 1 In first 2


Question: 12
Consider the following class hierarchy:

class Base
{
}

class Derived : private Base
{
}

Which of the following are true?

Ans: d. Derived can access public and protected member functions of Base


Question: 13
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?

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


Question: 14
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?

Ans: e. The code will compile without errors


Question: 15
A pure virtual function can be declared by------------?

Ans: b. equating it to 0


Question: 16
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?

Ans: d. foo = new char*[200];


Question: 17
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?

Ans: b. Derived Exception


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

Ans: h. None of the above


Question: 19
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?

Ans: e. None of the above


Question: 20
What will be the output of the following code?

#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();
}

Ans: In Base In Base In Derived


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

Ans: c. Constructors can take parameters, but destructors cannot
e. It is illegal to define a constructor as virtual


Question: 22
Consider the following class hierarchy:

class Base
{
}

class Derived : public Base
{
}

Which of the following are true?

Ans: 
d. Derived can access public and protected member functions of Base
e. The following line of code is valid: Base *object = new Derived();


Question: 23
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?

Ans: c. A template function declaration


Question: 24
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

Ans: a. c, b. f


Question: 25
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

Ans: c. Only 1 and 2 are true


Question: 26
Which of the following operators cannot be overloaded?

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

Ans: d. .
e. ::
h. ?:


Question: 27
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?

Ans: d. Person(const Person &p);


Question: 28
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

Ans: e. All of the above


Question: 29
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
posted by khalequzzaman
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

Ans: d. You can create arrays of functions


Question: 30
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

Ans: b. Throw an exception from the constructor


Question: 31
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

Ans: b. protected


Question: 32
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?

Ans: d. 7


Question: 33
Which of the following are NOT valid C++ casts

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

Ans: e. void_cast


Question: 34
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?

Ans: a. Base Exception


Question: 35
Which of the following is NOT a standard sorting algorithm:

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

Ans: b. std::qsort


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

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

Ans: d. 1


Question: 37
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

Ans: d. std::cerr


Question: 38
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

Ans: b. Macro usage should be avoided because they are error prone


Question: 39
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?

Ans: d. There will be an error because in the declaration of innerObject the type Inner must be qualified by Outer


Question: 40
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?

Ans: e. 19


Question: 41
What is the output of the following code segment?

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

Ans: c. 12,10


Question: 42
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

Ans: a. A derived class can call the destructor of the parent class explicitly


Question: 43
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

Ans: c. terminate


Question: 44
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?

Ans: e. monthTable winter,spring={"March","April","May"};


Question: 45
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;
}

Ans: b. The printed output will be 5


Question: 46
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

Ans: a. add


Question: 47
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

Ans: c. Write the error to a log file


Question: 48
Which of the following are NOT valid C++ casts

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

Ans: e. void_cast


Question: 49
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

Ans: c. Declaration of a local variable


Question: 50
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

Ans: a. The * operator can be overloaded to perform division


Question: 51
State whether True or False.

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

Ans: a. True


Question: 52
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

Ans: a. fstream

Don't Miss A Single Updates

Remember to check your email account to confirm your subscription.

Blogger
Disqus
Post a comment ➜

No Comment