Unit 4 - Practice Quiz

CSE101 61 Questions
0 Correct 0 Wrong 61 Left
0/61

1 What is the correct syntax to declare an integer array named grades that can hold 10 values?

Declaring and initializing arrays in C Easy
A. int grades;
B. int grades[10];
C. array grades[10];
D. grades int[10];

2 In C, the index of the first element in an array is always:

Declaring and initializing arrays in C Easy
A. 1
B. The size of the array
C. 0
D. -1

3 Which of the following correctly initializes an integer array nums of size 3 with values 5, 10, and 15?

Declaring and initializing arrays in C Easy
A. int nums{} = {5, 10, 15};
B. int nums(3) = (5, 10, 15);
C. int nums[3] = (5, 10, 15);
D. int nums[3] = {5, 10, 15};

4 For an array declared as float prices[20];, what is the index of the last element?

Defining and processing 1D and 2D arrays Easy
A. 19
B. 20
C. 0
D. Undefined

5 How do you correctly declare a 2D array named matrix with 3 rows and 5 columns of integers?

Defining and processing 1D and 2D arrays Easy
A. int matrix[3][5];
B. int matrix(3)(5);
C. int matrix[3, 5];
D. int matrix[5][3];

6 Given the array int arr[] = {10, 20, 30, 40, 50};, what value does arr[2] hold?

Defining and processing 1D and 2D arrays Easy
A. 40
B. 30
C. 20
D. 10

7 Which of the following is a primary characteristic of arrays in C?

Array applications Easy
A. They can store elements of different data types.
B. They are a type of dynamic data structure.
C. Their size can be changed during program execution.
D. They store elements of the same data type.

8 When an array is passed as an argument to a function in C, what is actually being passed?

Passing arrays to functions Easy
A. The memory address of the first element of the array.
B. A copy of the entire array.
C. The last element of the array.
D. The size of the array.

9 Which function prototype correctly declares a function display that accepts a one-dimensional integer array data as a parameter?

Passing arrays to functions Easy
A. void display(array data);
B. void display(int &data);
C. void display(int);
D. void display(int data[]);

10 What is the fundamental principle of a Linear Search algorithm?

Searching including linear and binary search methods Easy
A. It requires the array to be sorted beforehand.
B. It divides the array in half with each comparison.
C. It checks each element of the array one by one from the beginning.
D. It starts searching from the middle of the array.

11 Which of these conditions is required for a Binary Search to work correctly?

Searching including linear and binary search methods Easy
A. The array must have an even number of elements.
B. The array must be unsorted.
C. The array must only contain integers.
D. The array must be sorted.

12 In the worst-case scenario, how many comparisons does a linear search make on an array of size ?

Searching including linear and binary search methods Easy
A.
B. 1
C.
D.

13 What is the basic operation in the Bubble Sort algorithm?

Sorting of array using bubble sort Easy
A. Placing each element in its correct sorted position directly.
B. Comparing two adjacent elements and swapping them if they are in the wrong order.
C. Finding the minimum element and placing it at the beginning.
D. Dividing the array into two halves and sorting them recursively.

14 After the first pass of a Bubble Sort on the array {3, 7, 2, 1}, what will the array look like? (Assuming sorting in ascending order).

Sorting of array using bubble sort Easy
A. {3, 7, 1, 2}
B. {1, 2, 3, 7}
C. {2, 1, 3, 7}
D. {3, 2, 1, 7}

15 What is a characteristic of Bubble Sort?

Sorting of array using bubble sort Easy
A. It requires a second array to perform the sort.
B. It is the fastest known sorting algorithm.
C. It is a simple sorting algorithm but not very efficient for large datasets.
D. It works by dividing the array into smaller parts.

16 To insert a new element into a full array at a specific position, what must you typically do first?

inserting and deleting elements of an array Easy
A. Shift all preceding elements one position to the left.
B. Shift all subsequent elements one position to the right.
C. Create a new, larger array.
D. Delete the last element to make space.

17 When an element is deleted from an array, what happens to the gap that is created?

inserting and deleting elements of an array Easy
A. The gap is left empty.
B. The size of the array automatically decreases.
C. The gap is filled with a zero or null value.
D. The elements after the gap are shifted one position to the left.

18 Why are arrays often referred to as a 'static' data structure in the context of C?

Array applications Easy
A. They do not use memory from the heap.
B. They can only store static variables.
C. The elements within them cannot be modified.
D. Their size is fixed at the time of declaration and cannot be changed during runtime.

19 In a 2D array declared as int arr[4][5];, how many elements can it store in total?

Defining and processing 1D and 2D arrays Easy
A. 5
B. 20
C. 4
D. 9

20 For which of the following scenarios would a Linear Search be more suitable than a Binary Search?

Searching including linear and binary search methods Easy
A. When search time is the most critical factor.
B. When the array is very large and sorted.
C. When the array is stored in a linked list.
D. When the array is small and unsorted.

21 What will be the contents of the array a after the following declaration?

c
int a[5] = {10, 20};

Declaring and initializing arrays in C Medium
A. This will cause a compilation error.
B. {10, 20, 20, 20, 20}
C. {10, 20, (garbage), (garbage), (garbage)}
D. {10, 20, 0, 0, 0}

22 What is the output of the following C code snippet, assuming int is 4 bytes?

c
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d", ((a + 1) + 2));

Defining and processing 1D and 2D arrays Medium
A. 6
B. Address of the element 6
C. 3
D. 5

23 Predict the output of the following C program:

c
#include <stdio.h>
void modifyArray(int arr[], int size) {
arr[0] = 99;
}
int main() {
int numbers[] = {10, 20, 30};
modifyArray(numbers, 3);
printf("%d", numbers[0]);
return 0;
}

Passing arrays to functions Medium
A. 10
B. 99
C. A garbage value
D. Compilation Error

24 What is the primary prerequisite for an array to be a valid input for the Binary Search algorithm?

Searching including linear and binary search methods Medium
A. The array must be sorted.
B. The array must contain only positive integers.
C. The size of the array must be a power of 2.
D. The array must not contain any duplicate elements.

25 Given the array [5, 1, 4, 2, 8], how many swaps are performed during the first pass of a standard Bubble Sort algorithm (sorting in ascending order)?

Sorting of array using bubble sort Medium
A. 2
B. 4
C. 3
D. 5

26 To insert an element at index k in an array of capacity C that currently holds n elements (where n < C), which of the following describes the correct procedure?

Inserting and deleting elements of an array Medium
A. Place the new element at index n and swap it with the element at index k.
B. Place the new element at index k and shift the old element from index k to index n.
C. Shift all elements from index k to n-1 one position to the left, then place the new element at index k.
D. Shift all elements from index k to n-1 one position to the right, then place the new element at index k.

27 Given the declaration int matrix[3][4]; on a system where an int takes 4 bytes, what is the memory address of matrix[1][2] if the base address of matrix is 2000? (Assume row-major order)

Defining and processing 1D and 2D arrays Medium
A. 2020
B. 2014
C. 2024
D. 2028

28 Which of the following C code snippets correctly calculates the sum of the anti-diagonal elements of a 3x3 square matrix mat?

Array applications Medium
A. for(i=0; i<3; i++) { sum += mat[i][2-i]; }
B. for(i=0; i<3; i++) { sum += mat[i][0]; }
C. for(i=0; i<3; i++) { for(j=0; j<3; j++) { sum += mat[i][j]; } }
D. for(i=0; i<3; i++) { sum += mat[i][i]; }

29 Which of the following is a syntactically correct prototype for a function that accepts a 2D integer array with an unspecified number of rows but exactly 5 columns?

Passing arrays to functions Medium
A. void process(int arr[][5]);
B. void process(int **arr);
C. void process(int arr[][]);
D. void process(int *arr[5]);

30 In a binary search algorithm, what is the approximate maximum number of comparisons required to find an element in a sorted array of 1,000,000 elements?

Searching including linear and binary search methods Medium
A. 10
B. 500,000
C. 20
D. 100

31 What is the state of the array [7, 2, 8, 4, 1] after the completion of the second pass of Bubble Sort (ascending order)?

Sorting of array using bubble sort Medium
A. [2, 4, 1, 7, 8]
B. [7, 2, 4, 1, 8]
C. [2, 4, 7, 1, 8]
D. [2, 7, 4, 1, 8]

32 Identify the primary logical flaw in the following C code snippet for deleting an element at pos from an array arr of size n.

c
for (i = pos; i < n; i++) {
arr[i] = arr[i + 1];
}
n = n - 1;

Inserting and deleting elements of an array Medium
A. The loop should start from i = pos + 1.
B. The assignment should be arr[i - 1] = arr[i].
C. The loop condition i < n leads to an out-of-bounds memory access.
D. The size n should be decremented before the loop begins.

33 What is the output of sizeof(arr) for the declaration double arr[] = {1.0, 2.0, 3.0}; on a typical 64-bit system where sizeof(double) is 8?

Declaring and initializing arrays in C Medium
A. Compilation Error
B. 12
C. 24
D. 3

34 Given the declaration int a[3][3];, which expression correctly represents the address of the element a[1][0]?

Defining and processing 1D and 2D arrays Medium
A. **a + 1
B. *a + 1
C. *(a + 1)
D. a[0] + 1

35 An efficient algorithm to find the single unique number in an array where all other numbers appear exactly twice is to use the XOR operator. What property of XOR makes this possible?

Array applications Medium
A. The property that x ^ 1 = ~x (bitwise NOT).
B. XOR is faster than addition or multiplication.
C. The property that x ^ x = 0 and x ^ 0 = x.
D. The property that x ^ y = y ^ x (commutative property) only.

36 For an array that is already sorted in ascending order, what is the time complexity of an optimized Bubble Sort algorithm that terminates if no swaps are made in a pass?

Sorting of array using bubble sort Medium
A.
B.
C.
D.

37 An array contains [10, 20, 30, 40, 50, 60, 70, 80]. How many comparisons are required to find the element 75 using binary search, ultimately concluding it's not present?

Searching including linear and binary search methods Medium
A. 7
B. 8
C. 4
D. 3

38 What is the output of the following program?

c
#include <stdio.h>
void func(int p) {
p = p + 3;
p = 100;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
func(arr);
printf("%d", arr[3]);
return 0;
}

Passing arrays to functions Medium
A. 5
B. 3
C. 4
D. 100

39 What is the output of this C code?

c
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int p = arr;
printf("%d",
(p++ + 3));
return 0;
}

Defining and processing 1D and 2D arrays Medium
A. 50
B. 40
C. Undefined Behavior
D. 30

40 What will be the final contents of the array after the following code snippet is executed?

c
int arr[10] = {5, 10, 15, 20, 25};
int i, n = 5;
int pos = 1; // position to delete

for (i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;

Inserting and deleting elements of an array Medium
A. {10, 15, 20, 25, 25}
B. {5, 10, 20, 25, 25}
C. {5, 15, 20, 25, (garbage)}
D. {5, 15, 20, 25, 25}

41 Consider the following C code snippet. What will be the output?

c
#include <stdio.h>

void modify(int arr[5]) {
arr = NULL; // Reassigning the pointer parameter
}

void update(int arr) {
(arr + 2) = 100;
}

int main() {
int data[] = {10, 20, 30, 40, 50};
modify(data);
update(data);
printf("%d %d", data[0], data[2]);
return 0;
}

Passing arrays to functions Hard
A. Compiler Error
B. 10 30
C. Causes a segmentation fault
D. 10 100

42 Given the declaration int matrix[4][5]; and assuming int is 4 bytes and the base address of matrix is 2000. What is the memory address of matrix[2][3] calculated using row-major ordering?

Defining and processing 2D arrays Hard
A. 2038
B. 2060
C. 2052
D. 2044

43 A standard recursive binary search algorithm is implemented. If it is used to search for a non-existent element in a sorted array of size n = 2^k for some integer k > 0, what is the maximum depth of the recursion tree?

Searching including linear and binary search methods Hard
A. k + 1
B. k - 1
C. 2^k
D. k

44 What is the exact number of swaps performed by a standard bubble sort algorithm to sort the array A = {6, 1, 2, 3, 4, 5}?

Sorting of array using bubble sort Hard
A. 5
B. 6
C. 10
D. 15

45 What is the output of the following C program?

c
#include <stdio.h>

int main() {
int arr[10] = {[3] = 5, [8] = 9};
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += arr[i];
}
printf("%d\n", sum);
return 0;
}

Declaring and initializing arrays in C Hard
A. Undefined Behavior
B. 0
C. Compiler Error
D. 14

46 What is the result of the expression *(*(*(arr + 1) + 1) + 1) for the 3D array int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};?

Defining and processing 2D arrays Hard
A. Compiler Error
B. 4
C. 8
D. 6

47 Analyze the pointer arithmetic in the following expression. What is its final value?
c
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
int val = ***(arr + 1) + 1;

Defining and processing 2D arrays Hard
A. Compiler Error due to type mismatch
B. 6
C. 8
D. 2

48 Which of the following function signatures is NOT a valid way to accept a 2D array int m[5][10] as a parameter in C?

Passing arrays to functions Hard
A. void func(int m[5][10]);
B. void func(int (*m)[10]);
C. void func(int **m);
D. void func(int m[][10]);

49 An array of size N currently contains n sorted elements. What is the tightest worst-case time complexity to insert k new elements into the array while maintaining the sorted order, assuming n + k <= N?

Inserting and deleting elements of an array Hard
A.
B.
C.
D.

50 Consider a sorted array that has been rotated k times, e.g., {6, 7, 8, 1, 2, 3, 4, 5}. Which condition inside a modified binary search correctly determines if the target element T lies in the sorted left half of the current search space [low...high]?

Searching including linear and binary search methods Hard
A. (arr[low] <= arr[mid] && T >= arr[low]) || T < arr[mid]
B. arr[mid] < T && T <= arr[high]
C. arr[low] <= T && T < arr[mid]
D. arr[low] <= arr[mid] && T >= arr[low] && T < arr[mid]

51 What is the output of this C code, assuming a 64-bit system where pointers are 8 bytes?

c
#include <stdio.h>

void print_size(int arr[10]) {
printf("%zu", sizeof(arr));
}

int main() {
int data[10];
printf("%zu ", sizeof(data));
print_size(data);
return 0;
}

Passing arrays to functions Hard
A. 8 8
B. 40 8
C. 8 40
D. 40 40

52 A polynomial is represented by an integer array p_arr where p_arr[i] stores the coefficient of the term. If a function differentiate takes this array and returns a new array representing the derivative , what will be the value of the element at index 4 in the returned array?

Array applications Hard
A. 20
B. 5
C. 4
D. 0

53 For an array of n elements, what is the time complexity of an optimized bubble sort algorithm (that terminates if no swaps occur in a pass) when the input array is already sorted in descending order?

Sorting of array using bubble sort Hard
A. for comparisons, for swaps
B. for comparisons, for swaps
C. for comparisons, for swaps
D. for comparisons, for swaps

54 What will be the values of arr[0][1] and arr[1][2] after this initialization?

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

Declaring and initializing arrays in C Hard
A. arr[0][1] is 1, arr[1][2] is 5
B. arr[0][1] is 0, arr[1][2] is 5
C. arr[0][1] is 0, arr[1][2] is 0
D. arr[0][1] is undefined, arr[1][2] is undefined

55 Given int arr[] = {10, 20, 30, 40, 50};, and int *p = &arr[1], *q = &arr[4];. What is the value of the expression *(q - 2) + (p - arr)?

Defining and processing 1D and 2D arrays Hard
A. 30
B. 41
C. Undefined Behavior
D. 31

56 Consider the following C function to delete an element from an array. What is the primary issue with this code?

c
// 'size' is a pointer to the variable storing the array's current size.
void deleteElement(int arr[], int size, int index) {
if (index >=
size || index < 0) return;

for (int i = index; i < size; i++) {
arr[i] = arr[i+1];
}
(
size)--;
}

Inserting and deleting elements of an array Hard
A. The loop condition i < *size causes it to read beyond the valid data range.
B. The function fails to handle deletion of the first element (index = 0).
C. The function does not work for arrays with duplicate elements.
D. Decrementing *size should happen before the loop.

57 A function void process(int n, int arr[n]) uses a Variable Length Array (VLA) parameter, a C99 feature. What is a significant consequence of using VLAs as parameters in recursive functions?

Passing arrays to functions Hard
A. It improves performance by allowing dynamic memory allocation.
B. It can lead to rapid stack overflow, even for moderate recursion depths.
C. It causes memory leaks because VLAs are allocated on the heap.
D. It is not possible to use VLAs in recursive functions.

58 You need to implement an in-place transpose for a square matrix A[N][N]. Which loop structure correctly performs the transpose without undoing the swaps?

c
// Assume swap(a, b) correctly swaps the values of a and b.

Array applications Hard
A. c
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
swap(&A[i][j], &A[j][i]);
}
}
B. c
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
swap(&A[i][j], &A[j][i]);
}
}
C. c
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
A[j][i] = A[i][j];
}
}
D. c
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
swap(&A[i][j], &A[j][i]);
}
}

59 A binary search algorithm has a bug in its middle calculation: mid = (low + high) / 2;. On a system where int is a 32-bit signed integer, which scenario could cause this line to fail by producing an incorrect mid value?

Searching including linear and binary search methods Hard
A. This calculation is always safe and will not fail.
B. When low is negative and high is positive.
C. When low and high are both very large positive numbers.
D. When the array contains duplicate elements.

60 What is the value of sizeof(arr) in the following C code snippet?

c
char arr[] = "Array";

Declaring and initializing arrays in C Hard
A. 4
B. 5
C. Depends on the compiler
D. 6

61 For the C declaration int arr[3][4];, which of the following expressions will NOT result in the address of the element arr[1][0]?

Defining and processing 2D arrays Hard
A. *(arr + 1)
B. arr[1]
C. &arr[1][0]
D. arr + 4