- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Advanced
- C++ Files and Streams
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
C++ Mock Test
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.
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"; } }
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]; }
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; }
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; }
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; }
Q 5 - Compiler generates ___ file
Answer : B
Explaination
Compilation is the process of translating high level language statements into equivalent machine code, which is object code.
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 _____
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.
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
Answer : B
Explaination
bool is the reserved keyword and cannot be used an identifier name.
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
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; }
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 ___
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(); }
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() { }
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
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; }
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; }
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; }
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?
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<<" "; }
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<<" "; }
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); }
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; }
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
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 |