Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Which of the following is the correct way to declare an integer array of size 10 in C?
Not quite. Try another option.
Answer: int arr[10];
An array is declared by writing the data type, followed by the array name and the size enclosed in square brackets, e.g. int arr[10];.
Question 2 of 60 · declaring arrays in C · Easy
In C, what is the index of the first element of an array?
Not quite. Try another option.
Answer:
Array indexing in C is zero-based, so the first element is always at index .
Question 3 of 60 · declaring arrays in C · Easy
For an array declared as int a[5];, what is the valid range of indices?
Not quite. Try another option.
Answer: to
An array of size 5 has 5 elements indexed from to (size minus one).
Question 4 of 60 · defining and processing 1d and 2d arrays · Easy
Which statement correctly initializes a 1D array at the time of declaration?
Not quite. Try another option.
Answer: int a[3] = {1, 2, 3};
Array initialization uses curly braces {} with comma-separated values, e.g. int a[3] = {1, 2, 3};.
Question 5 of 60 · defining and processing 1d and 2d arrays · Easy
How is an element in a 2D array m accessed at row i and column j?
Not quite. Try another option.
Answer: m[i][j]
A 2D array element is accessed using two subscripts: the row index and the column index, as in m[i][j].
Question 6 of 60 · defining and processing 1d and 2d arrays · Easy
How many elements can be stored in an array declared as int a[3][4];?
Not quite. Try another option.
Answer:
A 2D array of dimensions can store elements.
Question 7 of 60 · defining and processing 1d and 2d arrays · Easy
Which loop is most commonly used to process all elements of a 1D array?
Not quite. Try another option.
Answer: for loop
A for loop is typically used to iterate through array elements using an index variable.
Question 8 of 60 · array applications: sorting and searching · Easy
Which searching technique checks each element of an array one by one?
Not quite. Try another option.
Answer: Linear search
Linear search examines each element sequentially until the target is found or the array ends.
Question 9 of 60 · array applications: sorting and searching · Easy
Binary search requires the array to be:
Not quite. Try another option.
Answer: Sorted
Binary search works by repeatedly dividing a sorted array, so the elements must be in order.
Question 10 of 60 · array applications: sorting and searching · Easy
Which of the following is a sorting algorithm?
Not quite. Try another option.
Answer: Bubble sort
Bubble sort is a sorting algorithm that repeatedly swaps adjacent elements that are out of order.
Question 11 of 60 · character arrays · Easy
In C, a string is typically stored using which type of array?
Not quite. Try another option.
Answer: Character array
Strings in C are stored as arrays of characters (char), terminated by a null character.
Question 12 of 60 · character arrays · Easy
Which character marks the end of a string stored in a character array?
Not quite. Try another option.
Answer: \0
The null character \0 marks the end of a string in C.
Question 13 of 60 · declaration and initialization of string · Easy
How many bytes are needed to store the string "Hi" in a character array?
Not quite. Try another option.
Answer:
The string "Hi" needs 2 bytes for the characters plus 1 byte for the null terminator \0, totaling bytes.
Question 14 of 60 · declaration and initialization of string · Easy
Which of the following correctly initializes a string in C?
Not quite. Try another option.
Answer: char s[] = "Hello";
A string literal is enclosed in double quotes and can initialize a character array, e.g. char s[] = "Hello";.
Question 15 of 60 · string handling functions · Easy
Which function is used to find the length of a string in C?
Not quite. Try another option.
Answer: strlen()
strlen() returns the number of characters in a string, not counting the null terminator.
Question 16 of 60 · string handling functions · Easy
Which function is used to copy one string into another?
Not quite. Try another option.
Answer: strcpy()
strcpy() copies the contents of a source string into a destination string.
Question 17 of 60 · string handling functions · Easy
Which header file must be included to use string handling functions like strcat()?
Not quite. Try another option.
Answer: <string.h>
String handling functions such as strcat(), strcpy(), and strlen() are declared in <string.h>.
Question 18 of 60 · structure and union: declaration, definition and initialization · Easy
Which keyword is used to define a structure in C?
Not quite. Try another option.
Answer: struct
The struct keyword is used to define a structure that groups related variables of different types.
Question 19 of 60 · structure and union: declaration, definition and initialization · Easy
In a union, all members share:
Not quite. Try another option.
Answer: The same memory location
In a union, all members share the same memory, so only one member holds a valid value at a time.
Question 20 of 60 · structure and union: declaration, definition and initialization · Easy
Which operator is used to access a member of a structure variable?
Not quite. Try another option.
Answer: Dot operator .
The dot operator . is used to access members of a structure variable, e.g. student.age.
Question 21 of 60 · declaring arrays in C · Medium
What is the total number of bytes allocated for the array declared as int arr[10]; on a system where sizeof(int) is 4?
Not quite. Try another option.
Answer: bytes
An array of 10 integers occupies 10 * sizeof(int) bytes. With sizeof(int) = 4, the total is bytes.
Question 22 of 60 · declaring arrays in C · Medium
Which of the following array declarations is invalid in standard C?
Not quite. Try another option.
Answer: int arr[];
An array must have a size, either given explicitly or inferred from an initializer list. int arr[]; provides neither a size nor an initializer, so it is invalid.
Question 23 of 60 · defining and processing 1d and 2d arrays · Medium
Given int a[5] = {1, 2, 3};, what are the values of a[3] and a[4]?
Not quite. Try another option.
Answer: Both are
When an array is partially initialized, the remaining elements are automatically set to . So a[3] and a[4] both hold .
Question 24 of 60 · defining and processing 1d and 2d arrays · Medium
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?
Not quite. Try another option.
Answer:
In row-major order, offset .
Question 25 of 60 · defining and processing 1d and 2d arrays · Medium
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);
Not quite. Try another option.
Answer: 20
The loop sums all elements: .
Question 26 of 60 · defining and processing 1d and 2d arrays · Medium
Which initialization correctly creates a 2D array with 2 rows and 3 columns, all initialized to zero?
Not quite. Try another option.
Answer: int m[2][3] = {0};
Using {0} initializes the first element to 0 and the compiler sets all remaining elements to 0. The other forms use invalid syntax for C arrays.
Question 27 of 60 · array applications: sorting and searching · Medium
In a single pass of Bubble Sort (ascending) over the array {5, 1, 4, 2}, what is the array after the first complete pass?
Not quite. Try another option.
Answer: {1, 4, 2, 5}
After one pass, the largest element bubbles to the end. Comparisons produce {1, 4, 2, 5}, with in its final position.
Question 28 of 60 · array applications: sorting and searching · Medium
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)?
Not quite. Try another option.
Answer:
Mid = (not 23, go right), mid = (not 23, go right), mid = (found). That is comparisons.
Question 29 of 60 · array applications: sorting and searching · Medium
What is the worst-case time complexity of linear search on an array of elements?
Not quite. Try another option.
Answer:
In the worst case the target is the last element or absent, so all elements are checked, giving .
Question 30 of 60 · array applications: sorting and searching · Medium
In Selection Sort (ascending) applied to {29, 10, 14, 37, 13}, what is the array after the first pass?
Not quite. Try another option.
Answer: {10, 29, 14, 37, 13}
Selection sort finds the minimum () and swaps it with the first element (), giving {10, 29, 14, 37, 13}.
Question 31 of 60 · character arrays · Medium
How many bytes must a character array reserve to safely store the string "HELLO"?
Not quite. Try another option.
Answer: bytes
The 5 characters plus the terminating null character '\0' require bytes.
The array is initialized with exactly 4 characters and no null terminator, so sizeof(c) is bytes.
Question 33 of 60 · declaration and initialization of string · Medium
Which of the following correctly declares and initializes a string in C?
Not quite. Try another option.
Answer: char s[] = "Hi";
A string is a character array initialized with a double-quoted literal. char s[] = "Hi"; creates a 3-byte array including the null terminator. C has no built-in string type.
Question 34 of 60 · declaration and initialization of string · Medium
What is sizeof(str) for char str[] = "India";?
Not quite. Try another option.
Answer:
"India" has 5 characters plus the null terminator '\0', so the array size is bytes.
Question 35 of 60 · string handling functions · Medium
What value does strcmp("apple", "apple") return?
Not quite. Try another option.
Answer:
strcmp returns when both strings are identical. A positive or negative value indicates lexicographic difference.
Question 36 of 60 · string handling functions · Medium
After executing the following code, what is stored in dest?
C
char dest[20] = "Good";
strcat(dest, "Day");
Not quite. Try another option.
Answer: GoodDay
strcat appends the second string to the end of the first, producing GoodDay with no space inserted.
Question 37 of 60 · string handling functions · Medium
What does strlen("Program\0ming") return?
Not quite. Try another option.
Answer:
strlen counts characters up to the first null terminator '\0'. It counts Program (7 characters) and stops.
Question 38 of 60 · structure and union: declaration, definition and initialization · Medium
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)?
Not quite. Try another option.
Answer:
Due to padding for alignment, char (1) + 3 padding + int (4) + double (8) totals bytes, not the raw .
Question 39 of 60 · structure and union: declaration, definition and initialization · Medium
For union U { int i; char c; };, what is sizeof(union U) on a system with 4-byte int?
Not quite. Try another option.
Answer:
A union's size equals the size of its largest member. Here int (4 bytes) is largest, so sizeof(union U) is .
Question 40 of 60 · structure and union: declaration, definition and initialization · Medium
What does the following code print?
C
struct Pt { int x, y; };
struct Pt p = {3, 7};
printf("%d", p.x + p.y);
Not quite. Try another option.
Answer: 10
The structure initializes p.x = 3 and p.y = 7, so p.x + p.y = 3 + 7 = 10.
Question 41 of 60 · declaring arrays in C · Hard
Consider the declaration int arr[5] = {1, 2};. What are the values of arr[2], arr[3], and arr[4]?
Not quite. Try another option.
Answer: All are 0
When an array is partially initialized in C, the remaining elements are automatically initialized to 0 (for int). This only applies when at least one initializer is provided; an uninitialized int arr[5]; would contain garbage.
Question 42 of 60 · declaring arrays in C · Hard
Which of the following declarations is invalid in standard C89 (ANSI C)?
Not quite. Try another option.
Answer: int arr[n]; where n is a non-const variable
In C89, array sizes must be compile-time constants, so a variable-length array (VLA) using a non-const n is invalid. VLAs were only introduced in C99. The other declarations are all valid in C89.
Question 43 of 60 · defining and processing 1d and 2d arrays · Hard
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?
Not quite. Try another option.
Answer: 1044
Address .
Question 44 of 60 · defining and processing 1d and 2d arrays · Hard
What does the expression *(*(arr + 1) + 2) evaluate to for a 2D array arr?
Not quite. Try another option.
Answer: arr[1][2]
For a 2D array, *(arr + i) gives the address of row i, and adding j then dereferencing gives element [i][j]. So *(*(arr + 1) + 2) is exactly arr[1][2].
Question 45 of 60 · defining and processing 1d and 2d arrays · Hard
In int a[] = {10, 20, 30, 40, 50};, what is the value of *(a + 3) - *a?
Not quite. Try another option.
Answer: 30
*(a + 3) is a[3] which is 40, and *a is a[0] which is 10. The result is .
Question 46 of 60 · array applications: sorting and searching · Hard
During a single pass of bubble sort (ascending) on [5, 1, 4, 2, 8], what is the array state after the first complete pass?
Not quite. Try another option.
Answer: [1, 4, 2, 5, 8]
In one pass, adjacent pairs are swapped: (5,1)→swap, (5,4)→swap, (5,2)→swap, (5,8)→no swap. The largest element 8 bubbles to the end, giving [1, 4, 2, 5, 8].
Question 47 of 60 · array applications: sorting and searching · Hard
For binary search on a sorted array of 1000 elements, what is the maximum number of comparisons needed in the worst case?
Not quite. Try another option.
Answer: 10
Binary search worst case is or . Since , at most 10 comparisons are required.
Question 48 of 60 · array applications: sorting and searching · Hard
Which condition must hold for binary search to work correctly, and what happens if it is violated?
Not quite. Try another option.
Answer: The array must be sorted; violating it may return wrong results or miss the target
Binary search relies on the sorted order to decide which half to discard. On an unsorted array it may incorrectly eliminate the half containing the target, returning wrong results, but it does not crash or throw an error.
Question 49 of 60 · character arrays · Hard
What is the value of sizeof(str) for char str[] = "Hello";?
Not quite. Try another option.
Answer: 6
A string literal includes a terminating null character \0. So "Hello" occupies 5 characters plus 1 null byte, making sizeof(str) equal to 6.
Question 50 of 60 · character arrays · Hard
Given char arr[5] = {'H', 'e', 'l', 'l', 'o'};, what happens when you pass arr to printf("%s", arr);?
Not quite. Try another option.
Answer: Undefined behavior, as there is no null terminator
The array is fully filled with 5 characters and has no room for a \0. printf("%s") reads until it finds a null terminator, so it reads past the array bounds, causing undefined behavior.
Question 51 of 60 · declaration and initialization of string · Hard
What is the key difference between char *s = "Hello"; and char s[] = "Hello";?
Not quite. Try another option.
Answer: The pointer version points to read-only memory; modifying it is undefined behavior, while the array is modifiable
char *s points to a string literal often stored in read-only memory, so writing through it is undefined behavior. char s[] copies the literal into a local modifiable array that can be safely changed.
Question 52 of 60 · declaration and initialization of string · Hard
For char str[10] = "Hi";, what are the contents of str[2] through str[9]?
Not quite. Try another option.
Answer: All are \0 (null characters)
When a char array is initialized with a string literal shorter than its declared size, the remaining elements (including the one right after the copied string) are all set to \0, following partial initialization rules.
Question 53 of 60 · string handling functions · Hard
What is the result of strlen(str) after executing char str[20] = "abc"; str[1] = '\0';?
Not quite. Try another option.
Answer: 1
strlen counts characters up to the first \0. After setting str[1] to \0, the string effectively becomes "a", so strlen returns 1, even though other characters remain in memory.
Question 54 of 60 · string handling functions · Hard
Given char s1[10] = "abc", s2[] = "abd";, what does strcmp(s1, s2) return?
Not quite. Try another option.
Answer: A negative value
strcmp compares character by character. At the third character, 'c' (99) is less than 'd' (100), so it returns a negative value (the difference 99 - 100 = -1, but the standard only guarantees a negative value).
Question 55 of 60 · string handling functions · Hard
What is the danger of using strcat(dest, src) when dest was declared as char dest[6] = "Hi"; and src is "World"?
Not quite. Try another option.
Answer: Buffer overflow, since the result exceeds dest's capacity
strcat does no bounds checking. "Hi" + "World" + \0 needs 8 bytes, but dest holds only 6. Writing beyond the buffer causes a buffer overflow and undefined behavior.
Question 56 of 60 · structure and union: declaration, definition and initialization · Hard
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)?
Not quite. Try another option.
Answer: 16
Due to alignment padding: char (1 byte) + 3 padding to align int, then int (4 bytes) = 8 bytes so far, then double (8 bytes) aligned at offset 8 = total 16. The actual member data is only 13 bytes.
Question 57 of 60 · structure and union: declaration, definition and initialization · Hard
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]?
Not quite. Try another option.
Answer: 1
In a union, all members share memory. On a little-endian system, the least significant byte is stored first, so u.c[0] holds the low byte of 1, which is 1, while u.c[1] through u.c[3] are 0.
Question 58 of 60 · structure and union: declaration, definition and initialization · Hard
What is sizeof a union union U { int i; char c[10]; double d; }; on a system with 4-byte int, 8-byte double?
Not quite. Try another option.
Answer: 16
A union's size is the size of its largest member, adjusted for alignment. The char c[10] is largest at 10 bytes, but the union must be aligned to its strictest member (double, 8-byte), so 10 rounds up to 16.
Question 59 of 60 · structure and union: declaration, definition and initialization · Hard
Which statement about initializing a structure with struct Point p = {.y = 5, .x = 3}; (designated initializers) is correct?
Not quite. Try another option.
Answer: It is valid C99; members can be initialized out of order by name
Designated initializers, introduced in C99, allow naming members explicitly with .member = value, in any order. Here x becomes 3 and y becomes 5 regardless of the order written.
Question 60 of 60 · defining and processing 1d and 2d arrays · Hard
Given int a[2][3] = {1, 2, 3, 4, 5, 6};, what is the value of a[1][0]?
Not quite. Try another option.
Answer: 4
The flat initializer fills row by row: row 0 gets {1, 2, 3} and row 1 gets {4, 5, 6}. Therefore a[1][0] is the first element of the second row, which is 4.
That's unit 5
Answer a few questions and your score shows up here.
Nearly through. Did unit 5 of CAP1008 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 5 of CAP1008 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.