C++ Programming Multiple Choice Questions & Answers (MCQs) on “C++ Concepts – 2”.
1. Which of the following is the scope resolution operator?
a) .
b) *
c) ::
d) ~
Answer: c
Clarification: :: 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.
2. What will be the output of the following C++ code?
#includeusing 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
Clarification: While printing x we are using :: operator hence the refernce is given to global variable hence the global variable x = 1 is printed.
3. What will be the output of the following C++ code?
#includeusing namespace std; class A { ~A(){ cout<<"Destructor calledn"; } }; int main() { A a; return 0; }
a) Destructor called
b) Nothing will be printed
c) Error
d) Segmentation fault
Answer: c
Clarification: 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.
4. What will be the output of the following C++ code?
#includeusing namespace std; class A { ~A(){ cout<<"Destructor calledn"; } }; 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
Clarification: The pointer object is created is not deleted hence the destructor for these objects is not called hence nothing is printed on the screen.
5. What will be the output of the following C++ code?
#includeusing namespace std; int x[100]; int main() { cout << x[99] << endl; }
a) Garbage value
b) 0
c) 99
d) Error
Answer: b
Clarification: In C++ all the uninitialized variables are set to 0 therefore the value of all elements of the array is set to 0.
6. What will be the output of the following C++ code?
#includeusing namespace std; int main () { int cin; cin >> cin; cout << "cin: " << cin; return 0; }
a) cin: garbage value
b) Error
c) Segmentation fault
d) Nothing is printed
Answer: a
Clarification: cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error.
7. Which of the following operator has left to right associativity?
a) Unary operator
b) Logical not
c) Array element access
d) addressof
Answer: c
Clarification: Array element has left to right associativity i.e. expressions are evaluated from left to right in case of array element access.
8. Which of the following is accessed by a member function of a class?
a) The object of that class
b) All members of a class
c) The public part of a class
d) The private part of a class
Answer: b
Clarification: A member function of a class can access all the members of its class whether they are private, protected or public.
9. What is the size of a character literal in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
Answer: a
Clarification: The size of a character literal is 4 in case of C but it is one in case of C++. You can do printf(“%d”, (int)sizeof(‘a’)); in both C and C++ to check this.
10. What is the size of a character type in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4
Answer: c
Clarification: The size of a character type in both C and C++ is 1. You can do printf(“%d”, (int)sizeof(char)); in both C and C++ to check this.
11. Which of the following is correct?
a) struct tag is required in both C and C++ while declaring an object of the structure
b) struct is not required in C but required in C++ while declaring an object of the structure
c) struct is not required in C++ but required in C while declaring an object of the structure
d) struct tag is not required in both C and C++ while declaring an object of the structure
Answer: c
Clarification: C++ does not require struct keyword while declaring an object of the structure whereas in C we require struct tag for declaring an object.
12. Which of the following is correct?
a) struct cannot have member function in C but it can in C++
b) struct cannot have member function in C++ but it can in C
c) struct cannot have member function in both C and C++
d) struct can have member function in both C and C++
Answer: a
Clarification: struct can have member function in C++ whereas member functions are not allowed in case of C.
13. What happens if we run the following code in both C and C++?
#includestruct STRUCT { int a; int func() { printf("HELLO THIS IS STRUCTUREn"); } }; int main() { struct STRUCT s; s.func(); return 0; }
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
Answer: b
Clarification: As C does not allows the structure to have member functions, therefore, it gives an error in case of C but as C++ does allow structures to have member functions, therefore, the C++ does not give an error.
14. What happens if we run the following code in both C and C++?
#includestruct STRUCT { int a = 5; int func() { printf("%dn", a); } }; int main() { struct STRUCT s; s.func(); return 0; }
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
Answer: b
Clarification: As C does not allows to initialize any member inside the structure, therefore, the program gives error whereas in case of C++ this is allowed therefore the program does not give any error.
15. What happens if the following program is compiled in both C and C++?
#includestruct STRUCT { int static a; }; int main() { struct STRUCT s; return 0; }
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++
Answer: b
Clarification: C does not allow the programmer to declare any static members inside a class whether in C++ it is allowed to declare static variables.
