- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
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 cant 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 |