Consider the code: int *p; int arr[5] = {10, 20, 30, 40, 50}; p = arr; What is *(p+2)?
p points to arr[0]. p+2 points to arr[2], which contains 30.
What will be printed?
char *str = "Hello";
printf("%c", str[1]);
str[1] accesses the second character of the string, which is 'e'.
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.
Predict the output:
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d", arr[1][2]);
arr[1][2] refers to row 1, column 2, which contains 6.
Advertisement
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).
In a dynamic 2D array created as: int arr = (int)malloc(rows * sizeof(int*)); What is the next step?
After allocating the array of pointers, each row must be allocated separately.
What will happen?
char arr[5] = "Hello";
printf("%d", strlen(arr));
Array is too small. "Hello" needs 6 bytes including null terminator, but only 5 are allocated. This causes buffer overflow.
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 is the output?
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", p[2]);
p points to arr[0]. p[2] is equivalent to *(p+2), which points to arr[2] = 30.
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.
What is the maximum size of a string that can be stored in char str[50]?
A char array of size 50 can store maximum 49 characters because the last position is reserved for the null terminator '\0'.
Which function is used to find the length of a string in C?
strlen() is the standard library function defined in string.h that returns the length of a string excluding the null terminator.
How many bytes does a 2D array int arr[3][4] occupy in memory?
int occupies 4 bytes. Array size is 3×4 = 12 elements. Total: 12 × 4 = 48 bytes.
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'.