Which of the following will correctly compare two strings?
A if (str1 == str2) B if (strcmp(str1, str2) == 0) C Both A and B D Neither A nor B
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?
A strcat(dest, src) B strncat(dest, src, n) C sprintf(dest, "%s%s", dest, src) D Both B and C are safer options
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;
A Converts uppercase to lowercase B Converts lowercase to uppercase C Shifts ASCII values by 32 D Causes compilation error
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?
A str[5] contains '\0', str[6] is uninitialized B All elements from str[0] to str[9] contain characters C str[0] to str[4] have 'H','e','l','l','o', str[5] has '\0' D Runtime error occurs
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?
A The entire remaining string B A pointer to the next token C The delimiter character D The length of the token
strtok() returns a pointer to the next token separated by the delimiter. Returns NULL when no more tokens exist.
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;
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';
A String is successfully modified to 'Jello' B Undefined behavior or runtime error C Compilation error D String remains 'Hello'
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?
A strrev(str); B reverse(str); C str_reverse(str); D reversestr(str);
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));
A 4 B 5 C 8 D Compilation error
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?
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
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));
A H B e C l D Compilation error
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?
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
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?
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
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);
A Hello B Helo C Garbage output D Compilation error
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?
A 9 elements B 24 elements C 12 elements D 6 elements
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]?
A Compilation error B Runtime error C Undefined behavior - may access memory beyond array D Returns 0 by default
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?
A No difference, both represent strings B First is array allocation, second is pointer declaration requiring separate allocation C First is dynamic, second is static D Second can hold longer strings
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?
A Copies src to dest; risk is slow performance B Copies src to dest; risk is buffer overflow if dest is too small C Creates a new string; risk is memory leak D Compares two strings; no 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:
A 0 (equal) B A non-zero negative value C A non-zero positive value D Compilation error
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?
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
C uses row-major order. The entire array is contiguous in memory with all of row 0, then row 1, then row 2, etc.