Unit 5: Introduction to Arrays - Practice Quiz

CAP1008 — C Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which of the following is the correct way to declare an integer array of size 10 in C?

declaring arrays in C Easy
A. array int arr[10];
B. int arr(10);
C. int 10[arr];
D. int arr[10];

2 In C, what is the index of the first element of an array?

declaring arrays in C Easy
A. Depends on the size
B.
C.
D.

3 For an array declared as int a[5];, what is the valid range of indices?

declaring arrays in C Easy
A. to
B. to
C. to
D. to

4 Which statement correctly initializes a 1D array at the time of declaration?

defining and processing 1d and 2d arrays Easy
A. int a[3] = {1, 2, 3};
B. int a[3] = (1, 2, 3);
C. int a[3] = 1, 2, 3;
D. int a(3) = {1, 2, 3};

5 How is an element in a 2D array m accessed at row i and column j?

defining and processing 1d and 2d arrays Easy
A. m[i, j]
B. m(i)(j)
C. m[i][j]
D. m[i + j]

6 How many elements can be stored in an array declared as int a[3][4];?

defining and processing 1d and 2d arrays Easy
A.
B.
C.
D.

7 Which loop is most commonly used to process all elements of a 1D array?

defining and processing 1d and 2d arrays Easy
A. goto statement
B. if statement
C. switch statement
D. for loop

8 Which searching technique checks each element of an array one by one?

array applications: sorting and searching Easy
A. Binary search
B. Jump search
C. Hash search
D. Linear search

9 Binary search requires the array to be:

array applications: sorting and searching Easy
A. Two-dimensional
B. Unsorted
C. Empty
D. Sorted

10 Which of the following is a sorting algorithm?

array applications: sorting and searching Easy
A. Bubble sort
B. Binary search
C. Sequential access
D. Linear search

11 In C, a string is typically stored using which type of array?

character arrays Easy
A. Float array
B. Double array
C. Character array
D. Integer array

12 Which character marks the end of a string stored in a character array?

character arrays Easy
A. \n
B. \t
C. \0
D. 0

13 How many bytes are needed to store the string "Hi" in a character array?

declaration and initialization of string Easy
A.
B.
C.
D.

14 Which of the following correctly initializes a string in C?

declaration and initialization of string Easy
A. char s[] = "Hello";
B. string s = "Hello";
C. char s[] = 'Hello';
D. char s = "Hello";

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

string handling functions Easy
A. strlen()
B. strcat()
C. strcpy()
D. strcmp()

16 Which function is used to copy one string into another?

string handling functions Easy
A. strrev()
B. strcpy()
C. strlen()
D. strcmp()

17 Which header file must be included to use string handling functions like strcat()?

string handling functions Easy
A. <stdlib.h>
B. <stdio.h>
C. <math.h>
D. <string.h>

18 Which keyword is used to define a structure in C?

structure and union: declaration, definition and initialization Easy
A. record
B. class
C. structure
D. struct

19 In a union, all members share:

structure and union: declaration, definition and initialization Easy
A. A fixed size of 4 bytes
B. Separate memory locations
C. Different data types only
D. The same memory location

20 Which operator is used to access a member of a structure variable?

structure and union: declaration, definition and initialization Easy
A. Comma operator ,
B. Dot operator .
C. Scope operator ::
D. Arrow operator ->

21 What is the total number of bytes allocated for the array declared as int arr[10]; on a system where sizeof(int) is 4?

declaring arrays in C Medium
A. bytes
B. bytes
C. bytes
D. bytes

22 Which of the following array declarations is invalid in standard C?

declaring arrays in C Medium
A. int arr[3] = {0};
B. int arr[5];
C. int arr[] = {1, 2, 3};
D. int arr[];

23 Given int a[5] = {1, 2, 3};, what are the values of a[3] and a[4]?

defining and processing 1d and 2d arrays Medium
A. a[3] is and a[4] is
B. Both are garbage values
C. Both are
D. Both are

24 For a 2D array declared as int m[3][4]; stored in row-major order, what is the offset (in elements) of m[2][1] from the start of the array?

defining and processing 1d and 2d arrays Medium
A.
B.
C.
D.

25 What does the following loop print?

C
int a[4] = {2, 4, 6, 8};
int s = 0;
for (int i = 0; i < 4; i++)
    s += a[i];
printf("%d", s);

defining and processing 1d and 2d arrays Medium
A. 24
B. 10
C. 20
D. 18

26 Which initialization correctly creates a 2D array with 2 rows and 3 columns, all initialized to zero?

defining and processing 1d and 2d arrays Medium
A. int m[2][3] = {0};
B. int m[][] = {0};
C. int m[2][3] = 0;
D. int m[2,3] = {0};

27 In a single pass of Bubble Sort (ascending) over the array {5, 1, 4, 2}, what is the array after the first complete pass?

array applications: sorting and searching Medium
A. {1, 5, 4, 2}
B. {1, 2, 4, 5}
C. {5, 4, 2, 1}
D. {1, 4, 2, 5}

28 Binary search requires that the array is sorted. On the sorted array {2, 5, 8, 12, 16, 23}, how many comparisons are needed to find (comparing the middle element each time)?

array applications: sorting and searching Medium
A.
B.
C.
D.

29 What is the worst-case time complexity of linear search on an array of elements?

array applications: sorting and searching Medium
A.
B.
C.
D.

30 In Selection Sort (ascending) applied to {29, 10, 14, 37, 13}, what is the array after the first pass?

array applications: sorting and searching Medium
A. {29, 10, 13, 14, 37}
B. {10, 14, 13, 29, 37}
C. {13, 10, 14, 37, 29}
D. {10, 29, 14, 37, 13}

31 How many bytes must a character array reserve to safely store the string "HELLO"?

character arrays Medium
A. bytes
B. bytes
C. bytes
D. bytes

32 What does the following code print?

C
char c[] = {'C', 'O', 'D', 'E'};
printf("%d", sizeof(c));

character arrays Medium
A. 4
B. 5
C. 1
D. 8

33 Which of the following correctly declares and initializes a string in C?

declaration and initialization of string Medium
A. char s[] = 'Hi';
B. string s = "Hi";
C. char s[] = "Hi";
D. char s = "Hi";

34 What is sizeof(str) for char str[] = "India";?

declaration and initialization of string Medium
A.
B.
C.
D.

35 What value does strcmp("apple", "apple") return?

string handling functions Medium
A.
B.
C. The length of the string
D.

36 After executing the following code, what is stored in dest?

C
char dest[20] = "Good";
strcat(dest, "Day");

string handling functions Medium
A. Good Day
B. Day
C. GoodDay
D. DayGood

37 What does strlen("Program\0ming") return?

string handling functions Medium
A.
B.
C.
D.

38 Consider struct P { char c; int x; double d; };. On a typical system with 4-byte int, 8-byte double, and standard alignment, what is the most likely value of sizeof(struct P)?

structure and union: declaration, definition and initialization Medium
A.
B.
C.
D.

39 For union U { int i; char c; };, what is sizeof(union U) on a system with 4-byte int?

structure and union: declaration, definition and initialization Medium
A.
B.
C.
D.

40 What does the following code print?

C
struct Pt { int x, y; };
struct Pt p = {3, 7};
printf("%d", p.x + p.y);

structure and union: declaration, definition and initialization Medium
A. 37
B. 21
C. 10
D. 4

41 Consider the declaration int arr[5] = {1, 2};. What are the values of arr[2], arr[3], and arr[4]?

declaring arrays in C Hard
A. All are 2
B. All are 0
C. All are garbage values
D. A compile-time error occurs

42 Which of the following declarations is invalid in standard C89 (ANSI C)?

declaring arrays in C Hard
A. static int arr[100];
B. int arr[] = {1, 2, 3};
C. int arr[n]; where n is a non-const variable
D. int arr[3] = {0};

43 Given int a[3][4];, if the base address is 1000 and sizeof(int) is 4, what is the address of a[2][3] using row-major order?

defining and processing 1d and 2d arrays Hard
A. 1024
B. 1044
C. 1040
D. 1036

44 What does the expression *(*(arr + 1) + 2) evaluate to for a 2D array arr?

defining and processing 1d and 2d arrays Hard
A. arr[1][2]
B. arr[2][1]
C. arr[3]
D. *(arr + 3)

45 In int a[] = {10, 20, 30, 40, 50};, what is the value of *(a + 3) - *a?

defining and processing 1d and 2d arrays Hard
A. 30
B. 40
C. 10
D. 3

46 During a single pass of bubble sort (ascending) on [5, 1, 4, 2, 8], what is the array state after the first complete pass?

array applications: sorting and searching Hard
A. [1, 5, 4, 2, 8]
B. [1, 4, 2, 5, 8]
C. [1, 4, 2, 8, 5]
D. [1, 2, 4, 5, 8]

47 For binary search on a sorted array of 1000 elements, what is the maximum number of comparisons needed in the worst case?

array applications: sorting and searching Hard
A. 1000
B. 500
C. 9
D. 10

48 Which condition must hold for binary search to work correctly, and what happens if it is violated?

array applications: sorting and searching Hard
A. The array must be sorted; violating it may return wrong results or miss the target
B. The array must be of even length; violating it causes a crash
C. The array must contain unique values; violating it causes an infinite loop
D. The array must be sorted; violating it always throws a runtime error

49 What is the value of sizeof(str) for char str[] = "Hello";?

character arrays Hard
A. 4
B. 7
C. 5
D. 6

50 Given char arr[5] = {'H', 'e', 'l', 'l', 'o'};, what happens when you pass arr to printf("%s", arr);?

character arrays Hard
A. It causes a compile-time error
B. It prints exactly Hello
C. Undefined behavior, as there is no null terminator
D. It prints Hell only

51 What is the key difference between char *s = "Hello"; and char s[] = "Hello";?

declaration and initialization of string Hard
A. The pointer version points to read-only memory; modifying it is undefined behavior, while the array is modifiable
B. The array version points to read-only memory, while the pointer version is modifiable
C. The pointer version allocates 6 bytes on the stack like the array
D. Both are stored identically and both are fully modifiable

52 For char str[10] = "Hi";, what are the contents of str[2] through str[9]?

declaration and initialization of string Hard
A. str[2] is \0, the rest are garbage
B. All are \0 (null characters)
C. All are spaces
D. All are garbage values

53 What is the result of strlen(str) after executing char str[20] = "abc"; str[1] = '\0';?

string handling functions Hard
A. 1
B. 2
C. 3
D. 0

54 Given char s1[10] = "abc", s2[] = "abd";, what does strcmp(s1, s2) return?

string handling functions Hard
A. -3
B. 0
C. A negative value
D. A positive value

55 What is the danger of using strcat(dest, src) when dest was declared as char dest[6] = "Hi"; and src is "World"?

string handling functions Hard
A. It causes a compile-time size error
B. It returns NULL and leaves dest unchanged
C. Buffer overflow, since the result exceeds dest's capacity
D. It safely truncates src to fit dest

56 Consider struct P { char c; int i; double d; };. On a system with 4-byte int, 8-byte double, and standard alignment, what is the likely sizeof(struct P)?

structure and union: declaration, definition and initialization Hard
A. 8
B. 13
C. 16
D. 24

57 Given union U { int i; char c[4]; }; on a little-endian system, after union U u; u.i = 1;, what is u.c[0]?

structure and union: declaration, definition and initialization Hard
A. 4
B. Undefined
C. 1
D. 0

58 What is sizeof a union union U { int i; char c[10]; double d; }; on a system with 4-byte int, 8-byte double?

structure and union: declaration, definition and initialization Hard
A. 16
B. 10
C. 8
D. 22

59 Which statement about initializing a structure with struct Point p = {.y = 5, .x = 3}; (designated initializers) is correct?

structure and union: declaration, definition and initialization Hard
A. It is valid C99; members can be initialized out of order by name
B. It is only valid for arrays, not structures
C. It initializes both x and y to 5
D. It is invalid because members must appear in declaration order

60 Given int a[2][3] = {1, 2, 3, 4, 5, 6};, what is the value of a[1][0]?

defining and processing 1d and 2d arrays Hard
A. 5
B. 1
C. 4
D. 3