wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

FCPC-MidtermExam-DSA

Total questions: 70

Worksheet time: 1hrs 10mins

Name
Class
Date
1.

Which of the following is the correct syntax for a while loop in C?

a)

while (condition) { statement; }

b)

while condition { statement; }

c)

while (statement) { condition; }

d)

while [condition] { statement; }

2.

What will be the output of the following code? int i = 0; while(i < 5) { printf("%d", i); i++; }

a)

12345

b)

01234

c)

1 2 3 4 5

d)

5

3.

Which of the following loops will always execute at least once?

a)

for

b)

while

c)

do-while

d)

All loops can execute at least once

4.

What is the output of the following program? int i; for(i = 1; i <= 3; i++) { printf("%d ", i); }

a)

1 2 3

b)

0 1 2

c)

1 2 3 4

d)

3 2 1

5.

Which of the following is the correct syntax for a do-while loop?

a)

do { statement; } while (condition);

b)

do statement while (condition);

c)

while (condition) do { statement; };

d)

do statement until (condition);

6.

What is the correct way to declare a function that returns an integer in C?

a)

function int() {}

b)

int function() {}

c)

int function {}

d)

int function(void) {}

7.

Which of the following is true about the function prototype in C?

a)

It is required for all functions.

b)

It must be placed before the function definition.

c)

It can be omitted if the function definition is provided before the function call.

d)

It must include the function body.

8.

What is the output of the following code? void func(int x) { printf("%d", x); } int main() { func(5); return 0; }

a)

5

b)

5 0

c)

Error

d)

Nothing

9.

Which of the following is the correct way to call a function named add that takes two integer parameters?

a)

add(5, 10);

b)

add(5, 10)

c)

add 5, 10;

d)

add(5,10){}

10.

What does the return statement do in a function?

a)

Stops the execution of the program.

b)

Returns a value from the function.

c)

Exits the current loop.

d)

Defines a function's return type.

11.

What is the symbol used to access the value of a variable through its pointer in C?

a)

&

b)

*

c)

->

d)

&&

12.

Which of the following operators is used to get the memory address of a variable?

a)

*

b)

&

c)

->

d)

&&

13.

What will the following code print? int a = 10; int *ptr = &a; printf("%d", *ptr);

a)

10

b)

0

c)

a

d)

Error

14.

What is a pointer in C?

a)

A variable that holds the memory address of another variable.

b)

A function that returns a value.

c)

A variable used for memory allocation.

d)

A type of loop.

15.

Which of the following is the correct way to declare a pointer to an integer in C?

a)

int pointer;

b)

pointer int;

c)

int* pointer;

d)

pointer* int;

16.

Which sorting algorithm is the most efficient in terms of time complexity for large data sets?

a)

Bubble sort

b)

Quick sort

c)

Insertion sort

d)

Selection sort

17.

What is the time complexity of the bubble sort algorithm in the worst case?

a)

O(n)

b)

O(n^2)

c)

O(log n)

d)

O(n log n)

18.

Which of the following is the correct description of the selection sort algorithm?

a)

It repeatedly selects the largest (or smallest) element and places it in the correct position.

b)

It swaps adjacent elements if they are in the wrong order.

c)

It splits the array and recursively sorts each half.

d)

It compares each element to the previous one and inserts it in the correct position.

19.

In which sorting algorithm is the pivot element chosen and used to partition the array?

a)

Merge sort

b)

Quick sort

c)

Heap sort

d)

Bubble sort

20.

Which sorting algorithm has a time complexity of O(n log n) in the best, worst, and average cases?

a)

Merge sort

b)

Quick sort

c)

Insertion sort

d)

Selection sort

21.

What is the time complexity of binary search in the worst case?

a)

O(n)

b)

O(n^2)

c)

O(log n)

d)

O(n log n)

22.

Which of the following is true about linear search?

a)

It requires a sorted array.

b)

It searches by dividing the array into smaller parts.

c)

It examines each element of the array one by one.

d)

It has a time complexity of O(log n).

23.

Which searching algorithm is more efficient for large arrays, assuming the array is sorted?

a)

Linear search

b)

Binary search

c)

Sequential search

d)

Hashing

24.

What is the key requirement for performing binary search on an array?

a)

The array must be sorted.

b)

The array must be unsorted.

c)

The array must be of size n.

d)

The array must contain unique elements.

25.

Which of the following algorithms is used for searching an element in an unsorted list?

a)

Binary search

b)

Linear search

c)

Jump search

d)

Exponential search

26.

What is the time complexity of the linear search algorithm?

a)

O(n)

b)

O(n^2)

c)

O(log n)

d)

O(1)

27.

What is the main advantage of binary search over linear search?

a)

Binary search is faster for sorted arrays.

b)

Binary search can be used on unsorted arrays.

c)

Binary search is more accurate.

d)

Binary search requires no comparison.

28.

What is the worst-case time complexity of binary search?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n log n)

29.

Which of the following is NOT a characteristic of binary search?

a)

It works on sorted arrays.

b)

It requires random access to the elements.

c)

It can be implemented recursively.

d)

It is slower than linear search for small arrays.

30.

What happens when a function does not have a return statement?

a)

It causes a compilation error.

b)

It returns an undefined value.

c)

It automatically returns 0.

d)

It returns NULL.

31.

Which of the following is true about a do-while loop?

a)

The loop body is always executed at least once.

b)

The loop condition is checked before the loop body is executed.

c)

The loop condition is checked after each iteration.

d)

None of the above.

32.

What is the value of *ptr in the following code? int x = 10; int *ptr = &x;

a)

10

b)

x

c)

ptr

d)

&x

33.

What is the value of *ptr in the following code? int x = 10; int *ptr = &x;

a)

10

b)

x

c)

ptr

d)

&x

34.

Which sorting algorithm selects the smallest element and places it at the beginning of the array?

a)

Insertion sort

b)

Selection sort

c)

Merge sort

d)

Quick sort

35.

Which of the following algorithms divides the array into subarrays and then merges them back together?

a)

Quick sort

b)

Merge sort

c)

Bubble sort

d)

Selection sort

36.

What is the key idea behind the quicksort algorithm?

a)

Divide the array into smaller arrays and sort them.

b)

Repeatedly select a pivot and partition the array around it.

c)

Swap elements one by one.

d)

Sort the array in a single pass.

37.

Which of the following is an advantage of quicksort over other sorting algorithms?

a)

It guarantees O(n^2) time complexity.

b)

It has a small auxiliary space requirement.

c)

It is always faster than other sorting algorithms.

d)

It is stable.

38.

What is the average time complexity of quicksort?

a)

O(n)

b)

O(n^2)

c)

O(log n)

d)

O(n log n)

39.

What is the worst-case time complexity of bubble sort?

a)

O(n)

b)

O(n log n)

c)

O(n^2)

d)

O(log n)

40.

What does the merge sort algorithm use to combine the sorted subarrays?

a)

A pivot element

b)

A queue

c)

An auxiliary array

d)

A stack

41.

What is the worst-case time complexity of selection sort?

a)

O(1)

b)

O(n log n)

c)

O(n^2)

d)

O(log n)

42.

What is the average-case time complexity of insertion sort?

a)

O(n log n)

b)

O(n^2)

c)

O(n)

d)

O(log n)

43.

In which of the following algorithms is the "divide and conquer" strategy used?

a)

Selection sort

b)

Merge sort

c)

Bubble sort

d)

Insertion sort

44.

Which of the following is a characteristic of the binary search algorithm?

a)

It works on sorted arrays.

b)

It checks each element one by one.

c)

It has a time complexity of O(n).

d)

It works on unsorted arrays.

45.

What is the main disadvantage of the quicksort algorithm?

a)

It requires extra space.

b)

It is unstable.

c)

It has poor performance in the worst case.

d)

It requires a sorted array.

46.

Which of the following algorithms is unstable?

a)

Merge sort

b)

Quick sort

c)

Insertion sort

d)

Bubble sort

47.

What is the key operation in binary search?

a)

Checking the middle element and deciding whether to search the left or right half.

b)

Comparing elements with adjacent ones.

c)

Sorting the array.

d)

Dividing the array into smaller chunks.

48.

Which of the following searching algorithms is most efficient for a sorted array?

a)

Linear search

b)

Binary search

c)

Jump search

d)

Exponential search

49.

What is the space complexity of the merge sort algorithm?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n log n)

50.

Which of the following is the correct way to initialize a pointer to NULL?

a)

int *ptr = 0;

b)

int *ptr = NULL;

c)

int *ptr; ptr = NULL;

d)

All of the above

51.

Which header file is required to use string functions in C?

a)

stdio.h

b)

conio.h

c)

string.h

d)

stdlib.h

52.

Which function is used to find the length of a string in C?

a)

strlen()

b)

strlength()

c)

sizeof()

d)

strsize()

53.

What does the strcmp() function return if two strings are identical?

a)

1

b)

-1

c)

0

d)

Depends on the strings

54.

Which of the following functions is used to concatenate two strings?

a)

strcat()

b)

strjoin()

c)

stradd()

d)

strmerge()

55.

What is the output of strlen("hello")?

a)

4

b)

5

c)

6

d)

0

56.

Which function is used to copy one string to another in C?

a)

strcpy()

b)

strcopy()

c)

copystr()

d)

strncpy()

57.

What is the value returned by strcmp("ABC", "abc")?

a)

0

b)

A positive number

c)

A negative number

d)

None of the above

58.

Which function appends a portion of a string to another string?

a)

strcat()

b)

strncat()

c)

strappend()

d)

strmerge()

59.

Which function is used to tokenize a string in C?

a)

strslice()

b)

strparse()

c)

strtok()

d)

strsplit()

60.

What will be the output of strcat("Hello", " World")?

a)

HelloWorld

b)

Hello World

c)

Hello

d)

World

61.

Which function would you use to find a substring within a string?

a)

strfind()

b)

strstr()

c)

strloc()

d)

strindex()

62.

What does the strchr() function do?

a)

Finds a character in a string

b)

Compares two strings

c)

Finds a substring in a string

d)

Copies one string to another

63.

Which function is used to duplicate a string in C?

a)

strdup()

b)

strcopy()

c)

strcpy()

d)

strddup()

64.

How is a string represented in C?

a)

Array of integers

b)

Array of characters

c)

Pointer to an integer

d)

Linked list of characters

65.

What is the purpose of strncpy() function?

a)

To copy a string completely

b)

To copy only a portion of a string

c)

To compare two strings

d)

To find the length of a string

66.

Which function converts a string to an integer in C?

a)

strtoi()

b)

atoi()

c)

strint()

d)

convert()

67.

Which function can be used to convert a string to lowercase?

a)

strlower()

b)

tolower()

c)

strlwr()

d)

tolowercase()

68.

What does strrev() do?

a)

Reverses a string

b)

Finds the length of a string

c)

Finds a substring in a string

d)

Copies a string

69.

Which function converts an integer to a string in C?

a)

itoa()

b)

strint()

c)

strnum()

d)

numstr()

70.

What will be the output of strcat("abc", "def")?

a)

abcdef

b)

defabc

c)

abc def

d)

abc