C++ Mock Test


Advertisements

This section presents you various set of Mock Tests related to C++ Framework. You can download these sample mock tests at your local machine and solve offline at your convenience. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself.

Questions and Answers

C++ Mock Test II

Q 1 - What is the output of the following program?

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

A - 1

B - 2

C - 3

D - Compile Error

Answer : C

Explaination

Comma is called as the separator operator and the associativity is from left to right. Therefore ‘k’ is the expressions resultant.

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

Q 2 - In the following program f() is overloaded.

void f(int x) {

}

void f(signed x) {

}

main() {

}

A - True

B - False

Answer : B

Explaination

No, as both the functions signature is same.

Q 3 - In the following program f() is overloaded.

void f(int x) {

}

int f(signed x) { 
   return 1;
}

main() {

}

A - True

B - False

Answer : B

Explaination

No, as both the functions arguments is same and compiler ignores return type to consider overloading though different in return type.

Answer : B

Explaination

Only members of the derived class and the same class can access a protected member.

Answer : C

Explaination

Virtual functions gives the ability to override the functionality of base class into the derived class. Hence achieving dynamic/runtime polymorphism.

Q 6 - Choose the Object oriented programming language from below.

A - C++

B - Small talk

C - Simula

D - All the above.

Answer : D

Explaination

Q 7 - Class function which is called automatically as soon as the object is created is called as __

A - Constructor

B - Destructor

C - Friend function

D - Inline function.

Answer : A

Explaination

If not provided the same, default constructor from the compiler is called automatically otherwise the programmer’s provided constructor gets called.

Q 8 - Escape sequence character '\0' occupies __ amount of memory.

A - 0

B - 1

C - 2

D - 4

Answer : B

Explaination

As it is also a character is occupies 1 byte of memory.

Q 10 - The pointer which stores always the current active object address is __

A - auto_ptr

B - this

C - p

D - none of the above.

Answer : B

Explaination

this is the keyword and acts as a pointer which always holds current active objects.

Q 11 - We can use this pointer in static member function of the class.

A - True

B - False

Answer : B

Explaination

False, as the static members do exist before the object of the respective class are created. 'this' is not applicable in the said context.

Answer : D

Explaination

Q 13 - How many number of arguments can a destructor of a class receives?

A - 0

B - 1

C - 2

D - None of the above.

Answer : A

Explaination

The destructor receives no arguments and is only form to be provided. Hence destructor cannot be overloaded.

Q 14 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : B

Explaination

The overridden method f() of the created object for derived class gets called.

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

Q 15 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};

class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() {
   Base *p = new Derived(); 
   
   p->f();
}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : A

Explaination

The method f() is not overridden therefore as per the pointer type respective method is called.

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};
class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() {
   Base *p = new Derived(); 
   
   p->f();
}

Q 16 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};
class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() { 
   Derived obj; 
   obj.Base::f();
}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : A

Explaination

The method f() inherited from Base is referred using :: operator.

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};
class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() { 
   Derived obj; 
   obj.Base::f();
}

Q 17 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public:
   void f() { 
      cout<<"Base\n";
   }
};
class Derived:public Base {
public: 
   void f() {
      cout<<"Derived\n";
   };
};
main() { 
   Derived obj; 
   obj.Base::f();
}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : A

Explaination

Base object cannot refer to Derived members.

#include<iostream>

using namespace std;
class Base {
public:
   void f() { 
      cout<<"Base\n";
  }
};
class Derived:public Base {
public: 
   void f() {
      cout<<"Derived\n";
   };
};
main() { 
   Derived obj; 
   obj.Base::f();
}

Q 18 - What is the output of the following program?

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

A - Done

B - Compile error

C - Runtime error

D - None of the above

Answer : C

Explaination

It is invalid to release memory more than once.

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

Q 19 - What is the output of the following program?

#include<iostream>

using namespace std;
main() { 
   int const a = 5;
   
   a++; 
   cout<<a; 
}

A - 5

B - 6

C - Runtime error

D - Compile error

Answer : D

Explaination

Compile error - constant variable cannot be modified.

Q 20 - Which operator is used to resolve the scope of the global variable?

A - −>

B - .

C - *

D - ::

Answer : D

Explaination

Scope resolution operator is used to resolve for the global scope of a variable if the local and global variables conflict by name.

Q 21 - Which feature of the OOPS gives the concept of reusability?

A - Abstraction

B - Encapsulation

C - Inheritance

D - None of the above.

Answer : C

Explaination

The process of designing a new class (derived) from the existing (base) class to acquire the attributes of the existing is called as inheritance. Inheritance gives the concept of reusability for code/software components.

Q 22 - Objects created using new operator are stored in __ memory.

A - Cache

B - Heap

C - Stack

D - None of the above.

Answer : B

Explaination

new operator allocates memory dynamically know as Heap/free memory.

Q 24 - The programs machine instructions are store in __ memory segment.

A - Data

B - Stack

C - Heap

D - Code

Answer : D

Explaination

Code segments holds the program instructions and fetched by instruction pointer for execution.

Answer : D

Explaination

Options (a), (b) & (c) are applicable.

Answer Sheet

Question Number Answer Key
1 C
2 B
3 B
4 B
5 C
6 D
7 A
8 B
9 A
10 B
11 B
12 D
13 A
14 B
15 A
16 A
17 C
18 C
19 D
20 D
21 C
22 B
23 A
24 D
25 D
cpp_questions_answers.htm
Advertisements