WorksheetsFCPC-MidtermExam-DSA
Total questions: 70
Worksheet time: 1hrs 10mins
Which of the following is the correct syntax for a while loop in C?
while (condition) { statement; }
while condition { statement; }
while (statement) { condition; }
while [condition] { statement; }
What will be the output of the following code? int i = 0; while(i < 5) { printf("%d", i); i++; }
12345
01234
1 2 3 4 5
5
Which of the following loops will always execute at least once?
for
while
do-while
All loops can execute at least once
What is the output of the following program? int i; for(i = 1; i <= 3; i++) { printf("%d ", i); }
1 2 3
0 1 2
1 2 3 4
3 2 1
Which of the following is the correct syntax for a do-while loop?
do { statement; } while (condition);
do statement while (condition);
while (condition) do { statement; };
do statement until (condition);
What is the correct way to declare a function that returns an integer in C?
function int() {}
int function() {}
int function {}
int function(void) {}
Which of the following is true about the function prototype in C?
It is required for all functions.
It must be placed before the function definition.
It can be omitted if the function definition is provided before the function call.
It must include the function body.
What is the output of the following code? void func(int x) { printf("%d", x); } int main() { func(5); return 0; }
5
5 0
Error
Nothing
Which of the following is the correct way to call a function named add that takes two integer parameters?
add(5, 10);
add(5, 10)
add 5, 10;
add(5,10){}
What does the return statement do in a function?
Stops the execution of the program.
Returns a value from the function.
Exits the current loop.
Defines a function's return type.
What is the symbol used to access the value of a variable through its pointer in C?
&
*
->
&&
Which of the following operators is used to get the memory address of a variable?
*
&
->
&&
What will the following code print? int a = 10; int *ptr = &a; printf("%d", *ptr);
10
0
a
Error
What is a pointer in C?
A variable that holds the memory address of another variable.
A function that returns a value.
A variable used for memory allocation.
A type of loop.
Which of the following is the correct way to declare a pointer to an integer in C?
int pointer;
pointer int;
int* pointer;
pointer* int;
Which sorting algorithm is the most efficient in terms of time complexity for large data sets?
Bubble sort
Quick sort
Insertion sort
Selection sort
What is the time complexity of the bubble sort algorithm in the worst case?
O(n)
O(n^2)
O(log n)
O(n log n)
Which of the following is the correct description of the selection sort algorithm?
It repeatedly selects the largest (or smallest) element and places it in the correct position.
It swaps adjacent elements if they are in the wrong order.
It splits the array and recursively sorts each half.
It compares each element to the previous one and inserts it in the correct position.
In which sorting algorithm is the pivot element chosen and used to partition the array?
Merge sort
Quick sort
Heap sort
Bubble sort
Which sorting algorithm has a time complexity of O(n log n) in the best, worst, and average cases?
Merge sort
Quick sort
Insertion sort
Selection sort
What is the time complexity of binary search in the worst case?
O(n)
O(n^2)
O(log n)
O(n log n)
Which of the following is true about linear search?
It requires a sorted array.
It searches by dividing the array into smaller parts.
It examines each element of the array one by one.
It has a time complexity of O(log n).
Which searching algorithm is more efficient for large arrays, assuming the array is sorted?
Linear search
Binary search
Sequential search
Hashing
What is the key requirement for performing binary search on an array?
The array must be sorted.
The array must be unsorted.
The array must be of size n.
The array must contain unique elements.
Which of the following algorithms is used for searching an element in an unsorted list?
Binary search
Linear search
Jump search
Exponential search
What is the time complexity of the linear search algorithm?
O(n)
O(n^2)
O(log n)
O(1)
What is the main advantage of binary search over linear search?
Binary search is faster for sorted arrays.
Binary search can be used on unsorted arrays.
Binary search is more accurate.
Binary search requires no comparison.
What is the worst-case time complexity of binary search?
O(1)
O(n)
O(log n)
O(n log n)
Which of the following is NOT a characteristic of binary search?
It works on sorted arrays.
It requires random access to the elements.
It can be implemented recursively.
It is slower than linear search for small arrays.
What happens when a function does not have a return statement?
It causes a compilation error.
It returns an undefined value.
It automatically returns 0.
It returns NULL.
Which of the following is true about a do-while loop?
The loop body is always executed at least once.
The loop condition is checked before the loop body is executed.
The loop condition is checked after each iteration.
None of the above.
What is the value of *ptr in the following code? int x = 10; int *ptr = &x;
10
x
ptr
&x
What is the value of *ptr in the following code? int x = 10; int *ptr = &x;
10
x
ptr
&x
Which sorting algorithm selects the smallest element and places it at the beginning of the array?
Insertion sort
Selection sort
Merge sort
Quick sort
Which of the following algorithms divides the array into subarrays and then merges them back together?
Quick sort
Merge sort
Bubble sort
Selection sort
What is the key idea behind the quicksort algorithm?
Divide the array into smaller arrays and sort them.
Repeatedly select a pivot and partition the array around it.
Swap elements one by one.
Sort the array in a single pass.
Which of the following is an advantage of quicksort over other sorting algorithms?
It guarantees O(n^2) time complexity.
It has a small auxiliary space requirement.
It is always faster than other sorting algorithms.
It is stable.
What is the average time complexity of quicksort?
O(n)
O(n^2)
O(log n)
O(n log n)
What is the worst-case time complexity of bubble sort?
O(n)
O(n log n)
O(n^2)
O(log n)
What does the merge sort algorithm use to combine the sorted subarrays?
A pivot element
A queue
An auxiliary array
A stack
What is the worst-case time complexity of selection sort?
O(1)
O(n log n)
O(n^2)
O(log n)
What is the average-case time complexity of insertion sort?
O(n log n)
O(n^2)
O(n)
O(log n)
In which of the following algorithms is the "divide and conquer" strategy used?
Selection sort
Merge sort
Bubble sort
Insertion sort
Which of the following is a characteristic of the binary search algorithm?
It works on sorted arrays.
It checks each element one by one.
It has a time complexity of O(n).
It works on unsorted arrays.
What is the main disadvantage of the quicksort algorithm?
It requires extra space.
It is unstable.
It has poor performance in the worst case.
It requires a sorted array.
Which of the following algorithms is unstable?
Merge sort
Quick sort
Insertion sort
Bubble sort
What is the key operation in binary search?
Checking the middle element and deciding whether to search the left or right half.
Comparing elements with adjacent ones.
Sorting the array.
Dividing the array into smaller chunks.
Which of the following searching algorithms is most efficient for a sorted array?
Linear search
Binary search
Jump search
Exponential search
What is the space complexity of the merge sort algorithm?
O(1)
O(n)
O(log n)
O(n log n)
Which of the following is the correct way to initialize a pointer to NULL?
int *ptr = 0;
int *ptr = NULL;
int *ptr; ptr = NULL;
All of the above
Which header file is required to use string functions in C?
stdio.h
conio.h
string.h
stdlib.h
Which function is used to find the length of a string in C?
strlen()
strlength()
sizeof()
strsize()
What does the strcmp() function return if two strings are identical?
1
-1
0
Depends on the strings
Which of the following functions is used to concatenate two strings?
strcat()
strjoin()
stradd()
strmerge()
What is the output of strlen("hello")?
4
5
6
0
Which function is used to copy one string to another in C?
strcpy()
strcopy()
copystr()
strncpy()
What is the value returned by strcmp("ABC", "abc")?
0
A positive number
A negative number
None of the above
Which function appends a portion of a string to another string?
strcat()
strncat()
strappend()
strmerge()
Which function is used to tokenize a string in C?
strslice()
strparse()
strtok()
strsplit()
What will be the output of strcat("Hello", " World")?
HelloWorld
Hello World
Hello
World
Which function would you use to find a substring within a string?
strfind()
strstr()
strloc()
strindex()
What does the strchr() function do?
Finds a character in a string
Compares two strings
Finds a substring in a string
Copies one string to another
Which function is used to duplicate a string in C?
strdup()
strcopy()
strcpy()
strddup()
How is a string represented in C?
Array of integers
Array of characters
Pointer to an integer
Linked list of characters
What is the purpose of strncpy() function?
To copy a string completely
To copy only a portion of a string
To compare two strings
To find the length of a string
Which function converts a string to an integer in C?
strtoi()
atoi()
strint()
convert()
Which function can be used to convert a string to lowercase?
strlower()
tolower()
strlwr()
tolowercase()
What does strrev() do?
Reverses a string
Finds the length of a string
Finds a substring in a string
Copies a string
Which function converts an integer to a string in C?
itoa()
strint()
strnum()
numstr()
What will be the output of strcat("abc", "def")?
abcdef
defabc
abc def
abc
