Showing 31–40 of 100 questions
in Arrays & Strings
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.
What happens with this code?
char *ptr;
char str[] = "Programming";
ptr = str;
ptr[2] = 'X';
A
Compilation error
B
String becomes 'PrXgramming'
C
Undefined behavior
D
No change to str
Correct Answer:
B. String becomes 'PrXgramming'
EXPLANATION
ptr points to the character array str. Modifying ptr[2] modifies str[2], changing 'o' to 'X', resulting in 'PrXgramming'.
Consider a 3D array: int arr[2][3][4]. How many elements total?
A
9 elements
B
24 elements
C
12 elements
D
6 elements
Correct Answer:
B. 24 elements
EXPLANATION
Total elements = 2 × 3 × 4 = 24 elements in the 3D array.
What is the output?
char str[6] = {'H','e','l','l','o'};
printf("%s", str);
A
Hello
B
Helo
C
Garbage output
D
Compilation error
Correct Answer:
C. Garbage output
EXPLANATION
The array has 5 characters but is not null-terminated. printf("%s") will read beyond the array, printing garbage.
Which statement about 2D arrays in C is correct?
A
int arr[3][4] requires 12 contiguous bytes
B
arr[0] returns the address of the first row
C
arr[0][0] and *(*arr) access the same element
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
arr[0] points to the first row (array of 4 ints). arr[0][0] and *(*arr) both access the first element through pointer dereferencing.
What is the difference between arr[5] and *arr when arr is an array?
A
No difference, both access the same element
B
arr[5] accesses 6th element, *arr accesses 1st element
C
arr[5] is address, *arr is value
D
No valid comparison
Correct Answer:
B. arr[5] accesses 6th element, *arr accesses 1st element
EXPLANATION
arr[5] accesses the element at index 5 (6th element), while *arr (equivalent to arr[0]) accesses the first element.
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.
What will be printed?
char str[] = "Hello";
printf("%c", *(str+2));
A
H
B
e
C
l
D
Compilation error
EXPLANATION
str+2 points to the 3rd character (index 2). *(str+2) dereferences it, printing 'l'.