Which of the following will correctly compare two strings?
A compares pointers, not string content. strcmp() compares actual string values character by character.
What is the correct syntax to concatenate two strings safely in modern C?
strcat() is unsafe due to buffer overflow risk. strncat() and sprintf() provide bounds checking.
What does the following code do?
char str[] = "HELLO";
for(int i = 0; str[i]; i++) str[i] = str[i] + 32;
Adding 32 to uppercase ASCII values converts them to lowercase (A=65, a=97, difference=32).
Consider: char str[10]; strcpy(str, "Hello"); What is the state of memory after this?
strcpy copies "Hello" and adds null terminator at position 5. Positions 6-9 remain uninitialized.
What does strtok(str, delim) return on each call until the string ends?
strtok() returns a pointer to the next token separated by the delimiter. Returns NULL when no more tokens exist.
Advertisement
What is the correct way to declare a pointer to a character array?
char (*ptr)[] declares ptr as a pointer to a character array. The parentheses are crucial for correct precedence.
What happens when you try to modify a string literal in C?
char *str = "Hello";
str[0] = 'J';
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault.
Which of the following correctly reverses a string in-place?
strrev() is a non-standard but commonly used function (available in string.h in some compilers) that reverses a string in-place.
What is the output of the following code?
char str[] = "GATE";
printf("%d", sizeof(str));
sizeof(str) returns 5 because the array includes 4 characters plus 1 null terminator.
Consider: int arr[10]; int *ptr = arr; What does ptr[5] represent?
ptr[5] is equivalent to *(ptr+5), which accesses the 5th element. Pointer arithmetic adjusts by the size of int.
What will be printed?
char str[] = "Hello";
printf("%c", *(str+2));
str+2 points to the 3rd character (index 2). *(str+2) dereferences it, printing 'l'.
What is the difference between arr[5] and *arr when arr is an array?
arr[5] accesses the element at index 5 (6th element), while *arr (equivalent to arr[0]) accesses the first element.
Which statement about 2D arrays in C is correct?
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 output?
char str[6] = {'H','e','l','l','o'};
printf("%s", str);
The array has 5 characters but is not null-terminated. printf("%s") will read beyond the array, printing garbage.
Consider a 3D array: int arr[2][3][4]. How many elements total?
Total elements = 2 × 3 × 4 = 24 elements in the 3D array.
What will be the result of accessing arr[10] when arr is declared as int arr[5]?
C doesn't perform bounds checking. Accessing out-of-bounds memory causes undefined behavior and is a common source of bugs.
What is the difference between char str[20] and char *str in C?
char str[20] allocates 20 bytes on stack. char *str only declares a pointer and requires separate memory allocation (malloc/static string).
What does strcpy(dest, src) do, and what is its main risk?
strcpy() doesn't check destination buffer size, potentially causing buffer overflow. Modern alternatives include strncpy() or strcpy_s().
In a string comparison, strcmp('Apple', 'apple') returns:
strcmp() is case-sensitive. ASCII value of 'A' (65) is less than 'a' (97), so it returns negative value. But 'A' < 'a', so it returns -32 (negative).
What is the memory layout of a 2D array int arr[3][4] in C?
C uses row-major order. The entire array is contiguous in memory with all of row 0, then row 1, then row 2, etc.