Showing 41–50 of 100 questions
in Arrays & Strings
Consider: int arr[10]; int *ptr = arr; What does ptr[5] represent?
A
The 5th element of array arr
B
Address of the 5th element
C
Value at address arr + 5*sizeof(int)
D
Both A and C
Correct Answer:
D. Both A and C
EXPLANATION
ptr[5] is equivalent to *(ptr+5), which accesses the 5th element. Pointer arithmetic adjusts by the size of int.
What is the output of the following code?
char str[] = "GATE";
printf("%d", sizeof(str));
A
4
B
5
C
8
D
Compilation error
EXPLANATION
sizeof(str) returns 5 because the array includes 4 characters plus 1 null terminator.
Which of the following correctly reverses a string in-place?
A
strrev(str);
B
reverse(str);
C
str_reverse(str);
D
reversestr(str);
Correct Answer:
A. strrev(str);
EXPLANATION
strrev() is a non-standard but commonly used function (available in string.h in some compilers) that reverses a string in-place.
What happens when you try to modify a string literal in C?
char *str = "Hello";
str[0] = 'J';
A
String is successfully modified to 'Jello'
B
Undefined behavior or runtime error
C
Compilation error
D
String remains 'Hello'
Correct Answer:
B. Undefined behavior or runtime error
EXPLANATION
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault.
What is the correct way to declare a pointer to a character array?
A
char *ptr[];
B
char (*ptr)[];
C
*char ptr[];
D
char **ptr;
Correct Answer:
B. char (*ptr)[];
EXPLANATION
char (*ptr)[] declares ptr as a pointer to a character array. The parentheses are crucial for correct precedence.
How many bytes does a 2D array int arr[3][4] occupy in memory?
A
12 bytes
B
24 bytes
C
48 bytes
D
16 bytes
Correct Answer:
C. 48 bytes
EXPLANATION
int occupies 4 bytes. Array size is 3×4 = 12 elements. Total: 12 × 4 = 48 bytes.
Which function is used to find the length of a string in C?
A
strlen()
B
strsize()
C
stringlen()
D
getlength()
Correct Answer:
A. strlen()
EXPLANATION
strlen() is the standard library function defined in string.h that returns the length of a string excluding the null terminator.
What is the maximum size of a string that can be stored in char str[50]?
A
49 characters plus null terminator
B
50 characters plus null terminator
C
50 characters without null terminator
D
Unlimited characters
Correct Answer:
A. 49 characters plus null terminator
EXPLANATION
A char array of size 50 can store maximum 49 characters because the last position is reserved for the null terminator '\0'.
What does strtok(str, delim) return on each call until the string ends?
A
The entire remaining string
B
A pointer to the next token
C
The delimiter character
D
The length of the token
Correct Answer:
B. A pointer to the next token
EXPLANATION
strtok() returns a pointer to the next token separated by the delimiter. Returns NULL when no more tokens exist.
What is the output?
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", p[2]);
EXPLANATION
p points to arr[0]. p[2] is equivalent to *(p+2), which points to arr[2] = 30.