C++ Practice Questions
Topics Covered:
C++ Variables, Data Types, Functions, Classes, Loops, STL, and more
30 Questions
Practice Questions
1. What will be the output of the following C++ code?
#includeusing namespace std; int main(){ int i = 1; while(true){ if(i % 3 == 0) break; cout << i << " "; i++; } return 0; }
2. Which of the following symbols is used for single-line comments in C++?
3. What are the outputs of the following C++ code snippet?
#include#include using namespace std; int main(){ cout << pow(2, pow(3,2)) << endl; cout << pow(pow(2,3),2) << endl; return 0; }
4. What is the output of the following C++ code snippet?
#include#include
5. Which of the following lambda expressions correctly returns true for negative numbers in C++?
6. What is the output of the following C++ code?
#includeusing namespace std; class Human { public: string human_name; Human(string name): human_name(name) { } string getHumanName() { return human_name; } virtual bool isEmployee() { return false; } }; class IBEmployee : public Human { public: string emp_id; IBEmployee(string name, string id): Human(name), emp_id(id) { } bool isEmployee() override { return true; } string get_emp_id() { return emp_id; } }; int main(){ IBEmployee employee("Mr Employee", "IB007"); cout << employee.getHumanName() << " " << employee.isEmployee() << " " << employee.get_emp_id(); return 0; }
7. Which function is traditionally used in C++ to obtain the current time (as a time_t value representing seconds since the Unix epoch)?
8. In C++ file I/O, which of the following should you check to ensure the file was successfully opened?
9. Which of the following statements about C++ namespaces is false?
10. Which of the following statements about std::pow in C++ is correct?
11. Which method is used to check if a std::vector is empty?
12. What is the output of the following C++ code?
#includeusing namespace std; class X { private: int num1; public: int num2; X(): num1(5), num2(2) { } void display_values() { cout << num1 << " " << num2; } }; class Y: public X { private: int num1; public: Y(): X(), num1(1) { num2 = 6; } }; int main(){ Y obj; obj.display_values(); return 0; }
13. What is the key difference between std::vector and std::array in C++?
14. Which of the following statements about C++ constructors is false?
15. Given two vectors:
vector<char> list1 = {'s','r','a','s'}
and vector<char> list2 = {'a','a','n','h'}
, what is the result of concatenating corresponding elements into a vector of strings?
16. Which C++ library is commonly used for object serialization?
17. Which STL algorithm can be used to count the number of elements in a vector that are multiples of 5?
18. What is the output of the following recursive C++ code?
#includeusing namespace std; int test(int x){ if(x == 0) return 0; return x + test(x - 1); } int main(){ cout << test(5); return 0; }
19. Given
vector<int> list1 = {3,4,5,2,1,0}
, what is the content of list1
after performing list1.erase(list1.begin() + 1)
?
20. Which build system is commonly used for C++ projects?
21. Consider the following C++ function with a default argument:
#include(Assume that each call uses a fresh default value.)#include using namespace std; vector add_items(int x, vector items = vector ()){ items.push_back(x); return items; } int main(){ vector a = add_items(1); vector b = add_items(2); // Suppose we print the contents of a and b. // What are the expected outputs? return 0; }
22. Which of the following is the correct syntax for defining a function in C++?
23. What is the result of the expression
4 + 3 * 2
in C++?
24. Which of the following is NOT a characteristic of C++?
25. Which of the following declarations defines an immutable integer in C++?
26. What will be the output of the following C++ code snippet involving pointer arithmetic?
#includeusing namespace std; int main(){ int arr[] = {10, 20, 30, 40}; int *p = arr; cout << *(p + 2); return 0; }
27. What is the maximum possible length of an identifier in C++?
28. What is the output of the following C++ code that uses a lambda with std::transform?
#include#include #include using namespace std; int main(){ vector x = {0,1,4,9,16}; vector y; transform(x.begin(), x.end(), back_inserter(y), [](int a){ return a + 2; }); for(auto i : y) cout << i << " "; return 0; }
29. Which of the following variable declarations defines a mutable integer in C++?
30. What will be the output of the following C++ code that uses a static variable?
#includeusing namespace std; void func(){ static int count = 0; count++; cout << count << " "; } int main(){ func(); func(); func(); return 0; }