Showing 11–20 of 100 questions
in Arrays & Strings
For a 3D array int arr[2][3][4], what is arr[1][2][3] equivalent to?
A
arr + 2*3*4 + 2*4 + 3
B
arr + 1*3*4 + 2*4 + 3
C
arr + 3 + 2 + 1
D
arr + 1 + 2 + 3
Correct Answer:
B. arr + 1*3*4 + 2*4 + 3
EXPLANATION
In row-major order: address = base + (i*m*n + j*n + k) where m=3, n=4. So arr[1][2][3] = base + 1*12 + 2*4 + 3 = base + 23.
What potential issue exists with this code: char *ptr = "Hello"; ptr[0] = 'h';?
A
Compilation error
B
Attempting to modify a string literal (undefined behavior)
C
Memory leak
D
Syntax error in character assignment
Correct Answer:
B. Attempting to modify a string literal (undefined behavior)
EXPLANATION
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault at runtime.
What is the relationship between pointer arithmetic and array indexing in C?
A
Pointer arithmetic is faster than array indexing
B
arr[i] is equivalent to *(arr + i) - both are mathematically identical
C
Array indexing is only for static arrays, pointer arithmetic for dynamic
D
No relationship - they are completely different operations
Correct Answer:
B. arr[i] is equivalent to *(arr + i) - both are mathematically identical
EXPLANATION
C standard defines arr[i] as *(arr + i). Both notations access the same memory location and perform identically.
In a string with escape sequences like "Hello\nWorld", how many characters are counted by strlen()?
EXPLANATION
\n is a single character (newline). 'Hello' = 5 + \n = 1 + 'World' = 5, total = 11 characters (not counting null terminator).
Which of the following operations is NOT allowed on array names in C?
A
arr[0] (indexing)
B
arr++ (incrementing)
C
sizeof(arr) (size calculation)
D
&arr (address of)
Correct Answer:
B. arr++ (incrementing)
EXPLANATION
Array names are non-modifiable lvalues. You cannot use ++ on them. However, individual elements can be accessed and modified.
What does the expression *(arr + 3) represent for an integer array?
A
Address of 4th element
B
Value of 4th element (0-indexed as arr[3])
C
Size of array plus 3
D
Third pointer in array
Correct Answer:
B. Value of 4th element (0-indexed as arr[3])
EXPLANATION
arr + 3 points to the 4th element (0-indexed). Dereferencing with * gives its value, equivalent to arr[3].
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).
In C, what happens when you pass an array to a function?
A
The entire array is copied to function stack
B
Only the array name (pointer to first element) is passed
C
A deep copy is created
D
The array is passed by reference automatically
Correct Answer:
B. Only the array name (pointer to first element) is passed
EXPLANATION
Arrays decay to pointers when passed to functions. Only the address of the first element is passed, not a copy of entire array.
Which function is safe to use for string concatenation with size limiting?
A
strcat()
B
strncat()
C
concat()
D
appendstr()
Correct Answer:
B. strncat()
EXPLANATION
strncat() allows specifying maximum characters to concatenate, preventing buffer overflow. strcat() has no size limit.
What is the output of printf("%d", sizeof(arr)) for char arr[100]?
A
100
B
1
C
4 or 8 (depending on pointer size)
D
101 (including null terminator)
EXPLANATION
sizeof(arr) returns the total size of the array in bytes. For char arr[100], it returns 100 bytes regardless of content.