Showing 1–10 of 49 questions
in Arrays & Strings
For char arr[5] = {'a', 'b', 'c', 'd', 'e'}, is this a valid string?
A
Yes, always valid
B
No, missing null terminator
C
Only if last element is '\0'
D
Depends on compiler
Correct Answer:
B. No, missing null terminator
EXPLANATION
String functions expect null terminator. This array has no '\0', so it's a character array but NOT a proper string for str* functions.
What is the correct syntax to pass a 2D array to a function?
A
void func(int arr[][])
B
void func(int arr[3][4])
C
void func(int **arr)
D
Any of the above
Correct Answer:
B. void func(int arr[3][4])
EXPLANATION
First dimension can be omitted, but second must be specified: int arr[][4]. Option C (int **arr) is not equivalent - it's pointer to pointer.
What is the output of: char str[20]; scanf("%s", str); when input is 'Hello World'?
A
Entire 'Hello World' is stored
B
Only 'Hello' is stored (stops at whitespace)
C
Compilation error
D
Buffer overflow occurs
Correct Answer:
B. Only 'Hello' is stored (stops at whitespace)
EXPLANATION
%s format specifier stops reading at whitespace. To read entire line including spaces, use fgets() or %[^\n].
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].
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.
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.