wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Arrays Quiz

Total questions: 62

Worksheet time: 34mins

Name
Class
Date
1.

What is an array in C++?

a)

A collection of elements of the same type stored in contiguous memory locations

b)

A single variable that holds multiple values of different types

c)

A function that performs calculations

d)

A keyword for declaring variables

2.

How do you declare an integer array of size 5 in C++?

a)

int array[5];

b)

array arr(5);

c)

int array = {5};

d)

int array[];

3.

What is the index of the first element in an array?

a)

0

b)

1

c)

-1

d)

Depends on the array size

4.

What will happen if you access an array index that is out of bounds?

a)

Undefined behavior

b)

The program will crash

c)

It will return 0 by default

d)

Compiler error

5.

Which of the following is NOT a valid array declaration?

a)

double data[] = new double[5];

b)

int arr[5];

c)

float numbers[10];

d)

char str[20];

6.

Which statement correctly initializes an array?

a)

int arr[3] = {1, 2, 3};

b)

int arr = {1, 2, 3};

c)

arr[3] = {1, 2, 3};

d)

int arr[3]; arr = {1, 2, 3};

7.

What will be the output of the following code? int arr[] = {10, 20, 30, 40}; cout << arr[2];

a)

30

b)

10

c)

20

d)

40

8.

How can you find the size of an array in C++?

a)

sizeof(arr) / sizeof(arr[0])

b)

length(arr)

c)

size(arr)

d)

arr.length()

9.

Which loop is commonly used to traverse an array?

a)

for loop

b)

if statement

c)

switch statement

d)

do-while loop

10.

What does the following code output? int arr[3] = {1, 2}; cout << arr[2];

a)

0

b)

1

c)

2

d)

Undefined behavior

11.

How are arrays passed to functions in C++?

a)

As a pointer to the first element

b)

By value

c)

By reference

d)

They cannot be passed

12.

Which function prototype correctly accepts an integer array?

a)

All of the above

b)

void func(int arr[]);

c)

void func(int* arr);

d)

void func(int arr[10]);

13.

What happens when an array is passed to a function?

a)

The function receives a pointer to the first element

b)

The entire array is copied

c)

The function receives only the last element

d)

The function cannot access the array

14.

What is the correct way to modify an array inside a function?

a)

Directly modify the passed array

b)

Pass it by reference using &

c)

Return the modified array

d)

Use a global variable

15.

How do you ensure a function does not modify an array?

a)

Use const before the parameter

b)

Use static inside the function

c)

Copy the array manually

d)

Use #define

16.

Which is the correct declaration of a 2D array?

a)

int matrix[3][3];

b)

int matrix[][];

c)

int matrix{3,3};

d)

matrix arr(3,3);

17.

How do you access the third row, second column of a 2D array?

a)

arr[2][1]

b)

arr[3][2]

c)

arr(3,2)

d)

arr{2,1}

18.

What will be the output of this code? int mat[2][2] = { {1, 2}, {3, 4} }; cout << mat[1][1];

a)

4

b)

1

c)

2

d)

3

19.

Which loop is best suited for iterating over a 2D array?

a)

Nested for loop

b)

Single for loop

c)

do-while loop

d)

switch statement

20.

What does the following code do? int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; cout << arr[0][2];

a)

Outputs 3

b)

Outputs 1

c)

Outputs 4

d)

Outputs 6

21.

Which algorithm is the most efficient for sorting an array?

a)

Quick Sort

b)

Bubble Sort

c)

Selection Sort

d)

Linear Sort

22.

Which sorting algorithm repeatedly swaps adjacent elements?

a)

Bubble Sort

b)

Merge Sort

c)

Insertion Sort

d)

Radix Sort

23.

Which of the following is a disadvantage of linear search?

4 lines
24.

Which sorting algorithm repeatedly swaps adjacent elements?

a)

Bubble Sort

b)

Merge Sort

c)

Insertion Sort

d)

Radix Sort

25.

Which of the following is a disadvantage of linear search?

a)

Inefficient for large datasets

b)

Works only on sorted arrays

c)

Requires recursion

d)

Needs additional memory

26.

Which sorting algorithm divides an array into smaller sub-arrays?

a)

Merge Sort

b)

Bubble Sort

c)

Selection Sort

d)

Heap Sort

27.

What is a loop in C++?

a)

A control structure that repeats a block of code until a condition is met

b)

A function that executes once

c)

A variable used for iteration

d)

A data structure for storing numbers

28.

Which of the following is a type of loop in C++?

a)

While loop

b)

Conditional loop

c)

Recursion

d)

Switch loop

29.

Which loop is best when the number of iterations is known in advance?

a)

For loop

b)

While loop

c)

Do-while loop

d)

If-else statement

30.

What is the correct syntax for a for loop?

a)

for(initialization; condition; update) { // code }

b)

for condition (initialization; update) { // code }

c)

for (condition; initialization; update) { // code }

d)

for { initialization; condition; update }

31.

Which loop always executes at least once?

a)

Do-while loop

b)

For loop

c)

While loop

d)

Nested loop

32.

What is the output of this code? int i = 5; while (i > 0) {cout << i << "\n"; i--;}

a)

5 4 3 2 1

b)

5 4 3 2

c)

4 3 2 1

d)

Infinite loop

33.

Which of the following is true about while loops?

a)

It executes only if the condition is true

b)

It executes at least once

c)

It is used when the number of iterations is known

d)

It cannot have a break statement

34.

Which loop structure is preferred when a condition must be checked before execution?

a)

While loop

b)

Do-while loop

c)

For loop

d)

Recursive function

35.

What is the correct way to use a break statement in a loop?

a)

if (condition) break;

b)

break = true;

c)

loop.break();

d)

stop loop;

36.

What does the continue statement do in a loop?

a)

Skips the current iteration and proceeds to the next one

b)

Stops the loop execution

c)

Jumps to the end of the program

d)

Executes the loop indefinitely

37.

How many times will this loop execute? for (int i = 0; i < 5; i++) {cout << i << " ";}

a)

5 times

b)

4 times

c)

6 times

d)

Infinite times

38.

Which keyword is used to exit a loop immediately?

a)

Break

b)

Continue

c)

Return

d)

Exit

39.

Which of the following creates an infinite loop?

a)

while (true) { cout << "Hello"; }

b)

for (int i = 0; i < 10; i++) { cout << i; }

c)

do { cout << "Hello"; } while (false);

d)

while (i > 10) { cout << "Hello"; i++; }

40.

What happens when a loop condition is never false?

a)

Infinite loop

b)

The loop executes once

c)

The program crashes

d)

Compilation error

41.

What is the purpose of nested loops?

a)

To iterate over multi-dimensional data

b)

To stop execution at a certain point

c)

To execute only one iteration

d)

To prevent infinite loops

42.

Which loop is best when the number of iterations is known in advance?

a)

For loop

b)

While loop

c)

Do-while loop

d)

Infinite loop

43.

Which of the following loops executes at least once, even if the condition is false?

a)

Do-while loop

b)

For loop

c)

While loop

d)

Nested loop

44.

What is the syntax of a for loop in C++?

a)

for(initialization; condition; update) { // code }

b)

for condition (initialization; update) { // code }

c)

for { initialization; condition; update }

45.

syntax of a for loop in C++?

a)

for(initialization; condition; update) { // code }

b)

for condition (initialization; update) { // code }

c)

for { initialization; condition; update }

d)

for (condition; initialization; update) { // code }

46.

What does a while loop check before executing its body?

a)

The condition specified in parentheses

b)

The number of iterations

c)

The type of variable being used

d)

Whether there is a break statement

47.

Which of the following is an infinite loop?

a)

while (true) { cout << "Looping"; }

b)

for (int i = 0; i < 5; i++) { cout << i; }

c)

do { cout << "Hello"; } while (false);

d)

while (i > 10) { cout << "Loop"; i++; }

48.

How many times will the following loop execute? int i = 0; while (i < 3) {cout << i << " "; i++;}

a)

3 times

b)

2 times

c)

4 times

d)

Infinite times

49.

Which statement is used to exit a loop immediately?

a)

Break

b)

Continue

c)

Return

d)

Exit

50.

What does the continue statement do inside a loop?

a)

Skips the current iteration and moves to the next one

b)

Stops the loop execution

c)

Exits the program

d)

Restarts the loop from the beginning

51.

Which loop is best suited for iterating through an array of fixed size?

a)

For loop

b)

While loop

c)

Do-while loop

d)

Recursive function

52.

What will be the output of this for loop? for (int i = 0; i < 4; i++) {cout << i << " ";}

a)

0 1 2 3

b)

1 2 3 4

c)

0 1 2 3 4

d)

4 3 2 1

53.

What is the purpose of nested loops?

a)

To iterate over multi-dimensional data

b)

To stop execution at a certain point

c)

To execute only one iteration

d)

To prevent infinite loops

54.

Which loop is preferred when the number of iterations is unknown?

a)

While loop

b)

For loop

c)

Do-while loop

d)

Switch case

55.

What happens if the condition in a while loop is always true?

a)

The loop runs indefinitely

b)

The loop executes only once

c)

The loop stops automatically

d)

The program gives a syntax error

56.

Which of the following is a valid way to initialize a for loop?

a)

for (int i = 0; i < 10; i++)

b)

for (i = 0; i < 10; i++)

c)

for (int i = 0; i++)

d)

for { int i = 0; i < 10; i++ }

57.

Which of the following correctly represents a do-while loop?

a)

do { // code } while (condition);

b)

while (condition) { // code }

c)

for (initialization; condition; update) { // code }

d)

loop (condition) { // code }

58.

How many times does this loop execute? int i = 10; while (i > 5) {cout << i << " "; i--;}

a)

5 times

b)

4 times

c)

6 times

d)

Infinite times

59.

What does the following for loop do? for (int i = 5; i > 0; i--) {cout << i << " ";}

a)

Prints numbers in decreasing order

b)

Prints numbers in increasing order

c)

Prints only 0

d)

Causes an infinite loop

60.

How can you create an infinite for loop?

a)

for(;;) { cout << "Looping"; }

b)

for (i = 0; i++) { cout << "Looping"; }

c)

for (i = 1; i > 0; i++) { cout << "Looping"; }

d)

for (i = 0; i < 100; i--) { cout << "Looping"; }

61.

Which of the following is an example of an off-by-one error in a loop?

a)

Using i <= size instead of i < size

b)

Using an incorrect data type

c)

Forgetting to increment the loop variable

d)

Using break when not needed

62.

What will be the output of this loop? int i = 2; do {cout << i << " "; i++;} while (i < 5);

a)

2 3 4

b)

2 3

c)

2 3 4 5

d)

Infinite loop