Top C-plus-plus Interview Questions
Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.
Question
37
Explanation
What will be the output of the following program?
#include<iostream>
using namespace std;
int a = 1;
void fun()
{
int a = 2;
{
int a = 3;
cout << ::a << endl;
}
}
int main()
{
fun();
return 0;
}
The output of the program will be:
1
This is because the value of ::a is 1. The scope resolution operator when used with a variable name, always refers to global variable.
The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them.
Question
38
Explanation
Predict the output.
#include<iostream>
using namespace std;
int X[100];
int main()
{
cout << X[50] << endl;
}
The output will be 0. Because in C++ all the uninitialized global variables are initialized to 0.
Question
39
Explanation
Predict the output-
#include<iostream>
using namespace std;
union HELLO {
int a;
unsigned int b;
HELLO() { a = 10; }
unsigned int getb() {return b;}
};
int main()
{
HELLO obj;
cout << obj.getb();
return 0;
}
Output of the program will be 10.
We know that like structures, unions can also have methods. Members of union are public by default. Since data members of union share memory, the value of b becomes same as the value of a.
Union: A union is a special data type available in C that allows storing different data types in the same memory location.



.png)