WorksheetsQuiz 2 Progdas
Total questions: 20
Worksheet time: 10mins
What is the purpose of the #include directive in a C++ program?
To define a macro for constants
To start the execution of the program
To declare the main function
To include standard input/output stream functionalities
Which of the following correctly represents the syntax for declaring a constant macro in C++?
#define PI = 3.14159
const double PI = 3.14159
constant double PI = 3.14159
#define PI 3.14159
In the context of C++ programming, what is the primary function of the main() function?
It performs memory management
It contains all preprocessor directives
It handles exceptions
It starts the program execution
Which of the following data types is used to represent whole numbers in C++?
float
double
char
int
What does the following C++ statement do? cout << "Hello, World!";
It reads input from the user.
It outputs the string "Hello, World!" to the console.
It compares two strings.
It initializes a variable.
Which of the following is a correct example of a single-line comment in C++?
# This is a comment
// This is a comment
/* This is a comment */
<!—This is a comment—>
What is the output of the following C++ code snippet?
int x = 10;
int y = 20;
cout << x + y;
1020
10 + 20
10 20
30
Which of the following statements about variable declaration in C++ is true?
Variables can be used without being declared
Variables are automatically initialized to zero
Variables cannot store data types other than integers
Variables must be declared before they are used in a program
Which data type in C++ is best suited for representing a single character?
string
int
float
char
What is the role of the return statement in the main() function of a C++ program?
It declares variables
It initializes the program
It starts the execution of the main() function
It ends the program execution and returns a value to the operating system
Which statement about program control in C++ is TRUE?
Program control only manages the sequence of function calls
Program control can alter the order in which statements are executed
Program control is used solely for looping structures
Program control only affects the flow of main() function
What does a selection statement allow a program to do?
To repeat a set of instructions a specific number of times
To decide which of several actions to take based on a condition
To terminate the execution of a loop or function
To store values that will be used in later operations
Which characteristic distinguishes a switch statement from an if statement in C++?
An if statement is less versatile than a switch statement for multiple options
An if statement can only evaluate simple conditions
A switch statement can evaluate complex boolean expressions
A switch statement is restricted to comparing integral values
Which of the following operators is used to check for equality in a C++ conditional statement?
=
==
&&
||
Which selection structure allows you to evaluate multiple conditions, where each condition can lead to a different block of code being executed?
if
switch
for
while
Which of the following is NOT a valid condition to be used in a switch statement in C++?
An integer constant
A char literal
A floating-point value
An integral expression
Which operator combination is correctly used to check if two conditions are both true in a C++ if statement?
&&
||
==
><
Why might nested if statements be preferred over a switch statement in certain C++ scenarios?
Because nested if statements are simpler to write and understand
Because nested if statements are inherently faster
Because nested if statements consume less memory
Because nested if statements can handle more complex conditions
Which of the following conditions would you use to check if a variable age is between 12 and 17 inclusive?
age > 12 && age < 17
age < 12 || age > 17
age >= 12 && age <= 17
age == 12 || age == 17
What is the primary purpose of an if-else statement?
To repeat a set of statements until a condition is met
To choose between two alternative blocks of code based on a condition
To decide the number of times a block of code is executed
To exit a loop
