Showing 21–30 of 100 questions
in Arrays & Strings
What is the memory layout of a 2D array int arr[3][4] in C?
A
Random arrangement in memory
B
Row-major order - sequential rows stored consecutively
C
Column-major order - sequential columns stored consecutively
D
Scattered across memory with pointers
Correct Answer:
B. Row-major order - sequential rows stored consecutively
EXPLANATION
C uses row-major order. The entire array is contiguous in memory with all of row 0, then row 1, then row 2, etc.
In a string comparison, strcmp('Apple', 'apple') returns:
A
0 (equal)
B
A non-zero negative value
C
A non-zero positive value
D
Compilation error
Correct Answer:
C. A non-zero positive value
EXPLANATION
strcmp() is case-sensitive. ASCII value of 'A' (65) is less than 'a' (97), so it returns negative value. But 'A' < 'a', so it returns -32 (negative).
What does strcpy(dest, src) do, and what is its main risk?
A
Copies src to dest; risk is slow performance
B
Copies src to dest; risk is buffer overflow if dest is too small
C
Creates a new string; risk is memory leak
D
Compares two strings; no risk
Correct Answer:
B. Copies src to dest; risk is buffer overflow if dest is too small
EXPLANATION
strcpy() doesn't check destination buffer size, potentially causing buffer overflow. Modern alternatives include strncpy() or strcpy_s().
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.
What is the difference between char str[20] and char *str in C?
A
No difference, both represent strings
B
First is array allocation, second is pointer declaration requiring separate allocation
C
First is dynamic, second is static
D
Second can hold longer strings
Correct Answer:
B. First is array allocation, second is pointer declaration requiring separate allocation
EXPLANATION
char str[20] allocates 20 bytes on stack. char *str only declares a pointer and requires separate memory allocation (malloc/static string).
What will be the result of accessing arr[10] when arr is declared as int arr[5]?
A
Compilation error
B
Runtime error
C
Undefined behavior - may access memory beyond array
D
Returns 0 by default
Correct Answer:
C. Undefined behavior - may access memory beyond array
EXPLANATION
C doesn't perform bounds checking. Accessing out-of-bounds memory causes undefined behavior and is a common source of bugs.
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.