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 IV

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

#include<iostream>

using namespace std;
main() { 
   float t = 2;
	
	switch(t) {
      case 2: cout<<”Hi”;
		default: cout<<"Hello";
	}
}

A - Hi

B - HiHello

C - Hello

D - Error

Answer : D

Explaination

Error, switch expression can’t be float value

#include<iostream>

using namespace std;
main() { 
   float t = 2;
	
	switch(t) {
		case 2: cout<<”Hi”;
		default: cout<<"Hello";
	}
}

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

#include<iostream>

using namespace std;
main() { 
   int a[] = {1, 2}, *p = a;
   
   cout<<p[1]; 
}

A - 1

B - 2

C - Compile error

D - Runtime error

Answer : B

Explaination

as ‘p’ holds the base address then we can access array using ‘p’ just like with ‘a’

#include<iostream>

using namespace std;
main() { 
   int a[] = {1, 2}, *p = a;
   
   cout<<p[1]; 
}

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

#include<iostream>

using namespace std;
main() { 
   int i = 13, j = 60;
   
   i^=j;
   j^=i;
   i^=j;
   cout<<i<<" "<<j;
}

A - 73 73

B - 60 13

C - 13 60

D - 60 60

Answer : B

Explaination

60 13, its swapping.

#include<iostream>

using namespace std;
main() { 
   int i = 13, j = 60;
   
   i^=j;
   j^=i;
   i^=j;
   cout<<i<<" "<<j;
}

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

#include<iostream>

using namespace std;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

A - A

B - Garbage value

C - 65

D - 97

Answer : B

Explaination

65, as the union variables share common memory for all its elements, x gets ‘A’ whose ASCII value is 65 and is printed.

#include<iostream>

using namespace std;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

Answer : B

Explaination

Compilation is the process of translating high level language statements into equivalent machine code, which is object code.

Q 6 - Special symbol permitted with in the identifier name.

A - $

B - @

C - _

D - .

Answer : C

Explaination

The only permitted special symbol is under score (_) in the identifier.

Q 7 - A single line comment in C++ language source code can begin with _____

A - ;

B - :

C - /*

D - //

Answer : D

Explaination

Two immediate forward slashes are used to comment a single line. A single can be commented by beginning with /* and should be terminated with */ , in general used for multi-line comments.

Q 8 - An inline function can execute faster than a normal function.

A - True

B - False

Answer : A

Explaination

As the code of inline function gets expanded at the line of call, therefore it gets executed faster with no overhead of context switch

Q 9 - Choose the invalid identifier from the below

A - Int

B - bool

C - DOUBLE

D - __0__

Answer : B

Explaination

bool is the reserved keyword and cannot be used an identifier name.

Q 10 - Identify the C++ compiler of Linux

A - cpp

B - g++

C - Borland

D - vc++

Answer : B

Explaination

g++ is GNU C++ compiler for linux. Borland and vc++ (Microsoft visual c++) for windows.

Q 11 - Following is the invalid inclusion of a file to the current program. Identify it

A - #include <file>

B - #include "file"

C - #include < file

D - All of the above are invalid

Answer : C

Explaination

option (a) & (b) are valid. There is no such syntax or provision as in option (c).

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

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

A - 5

B - Address of ‘x’

C - Compile error

D - 10

Answer : D

Explaination

A function can return reference, hence it can appear on the left hand side of the assignment operator.

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

Q 13 - The default executable generation on UNIX for a C++ program is ___

A - a.exe

B - a

C - a.out

D - out.a

Answer : C

Explaination

“a.out” is the default name of the executable generated on both the UNIX and Linux operating systems.

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

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

A - 3 2 1 0

B - 3 2 1

C - 3 3 3

D - Compile error

Answer : B

Explaination

As the static variable retains its value from the function calls, the recursion happens thrice.

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

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

main() {
}

A - No output

B - Garbage

C - Compile error

D - Runtime error

Answer : A

Explaination

It is valid to have main() function empty, therefore producing no displayable output.

Q 16 - Does both the loops in the following programs prints the correct string length?

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0; 
   
   while(s[i++]);
      cout<<i;
}

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

Answer : B

Explaination

In while loop 'i' gets incremented after checking for '\0', hence giving 1 more than the length.

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0;
   
   while(s[i++]);
      cout<<i;
}

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

#include<iostream>

using namespace std;
main() {	
   int a[] = {10, 20, 30};
   
	cout<<*a+1;
}

A - 10

B - 20

C - 11

D - 21

Answer : C

Explaination

*a refers to 10 and adding a 1 to it gives 11.

#include<iostream>

using namespace std;
main() {	
   int a[] = {10, 20, 30};
   
	cout<<*a+1;
}

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

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : B

Explaination

*s=’N’, changes the character at base address to ‘N’.

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

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

#include<iostream>

using namespace std;
main() {
   char *s = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : D

Explaination

*s=’N’, trying to change the character at base address to ‘N’ of a constant string leads to runtime error.

#include<iostream>

using namespace std;
main() {
   char *s = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

Q 20 - What is the built in library function to compare two strings?

A - string_cmp()

B - strcmp()

C - equals()

D - str_compare()

Answer : B

Explaination

strcmp() is the built in function from “string.h” to compare two strings. Returns 0 if both are same strings. Returns -1 if first < second string. Returns 1 first > second.

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

#include<iostream>

using namespace std;
void main() {
   char *s = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

A - C++ C++

B - C++ ++

C - ++ ++

D - Compile error

Answer : B

Explaination

After s++, s points the string “++”.

#include<iostream>

using namespace std;
void main() {
   char *s = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

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

#include<iostream>

using namespace std;
void main() {
   char s[] = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

A - C++ C++

B - C++ ++

C - ++ ++

D - Compile error

Answer : D

Explaination

‘s’ refers to a constant address and cannot be incremented.

#include<iostream>

using namespace std;
void main() {
   char s[] = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

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

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

using namespace std;
main() { 
   char s[] = "Hello\0Hi";
   
   cout<<strlen(s)<<" "<<sizeof(s);
}

A - 5 9

B - 7 20

C - 5 20

D - 8 20

Answer : A

Explaination

Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

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

using namespace std;
main() { 
   char s[] = "Hello\0Hi";
   
   cout<<strlen(s)<<" "<<sizeof(s);
}

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

#include<iostream>

using namespace std;
main() { 
   class student { 
      int rno = 10;
   } v;
  
   cout<<v.rno;
}

A - 10

B - Garbage

C - Runtime error

D - Compile error

Answer : D

Explaination

Class member variables cannot be initialized.

#include<iostream>

using namespace std;
main() { 
   class student { 
      int rno = 10;
   } v;
  
   cout<<v.rno;
}

Q 25 - i) Exceptions can be traced and controlled using conditional statements.

ii) For critical exceptions compiler provides the handler

A - Only (i) is true

B - Only (ii) is true

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

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

Answer : B

Explaination

Conditional statements are used to take alternate actions depending upon certain condition but not multi branching. C++ too provides some critical exception handlers.

Answer Sheet

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