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 III

Answer : C

Explaination

A constructor can’t be overridden.

Q 2 - An array can be passed to the function with call by value mechanism.

A - True

B - False

Answer : B

Explaination

An array never is passed with call by value mechanism

Q 3 - A C++ program statements can be commented using

A - Single line comment

B - Multi line comment

C - Either (a) or (b)

D - Both (a) and (b).

Answer : D

Explaination

Both styles of commenting is available in C++.

Q 4 - HAS-A relationship between the classes is shown through.

A - Inheritance

B - Container classes

C - Polymorphism

D - None of the above.

Answer : B

Explaination

A class containing anther class object as its member is called as container class and exhibits HAS A relationship.

Answer : B

Explaination

Defining a templated class is defining a generic class. Hence functionality of the class is generalized for several types, if applicable.

Answer : A

Explaination

Q 7 - Which type of data file is analogous to an audio cassette tape?

A - Random access file

B - Sequential access file

C - Binary file

D - Source code file

Answer : B

Explaination

As the access is linear.

Q 8 - i) single file can be opened by several streams simultaneously.

ii) several files simultaneously can be opened by a single stream

A - (i) and (ii) are true

B - (i) and (ii) are false

C - Only (i) is true

D - Only (ii) is true

Answer : C

Explaination

Q 9 - With respective to streams >> (operator) is called as

A - Insertion operator

B - Extraction operator

C - Right shift operator

D - Left shift operator

Answer : B

Explaination

It extracts the data from stream into variables.

Q 10 - (i) ‘ios’ is the base class of ‘istream’

(ii) All the files are classified into only 2 types. (1) Text Files (2) Binary Files.

A - Only (i) is true

B - Only (ii) is true

C - Both (i) & (ii) are true

D - Both (i) & (ii) are false

Answer : C

Explaination

Answer : A

Explaination

When the program is in execution phase the possible unavoidable error is called as an exception.

Q 12 - i) Exception handling technically provides multi branching.

ii) Exception handling can be mimicked using ‘goto’ construct.

A - Only (i) is true

B - Only (ii) is true

C - Both (i) & (ii) are true

D - Both (i) && (ii) are false

Answer : A

Explaination

goto just does the unconditional branching.

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

#include<iostream>

using namespace std;
main() { 
   const int 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.

#include<iostream>

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

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

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

A - Equal strings

B - Unequal strings

C - No output

D - Compilation error

Answer : C

Explaination

No output, as we are comparing both base addresses and are not same.

#include<iostream>

using namespace std;
main() {
   char s[] = "hello", t[] = "hello";
   if(s==t)
      cout<<"eqaul strings";
}

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

#include<iostream>

using namespace std;
main() {
   enum { 
      india, is = 7, GREAT 
   };

   cout<<india<<" "<<GREAT;
}

A - 0 1

B - 0 2

C - 0 8

D - Compile error

Answer : C

Explaination

0 8, enums gives the sequence starting with 0. If assigned with a value the sequence continues from the assigned value.

#include<iostream>

using namespace std;
main() {
   enum { 
      india, is = 7, GREAT 
   };

   cout<<india<<" "<<GREAT;
}

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

#include<iostream>

using namespace std;
main() {
   short unsigned int i = 0; 
   
   cout<<i--;
}

A - 0

B - Compile error

C - 65535

D - 32767

Answer : A

Explaination

0, with post decrement operator value of the variable will be considered as the expression’s value and later gets decremented

#include<iostream>

using namespace std;
main() {
   short unsigned int i = 0; 
   
   cout<<i--;
}

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

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

A - Compile error

B - Hi

C - HelloHi

D - Hello

Answer : A

Explaination

compile error, keyword break can appear only within loop/switch statement.

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

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

#include<iostream>

using namespace std;
void f() { 
   static int i; 
   
   ++i; 
   cout<<i<<" "; 
}

main() { 
   f(); 
   f(); 
   f(); 
}

A - 1 1 1

B - 0 0 0

C - 3 2 1

D - 1 2 3

Answer : D

Explaination

1 2 3, A static local variables retains its value between the function calls and the default value is 0.

#include<iostream>

using namespace std;
void f() { 
   static int i; 
   
   ++i; 
   cout<<i<<" "; 
}

main() { 
   f(); 
   f(); 
   f(); 
}

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

#include<iostream>

using namespace std;
void f() {
   cout<<"Hello"<<endl;
}
main() {
}

A - No output

B - Error, as the function is not called.

C - Error, as the function is defined without its declaration

D - Error, as the main() function is left empty

Answer : A

Explaination

No output, apart from the option (a) rest of the comments against the options are invalid

#include<iostream>

using namespace std;
void f() {
	cout<<"Hello"<<endl;
}
main() 
{
}

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

#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   int x = 1;

   switch(x) {
   case 1 :
      cout << "Hi!" << endl; 
      break;
   default :
      cout << "Hello!" << endl;
   }
}

A - Hello

B - Hi

C - HelloHi

D - Compile error

Answer : B

Explaination

Hi, control reaches default-case after comparing the rest of case constants.

#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   int x = 1;

   switch(x) {
   case 1 :
      cout << "Hi!" << endl; 
      break;
   default :
      cout << "Hello!" << endl;
   }
}

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

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

A - 3 5

B - 5 3

C - 5 5

D - Compile error

Answer : B

Explaination

5 3, call by value mechanism can’t alter actual arguments.

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

Q 22 - What will be the output of the following program?

#include<iostream>
#include<string.h>

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

A - 0

B - 1

C - -1

D - Invalid use of strcmp() function

Answer : A

Explaination

0, strcmp return 0 if both the strings are equal

#include<iostream>
#include<string.h>

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

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

#include<iostream>

using namespace std;
main() { 
   int r, x = 2;
   
   float y = 5;
  
   r = y%x;
   cout<<r; 
}

A - 1

B - 0

C - 2

D - Compile error

Answer : D

Explaination

Answer Compile Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

#include<iostream>

using namespace std;
main() { 
   int r, x = 2;
   
   float y = 5;
  
   r = y%x;
   cout<<r; 
}

Q 24 - What is the size of the following union definition?

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

A - 1

B - 2

C - 4

D - 8

Answer : C

Explaination

union size is biggest element size of it. All the elements of the union share common memory.

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

Q 25 - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

Answer : D

Explaination

The size of ‘int’ depends upon the complier i.e. whether it is a 16 bit or 32 bit.

Answer Sheet

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