Unit 5: Introduction to Arrays - Practice Quiz
1 Which of the following is the correct way to declare an integer array of size 10 in C?
array int arr[10];
int arr(10);
int 10[arr];
int arr[10];
2 In C, what is the index of the first element of an array?
3
For an array declared as int a[5];, what is the valid range of indices?
4 Which statement correctly initializes a 1D array at the time of declaration?
int a[3] = {1, 2, 3};
int a[3] = (1, 2, 3);
int a[3] = 1, 2, 3;
int a(3) = {1, 2, 3};
5
How is an element in a 2D array m accessed at row i and column j?
m[i, j]
m(i)(j)
m[i][j]
m[i + j]
6
How many elements can be stored in an array declared as int a[3][4];?
7 Which loop is most commonly used to process all elements of a 1D array?
goto statement
if statement
switch statement
for loop
8 Which searching technique checks each element of an array one by one?
9 Binary search requires the array to be:
10 Which of the following is a sorting algorithm?
11 In C, a string is typically stored using which type of array?
12 Which character marks the end of a string stored in a character array?
\n
\t
\0
0
13
How many bytes are needed to store the string "Hi" in a character array?
14 Which of the following correctly initializes a string in C?
char s[] = "Hello";
string s = "Hello";
char s[] = 'Hello';
char s = "Hello";
15 Which function is used to find the length of a string in C?
strlen()
strcat()
strcpy()
strcmp()
16 Which function is used to copy one string into another?
strrev()
strcpy()
strlen()
strcmp()
17
Which header file must be included to use string handling functions like strcat()?
<stdlib.h>
<stdio.h>
<math.h>
<string.h>
18 Which keyword is used to define a structure in C?
record
class
structure
struct
19 In a union, all members share:
20 Which operator is used to access a member of a structure variable?
,
.
::
->
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?
22 Which of the following array declarations is invalid in standard C?
int arr[3] = {0};
int arr[5];
int arr[] = {1, 2, 3};
int arr[];
23
Given int a[5] = {1, 2, 3};, what are the values of a[3] and a[4]?
a[3] is and a[4] is
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?
25
What does the following loop print?
int a[4] = {2, 4, 6, 8};
int s = 0;
for (int i = 0; i < 4; i++)
s += a[i];
printf("%d", s);
24
10
20
18
26 Which initialization correctly creates a 2D array with 2 rows and 3 columns, all initialized to zero?
int m[2][3] = {0};
int m[][] = {0};
int m[2][3] = 0;
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?
{1, 5, 4, 2}
{1, 2, 4, 5}
{5, 4, 2, 1}
{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)?
29 What is the worst-case time complexity of linear search on an array of elements?
30
In Selection Sort (ascending) applied to {29, 10, 14, 37, 13}, what is the array after the first pass?
{29, 10, 13, 14, 37}
{10, 14, 13, 29, 37}
{13, 10, 14, 37, 29}
{10, 29, 14, 37, 13}
31
How many bytes must a character array reserve to safely store the string "HELLO"?
32
What does the following code print?
char c[] = {'C', 'O', 'D', 'E'};
printf("%d", sizeof(c));
4
5
1
8
33 Which of the following correctly declares and initializes a string in C?
char s[] = 'Hi';
string s = "Hi";
char s[] = "Hi";
char s = "Hi";
34
What is sizeof(str) for char str[] = "India";?
35
What value does strcmp("apple", "apple") return?
36
After executing the following code, what is stored in dest?
char dest[20] = "Good";
strcat(dest, "Day");
Good Day
Day
GoodDay
DayGood
37
What does strlen("Program\0ming") return?
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)?
39
For union U { int i; char c; };, what is sizeof(union U) on a system with 4-byte int?
40
What does the following code print?
struct Pt { int x, y; };
struct Pt p = {3, 7};
printf("%d", p.x + p.y);
37
21
10
4
41
Consider the declaration int arr[5] = {1, 2};. What are the values of arr[2], arr[3], and arr[4]?
2
0
42 Which of the following declarations is invalid in standard C89 (ANSI C)?
static int arr[100];
int arr[] = {1, 2, 3};
int arr[n]; where n is a non-const variable
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?
1024
1044
1040
1036
44
What does the expression *(*(arr + 1) + 2) evaluate to for a 2D array arr?
arr[1][2]
arr[2][1]
arr[3]
*(arr + 3)
45
In int a[] = {10, 20, 30, 40, 50};, what is the value of *(a + 3) - *a?
30
40
10
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?
[1, 5, 4, 2, 8]
[1, 4, 2, 5, 8]
[1, 4, 2, 8, 5]
[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?
1000
500
9
10
48 Which condition must hold for binary search to work correctly, and what happens if it is violated?
49
What is the value of sizeof(str) for char str[] = "Hello";?
4
7
5
6
50
Given char arr[5] = {'H', 'e', 'l', 'l', 'o'};, what happens when you pass arr to printf("%s", arr);?
Hello
Hell only
51
What is the key difference between char *s = "Hello"; and char s[] = "Hello";?
52
For char str[10] = "Hi";, what are the contents of str[2] through str[9]?
str[2] is \0, the rest are garbage
\0 (null characters)
53
What is the result of strlen(str) after executing char str[20] = "abc"; str[1] = '\0';?
1
2
3
0
54
Given char s1[10] = "abc", s2[] = "abd";, what does strcmp(s1, s2) return?
-3
0
55
What is the danger of using strcat(dest, src) when dest was declared as char dest[6] = "Hi"; and src is "World"?
NULL and leaves dest unchanged
dest's capacity
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)?
8
13
16
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]?
4
1
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?
16
10
8
22
59
Which statement about initializing a structure with struct Point p = {.y = 5, .x = 3}; (designated initializers) is correct?
x and y to 5
60
Given int a[2][3] = {1, 2, 3, 4, 5, 6};, what is the value of a[1][0]?
5
1
4
3
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →