
































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!

































Beginner preparation | Core syntax, logic, functions, arrays, and OOP
PACK 1: QUESTIONS 1– Question 1 | Program structure Which function is the usual entry point of a standard C++ program? A. main() B. start() C. begin() D. run() Correct answer: A. main() Rationale: In a typical hosted C++ program, execution begins in main(). Question 2 | Output Which statement prints text to the console? A. std::cin >> "Hello"; B. print("Hello"); C. echo("Hello"); D. std::cout << "Hello"; Correct answer: D. std::cout << "Hello"; Rationale: std::cout is the standard output stream; the insertion operator << sends data to it. Question 3 | Input Which stream is commonly used to read input from the keyboard? A. std::cerr B. std::endl C. std::cin D. std::cout Correct answer: C. std::cin Rationale: std::cin is the standard input stream and commonly reads keyboard input with >>. Question 4 | Header Which header provides the standard input and output stream objects? A. #include **B. #include ** C. #include D. #include Correct answer: B. #include Rationale: The iostream header declares standard stream facilities such as std::cin and std::cout. Question 5 | Namespace What does std::cout mean? A. cout is a name in the std namespace B. std is a variable storing cout C. cout is a data type D. std:: is a comment marker Correct answer: A. cout is a name in the std namespace Rationale: The scope resolution operator :: qualifies cout as a member of the std namespace. Question 6 | Semicolon What is the purpose of a semicolon in C++? A. It starts a class B. It ends every program C. It declares a namespace D. It terminates many statements Correct answer: D. It terminates many statements Rationale: Many C++ statements end with a semicolon; braces define blocks and are not replaced by semicolons.
Question 13 | Arithmetic What is the result of 7 / 2 when both operands are integers? A. 3 B. 3. C. 4 D. 2 Correct answer: A. 3 Rationale: Integer division discards the fractional part, so 7 / 2 evaluates to 3. Question 14 | Remainder Which operator gives the remainder of integer division? A. / B. * C. ^ D. % Correct answer: D. % Rationale: The modulus operator % returns the remainder for integral operands. Question 15 | Comparison Which operator checks whether two values are equal? A. != B. <= C. == D. = Correct answer: C. == Rationale: == is the equality comparison operator; = is assignment. Question 16 | Logic Which operator means logical AND? A. &= B. && C. || D.! Correct answer: B. && Rationale: && evaluates to true when both operands are true. Question 17 | Condition Which keyword begins a conditional branch? A. if B. repeat C. when D. choose Correct answer: A. if Rationale: if evaluates a condition and runs its associated statement or block when true. Question 18 | Else What is the role of else? A. Repeat a loop forever B. Declare an array C. End a function D. Provide an alternative branch when the if condition is false Correct answer: D. Provide an alternative branch when the if condition is false Rationale: else provides the alternative path when the preceding if condition is false.
Question 19 | Switch Which construct is useful for selecting among several constant integral or enum cases? A. namespace B. typedef C. switch D. while Correct answer: C. switch Rationale: switch selects among case labels based on an integral or enumeration expression. Question 20 | For loop What is a common use of a for loop? A. Declaring a namespace B. Repeating code with an initialization, condition, and update C. Defining a class only D. Reading a file only Correct answer: B. Repeating code with an initialization, condition, and update Rationale: A for loop groups initialization, continuation condition, and update in one loop header. Question 21 | While loop When does a while loop test its condition? A. Before each iteration B. Only after the loop ends C. Only once when the program starts D. Never Correct answer: A. Before each iteration Rationale: A while loop checks its condition before entering each iteration, so it may execute zero times. Question 22 | Do while What is special about a do-while loop? A. It cannot use a condition B. It always executes exactly twice C. It is used only with arrays D. Its body executes at least once before the condition is tested Correct answer: D. Its body executes at least once before the condition is tested Rationale: A do-while loop checks the condition after the body, guaranteeing at least one execution. Question 23 | Break What does break do inside a loop? A. Restarts the program B. Declares a new variable C. Immediately exits the nearest enclosing loop or switch D. Skips only the current statement Correct answer: C. Immediately exits the nearest enclosing loop or switch Rationale: break exits the nearest enclosing loop or switch statement. Question 24 | Continue What does continue do in a loop? A. Returns a value from main B. Skips the rest of the current iteration and proceeds to the next iteration C. Ends the entire program D. Exits every nested loop Correct answer: B. Skips the rest of the current iteration and proceeds to the next iteration Rationale: continue transfers control to the next iteration of the nearest loop.
Question 31 | Indexing What is the first valid index of a C++ array with at least one element? A. - B. The array's size C. 0 D. 1 Correct answer: C. 0 Rationale: C++ arrays are zero-indexed, so the first element is at index 0. Question 32 | Vector Which standard library container can grow or shrink dynamically? A. bool B. std::vector C. built-in int array D. char Correct answer: B. std::vector Rationale: std::vector is a sequence container that manages a resizable array. Question 33 | Header for vector Which header is needed to use std::vector? **A. #include ** B. #include only C. #include only D. #include only Correct answer: A. #include Rationale: The vector header declares std::vector. Question 34 | String operations Which member function returns the number of characters in a std::string? A. push() B. inserted() C. countall() D. size() Correct answer: D. size() Rationale: std::string::size() returns the number of characters in the string. Question 35 | Pointer What does a pointer store? A. A class definition B. A loop condition C. A memory address D. Only a decimal number Correct answer: C. A memory address Rationale: A pointer is an object whose value is an address or a null pointer value. Question 36 | Reference What is a reference in C++? A. A file handle in every case B. An alias for an existing object C. A copy of an entire program D. A special loop Correct answer: B. An alias for an existing object Rationale: A reference provides another name for an existing object and must be initialized when declared.
Question 37 | Null pointer What does nullptr represent? A. A null pointer value B. A pointer to the first array element C. A number equal to zero in every type D. The end of a string Correct answer: A. A null pointer value Rationale: nullptr is the null pointer literal introduced in modern C++, used to represent a pointer that points to no object or function. Question 38 | Class What is a class? A. A loop that runs forever B. A built-in arithmetic operator C. A header file D. A user-defined type that can combine data and functions Correct answer: D. A user-defined type that can combine data and functions Rationale: A class defines a type whose objects can contain data members and member functions. Question 39 | Object What is an object of a class? A. A function parameter only B. A compiler warning C. An instance of that class D. The class name written in a comment Correct answer: C. An instance of that class Rationale: An object is an instance created from a class type. Question 40 | Constructor When is a constructor generally called? A. Only after a destructor B. When an object is initialized C. Only when a program closes D. Whenever a comment is read Correct answer: B. When an object is initialized Rationale: Constructors initialize objects when they are created. Question 41 | Encapsulation What is encapsulation in object-oriented programming? A. Bundling data and related operations while controlling access B. Writing all variables as global C. Avoiding functions D. Converting every class to an array Correct answer: A. Bundling data and related operations while controlling access Rationale: Encapsulation combines state and behavior and uses access controls to protect class invariants. Question 42 | Access control Which access specifier makes class members accessible from outside through the class interface? A. private B. protected_only C. hidden D. public Correct answer: D. public Rationale: public members are accessible to code that can access the object; private members are restricted to the class and its friends.
Question 49 | Exception handling Which keywords are commonly used for exception handling? A. try, catch, and throw B. if, else, and switch C. for, while, and do D. class, public, and private Correct answer: A. try, catch, and throw Rationale: try marks a block to monitor, throw signals an exception, and catch handles matching exceptions. Question 50 | Standard library What is the C++ Standard Library? A. A single mandatory application B. A programming language separate from C++ C. A list of compiler errors only D. A collection of standard types and functions for common programming tasks Correct answer: D. A collection of standard types and functions for common programming tasks Rationale: The Standard Library provides reusable facilities such as containers, algorithms, strings, streams, and utilities.
PACK 2: QUESTIONS 51– Question 51 | Program structure Which function is the usual entry point of a standard C++ program? Choose the best answer. A. run() B. main() C. start() D. begin() Correct answer: B. main() Rationale: In a typical hosted C++ program, execution begins in main(). Question 52 | Output Which statement prints text to the console? Choose the best answer. A. std::cout << "Hello"; B. std::cin >> "Hello"; C. print("Hello"); D. echo("Hello"); Correct answer: A. std::cout << "Hello"; Rationale: std::cout is the standard output stream; the insertion operator << sends data to it. Question 53 | Input Which stream is commonly used to read input from the keyboard? Choose the best answer. A. std::cout B. std::cerr C. std::endl D. std::cin Correct answer: D. std::cin Rationale: std::cin is the standard input stream and commonly reads keyboard input with >>. Question 54 | Header Which header provides the standard input and output stream objects? Choose the best answer. A. #include B. #include **C. #include ** D. #include Correct answer: C. #include Rationale: The iostream header declares standard stream facilities such as std::cin and std::cout. Question 55 | Namespace What does std::cout mean? Choose the best answer. A. std:: is a comment marker B. cout is a name in the std namespace C. std is a variable storing cout D. cout is a data type Correct answer: B. cout is a name in the std namespace Rationale: The scope resolution operator :: qualifies cout as a member of the std namespace. Question 56 | Semicolon What is the purpose of a semicolon in C++? Choose the best answer. A. It terminates many statements B. It starts a class C. It ends every program D. It declares a namespace Correct answer: A. It terminates many statements Rationale: Many C++ statements end with a semicolon; braces define blocks and are not replaced by semicolons.
Question 63 | Arithmetic What is the result of 7 / 2 when both operands are integers? Choose the best answer. A. 2 B. 3 C. 3. D. 4 Correct answer: B. 3 Rationale: Integer division discards the fractional part, so 7 / 2 evaluates to 3. Question 64 | Remainder Which operator gives the remainder of integer division? Choose the best answer. A. % B. / C. * D. ^ Correct answer: A. % Rationale: The modulus operator % returns the remainder for integral operands. Question 65 | Comparison Which operator checks whether two values are equal? Choose the best answer. A. = B. != C. <= D. == Correct answer: D. == Rationale: == is the equality comparison operator; = is assignment. Question 66 | Logic Which operator means logical AND? Choose the best answer. A.! B. &= C. && D. || Correct answer: C. && Rationale: && evaluates to true when both operands are true. Question 67 | Condition Which keyword begins a conditional branch? Choose the best answer. A. choose B. if C. repeat D. when Correct answer: B. if Rationale: if evaluates a condition and runs its associated statement or block when true. Question 68 | Else What is the role of else? Choose the best answer. A. Provide an alternative branch when the if condition is false B. Repeat a loop forever C. Declare an array D. End a function Correct answer: A. Provide an alternative branch when the if condition is false Rationale: else provides the alternative path when the preceding if condition is false.
Question 69 | Switch Which construct is useful for selecting among several constant integral or enum cases? Choose the best answer. A. while B. namespace C. typedef D. switch Correct answer: D. switch Rationale: switch selects among case labels based on an integral or enumeration expression. Question 70 | For loop What is a common use of a for loop? Choose the best answer. A. Reading a file only B. Declaring a namespace C. Repeating code with an initialization, condition, and update D. Defining a class only Correct answer: C. Repeating code with an initialization, condition, and update Rationale: A for loop groups initialization, continuation condition, and update in one loop header. Question 71 | While loop When does a while loop test its condition? Choose the best answer. A. Never B. Before each iteration C. Only after the loop ends D. Only once when the program starts Correct answer: B. Before each iteration Rationale: A while loop checks its condition before entering each iteration, so it may execute zero times. Question 72 | Do while What is special about a do-while loop? Choose the best answer. A. Its body executes at least once before the condition is tested B. It cannot use a condition C. It always executes exactly twice D. It is used only with arrays Correct answer: A. Its body executes at least once before the condition is tested Rationale: A do-while loop checks the condition after the body, guaranteeing at least one execution. Question 73 | Break What does break do inside a loop? Choose the best answer. A. Skips only the current statement B. Restarts the program C. Declares a new variable D. Immediately exits the nearest enclosing loop or switch Correct answer: D. Immediately exits the nearest enclosing loop or switch Rationale: break exits the nearest enclosing loop or switch statement. Question 74 | Continue What does continue do in a loop? Choose the best answer. A. Exits every nested loop B. Returns a value from main C. Skips the rest of the current iteration and proceeds to the next iteration D. Ends the entire program Correct answer: C. Skips the rest of the current iteration and proceeds to the next iteration Rationale: continue transfers control to the next iteration of the nearest loop.
Question 81 | Indexing What is the first valid index of a C++ array with at least one element? Choose the best answer. A. 1 B. - C. The array's size D. 0 Correct answer: D. 0 Rationale: C++ arrays are zero-indexed, so the first element is at index 0. Question 82 | Vector Which standard library container can grow or shrink dynamically? Choose the best answer. A. char B. bool C. std::vector D. built-in int array Correct answer: C. std::vector Rationale: std::vector is a sequence container that manages a resizable array. Question 83 | Header for vector Which header is needed to use std::vector? Choose the best answer. A. #include only **B. #include ** C. #include only D. #include only Correct answer: B. #include Rationale: The vector header declares std::vector. Question 84 | String operations Which member function returns the number of characters in a std::string? Choose the best answer. A. size() B. push() C. inserted() D. countall() Correct answer: A. size() Rationale: std::string::size() returns the number of characters in the string. Question 85 | Pointer What does a pointer store? Choose the best answer. A. Only a decimal number B. A class definition C. A loop condition D. A memory address Correct answer: D. A memory address Rationale: A pointer is an object whose value is an address or a null pointer value. Question 86 | Reference What is a reference in C++? Choose the best answer. A. A special loop B. A file handle in every case C. An alias for an existing object D. A copy of an entire program Correct answer: C. An alias for an existing object Rationale: A reference provides another name for an existing object and must be initialized when declared.
Question 87 | Null pointer What does nullptr represent? Choose the best answer. A. The end of a string B. A null pointer value C. A pointer to the first array element D. A number equal to zero in every type Correct answer: B. A null pointer value Rationale: nullptr is the null pointer literal introduced in modern C++, used to represent a pointer that points to no object or function. Question 88 | Class What is a class? Choose the best answer. A. A user-defined type that can combine data and functions B. A loop that runs forever C. A built-in arithmetic operator D. A header file Correct answer: A. A user-defined type that can combine data and functions Rationale: A class defines a type whose objects can contain data members and member functions. Question 89 | Object What is an object of a class? Choose the best answer. A. The class name written in a comment B. A function parameter only C. A compiler warning D. An instance of that class Correct answer: D. An instance of that class Rationale: An object is an instance created from a class type. Question 90 | Constructor When is a constructor generally called? Choose the best answer. A. Whenever a comment is read B. Only after a destructor C. When an object is initialized D. Only when a program closes Correct answer: C. When an object is initialized Rationale: Constructors initialize objects when they are created. Question 91 | Encapsulation What is encapsulation in object-oriented programming? Choose the best answer. A. Converting every class to an array B. Bundling data and related operations while controlling access C. Writing all variables as global D. Avoiding functions Correct answer: B. Bundling data and related operations while controlling access Rationale: Encapsulation combines state and behavior and uses access controls to protect class invariants. Question 92 | Access control Which access specifier makes class members accessible from outside through the class interface? Choose the best answer. A. public B. private C. protected_only D. hidden Correct answer: A. public Rationale: public members are accessible to code that can access the object; private members are restricted to the class and its friends.
Question 99 | Exception handling Which keywords are commonly used for exception handling? Choose the best answer. A. class, public, and private B. try, catch, and throw C. if, else, and switch D. for, while, and do Correct answer: B. try, catch, and throw Rationale: try marks a block to monitor, throw signals an exception, and catch handles matching exceptions. Question 100 | Standard library What is the C++ Standard Library? Choose the best answer. A. A collection of standard types and functions for common programming tasks B. A single mandatory application C. A programming language separate from C++ D. A list of compiler errors only Correct answer: A. A collection of standard types and functions for common programming tasks Rationale: The Standard Library provides reusable facilities such as containers, algorithms, strings, streams, and utilities.
PACK 3: QUESTIONS 101– Question 101 | Program structure Which function is the usual entry point of a standard C++ program? Which statement is correct? A. begin() B. run() C. main() D. start() Correct answer: C. main() Rationale: In a typical hosted C++ program, execution begins in main(). Question 102 | Output Which statement prints text to the console? Which statement is correct? A. echo("Hello"); B. std::cout << "Hello"; C. std::cin >> "Hello"; D. print("Hello"); Correct answer: B. std::cout << "Hello"; Rationale: std::cout is the standard output stream; the insertion operator << sends data to it. Question 103 | Input Which stream is commonly used to read input from the keyboard? Which statement is correct? A. std::cin B. std::cout C. std::cerr D. std::endl Correct answer: A. std::cin Rationale: std::cin is the standard input stream and commonly reads keyboard input with >>. Question 104 | Header Which header provides the standard input and output stream objects? Which statement is correct? A. #include B. #include C. #include D. #include Correct answer: D. #include Rationale: The iostream header declares standard stream facilities such as std::cin and std::cout. Question 105 | Namespace What does std::cout mean? Which statement is correct? A. cout is a data type B. std:: is a comment marker C. cout is a name in the std namespace D. std is a variable storing cout Correct answer: C. cout is a name in the std namespace Rationale: The scope resolution operator :: qualifies cout as a member of the std namespace. Question 106 | Semicolon What is the purpose of a semicolon in C++? Which statement is correct? A. It declares a namespace B. It terminates many statements C. It starts a class D. It ends every program Correct answer: B. It terminates many statements Rationale: Many C++ statements end with a semicolon; braces define blocks and are not replaced by semicolons.