Placement Papers: Quark C Plus Plus Question Paper

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Quark Media House India Pvt. Ltd.

Note: There are 20 questions and no negative marking.

Time allotted is 30 minutes.

  1. What does the following do: Void afunction (int * x) { x = new int; * x = 12; } int main () { int v = 10; afunction (&v); cout<<v; }

    1. Outputs 12

    2. Outputs 10

    3. Outputs the address of v

    Ans b

  2. How long does this loop run: For (int x = 0; x = 3; x + + )

    1. Never

    2. Three times

    3. Forever

    Ans. c

  3. Which uses less memory?

    1. struct astruct { int x; float y; int v; }

    2. union aunion { int x; float v; }

    3. char array[10]

    Ans. b

  4. Evaluate: Int fn (int v) { if (v = = 1 | v = = 0) return 1; if (v%2 = = 0) return fn (v/2) + 2; else return fn (v − 1) + 3; } for fn (7)

    1. 10

    2. 11

    3. 1

    Ans. b

  5. Which of the Standard C + + casts can be used to perform a safe downcast:

    1. reinterpret_cast

    2. dynamic_cast

    3. static_cast

    4. const_cast

    Ans. b

  6. class professor { }; class teacher: Public virtual professor { }; class researcher: Public virtual professor { }; class myprofessor: Public teacher, public researcher { }; Referring to the sample code above, if an object of class “myprofessor” were created, how many instances of professor will it contain?

    1. 0

    2. 1

    3. 2

    4. 3

    5. 4

    Ans. b, here professor will be called a virtual base class since teacher and researcher derive from it virtually. This is used in multiple inheritance as shown here. If professor was not inherited virtually then there would be 2 instances of professor in the object of myprofessor.

  7. string somestring; Which of the following choices will convert a standard C + + string object “somestring” to a C string?

    1. Copy. Somestring ()

    2. somestring. c_str ()

    3. &somestring [1]

    4. std: Cstring (somestring)

    5. (char * ) somestring

    Ans. b

  8. class basex { int x; public: Void setx (int y) { x = y; } }; class derived: Basex { }; What is the access level for the member function “setx” in the class “derived” above?

    1. private

    2. local

    3. global

    4. public

    5. protected

    Ans. a

    Table of Member Access Privileges

    Access in Base Class Base Class Inherited as Access in Derived Class

    Public Protected Private Public Public Protected No access1

    Public Protected Private Protected Protected Protected No access1

    Public Protected Private Private Private Private No access1

    1 Unless friend declarations within the base class explicitly grant access.

    So, the highest member accessibility is defined by the way a class is inherited, if it is inherited privately, then the highest member accessibility will be private. Default inheritance is private.

  9. class Alpha { public: Char data[10000]; Alpha (); ~Alpha (); }; class Beta { public: Beta () { n = 0; } void FillData (Alpha a); private: Int n; }; How do you make the above sample code more efficient?

    1. If possible, make the constructor for Beta private to reduce the overhead of public constructors.

    2. Change the return type in FillData to int to negate the implicit return conversion from “int” to “void”

    3. Make the destructor for Alpha virtual.

    4. Make the constructor for Alpha virtual.

    5. Pass a const reference to Alpha in FillData

    Ans. Since u r passing a reference hence a new array will not be created in memory, whereas if u pass by value, then an array of 10000 chars will be created. Passing by reference only creates an alias for the original parameter (i.e., it points to the original parameter) and is same as passing by address, the only difference is that it can be used like an object instead of as a pointer, i.e., if param is &a, then u will write a. Member, whereas if param is * a then u will write a → member.

  10. class Foo { int x; public: Foo (int I); }; Ans. d. Copy constructor takes an arg of its own type which is passed by ref and which should not be changed hence it is const

  11. class HasStatic { static int I; }; Referring to the sample code above, what is the appropriate method of defining the member variable “I” and assigning it the value 10, outside of the class declaration?

    1. HasStatic I = 10

    2. int static I = 10

    3. static I (10)

    4. static I = 10

    5. int HasStatic: I = 10

    Ans. e

  12. class X { private: Int a; protected: X () { cout<< “X constructor was called” <<endl; } ~X () { cout<< “X destructor was called” <<endl} }; Referring to the code above, which one of the following statements regarding “X” is TRUE?

    1. X is an abstract class.

    2. Only subclasses of X may create X objects.

    3. Instances of X cannot be created.

    4. X objects can only be created using the default copy constructor.

    5. Only friends can create instances of X objects.

    Ans. b. Instances of X can be created only inside its subclasses.

  13. class Foo { const int x; protected: Foo (int f); ~Foo (); }; Foo f; Referring to the sample code above, why will the class declaration not compile?

    1. The variable x is const.

    2. The destructor is protected.

    3. The destructor is not public.

    4. The constructor is protected.

    5. There is no default constructor.

  14. class Foo { public: Foo (int i) { } }; class Bar: Virtual Foo { public: Bar () { } }; Bar b; Referring to the above code, when the object ‘b’ is defined, a compiler error will occur. What action fixes the compiler error?

    1. Adding a virtual destructor to the class Bar

    2. Adding a constructor to Bar which takes an int parameter

    3. Adding “Foo ()” to the Bar constructor

    4. Adding a copy constructor to the class Foo

    5. Adding “Foo (0)” to the Bar: Bar initializer list

    Ans. e

  15. Which one of the following describes characteristics of “protected” inheritance?

    1. The base class has access only to the public or protected members of the derived class.

    2. The derived class has non-public, inheritable, access to all but the private members of the base class.

    3. The derived class has access to all members of the base class.

    4. The private members of the base class are visible within the derived class.

    5. Public members of the derived class are privately accessible from the base class.

    Ans. b

  16. The “virtual” specifier in a member function enables which one of the following?

    1. Monmorphism

    2. Late binding

    3. Metamorphism

    4. Solomorphism

    5. Inheritance

    Ans. b.

  17. class X { public: Int x; static void f (int z); }; void X: f (int y) { x = y; } What is the error in the sample code above?

    1. The class X does not have any protected members.

    2. The static member function f () accesses the non-static z.

    3. The static member function f () accesses the non-static x.

    4. The member function f () must return a value.

    5. The class X does not have any private members.

    Ans. c

  18. template & lt; class T, class X> class Obj { T my_t; X my_x; public: Obj (T t, X x): My_t (t), my_x (x) { } }; Referring to the sample code above, which one of the following is a valid conversion operator for the type T?

    1. T operator T () { return my_t; }

    2. T operator (T) const { return my_t; }

    3. operator (T) { return my_t; }

    4. T operator T (const Obj &obj) { return obj. My_t; }

    5. operator T () const { return my_t; }

    Ans. e

  19. catch (exception &e) { … } Referring to the sample code above, which one of the following lines of code produces a written description of the type of exception that “e” refers to?

    1. cout << e. Type ()

    2. cout << e. Name ()

    3. cout << typeid (e). Name ()

    4. cout << e. What ()

    5. cout << e

    Ans. c

  20. int f () { int I = 12; int &r = I; r + = r/4; int * p = &r; * p + = r; return I; } Referring to the sample code above, what is the return value of the function “f ()”

    1. 12

    2. 15

    3. 24

    4. 17

    5. 30

Ans. e

Sign In