wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CodeMavarick

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

Question: What will be the output of the following code snippet?

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

printf("%d", arr[5]);

a)

5

b)

0

c)

Garbage Value

d)

Compilation Error

2.

What is the time complexity of accessing an element in an array by its index?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n^2)

3.

What is the time complexity of the following code?

for(int i = 0; i < n; i++) {

for(int j = 0; j < n; j++) {

printf("Hello");

} }

a)

O(n)

b)

O(n log n)

c)

O(n^2)

d)

O(log n)

4.

What does the following code do?

struct Node {

int data;

struct Node* next; };

struct Node* head = NULL;

head->data = 10;

a)

Creates a node with data 10

b)

Causes a segmentation fault

c)

Initializes an empty linked list

d)

Links two nodes

5.

What is the time complexity of inserting a node at the end of a singly linked list with no tail pointer?

a)

O(1)

b)

O(n)

c)

O(n log n)

d)

O(n^2)

6.

Spot the error:

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = 5;

newNode->next = newNode;

a)

No error

b)

Creates a circular reference

c)

Missing free() call

d)

Incorrect malloc syntax

7.

How many moves are required to solve the Tower of Hanoi puzzle with 5 disks?

a)

32

b)

16

c)

31

d)

25

8.

Find the error in this stack push operation:

void push(int stack[], int* top, int value) {

stack[++(*top)] = value;

}

a)

No error

b)

Missing bounds check

c)

Incorrect increment syntax

d)

Value not assigned correctly

9.

What will this code output?

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

for(int i = 0; i <= 3; i++) {

printf("%d ", arr[i]);

}

a)

1 2 3

b)

1 2 3 0

c)

1 2 3 followed by garbage

d)

Compilation error

10.

Which line contains an error?

1: struct Node* head = NULL;

2: head = (struct Node*)malloc(sizeof(struct Node));

3: head->data = 10;

4: free(head->next);

a)

Line 1

b)

Line 2

c)

Line 3

d)

line 4

11.

What happens when you execute this code?

int arr[3];

arr[3] = 10;

printf("%d", arr[0]);

a)

Prints 10

b)

Prints 0

c)

Undefined behavior

d)

Compilation error

12.

What is the time complexity of reversing an array of size n using a two-pointer approach (swapping elements from both ends)?

a)

O(1)

b)

O(n)

c)

O(n/2)

d)

O(n^2)

13.

Which of these has a tighter bound on its worst-case time complexity?

a)

Linear search on an unsorted array: O(n)

b)

Binary search on a sorted array: O(log n)

c)

Bubble sort: O(n^2)

d)

Accessing an array element: O(1)

14.

In a singly linked list with n nodes, what is the minimum number of pointers that must be updated to delete the k-th node (1 ≤ k < n)?

a)

0

b)

1

c)

2

d)

3

15.

If the Tower of Hanoi recursion is modified to move disks directly between source and destination (skipping the auxiliary peg), what happens?

a)

It solves faster

b)

It becomes impossible for n > 1

c)

Time complexity drops to O(n)

d)

Only even-numbered disks can be moved

16.

Spot the error:

a)

Memory leak

b)

Infinite loop

c)

Incorrect list construction

d)

Null pointer dereference

17.

Which of the following statements are true about this code when executed with n = 5?

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

int sum = 0;

for(int i = 0; i <= n; i++) {

sum += arr[i];
}

printf("%d", sum);

a)

It correctly computes the sum of all elements (15).

b)

It accesses memory out of bounds.

c)

It causes a compilation error due to n not being defined.

d)

The output is unpredictable due to undefined behavior.

18.

Which outcomes are possible when running this circular queue code with capacity 3?

a)

Prints "1 3"

b)

Prints "1 4"

c)

The queue overflows with undefined behavior

d)

The last enqueue silently fails, keeping the queue valid

19.

What is the primary advantage of using an array over other data structures?

a)

Dynamic size

b)

Fast random access

c)

Efficient insertion at the beginning

d)

Automatic memory management

20.

What is the main difference between a singly linked list and an array?

a)

A linked list stores elements in contiguous memory

b)

A linked list has a fixed size

c)

A linked list uses pointers to connect elements

d)

A linked list allows faster indexing