C++ MCQs

C++ is a high-performance programming language known for its rich feature set and extensive use in system and application software development. It supports both procedural and object-oriented programming, giving developers flexibility in how they write their code. C++ is widely used in game development, real-time simulations, and applications requiring high performance.
halo_section_image

1. Which of the following is possible in c++?

2. What is the difference between a deep copy and a shallow copy in C++?

3. Why pre-increment is faster than post-increment in C++?

4. How many types of templates are there in C++?

5. What is the size of an object of an empty class in C++?

6. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; strcpy( str3, str1); strcat( str1, str2); len = strlen(str1); cout << len << endl; return 0; }

7. Predict the output : 2 #include <bits/stdc++.h> using namespace std; void doo(vector<int> arr, int n) { for (int i = 0 ; i < n ; i++) arr[i] = arr[i] * 3; } int main() { vector<int> nums = {1, 2, 3, 4, 5}; doo(nums, 5); int sum = 0; for (int i = 0 ; i < 5 ; i++) sum += nums[i]; cout << sum << "\n"; return 0; }

8. What is the correct syntax for using smart pointers in C++? (Assume header files are already included)

9. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int array[] = {0, 2, 4, 6, 7, 5, 3}; int n, result = 0; for (n = 0 ;n < 7 ;n++){ result += array[n]; } cout << result; return 0; }

10. What is the output of the below code? #include <bits/stdc++.h> int main() { char str[] = {'a', 'b', 'c'}; std::cout << str << '\n'; return 0; }

11. Which of the following code is the correct syntax for creating a class in C++?

12. What is the correct way to initialize a vector in C++?

13. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { string str ("Microsoft"); for (size_t i = 0; i < str.length();) { cout << str.at(i-1); } return 0;

14. Predict the output : 3 #include <bits/stdc++.h> using namespace std; void doo(int arr[], int n) { for (int i = 0; i < n; i++) arr[i] = arr[i] * 3; } int main() { int nums[5] = {1, 2, 3, 4, 5}; doo(nums, 5); int sum = 0; for (int i = 0; i < 5 ; i++) sum += nums[i]; cout << sum << "\n"; return 0; }

15. What is the syntax for printing a message in c++?

16. When we create an object of a derived class in c++, then?

17. Which of the following are the correct syntax for creating templates in C++? 1. template<class T> 2. template<typename T> 3. <template T> 4. template<class T, class U>

18. What is true about his statement in C++? std::vector<int> vecInts(5);

19. What is the time complexity of concatenation of string in C++? // E.g find time complexity of below code. string str = "outscal"; str += "clan";

20. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[][] = {{1,2},{3,4}}; int i, j, sum = -10; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) sum += arr[i][j]; cout << sum; return 0; }

21. Which of the following is a data type in c++?

22. Consider the following definition in C++ programming language. struct node { int data; struct node * next; } typedef struct node NODE; NODE *ptr; Which of the following c code is used to create new node?

23. Predict the output: #include <bits/stdc++.h> using namespace std; int main() { char str[] = "GAME DEVELOPER"; char* token = strtok(str, " "); while (token != NULL) { cout << token << '\n'; token = strtok(NULL, " "); } return 0; } a)GAME DEVELOPER b)GAMEDEVELOPER c)GAME d)GAME DEVELOPER

24. Predict the output: 1 #include <bits/stdc++.h> void fun(int n) { if (n == 0) return; std::cout << n%2; fun(n/2); } int main() { fun(25); return 0; }

25. Predict the output: #include <bits/stdc++.h> using namespace std; int solve(vector<int>& nums) { int c = max(0,nums[0]); int m = nums[0]; for(int i=1;i<nums.size();i++){ c += nums[i]; c = max(0, c); m = max(m, c); } return m; } int main() { vector<int> arr1 = {-1, -2, -3, -4, -9}, arr2 = {-1, 1, 3, -1, 5, -2}; cout << solve(arr1) + solve(arr2); return 0; }

26. Predict the output: #include <bits/stdc++.h> using namespace std; int solve(int n) { int i, j, arr[n], k; arr[0] = 1; for (i = 1; i < n; i++) { arr[i] = 0; for (j = 0, k = i - 1; j < i; j++, k--) arr[i] += arr[j] * arr[k]; } return arr[n - 1]; } int main() { cout << solve(8); return 0; }

27. Is it possible to initialize any Vector with an Array in C++?

28. What will be the output of the following C++ code? #include <iostream> #include <vector> using namespace std; int main () { vector<int> first; first.assign (7,100); vector<int>::iterator it; it=first.begin()+1; int myints[] = {1776,7,4}; cout << int (first.size()) << '\n'; return 0; }

29. What is the output of the below code? // stoi is used to convert string into integer #include <bits/stdc++.h> using namespace std; int main() { string s = "0x10A"; cout << stoi(s, nullptr, 16) << endl; return 0; }

30. What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; int main() { string str {"Steve jobs"}; unsigned long int found = str.find_first_of("aeiou"); while (found != string :: npos) { str[found] = '*'; found = str.find_first_of("aeiou", found + 1); } cout << str << "\n"; return 0; }

31. Predict the output: #include <bits/stdc++.h> using namespace std; template <typename T> int solve(const vector<T> &a) { vector<T> u; for (const T &x : a) { auto it = lower_bound(u.begin(), u.end(), x); if (it == u.end()) u.push_back(x); else *it = x; } return (int)u.size(); } int main() { vector<int> arr{1, 2, 3, 4, 4, 5, 5, 7. 8. 9, 6, 7, 11}; cout << solve(arr); return 0; }

32. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3}; cout << arr[3] << " " << arr[-1] << '\n'; return 0; }

33. What is the purpose of a header file in C++ programming?

34. You must have seen the #pragma once directive many times while working with C++ header files. Ever wondered what it does? What's the significance of the #pragma once directive?

35. What will be the output of the following C++ code? #include <iostream> using namespace std; class OutscalRectangle { int x, y; public: void setDimensions(int, int); int calculateArea() { return (x * y); } }; void OutscalRectangle::setDimensions(int a, int b) { x = a; y = b; } int main() { OutscalRectangle rectangle; rectangle.setDimensions(3, 4); cout << "The area of the rectangle is: " << rectangle.calculateArea(); return 0; }

36. What is the difference between the pointer declarations int* numPtr; and int *numPtr; in C++?

37. What happens if you try to dereference a null pointer in C++? #include <iostream> int main() { int* ptr = nullptr; std::cout << "Dereferencing a null pointer: " << *ptr << std::endl; return 0; }

38. What role does the main() function play in a C++ program?

39. What is the primary purpose of cout in C++?

40. What purpose does the semicolon (;) serve at the end of most lines in C++ code?

41. Consider the following code snippet: int main() { // Single line comment /* Multi-line comment */ return 0; } Which statement about comments in C++ is accurate?

42. What is the correct syntax to write comments in C++? 1. # This is a comment 2. / This is a comment 3. // This is a comment 4. /* This is a new comment */

43. Consider the following C++ function int fun(int n) { int i, j; for(i = 1; i<= n; i++) { for(j=1; j<n; j += i) { cout<<i<<j; } } } Time complexity of fun in terms of θ notation is?

44. What will be the output of the following C++ code? #include<iostream> int main() { const int a=10; a++; std::cout<<a; return 0; }

45. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3,4,5}; int sum = 10; for (int i = 0 ; i < 5 ; i++) { sum += arr[i]; sum -= sum; } cout << sum; return 0; }

46. Predict the output: #include <bits/stdc++.h> using namespace std; int main() { char str[] = "abc"; cout << sizeof(str) << " " << strlen(str) << '\n'; return 0; }

47. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { string str ("microsoft"); string::reverse_iterator r; for (r = str.rbegin() ; r < str.rend(); r++ ) cout << *r; return 0; }

48. What is the output of the below code? #include <bits/stdc++.h> int main() { char str[] = "abc"; std::cout << str << '\n'; return 0; }

49. What is the function of \n as a line terminator in C++?

50. Which of the following statements about variable declaration in C++ is NOT true?

51. What is the size of an empty class in C++?

52. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3,4,5,6}; cout << -5[arr]; return 0; }

53. What should be filled in place of _______ to run the below code without any error? #include <bits/stdc++.h> using namespace std; int main() { string s1 = "oustcalclan"; string s2 = "clann"; size_t pos = s1.find(s2); if (pos == _______) cout << "Not Found" << '\n'; else cout << "Found" << '\n'; return 0; }

54. Which of the following is correct about structure and class in C++? 1. Class members are private by default, and members of a struct are public by default. 2. When deriving a class from the base class, the default access specifier is public. 3. When deriving a class from the base class, the default access specifier is private.

55. How Encapsulation is achieved in C++?

56. Predict the time complexity of the below code: #include <bits/stdc++.h> using namespace std; int dp[100][100]; int matrixChainMemoised(int *p, int i, int j) { if (i == j) { return 0; } if (dp[i][j] != -1) { return dp[i][j]; } dp[i][j] = INT_MAX; for (int k = i; k < j; k++) { dp[i][j] = min( dp[i][j], matrixChainMemoised(p, i, k) + matrixChainMemoised(p, k + 1, j) + p[i - 1] * p[k] * p[j] ); } return dp[i][j]; } int main() { int arr[] = {1, 2, 3, 4}; int n = sizeof(arr) / sizeof(arr[0]); memset(dp, -1, sizeof dp); cout << matrixChainMemoised(arr, 1, n-1); return 0; }

57. Which of the following is the correct way of defining a function in C++? 1. void fun() {} 2. void function(int a, int b) {} 3. int func() { return -1; } 4. int func(double x) { return 0.0; }

58. What is causing unpredictable behaviour on compilation error in the following C++ code snippet? int arr[5] = { 1,2,3,4,5 }; cout<< endl << arr[5]<<endl;

59. What is the difference between a class template and a function template in C++?

60. Which of the following C++ line correctly creates an instance of the Player class named player?

61. What is the purpose of a destructor in C++?

62. How is Abstraction achieved in C++?

63. Why is it important to separate code into header (.h) and source (.cpp) files?

64. Consider the line of code int *ptr = new int;. What does this statement achieve in C++ and what are the implications of using it?

65. What is the difference between using new and malloc for dynamic memory allocation in C++?

66. What is the purpose of using namespaces in C++?

67. What is "scope resolution" in the context of namespaces, and how is it used in C++?

68. What will be the output of the following C++ program? #include <iostream> using namespace std; void counter() { static int count = 0; cout << count << " "; count++; } int main() { for (int i = 0; i < 5; i++) { counter(); } return 0; }

69. What will be the output of the following C++ program? #include <iostream> using namespace std; void process() { static int value = 10; static int* ptr = &value; cout << *ptr << " "; (*ptr)++; cout << *ptr << endl; } int main() { process(); process(); return 0; }

70. What is the output of the following C++ code? #include <iostream> using namespace std; class Game{ public: virtual void func(){ cout<<"Welcome to Game\n"; } }; class OutscalGame: public Game{ public: void func(){ cout<<"Welcome to Outscal Game\n"; } }; int main() { OutscalGame ob; ob.func(); return 0; }

71. How do interfaces and abstract classes differ in defining blueprints for derived classes in C++?

72. Consider the following code snippet: cppCopy code #include <iostream>class IShape { public: virtual void draw() const = 0; }; class Circle : public IShape { public: void draw() const override { std::cout << "Drawing a circle." << std::endl; } }; class Square : public IShape { public: void draw() const override { std::cout << "Drawing a square." << std::endl; } }; int main() { const IShape* shapes[] = {new Circle(), new Square()}; for (const auto& shape : shapes) { shape->draw(); } return 0; } What will be the output of the program?

73. What will be the output of the following C++ code? #include <iostream> using namespace std; class OutscalGames { public: static int value; OutscalGames() { value += 5; } void display() const { cout << "Value: " << value << endl; } }; int OutscalGames::value = 50; int main() { const OutscalGames obj; obj.display(); return 0; }

74. What will be the output of the following C++ code? #include <iostream> class Game { public: virtual void print() const { std::cout << "Game Called" << std::endl; } }; class OutscalGame : public Game { public: void print(){ std::cout << "Outscal Game Called" << std::endl; } }; int main() { const Game* ptr = new OutscalGame(); ptr->print(); delete ptr; return 0; }

75. What issue can arise due to naming conflicts in C++?

76. What is the primary effect of the static keyword when applied to a variable inside a function in C++?

77. How does the static keyword helps in implementing the Singleton design pattern in C++?

78. What is Inheritance in C++?

79. How many Access specifier are there in C++?

80. In a game developed using with C++, you have a base class Enemy and derived classes Orc and Troll. Both Orc and Troll share some methods from another class Knife. How can you design this inheritance to maintain a clean structure and avoid duplication

81. What is the primary issue caused by the diamond problem in C++ inheritance?

82. Can a virtual function be overloaded in C++?

83. How is the delta time used in the implementation of the moveDown() method in EnemyController.cpp?

84. Considering the standard practice for naming interfaces in C++, how would you name the interface for a class representing a geometric shape?

85. Which of the following is used to implement the C++ interfaces

86. What will be the output of the following C++ code? #include <iostream> using namespace std; class Game { float i, j; }; class OutscalGame { int x, y; public: OutscalGame (int a, int b) { x = a; y = b; } int result() { return x + y; } }; int main () { Game d; OutscalGame * padd; padd = (OutscalGame*) &d; cout<< padd->result(); return 0; }

87. What is the primary purpose of static_cast in C++?

88. What will be the output of the following C++ program? #include <iostream> class Outscal { public: void fun(); }; static void Outscal::fun() { std::cout<<"Welcome to Outscal Games."<<endl; } int main() { Outscal::fun(); return 0; }

89. Which of the following are correct about structure and class in C++? A) Class members are private by default, and members of a struct are public by default. B) When deriving a class from the base class, the default access specifier is public. C) When deriving a class from the base class, the default access specifier is private.

90. What is the role of static members and static member functions in a C++ interface?

91. What is a friend function in C++?

92. What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class TreasureChest { int capacity = 0; public: TreasureChest(int cap){ capacity = cap; } friend void revealTreasure(); }; void revealTreasure() { TreasureChest chest(10); cout << chest.capacity << endl; } int main() { revealTreasure(); }

93. In C++, can 'friend' functions access private members of a derived class?

94. Why it is important to write “using namespace std” in C++ program?

95. Consider the following C++ program and focus on the initialization of the object t3. What will be the output of the program when t3 is initialized? #include<iostream> using namespace std; class OutscalCoordinates { private: int x; int y; public: OutscalCoordinates(int i = 0, int j = 0); OutscalCoordinates(const OutscalCoordinates &t); }; OutscalCoordinates::OutscalCoordinates(int i, int j) { x = i; y = j; cout << "Coordinates of Player\n"; } OutscalCoordinates::OutscalCoordinates(const OutscalCoordinates &t) { y = t.y; cout << "Coordinates of Enemy\n"; } int main() { OutscalCoordinates *t1, *t2; t1 = new OutscalCoordinates(10, 15); t2 = new OutscalCoordinates(*t1); OutscalCoordinates t3 = *t1; return 0; }

96. What is true about the memory allocation for static variables in C++?

97. In C++, which real-life scenario correctly illustrates the concept of multiple inheritance?

98. How do constructors in base and derived classes affect an object’s initialization in C++?

99. Can more than one class be derived from a base class in C++?

100. What is the output of the following C++ code? #include<iostream> using namespace std; class Country{ private: int population; public: void setPopulation(int pop){ this->population = pop; } int getPopulation(){ return this->population; } }; class USA: private Country{ }; int main(){ USA obj; obj.setPopulation(100); cout<<obj.getPopulation(); return 0; }

101. What is the order in which constructors are called when creating an object of a derived class in C++?

102. Why is it important to mark the destructor of a base class as virtual in C++?

103. What is the key difference between const and static variables in C++?

104. Is it possible to define member variables inside an interface in C++?

105. Which of the following is the least safe type casting in C++?

1. Which of the following is possible in c++?

A. Virtual constructor

B. Virtual member variables

C. Virtual functions

D. All of the above

2. What is the difference between a deep copy and a shallow copy in C++?

A. A deep copy creates a completely new and independent copy of an object, while a shallow copy only copies the reference to an object

B. A shallow copy creates a completely new and independent copy of an object, while a deep copy only copies the reference to an object

C. A deep copy and a shallow copy are the same things

D. None of the above

3. Why pre-increment is faster than post-increment in C++?

A. The question statement is wrong, both take equal time

B. Former takes one-byte instruction while latter takes two bytes instruction

C. Former takes two bytes instruction while the latter takes one-byte instruction

D. None of the above

4. How many types of templates are there in C++?

A. 1

B. 2

C. 3

D. 4

5. What is the size of an object of an empty class in C++?

A. Sum of the size of all the variables within a class.

B. It is the largest size of the variable of the same class.

C. Sum of the size of all inherited variables along with the variables of the same class.

D. Size of an object of an empty class in C++ is at least 1 byte.

6. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ; strcpy( str3, str1); strcat( str1, str2); len = strlen(str1); cout << len << endl; return 0; }

A. 5

B. 55

C. 11

D. 10

7. Predict the output : 2 #include <bits/stdc++.h> using namespace std; void doo(vector<int> arr, int n) { for (int i = 0 ; i < n ; i++) arr[i] = arr[i] * 3; } int main() { vector<int> nums = {1, 2, 3, 4, 5}; doo(nums, 5); int sum = 0; for (int i = 0 ; i < 5 ; i++) sum += nums[i]; cout << sum << "\n"; return 0; }

A. 15

B. 45

C. 40

D. Error

8. What is the correct syntax for using smart pointers in C++? (Assume header files are already included)

A. std::unique_ptr<int> p(new int);

B. std::unique_ptr<int> p[new int];

C. std::unique_ptr<int> p = new int;

D. std::unique_ptr<int> p = new int[];

9. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int array[] = {0, 2, 4, 6, 7, 5, 3}; int n, result = 0; for (n = 0 ;n < 7 ;n++){ result += array[n]; } cout << result; return 0; }

A. 25

B. 26

C. 27

D. 24

10. What is the output of the below code? #include <bits/stdc++.h> int main() { char str[] = {'a', 'b', 'c'}; std::cout << str << '\n'; return 0; }

A. abc

B. abcgarbage

C. garbage

D. none of the above

11. Which of the following code is the correct syntax for creating a class in C++?

A. Class Test {};

B. class Test {}

C. class Test {};

D. Class Test {}

12. What is the correct way to initialize a vector in C++?

A. std::vector<integer> vecOfInts;

B. std::vector int <vecOfInts>

C. std::vector(int) vecOfInts;

D. std::vector<int> vecOfInts;

13. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { string str ("Microsoft"); for (size_t i = 0; i < str.length();) { cout << str.at(i-1); } return 0;

A. M

B. Microsoft

C. Micro

D. runtime error

14. Predict the output : 3 #include <bits/stdc++.h> using namespace std; void doo(int arr[], int n) { for (int i = 0; i < n; i++) arr[i] = arr[i] * 3; } int main() { int nums[5] = {1, 2, 3, 4, 5}; doo(nums, 5); int sum = 0; for (int i = 0; i < 5 ; i++) sum += nums[i]; cout << sum << "\n"; return 0; }

A. 15

B. 45

C. 40

D. Error

15. What is the syntax for printing a message in c++?

A. cout<<”message”;

B. cin>>"message";

C. print(”message”);

D. cout(”message”);

16. When we create an object of a derived class in c++, then?

A. Derived class constructor is called first the base class constructor.

B. Base class constructor is called first then derived class constructor.

C. Base class constructor will not be called.

D. None of the above

17. Which of the following are the correct syntax for creating templates in C++? 1. template<class T> 2. template<typename T> 3. <template T> 4. template<class T, class U>

A. 1, 2

B. 1, 4

C. 1, 2, 4

D. 3

18. What is true about his statement in C++? std::vector<int> vecInts(5);

A. Initialize a Vector with 5 int & all default value is 0

B. Initialize a Vector with 5 int

C. Initialize a Vector with 5 int & all default values will be garbage

D. Initialize a Vector with 5 int & All default value with be -1

19. What is the time complexity of concatenation of string in C++? // E.g find time complexity of below code. string str = "outscal"; str += "clan";

A. O(1)

B. O(M) → M is the length of string to be concatenated

C. O(N) → N is the length of the original string

D. Unspecified, but generally linear

20. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[][] = {{1,2},{3,4}}; int i, j, sum = -10; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) sum += arr[i][j]; cout << sum; return 0; }

A. 0

B. 10

C. Garbage Value

D. Compilation error

21. Which of the following is a data type in c++?

A. int

B. float

C. double

D. all of the above

22. Consider the following definition in C++ programming language. struct node { int data; struct node * next; } typedef struct node NODE; NODE *ptr; Which of the following c code is used to create new node?

A. ptr = (NODE*)malloc(sizeof(NODE));

B. ptr = (NODE*)malloc(NODE);

C. ptr = (NODE*)malloc(sizeof(NODE*));

D. ptr = (NODE)malloc(sizeof(NODE));

23. Predict the output: #include <bits/stdc++.h> using namespace std; int main() { char str[] = "GAME DEVELOPER"; char* token = strtok(str, " "); while (token != NULL) { cout << token << '\n'; token = strtok(NULL, " "); } return 0; } a)GAME DEVELOPER b)GAMEDEVELOPER c)GAME d)GAME DEVELOPER

A. a)

B. b)

C. c)

D. d)

24. Predict the output: 1 #include <bits/stdc++.h> void fun(int n) { if (n == 0) return; std::cout << n%2; fun(n/2); } int main() { fun(25); return 0; }

A. 10011

B. 11111

C. 10111

D. 11001

25. Predict the output: #include <bits/stdc++.h> using namespace std; int solve(vector<int>& nums) { int c = max(0,nums[0]); int m = nums[0]; for(int i=1;i<nums.size();i++){ c += nums[i]; c = max(0, c); m = max(m, c); } return m; } int main() { vector<int> arr1 = {-1, -2, -3, -4, -9}, arr2 = {-1, 1, 3, -1, 5, -2}; cout << solve(arr1) + solve(arr2); return 0; }

A. Compilation error

B. 8

C. 0

D. 5

26. Predict the output: #include <bits/stdc++.h> using namespace std; int solve(int n) { int i, j, arr[n], k; arr[0] = 1; for (i = 1; i < n; i++) { arr[i] = 0; for (j = 0, k = i - 1; j < i; j++, k--) arr[i] += arr[j] * arr[k]; } return arr[n - 1]; } int main() { cout << solve(8); return 0; }

A. 42

B. 132

C. 429

D. 1430

27. Is it possible to initialize any Vector with an Array in C++?

A. Yes

B. No

28. What will be the output of the following C++ code? #include <iostream> #include <vector> using namespace std; int main () { vector<int> first; first.assign (7,100); vector<int>::iterator it; it=first.begin()+1; int myints[] = {1776,7,4}; cout << int (first.size()) << '\n'; return 0; }

A. 10

B. 9

C. 8

D. 7

29. What is the output of the below code? // stoi is used to convert string into integer #include <bits/stdc++.h> using namespace std; int main() { string s = "0x10A"; cout << stoi(s, nullptr, 16) << endl; return 0; }

A. Compilation error

B. Garbage Value

C. 266

D. 1010

30. What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; int main() { string str {"Steve jobs"}; unsigned long int found = str.find_first_of("aeiou"); while (found != string :: npos) { str[found] = '*'; found = str.find_first_of("aeiou", found + 1); } cout << str << "\n"; return 0; }

A. Steve

B. jobs

C. St*v* j*bs

D. St*v*

31. Predict the output: #include <bits/stdc++.h> using namespace std; template <typename T> int solve(const vector<T> &a) { vector<T> u; for (const T &x : a) { auto it = lower_bound(u.begin(), u.end(), x); if (it == u.end()) u.push_back(x); else *it = x; } return (int)u.size(); } int main() { vector<int> arr{1, 2, 3, 4, 4, 5, 5, 7. 8. 9, 6, 7, 11}; cout << solve(arr); return 0; }

A. 9

B. 10

C. 13

D. Error

32. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3}; cout << arr[3] << " " << arr[-1] << '\n'; return 0; }

A. Compilation error

B. Runtime Error

C. 0 0

D. Garbage Value Garbage Value

33. What is the purpose of a header file in C++ programming?

A. To contain the implementation code of functions

B. To include code from other scripts containing necessary declarations

C. To define the behaviour of classes and objects

D. To handle input/output operations

34. You must have seen the #pragma once directive many times while working with C++ header files. Ever wondered what it does? What's the significance of the #pragma once directive?

A. It acts as a header file guard, preventing the same header file from being included multiple times in a single compilation unit.

B. It triggers compiler optimizations, improving the compilation speed by eliminating redundant includes.

C. It allocates a fixed memory segment for header file contents, ensuring efficient memory utilization.

D. It enforces modular coding practices, adhering to strict standards and preventing code duplication.

35. What will be the output of the following C++ code? #include <iostream> using namespace std; class OutscalRectangle { int x, y; public: void setDimensions(int, int); int calculateArea() { return (x * y); } }; void OutscalRectangle::setDimensions(int a, int b) { x = a; y = b; } int main() { OutscalRectangle rectangle; rectangle.setDimensions(3, 4); cout << "The area of the rectangle is: " << rectangle.calculateArea(); return 0; }

A. The area of the rectangle is: 24

B. The area of the rectangle is: 12

C. There is a compile error because OutscalRectangle is used as both a class name and a variable name.

D. The area of the rectangle is: 56

36. What is the difference between the pointer declarations int* numPtr; and int *numPtr; in C++?

A. int* numPtr; declares a pointer to an integer, whereas int *numPtr; declares an integer that is being dereferenced.

B. There is no difference; both statements declare a pointer to an integer.

C. int* numPtr; declares a normal integer, while int *numPtr; declares a pointer to an integer.

D. int* numPtr; declares two pointers to integers, whereas int *numPtr; declares one pointer to an integer.

37. What happens if you try to dereference a null pointer in C++? #include <iostream> int main() { int* ptr = nullptr; std::cout << "Dereferencing a null pointer: " << *ptr << std::endl; return 0; }

A. It returns the memory address of the null pointer.

B. It leads to undefined behavior, possibly resulting in a program crash.

C. It automatically assigns a value of zero to the pointer.

D. It triggers a compile-time error.

38. What role does the main() function play in a C++ program?

A. main() is optional in a C++ program.

B. main() is where the program execution begins.

C. main() cannot accept any arguments.

D. main() must always return a value.

39. What is the primary purpose of cout in C++?

A. It is a function used for getting user input.

B. It is used to display output to the console.

C. It is a keyword to declare variables.

D. It stands for "character output" in C++.

40. What purpose does the semicolon (;) serve at the end of most lines in C++ code?

A. It signifies the end of the program.

B. It separates multiple statements within a loop.

C. It's a syntactical requirement to terminate statements.

D. It improves code readability for developers.

41. Consider the following code snippet: int main() { // Single line comment /* Multi-line comment */ return 0; } Which statement about comments in C++ is accurate?

A. Multi-line comments cannot be nested within single-line comments.

B. Both single-line and multi-line comments can be nested within each other.

C. Comments can be used as a substitute for writing proper code documentation.

D. Comments have no impact on the compiled code size.

42. What is the correct syntax to write comments in C++? 1. # This is a comment 2. / This is a comment 3. // This is a comment 4. /* This is a new comment */

A. 1

B. 2

C. 3

D. 3, 4

43. Consider the following C++ function int fun(int n) { int i, j; for(i = 1; i<= n; i++) { for(j=1; j<n; j += i) { cout<<i<<j; } } } Time complexity of fun in terms of θ notation is?

A. Θ(n√n)

B. Θ(n^2)

C. Θ(nlogn)

D. Θ(n^2logn)

44. What will be the output of the following C++ code? #include<iostream> int main() { const int a=10; a++; std::cout<<a; return 0; }

A. 10

B. 11

C. Compilation Error

D. Syntax Error

45. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3,4,5}; int sum = 10; for (int i = 0 ; i < 5 ; i++) { sum += arr[i]; sum -= sum; } cout << sum; return 0; }

A. 10

B. 0

C. 15

D. 25

46. Predict the output: #include <bits/stdc++.h> using namespace std; int main() { char str[] = "abc"; cout << sizeof(str) << " " << strlen(str) << '\n'; return 0; }

A. 3 3

B. 4 3

C. 3 4

D. 4 4

47. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main () { string str ("microsoft"); string::reverse_iterator r; for (r = str.rbegin() ; r < str.rend(); r++ ) cout << *r; return 0; }

A. microsoft

B. micro

C. tfosorcim

D. tfos

48. What is the output of the below code? #include <bits/stdc++.h> int main() { char str[] = "abc"; std::cout << str << '\n'; return 0; }

A. abc

B. abcgarbage

C. garbage

D. none of the above

49. What is the function of \n as a line terminator in C++?

A. It denotes the beginning of a function block.

B. It indicates the start of a new line in the console output.

C. It's used for commenting out code lines.

D. It terminates the program's execution.

50. Which of the following statements about variable declaration in C++ is NOT true?

A. A variable must be declared before it can be used.

B. Variable names can start with an underscore (_).

C. The declaration and initialization of a variable must occur in the same statement.

D. Variable declarations specify the data type and name of the variable.

51. What is the size of an empty class in C++?

A. Sum of the size of all the variables within a class.

B. The size of the class is the largest size of the variable of the same class.

C. Sum of the size of all inherited variables along with the variables of the same class.

D. Atleast 1 byte

52. What is the output of the below code? #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1,2,3,4,5,6}; cout << -5[arr]; return 0; }

A. Compilation error

B. Garbage Value

C. -5

D. -6

53. What should be filled in place of _______ to run the below code without any error? #include <bits/stdc++.h> using namespace std; int main() { string s1 = "oustcalclan"; string s2 = "clann"; size_t pos = s1.find(s2); if (pos == _______) cout << "Not Found" << '\n'; else cout << "Found" << '\n'; return 0; }

A. -1

B. string::size_t

C. s1.length()

D. string::npos

54. Which of the following is correct about structure and class in C++? 1. Class members are private by default, and members of a struct are public by default. 2. When deriving a class from the base class, the default access specifier is public. 3. When deriving a class from the base class, the default access specifier is private.

A. 1

B. 2

C. 1, 2

D. 1, 3

55. How Encapsulation is achieved in C++?

A. by using private members

B. by using protected Members

C. by using access specifiers

D. by using the concept of abstraction

56. Predict the time complexity of the below code: #include <bits/stdc++.h> using namespace std; int dp[100][100]; int matrixChainMemoised(int *p, int i, int j) { if (i == j) { return 0; } if (dp[i][j] != -1) { return dp[i][j]; } dp[i][j] = INT_MAX; for (int k = i; k < j; k++) { dp[i][j] = min( dp[i][j], matrixChainMemoised(p, i, k) + matrixChainMemoised(p, k + 1, j) + p[i - 1] * p[k] * p[j] ); } return dp[i][j]; } int main() { int arr[] = {1, 2, 3, 4}; int n = sizeof(arr) / sizeof(arr[0]); memset(dp, -1, sizeof dp); cout << matrixChainMemoised(arr, 1, n-1); return 0; }

A. O(n)

B. O(n^2)

C. O(n^2 log n)

D. O(n^3)

57. Which of the following is the correct way of defining a function in C++? 1. void fun() {} 2. void function(int a, int b) {} 3. int func() { return -1; } 4. int func(double x) { return 0.0; }

A. 1, 2, 4

B. 1, 2, 3, 4

C. 2, 3

D. 1, 2, 3

58. What is causing unpredictable behaviour on compilation error in the following C++ code snippet? int arr[5] = { 1,2,3,4,5 }; cout<< endl << arr[5]<<endl;

A. There is no error; the code will print the last element of the array.

B. The array is initialized with too many elements.

C. The syntax for printing the array element is incorrect.

D. The code tries to access an element outside the bounds of the array.

59. What is the difference between a class template and a function template in C++?

A. A class template is a blueprint for creating objects, while a function template is a blueprint for creating functions

B. A class template is a blueprint for creating functions, while a function template is a blueprint for creating objects

C. Both are the same

D. None of the above

60. Which of the following C++ line correctly creates an instance of the Player class named player?

A. Player player = new Player();

B. Player player();

C. Player player;

D. new Player player;

61. What is the purpose of a destructor in C++?

A. To initialize objects

B. To allocate memory

C. To destroy objects and free resources

D. To declare variables

62. How is Abstraction achieved in C++?

A. By using private members, ensuring that data is accessible only within the class itself.

B. By using access specifiers, such as private, protected, and public, to control the visibility of class members.

C. By hiding complex implementation details and exposes only essential features.

D. By creating getter and setter methods for class variables, allowing controlled access to private data.

63. Why is it important to separate code into header (.h) and source (.cpp) files?

A. To make the program smaller by removing parts that are not used.

B. To keep things organized by using headers to list what the program does and using source files to show how it does those things.

C. To help automatically write detailed notes about the program.

D. To make the program start faster by preparing some parts of it in advance.

64. Consider the line of code int *ptr = new int;. What does this statement achieve in C++ and what are the implications of using it?

A. It declares a pointer variable ptr and allocates memory for an integer, initializing it with a default value.

B. It creates a pointer ptr that directly assigns a new integer value from the heap, preventing memory leaks.

C. It allocates memory for an integer on the heap and assigns the address of this memory to the pointer ptr.

D. It initializes the pointer ptr with a random memory address and then tries to assign an integer value.

65. What is the difference between using new and malloc for dynamic memory allocation in C++?

A. There is no difference; they both perform the same task.

B. new automatically calls the constructor for objects, while malloc does not.

C. malloc is used for arrays, while new is used for single objects.

D. new allocates memory on the heap, while malloc allocates memory on the stack.

66. What is the purpose of using namespaces in C++?

A. To increase compile times.

B. To reduce dependency.

C. To organize code into logical groups.

D. To introduce bugs in the code.

67. What is "scope resolution" in the context of namespaces, and how is it used in C++?

A. Defines visibility boundaries for functions.

B. Manages memory for objects in namespaces.

C. Specifies which namespace an identifier belongs to, resolving name conflicts.

D. Optimizes program execution efficiency.

68. What will be the output of the following C++ program? #include <iostream> using namespace std; void counter() { static int count = 0; cout << count << " "; count++; } int main() { for (int i = 0; i < 5; i++) { counter(); } return 0; }

A. 0 0 0 0 0

B. 0 1 2 3 4

C. 1 2 3 4 5

D. 5 5 5 5 5

69. What will be the output of the following C++ program? #include <iostream> using namespace std; void process() { static int value = 10; static int* ptr = &value; cout << *ptr << " "; (*ptr)++; cout << *ptr << endl; } int main() { process(); process(); return 0; }

A. 10 10 10 10

B. 10 11 11 12

C. 10 10 11 11

D. Compilation Error

70. What is the output of the following C++ code? #include <iostream> using namespace std; class Game{ public: virtual void func(){ cout<<"Welcome to Game\n"; } }; class OutscalGame: public Game{ public: void func(){ cout<<"Welcome to Outscal Game\n"; } }; int main() { OutscalGame ob; ob.func(); return 0; }

A. Welcome to Outscal Game

B. Welcome to Game

C. Welcome to Game Welcome to Outscal Game

D. Welcome to Outscal Game Welcome to Game

71. How do interfaces and abstract classes differ in defining blueprints for derived classes in C++?

A. Interfaces specify method declarations for derived classes to implement, while abstract classes can also include method implementations.

B. Interfaces allow for multiple inheritance, whereas abstract classes are limited to single inheritance.

C. Abstract classes impose stricter requirements on derived classes, while interfaces offer more flexibility.

D. Both serve the purpose of defining blueprints for derived classes, with interfaces focusing on method declarations and abstract classes allowing method implementations.

72. Consider the following code snippet: cppCopy code #include <iostream>class IShape { public: virtual void draw() const = 0; }; class Circle : public IShape { public: void draw() const override { std::cout << "Drawing a circle." << std::endl; } }; class Square : public IShape { public: void draw() const override { std::cout << "Drawing a square." << std::endl; } }; int main() { const IShape* shapes[] = {new Circle(), new Square()}; for (const auto& shape : shapes) { shape->draw(); } return 0; } What will be the output of the program?

A. Drawing a circle. Drawing a square.

B. Drawing a square. Drawing a circle.

C. Drawing a circle. Drawing a circle.

D. Compilation Error.

73. What will be the output of the following C++ code? #include <iostream> using namespace std; class OutscalGames { public: static int value; OutscalGames() { value += 5; } void display() const { cout << "Value: " << value << endl; } }; int OutscalGames::value = 50; int main() { const OutscalGames obj; obj.display(); return 0; }

A. Value: 50

B. Value: 55

C. Compilation error

D. Runtime error

74. What will be the output of the following C++ code? #include <iostream> class Game { public: virtual void print() const { std::cout << "Game Called" << std::endl; } }; class OutscalGame : public Game { public: void print(){ std::cout << "Outscal Game Called" << std::endl; } }; int main() { const Game* ptr = new OutscalGame(); ptr->print(); delete ptr; return 0; }

A. Game Called

B. Outscal Game Called

C. Game Called Outscal Game Called

D. Outscal Game Called Game Called

75. What issue can arise due to naming conflicts in C++?

A. Reduced code readability.

B. Increased compile times.

C. Global scope pollution.

D. Improved modularity.

76. What is the primary effect of the static keyword when applied to a variable inside a function in C++?

A. It makes the variable persistent between multiple function calls.

B. It restricts the variable's scope to the inside of the block where it's declared.

C. It initializes the variable to zero.

D. It protects the variable from being modified by other functions.

77. How does the static keyword helps in implementing the Singleton design pattern in C++?

A. By ensuring that multiple instances of a class can be created.

B. By enabling lazy initialization of the class’s unique instance.

C. By allowing the class instance to be duplicated using the copy constructor.

D. By providing thread-local storage for the Singleton instance.

78. What is Inheritance in C++?

A. Wrapping of data into a single class

B. Deriving new classes from existing classes

C. Overloading of classes

D. Classes with same names

79. How many Access specifier are there in C++?

A. 1

B. 2

C. 3

D. 4

80. In a game developed using with C++, you have a base class Enemy and derived classes Orc and Troll. Both Orc and Troll share some methods from another class Knife. How can you design this inheritance to maintain a clean structure and avoid duplication

A. Use multiple inheritance to inherit from both Enemy and Knife.

B. Use multilevel inheritance with Knife as the base, then Enemy, and then Orc and Troll.

C. Use hierarchical inheritance with Enemy as the base class and Orc and Troll as derived classes. Use association for Knife.

D. Use single inheritance with Enemy as the base class and create separate classes for Orc and Troll without any shared behaviour.

81. What is the primary issue caused by the diamond problem in C++ inheritance?

A. Namespace pollution

B. Multiple copies of the base class are created in the derived class

C. Memory leaks

D. Increased compilation time

82. Can a virtual function be overloaded in C++?

A. Yes, but only if it's defined in the base class.

B. No, it violates the principles of polymorphism.

C. Yes, but only if it's defined in the derived class.

D. No, virtual functions cannot be overloaded.

83. How is the delta time used in the implementation of the moveDown() method in EnemyController.cpp?

A. It is added to the movement speed

B. It is subtracted from the movement speed

C. It is multiplied by the movement speed

D. It is divided by the movement speed

84. Considering the standard practice for naming interfaces in C++, how would you name the interface for a class representing a geometric shape?

A. IShape

B. ShapeInterface

C. GeometricShapeInterface

D. AbstractShape

85. Which of the following is used to implement the C++ interfaces

A. absolute variables

B. abstract classes

C. constant variables

D. default variables

86. What will be the output of the following C++ code? #include <iostream> using namespace std; class Game { float i, j; }; class OutscalGame { int x, y; public: OutscalGame (int a, int b) { x = a; y = b; } int result() { return x + y; } }; int main () { Game d; OutscalGame * padd; padd = (OutscalGame*) &d; cout<< padd->result(); return 0; }

A. 20

B. runtime error

C. random number

D. runtime error or random number

87. What is the primary purpose of static_cast in C++?

A. It performs dynamic type checking at runtime.

B. It facilitates implicit type conversions between compatible data types.

C. It ensures type safety by preventing invalid type conversions.

D. It allows explicit type conversions between related data types at compile time.

88. What will be the output of the following C++ program? #include <iostream> class Outscal { public: void fun(); }; static void Outscal::fun() { std::cout<<"Welcome to Outscal Games."<<endl; } int main() { Outscal::fun(); return 0; }

A. fun() is static

B. Empty Screen

C. Compiler Error

89. Which of the following are correct about structure and class in C++? A) Class members are private by default, and members of a struct are public by default. B) When deriving a class from the base class, the default access specifier is public. C) When deriving a class from the base class, the default access specifier is private.

A. A

B. B

C. A, B

D. A, C

90. What is the role of static members and static member functions in a C++ interface?

A. Providing access to class-specific data and behaviors.

B. Allowing for dynamic binding of function calls.

C. Facilitating integration of multiple inheritance.

D. Defining common behaviors through abstract methods.

91. What is a friend function in C++?

A. A function which can access all the private, protected and public members of a class

B. A function which is not allowed to access any member of any class

C. A function which is allowed to access public and protected members of a class

D. A function which is allowed to access only public members of a class

92. What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class TreasureChest { int capacity = 0; public: TreasureChest(int cap){ capacity = cap; } friend void revealTreasure(); }; void revealTreasure() { TreasureChest chest(10); cout << chest.capacity << endl; } int main() { revealTreasure(); }

A. 10

B. 0

C. No Output

D. Compilation Error

93. In C++, can 'friend' functions access private members of a derived class?

A. Yes, always.

B. No, never.

C. Only if the derived class explicitly declares the base class as a friend.

D. Only if the derived class is declared as a friend within the base class.

94. Why it is important to write “using namespace std” in C++ program?

A. To ensure that all features of the C++ Standard Library are accessible without needing to prefix them with std::.

B. To allow direct access to the features of the C++ Standard Library, like those in <iostream>, <vector>, and more, without having to use the std:: prefix, improving code brevity and readability.

C. To automatically include all headers from the C++ Standard Library without writing multiple #include directives.

D. To increase the performance of the program by pre-loading all standard library functions into the global namespace.

95. Consider the following C++ program and focus on the initialization of the object t3. What will be the output of the program when t3 is initialized? #include<iostream> using namespace std; class OutscalCoordinates { private: int x; int y; public: OutscalCoordinates(int i = 0, int j = 0); OutscalCoordinates(const OutscalCoordinates &t); }; OutscalCoordinates::OutscalCoordinates(int i, int j) { x = i; y = j; cout << "Coordinates of Player\n"; } OutscalCoordinates::OutscalCoordinates(const OutscalCoordinates &t) { y = t.y; cout << "Coordinates of Enemy\n"; } int main() { OutscalCoordinates *t1, *t2; t1 = new OutscalCoordinates(10, 15); t2 = new OutscalCoordinates(*t1); OutscalCoordinates t3 = *t1; return 0; }

A. Coordinates of Player

B. Coordinates of Enemy

C. No output as t3 is a default constructed object.

D. Compilation error due to incorrect usage of pointers.

96. What is true about the memory allocation for static variables in C++?

A. static variables are stored in a special area of memory called the stack.

B. static variables are stored in the heap and persist until the program ends.

C. static variables are stored in the same area as global variables, and they persist for the lifetime of the program.

D. static variables are allocated and deallocated by the programmer using new and delete.

97. In C++, which real-life scenario correctly illustrates the concept of multiple inheritance?

A. Fish Class: A Fish class inherits from Ocean and Swimmer classes

B. Electric Car Class: A Electric Car class inherits from both Vehicle and Electronics classes.

C. Motorboat Class: A Motorboat class inherits from Boat and Water classes,

D. Library Class: A Library class inherits from Book and Patron classes

98. How do constructors in base and derived classes affect an object’s initialization in C++?

A. Constructors of a derived class replace constructors from the base class, erasing the base class's properties.

B. The constructor of the base class must always be called explicitly in the derived class's constructor.

C. The base class constructor is called automatically before the derived class's constructor to ensure the whole object is properly initialized.

D. Constructors are not inherited; thus, the base class's constructor is ignored.

99. Can more than one class be derived from a base class in C++?

A. True

B. False

100. What is the output of the following C++ code? #include<iostream> using namespace std; class Country{ private: int population; public: void setPopulation(int pop){ this->population = pop; } int getPopulation(){ return this->population; } }; class USA: private Country{ }; int main(){ USA obj; obj.setPopulation(100); cout<<obj.getPopulation(); return 0; }

A. 100

B. 0

C. Compilation Error

D. Garbage Value is printed

101. What is the order in which constructors are called when creating an object of a derived class in C++?

A. The derived class constructor is called first, then the base class constructor.

B. The base class constructor is called first, then the derived class constructor.

C. Constructors are called in alphabetical order of the class names.

D. The order of constructor calls is determined at runtime.

102. Why is it important to mark the destructor of a base class as virtual in C++?

A. To improve code readability

B. To allow for function overloading

C. To ensure proper cleanup of resources in derived classes

D. To facilitate dynamic binding

103. What is the key difference between const and static variables in C++?

A. const variables cannot be modified after initialization, while static variables retain their value across function calls.

B. const variables can only be accessed within the same source file, while static variables can be accessed globally.

C. const variables are stored in read-only memory, while static variables are stored in the heap.

D. const variables are initialized at compile-time, while static variables are initialized at runtime.

104. Is it possible to define member variables inside an interface in C++?

A. Yes

B. Yes, if they are static

C. Yes, if they are const

D. No

105. Which of the following is the least safe type casting in C++?

A. static_cast

B. const_cast

C. reinterpret_cast

D. dynamic_cast

Introduction

Whether you're a beginner to learn the fundamentals or an experienced developer looking to brush up on your skills, our MCQs on C++ offer an interactive way to test your understanding of concepts.


What is C++

C++ is the most common and important programming language. ⁤⁤The programming language was invented by Bjarne Stroustrup in 1979. ⁤⁤The main purpose of designing C++ is for system programming to increase performance, efficiency, and flexibility. C++ supports procedural, object-oriented, game development, and generic programming which makes this programming language versatile for various applications. ⁤⁤One of the main key features of C++ is it is rich in libraries. ⁤⁤C++ also supports the low level of memory manipulation which becomes very important for developing operating systems and game engines.


Class and Objects present in C++

  1. Class is considered a user-defined data type that contains data members and member functions. A class works as a blueprint for the object.
  2. Object is an instance of a class.

When a class is defined without an object, no memory is allocated to it. Once an object of the class is created, memory will be allocated.


Access Specifier in C++

In C++, access specifiers are keywords that define the accessibility of members (attributes and methods) of a class. There are three primary access specifiers:

  • Public
  • Private
  • Protected

About Variables in C++

Variables are the identifiers that showcase data storage locations in computer memory. Variables can be defined based on scope, storage duration, and linkage.


Advantages of C++ MCQs

The main advantages of C++ MCQs questions are as follows:

  • Knowledge Enhancement
  • Self-Assessment
  • Preparation for Exams or Interviews
  • Quick Revision
  • Interactive Learning
  • Accessibility

Conclusion

We hope this collection of C++ Multiple Choice Questions has been a valuable resource in your learning journey. Keep practicing and exploring C++ mcq questions and answers to solidify your understanding.