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.
What is the output of printf("%d", sizeof(arr)) for char arr[100]?
sizeof(arr) returns the total size of the array in bytes. For char arr[100], it returns 100 bytes regardless of content.
Which function is safe to use for string concatenation with size limiting?
strncat() allows specifying maximum characters to concatenate, preventing buffer overflow. strcat() has no size limit.
In C, what happens when you pass an array to a function?
Arrays decay to pointers when passed to functions. Only the address of the first element is passed, not a copy of entire array.
What is the correct way to declare and initialize a string in C?
Option B shows two valid ways: pointer to string literal or character array. Option A is wrong (single char), C is wrong (no String type in C).
Advertisement
What does the expression *(arr + 3) represent for an integer array?
arr + 3 points to the 4th element (0-indexed). Dereferencing with * gives its value, equivalent to arr[3].
Which of the following operations is NOT allowed on array names in C?
Array names are non-modifiable lvalues. You cannot use ++ on them. However, individual elements can be accessed and modified.
In a string with escape sequences like "Hello\nWorld", how many characters are counted by strlen()?
\n is a single character (newline). 'Hello' = 5 + \n = 1 + 'World' = 5, total = 11 characters (not counting null terminator).
What is the relationship between pointer arithmetic and array indexing in C?
C standard defines arr[i] as *(arr + i). Both notations access the same memory location and perform identically.
What potential issue exists with this code: char *ptr = "Hello"; ptr[0] = 'h';?
String literals are stored in read-only memory. Attempting to modify them causes undefined behavior or segmentation fault at runtime.
For a 3D array int arr[2][3][4], what is arr[1][2][3] equivalent to?
In row-major order: address = base + (i*m*n + j*n + k) where m=3, n=4. So arr[1][2][3] = base + 1*12 + 2*4 + 3 = base + 23.
What is the output of: int arr[] = {10, 20, 30}; printf("%d", *(arr+1));?
arr+1 points to second element (arr[1]). Dereferencing gives value 20.
In string handling, what is the difference between fgets() and gets()?
gets() has no buffer overflow protection and was removed in C11. fgets(str, size, stdin) is the safer alternative with size limiting.
What will be the size in bytes of int arr[5] on a 32-bit system?
On 32-bit systems, int is typically 4 bytes. 5 elements × 4 bytes = 20 bytes total.
Which of the following correctly declares a pointer to an array (not array of pointers)?
Parentheses change precedence. int (*ptr)[5] is pointer to array of 5 ints. int *arr[5] is array of 5 pointers.
What is the output of: char str[20]; scanf("%s", str); when input is 'Hello World'?
%s format specifier stops reading at whitespace. To read entire line including spaces, use fgets() or %[^\n].
In what scenario would you use memcpy() instead of strcpy()?
strcpy() assumes null-terminated strings. memcpy() copies exact number of bytes, suitable for binary data or embedded nulls.
What is the correct syntax to pass a 2D array to a function?
First dimension can be omitted, but second must be specified: int arr[][4]. Option C (int **arr) is not equivalent - it's pointer to pointer.
What does the strspn() function do?
strspn(str, charset) returns length of initial segment of str containing only characters from charset. Useful for token parsing.
For char arr[5] = {'a', 'b', 'c', 'd', 'e'}, is this a valid string?
String functions expect null terminator. This array has no '\0', so it's a character array but NOT a proper string for str* functions.