Showing 1–10 of 21 questions
in Arrays & Strings
What does the strspn() function do?
A
Finds substring in a string
B
Spans the initial segment containing only characters from specified set
C
Compares two strings
D
Reverses a string
Correct Answer:
B. Spans the initial segment containing only characters from specified set
EXPLANATION
strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.
In what scenario would you use memcpy() instead of strcpy()?
A
memcpy() is always faster
B
When copying binary data or strings without null terminators
C
When the destination is smaller
D
memcpy() is safer
Correct Answer:
B. When copying binary data or strings without null terminators
EXPLANATION
strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.
Which of the following correctly declares a pointer to an array (not array of pointers)?
A
int *arr[5];
B
int (*ptr)[5];
C
int [5]*ptr;
D
int &arr[5];
Correct Answer:
B. int (*ptr)[5];
EXPLANATION
Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.
In string handling, what is the difference between fgets() and gets()?
A
fgets() is slower
B
fgets() includes newline character, gets() doesn't
C
fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
D
gets() works only with integers
Correct Answer:
C. fgets() takes size parameter and is safer; gets() is unsafe (removed from C11)
EXPLANATION
gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.
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.
What is the most efficient way to copy one array to another in C?
A
memcpy(dest, src, sizeof(src));
B
strcpy(dest, src);
C
for loop with element-by-element assignment
D
Array assignment dest = src;
Correct Answer:
A. memcpy(dest, src, sizeof(src));
EXPLANATION
memcpy() is the most efficient as it performs block memory copy. strcpy() is only for strings, and array assignment doesn't work in C.
How should dynamic 1D array of strings be declared for 5 strings of length 20?
A
char **str = (char**)malloc(5 * sizeof(char*)); for loop to allocate each string
B
char str[5][20];
C
char *str[5];
D
Both A and C after proper allocation
Correct Answer:
A. char **str = (char**)malloc(5 * sizeof(char*)); for loop to allocate each string
EXPLANATION
For dynamic allocation, use double pointer char** and allocate memory for each string separately. Options B and C are static allocations.
What will the following code print?
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d", *p++, *++p);
A
10 20
B
10 30
C
20 20
D
Undefined behavior
Correct Answer:
D. Undefined behavior
EXPLANATION
This code exhibits undefined behavior due to lack of sequence points between modifications and access of p. Different compilers may produce different results.