C++ Questions and Answers Part-6

1. Which of the following is an exit-controlled loop?
a) for
b) while
c) do-while
d) all of the mentioned

Answer: c
Explanation: do-while is called exit controlled loop because in do-while termination condition is checked when we have executed the body of the loop i.e. we are exiting the body and then checking the condition, therefore, it is called exit controlled loop.

2. Which of the following is an entry-controlled loop?
a) for
b) while
c) do-while
d) both while and for

Answer: d
Explanation: Both while and for loops are called entry controlled loop because in both of them the termination condition is checked before we enter the body of the loop hence they are called entry controlled loop.

3. In which part of the for loop termination condition is checked?
for(I;II;III)
{IV}
a) I
b) II
c) III
d) IV

Answer: b
Explanation: In II part the termination condition of the for loop is checked.

4. What is dynamic binding?
a) The process of linking the actual code with a procedural call during run-time
b) The process of linking the actual code with a procedural call during compile-time
c) The process of linking the actual code with a procedural call at any-time
d) All of the mentioned

Answer: a
Explanation: Binding of calls and variables with actual code at run-time is called dynamic binding. For example in the concept of polymorphism types are decided are defined during the execution of code which leads to the different function calls depending upon the types used this is called dynamic binding. As the function call is decided during the run-time therefore dynamic binding happens at run-time.

5. What is static binding?
a) The process of linking the actual code with a procedural call during run-time
b) The process of linking the actual code with a procedural call during compile-time
c) The process of linking the actual code with a procedural call at any-time
d) All of the mentioned

Answer: b
Explanation: Binding of calls and variables with actual code at compile-time is called static binding. For example normally whenever we declare a variable we define its type hence compiler knows what type should be binded to that variable i.e. compiler can decide about that variable this is called static binding.

6. 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.

7. 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.

8. 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.

9. 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: d
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.

10. 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.