11.
Which of the following is the scope resolution operator?
a) .
b) *
c) ::
d) ~
Answer: c
Explanation:
:: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function.
12.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int x = 1;
int main()
{
int x = 2;
{
int x = 3;
cout << ::x << endl;
}
return 0;
}
a) 1
b) 2
c) 3
d) 123
Answer: a
Explanation:
While printing x we are using :: operator hence the refernce is given to global variable hence the global variable x = 1 is printed.
13.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class A
{
~A(){
cout<<“Destructor called\n”;
}
};
int main()
{
A a;
return 0;
}
a) Destructor called
b) Nothing will be printed
c) Error
d) Segmentation fault
Answer: c
Explanation: Whenever a destructor is private then one should not define any normal object as it will be destroyed at the end of the program which will call destructor and as destructor is private the program gives error during compile while in case of pointer object the compiler at compile does not know about the object, therefore, does not gives compile error. Hence when the destructor is private then the programmer can declare pointer object but cannot declare a normal object.
14.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class A
{
~A(){
cout<<“Destructor called\n”;
}
};
int main()
{
A *a1 = new A();
A *a2 = new A();
return 0;
}
a) Destructor called
b)
Destructor called
Destructor called
c) Error
d) Nothing is printed
Answer: c
Explanation:
The pointer object is created is not deleted hence the destructor for these objects is not called hence nothing is printed on the screen.
15.
What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int x[100];
int main()
{
cout << x[99] << endl;
}
a) Garbage value
b) 0
c) 99
d) Error
Answer: b
Explanation:
In C++ all the uninitialized variables are set to 0 therefore the value of all elements of the array is set to 0.