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