























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























































































Question 1. Which header must be included to use std::cout and std::cin? A) B) C) D) Answer: A Explanation: \ defines the standard stream objects std::cout and std::cin. Question 2. What is the return type of the main function in a standard C++ program? A) void B) int C) char D) long Answer: B Explanation: By convention, main returns an int to indicate the program’s exit status to the operating system. Question 3. Which of the following declares a constant integer with value 10? A) const int MAX = 10; B) #define MAX 10 C) int const MAX = 10; D) All of the above Answer: D
Explanation: All three forms create a constant integer named MAX with value 10; the first two are preferred in modern C++. Question 4. What is the result of the expression 5 / 2 in C++ when both operands are of type int? A) 2. B) 2 C) 3 D) Compilation error Answer: B Explanation: Integer division truncates the fractional part, yielding 2. Question 5. Which operator has the highest precedence? A) + (addition) B) * (multiplication) C) == (equality) D) && (logical AND) Answer: B Explanation: Multiplication (*) has higher precedence than addition (+), equality (==), and logical AND (&&). Question 6. Which of the following statements correctly performs a static cast from double to int? A) int x = static_cast(3.14); B) int x = (int)3.14; C) int x = reinterpret_cast(3.14); D) int x = const_cast(3.14); Answer: A
D) if (x > 5 ; x < 10) Answer: A Explanation: The logical AND operator (&&) requires both conditions to be true. Question 10. In a switch statement, which keyword is used to exit the switch block early? A) exit B) break C) continue D) return Answer: B Explanation: break terminates the current case and exits the switch. Question 11. Which loop guarantees that its body is executed at least once? A) for B) while C) do-while D) range-based for Answer: C Explanation: The condition of a do-while loop is evaluated after the loop body runs. Question 12. What does the continue statement do inside a loop? A) Terminates the loop entirely B) Skips the rest of the current iteration and proceeds to the next
C) Jumps to the beginning of the program D) Causes a compile-time error Answer: B Explanation: continue bypasses remaining statements in the loop body and proceeds with the next iteration. Question 13. Which of the following is a valid function prototype? A) void foo(int a, double b); B) int foo(int a double b); C) void foo(int a; double b); D) foo void(int a, double b); Answer: A Explanation: A prototype consists of the return type, function name, and a comma-separated parameter list. Question 14. When a function parameter is declared as int &ref, how is the argument passed? A) By value B) By reference C) By pointer D) By copy-elision Answer: B Explanation: The ampersand indicates a reference parameter, allowing the function to modify the original argument. Question 15. Which of the following allows a function to be called with fewer arguments than parameters? A) Overloading
Question 18. Which storage class gives a variable internal linkage, limiting its visibility to the current translation unit? A) extern B) static C) auto D) register Answer: B Explanation: static at namespace scope gives internal linkage; the variable cannot be accessed from other .cpp files. Question 19. What is the base case in a recursive function? A) The case that calls itself again B) The condition that stops further recursive calls C) The first call of the function D) The case that uses global variables Answer: B Explanation: The base case provides a terminating condition, preventing infinite recursion. Question 20. Which declaration defines a pointer to a function that takes an int and returns a double? A) double (*fp)(int); B) double fp(int); C) double fp(int); D) double fp(int *); Answer: A Explanation: The syntax double (*fp)(int) declares fp as a pointer to a function taking an int and returning double.
Question 21. Which lambda expression captures all external variables by value? A) &{ /.../ } B) ={ /.../ } C) this{ /.../ } D) { /.../ } Answer: B Explanation: The capture list [=] copies all used external variables into the lambda. Question 22. How many elements does the array int arr[5] = {1,2,3}; contain? A) 3 B) 5 C) 2 D) Undefined Answer: B Explanation: The array has a fixed size of 5; the remaining elements are zero-initialized. Question 23. Which expression correctly accesses the third element of a two-dimensional array int mat[4][5];? A) mat[2][3] B) mat[3][2] C) *(mat + 2 + 3) D) mat[2][3] is the fourth row, fourth column Answer: B
Answer: A Explanation: int &ref = x; binds ref as an alias to x. Question 27. Which of the following is true about a dangling pointer? A) It points to a valid memory location. B) It points to memory that has been deallocated. C) It is automatically set to nullptr after delete. D) It can be safely dereferenced. Answer: B Explanation: After delete, a pointer still holds the old address, creating a dangling pointer; dereferencing it leads to undefined behavior. Question 28. Which smart pointer provides exclusive ownership of a dynamically allocated object? A) std::shared_ptr B) std::weak_ptr C) std::unique_ptr D) std::auto_ptr Answer: C Explanation: std::unique_ptr enforces sole ownership; the object is destroyed when the unique_ptr goes out of scope. Question 29. Which statement correctly creates a shared pointer to an int initialized with value 42? A) std::shared_ptr p = std::make_shared(42); B) std::shared_ptr p(new int(42)); C) std::shared_ptr p = std::shared_ptr(42); D) Both A and B are acceptable.
Answer: D Explanation: Both make_shared and direct new are valid ways to construct a shared_ptr; make_shared is preferred for efficiency. Question 30. Which access specifier makes class members accessible only within the class and its friends? A) public B) protected C) private D) internal Answer: C Explanation: private members are hidden from all code except the class itself and its friend functions/classes. Question 31. Which of the following correctly declares a class named Point with a public constructor taking two doubles? A) class Point { public: Point(double x, double y); }; B) class Point { private: Point(double x, double y); }; C) struct Point { Point(double x, double y); }; D) class Point { protected: Point(double x, double y); }; Answer: A Explanation: The constructor is declared in the public section, making it accessible to create objects. Question 32. What is the purpose of a copy constructor? A) To destroy an object B) To create a new object as a copy of an existing one C) To allocate dynamic memory
C) Faster execution time D) None of the above Answer: B Explanation: Encapsulation hides internal details, allowing implementation changes without breaking external code. Question 36. Which keyword allows a non-member function to access private members of a class? A) static B) friend C) mutable D) extern Answer: B Explanation: Declaring a function as a friend grants it access to the class’s private and protected members. Question 37. Which inheritance mode makes the public and protected members of the base class become private members of the derived class? A) public inheritance B) protected inheritance C) private inheritance D) virtual inheritance Answer: C Explanation: Private inheritance changes the accessibility of inherited members to private in the derived class. Question 38. In multiple inheritance, which construct resolves ambiguity when two base classes have a member with the same name?
A) Virtual inheritance B) Scope resolution operator C) Using declaration D) Both B and C Answer: D Explanation: The derived class can specify which base’s member to use with Base1::member (scope resolution) or bring it into scope with a using declaration. Question 39. Which keyword makes a member function capable of being overridden in derived classes? A) static B) inline C) virtual D) const Answer: C Explanation: The virtual specifier enables run-time polymorphism via overriding in derived classes. Question 40. What is an abstract class? A) A class that cannot be instantiated because it contains at least one pure virtual function B) A class that has only static members C) A class with no constructors D) A class that uses templates Answer: A Explanation: Pure virtual functions (virtual void foo() = 0;) make a class abstract, preventing direct instantiation.
Explanation: this is a pointer to the current object; using this->member accesses its members. Question 44. Which statement declares a static data member count of type int inside class Item? A) static int count; B) int static count; C) static int Item::count; D) int count static; Answer: A Explanation: Inside the class definition, static int count; declares a static member; it must be defined outside the class as int Item::count = 0;. Question 45. According to the Rule of Three, which functions should be explicitly defined if a class manages a raw pointer? A) Destructor, copy constructor, copy assignment operator B) Default constructor, move constructor, move assignment operator C) Only destructor D) None; the compiler generates them automatically Answer: A Explanation: Managing dynamic memory requires proper handling of copying and destruction to avoid leaks and double-deletes. Question 46. Which STL container provides constant-time insertion and removal at both ends but linear time random access? A) std::vector B) std::list C) std::deque
D) std::array Answer: C Explanation: std::deque (double-ended queue) offers O(1) push/pop at front/back and O(1) amortized random access, but internal layout makes it less cache-friendly than vector. Question 47. Which container automatically sorts its elements upon insertion? A) std::vector B) std::list C) std::set D) std::unordered_map Answer: C Explanation: std::set maintains ordered unique elements using a balanced binary tree. Question 48. Which iterator category supports only forward traversal and element modification? A) Input iterator B) Output iterator C) Forward iterator D) Random-access iterator Answer: C Explanation: Forward iterators can read and write and move only forward; they support multiple passes. Question 49. Which algorithm header provides the std::sort function? A) \
A) template T max(T a, T b) { return (a > b)? a : b; } B) template T max(T a, T b) { return (a < b)? a : b; } C) template T max(const T& a, const T& b) { return a > b? a : b; } D) Both A and C are valid. Answer: D Explanation: Both forms are legal; using const references (C) avoids unnecessary copies. Question 53. Which syntax specializes a class template Array for bool to implement a space-efficient bitset? A) template <> class Array { /.../ }; B) template class Array { /.../ }; C) template <> class Array { /.../ }; D) class Array { /.../ }; Answer: A Explanation: Full specialization uses template <> followed by the specialized parameter list. Question 54. Which statement correctly throws a standard out_of_range exception with a custom message? A) throw std::out_of_range("Index out of bounds"); B) throw out_of_range("Index out of bounds"); C) throw std::exception("Index out of bounds"); D) throw std::runtime_error; Answer: A Explanation: std::out_of_range is constructed with a const char* message.
Question 55. Which block of code correctly implements RAII for a file handle using std::ifstream? A) { std::ifstream file("data.txt"); /* use file / } // file closed automatically B) std::ifstream file = new std::ifstream("data.txt"); /* use file / delete file; C) std::ifstream file; file.open("data.txt"); / use file */ file.close(); D) Both A and C are acceptable, but A relies on automatic destruction. Answer: D Explanation: Both A (automatic storage) and C (explicit open/close) manage the resource; A exemplifies RAII by letting the destructor close the file. Question 56. Which of the following is the correct syntax to open a file for appending text? A) std::ofstream out("log.txt", std::ios::app); B) std::ofstream out("log.txt", std::ios::ate); C) std::ofstream out("log.txt", std::ios::out); D) std::ofstream out("log.txt", std::ios::binary); Answer: A Explanation: ios::app positions the write pointer at the end of the file and writes in append mode. Question 57. Which compilation command will generate an object file main.o from main.cpp without linking? A) g++ -c main.cpp -o main.o B) g++ main.cpp -o main.o C) g++ -o main.o main.cpp D) g++ -S main.cpp -o main.o Answer: A