Showing 1–10 of 30 questions
in Arrays & Strings
Consider the following code:
char str[] = "HELLO";
int len = 0;
while(str[len] != '\0') {
len++;
}
printf("%d", len);
What will be the output?
A
5
B
6
C
4
D
Undefined behavior
EXPLANATION
The string "HELLO" has 5 characters. The while loop counts characters until it encounters the null terminator '\0'. The output will be 5. The null terminator is not counted in the length.
What will be the size in bytes of int arr[5] on a 32-bit system?
A
5 bytes
B
10 bytes
C
20 bytes
D
Depends on compiler
Correct Answer:
C. 20 bytes
EXPLANATION
On 32-bit systems, int is typically 4 bytes. 5 elements × 4 bytes = 20 bytes total.
What is the output of: int arr[] = {10, 20, 30}; printf("%d", *(arr+1));?
A
10
B
20
C
30
D
Garbage value
EXPLANATION
arr+1 points to second element (arr[1]). Dereferencing gives value 20.
What is the correct way to declare and initialize a string in C?
A
char str = "Hello";
B
char *str = "Hello"; OR char str[] = "Hello";
C
String str = "Hello";
D
char str[10] = {"Hello"};
Correct Answer:
B. char *str = "Hello"; OR char str[] = "Hello";
EXPLANATION
Option B shows two valid ways: pointer to string literal or character array. Option A is wrong (single char), C is wrong (no String type in C).
How many elements can be stored in a 2D array declared as int matrix[4][5]?
A
4 elements
B
5 elements
C
9 elements
D
20 elements
Correct Answer:
D. 20 elements
EXPLANATION
A 2D array with dimensions [4][5] contains 4 × 5 = 20 elements total.
In the declaration char *str[5], what does the array represent?
A
Array of 5 character pointers
B
Pointer to array of 5 characters
C
Single pointer to a string of 5 characters
D
5 pointers to individual characters
Correct Answer:
A. Array of 5 character pointers
EXPLANATION
The syntax char *str[5] declares an array of 5 pointers, each pointing to a character (or string). The brackets bind tighter than asterisk.
Which of the following correctly initializes a 2D array of integers?
A
int arr[3][3] = {1,2,3,4,5,6,7,8,9};
B
int arr[][] = {1,2,3,4,5,6};
C
int arr[3][] = {{1,2},{3,4},{5,6}};
D
int arr[3][3];
Correct Answer:
A. int arr[3][3] = {1,2,3,4,5,6,7,8,9};
EXPLANATION
Option A is valid - 2D arrays can be initialized with all elements in a single brace. Options B and C are invalid as at least one dimension must be specified.
What is the size of the string 'Hello' when stored in a character array with null terminator?
A
5 bytes
B
6 bytes
C
7 bytes
D
Depends on compiler
Correct Answer:
B. 6 bytes
EXPLANATION
'Hello' has 5 characters plus 1 null terminator (\0), making total 6 bytes.
In C, when you declare an array like int arr[10], what does the array name 'arr' represent?
A
A constant pointer to the first element of the array
B
A variable pointer that can be reassigned
C
The total size of the array in bytes
D
A reference to the last element
Correct Answer:
A. A constant pointer to the first element of the array
EXPLANATION
Array names decay to pointers to their first element in most contexts. 'arr' is equivalent to &arr[0] and cannot be reassigned.
How many times will the following loop execute?
int arr[5]; for(int i=0; i
A
4 times
B
5 times
C
Infinite loop
D
0 times
Correct Answer:
B. 5 times
EXPLANATION
Loop runs from i=0 to i=4, executing 5 times total. All array elements are initialized to 0.