NEW
Font size
WorksheetsC++ Programming Quiz (second)
Total questions: 53
Worksheet time: 27mins
What does a while loop check before executing its body?
The condition specified in parentheses
The number of iterations
The type of variable being used
Whether there is a break statement
Which loop is best suited for iterating through an array of fixed size?
For loop
While loop
Do-while loop
Recursive function
Which of the following is true about while loops?
It executes only if the condition is true
It executes at least once
It is used when the number of iterations is known
It cannot have a break statement
Which of the following loops executes at least once, even if the condition is false?
Do-while loop
For loop
While loop
Nested loop
What will be the output of this code: int mat[2][2] = { (1, 2), (3, 4) }; cout << mat[1][1];
4
1
2
3
How do you ensure a function does not modify an array?
Use const before the parameter
Use static inside the function
Copy the array manually
Use #define
Which loop is best suited for iterating over a 2D array?
Nested for loop
Single for loop
do-while loop
switch statement
What is the syntax of a for loop in C++?
for(initialization; condition; update) { // code }
for condition (initialization; update) { // code }
for ( initialization; condition; update )
for (condition; initialization; update) { // code }
How do you declare an integer array of size 5 in C++?
int array[5];
array arr[5];
int array = (5);
int array();
What is the correct way to use a break statement in a loop?
if (condition) break;
break = true;
loop.break();
stop loop;
What does the following code do? int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; cout << arr[0][2];
Outputs 3
Outputs 1
Outputs 4
Outputs 6
Which loop structure is preferred when a condition must be checked before execution?
While loop
Do-while loop
For loop
Recursive function
What is the purpose of nested loops?
To iterate over multi-dimensional data
To stop execution at a certain point
To execute only one iteration
To prevent infinite loops
What happens when a loop condition is never false?
Infinite loop
The loop executes once
The program crashes
Compilation error
How are arrays passed to functions in C++?
As a pointer to the first element
By value
By reference
They cannot be passed
Which of the following creates an infinite loop?
while (true) { cout << "Hello"; }
for (int i = 0; i < 10; i++) { cout << i; }
do { cout << "Hello"; } while (false);
while (i > 10) { cout << "Hello"; i++; }
What does the continue statement do in a loop?
Skips the current iteration and proceeds to the next one
Stops the loop execution
Jumps to the end of the program
Executes the loop indefinitely
Which of the following is a disadvantage of linear search?
Inefficient for large datasets
Works only on sorted arrays
Requires recursion
Needs additional memory
How do you access the third row, second column of a 2D array?
arr[2][1]
arr[3][2]
arr(3,2)
arr(2,1)
Which keyword is used to exit a loop immediately?
Break
Continue
Return
Exit
Which statement correctly initializes an array?
int arr[3] = {1, 2, 3};
int arr = {1, 2, 3};
arr[3] = {1, 2, 3};
int arr[3]; arr = {1, 2, 3};
Which of the following is NOT a valid array declaration?
double data[] = new double[5];
int arr[5];
float numbers[10];
char str[20];
Which of the following is a valid way to initialize a for loop?
for (int i = 0; i < 10; i++)
for (i = 0; i < 10; i++)
for (int i = 0; i++)
for ( int i = 0; i < 10; i++ )
Which of the following is a type of loop in C++?
While loop
Conditional loop
Recursion
Switch loop
Which sorting algorithm divides an array into smaller sub-arrays?
Merge Sort
Bubble Sort
Selection Sort
Heap Sort
What happens when an array is passed to a function?
The function receives a pointer to the first element
The entire array is copied
The function receives only the last element
The function cannot access the array
Which loop is preferred when the number of iterations is unknown?
While loop
For loop
Do-while loop
Switch case
What is the output of this code? int i = 5; while (i > 0) {cout << i << " "; i--;}
5 4 3 2 1
5 4 3 2
4 3 2 1
Infinite loop
Which of the following is an infinite loop?
while (true) { cout << "Looping"; }
for (int i = 0; i < 5; i++) { cout << i; }
do { cout << "Hello"; } while (false);
while (i > 10) { cout << "Loop"; i++; }
What happens if the condition in a while loop is always true?
The loop runs indefinitely
The loop executes only once
The loop stops automatically
The program gives a syntax error
What is an array in C++?
A collection of elements of the same type stored in contiguous memory locations
A single variable that holds multiple values of different types
A function that performs calculations
A keyword for declaring variables
How many times will this loop execute? for (int i = 0; i < 5; i++) {cout << i << " ";}
5 times
4 times
6 times
Infinite times
How can you find the size of an array in C++?
sizeof(arr) / sizeof(arr[0])
length(arr)
size(arr)
arr.length()
Which function prototype correctly accepts an integer array?
All of the above
void func(int arr[])
void func(int* arr);
void func(int arr[10]);
Which sorting algorithm repeatedly swaps adjacent elements?
Bubble Sort
Merge Sort
Insertion Sort
Radix Sort
What is a loop in C++?
A control structure that repeats a block of code until a condition is met
A function that executes once
A variable used for iteration
A data structure for storing numbers
What is the correct way to modify an array inside a function?
Directly modify the passed array
Pass it by reference using &
Return the modified array
Use a global variable
Which of the following is an example of an off-by-one error in a loop?
Using i <= size instead of i < size
Using an incorrect data type
Forgetting to increment the loop variable
Using break when not needed
What does the following for loop do? for (int i = 5; i > 0; i--) {cout << i << " ";}
Prints numbers in decreasing order
Prints numbers in increasing order
Prints only 0
Causes an infinite loop
What is the index of the first element in an array?
0
1
-1
Depends on the array size
Which loop is best when the number of iterations is known in advance?
For loop
While loop
Do-while loop
Infinite loop
What will happen if you access an array index that is out of bounds?
Undefined behavior
The program will crash
It will return 0 by default
Compiler error
How can you create an infinite for loop?
for(;;) { cout << "Looping"; }
for (i = 0; i++) { cout << "Looping"; }
for (i = 1; i > 0; i++) { cout << "Looping"; }
for (i = 0; i < 100; i--) { cout << "Looping"; }
Which of the following correctly represents a do-while loop?
do { // code } while (condition);
while (condition) { // code }
for (initialization; condition; update) { // code }
loop (condition) { // code }
Which algorithm is the most efficient for sorting an array?
Quick Sort
Bubble Sort
Selection Sort
Linear Sort
Which loop is commonly used to traverse an array?
for loop
if statement
switch statement
do-while loop
What does the following code output? int arr[3] = {1, 2}; cout << arr[2];
Undefined behavior
0
1
2
What will be the output of the following code? int arr[] = {10, 20, 30, 40}; cout << arr[2];
30
10
20
40
How many times will the following loop execute? int i = 0; while (i < 3) {cout << i << " "; i++;}
3 times
2 times
4 times
Infinite times
What will be the output of this for loop? for (int i = 0; i < 4; i++) {cout << i << " ";}
0 1 2 3
1 2 3 4
0 1 2 3 4
4 3 2 1
Which is the correct declaration of a 2D array?
int matrix[3][3];
int matrix[][];
int matrix(3,3);
matrix arr(3,3);
What will be the output of this loop? int i = 2; do {cout << i << " "; i++;} while (i < 5);
2 3 4
2 3
2 3 4 5
Infinite loop
How many times does this loop execute? int i = 10; while (i > 5) {cout << i << " "; i--;}
5 times
4 times
6 times
Infinite times
