C Arrays (1D & 2D) - Tricky MCQ

Previous C Arrays - 1D & 2D Next

Tricky Questions on 1D & 2D Arrays

1

What is the output of: printf("%d", sizeof(arr)/sizeof(arr[0])) when arr is passed to a function?

Correct Answer: C) Size of pointer divided by size of first element

When an array is passed to a function, it decays to a pointer to its first element. Inside the function, sizeof(arr) gives the size of the pointer (usually 4 or 8 bytes), not the size of the original array. This is a common pitfall when trying to calculate array size inside functions.

2

Can we use arr[-1] in C arrays?

Correct Answer: B) Yes, it's valid C syntax

Negative array indices are syntactically valid in C due to pointer arithmetic. arr[-1] is equivalent to *(arr - 1). However, accessing memory before the array (outside allocated bounds) leads to undefined behavior. It may work in some cases but is dangerous and should be avoided.

3

What is arr[i] equivalent to in pointer terms?

Correct Answer: D) All of the above

Due to the commutative property of addition and how array indexing works in C, arr[i] is exactly equivalent to *(arr + i), which is also equivalent to *(i + arr), which further simplifies to i[arr]. All four forms are valid and compile to the same machine code.

4

What happens when you declare: int arr[0]; ?

Correct Answer: D) Depends on compiler (GCC allows it as extension)

In standard C, zero-length arrays are invalid. However, GCC allows them as an extension. When used as the last member of a struct (C99's flexible array member), [] is preferred over [0] or [1]. Zero-length arrays have sizeof equal to 0 and can be useful for variable-length structures.

5

What is the difference between int arr[] and int *arr?

Correct Answer: D) Both B and C

int arr[] declares an array (complete type), while int *arr declares a pointer. sizeof(arr) gives size of entire array for array declaration, but size of pointer for pointer declaration. Arrays cannot be reassigned (arr = something is invalid), while pointers can. However, in function parameters, they're equivalent due to array decay.

6

Can we have arrays with runtime size in C?

Correct Answer: D) Both B and C

In C89, array sizes must be compile-time constants. However, you can create dynamic arrays at runtime using malloc(). C99 introduced VLAs (Variable Length Arrays) that allow arrays with runtime-determined size on the stack, but they have limitations (can't be global/static, size can't be changed). VLAs are optional in C11.

7

What is the memory layout of 2D array int arr[3][4]?

Correct Answer: B) Contiguous block of 12 integers in row-major order

C uses row-major order for multidimensional arrays. int arr[3][4] allocates a single contiguous block of 3×4 = 12 integers. Elements are stored row by row: arr[0][0], arr[0][1], arr[0][2], arr[0][3], arr[1][0], arr[1][1], etc. This is different from an array of pointers (like int* arr[3]) which requires separate allocations for each row.

8

What does arr represent when declared as int arr[5]?

Correct Answer: C) Both, depending on context

arr is the array itself, not a pointer. However, in most expressions, array names decay to pointers to their first element (exception: sizeof, &, string literal initialization). So arr usually behaves like &arr[0]. But &arr gives address of entire array (same value but different type: pointer to array vs pointer to element).

9

What is array-to-pointer decay?

Correct Answer: A) Automatic conversion of array to pointer to first element

Array-to-pointer decay is an implicit conversion that occurs when an array is used in most expressions. The array name converts to a pointer to its first element. This happens in function calls (parameters), assignment (except initialization), and most operations. Exceptions include sizeof, &, and string literal initialization of char arrays.

10

Can we compare two arrays using == operator?

Correct Answer: C) Yes, but only compares addresses

When you use == with arrays, array-to-pointer decay occurs, so you're comparing pointers (addresses), not array contents. Two arrays with identical contents will compare as false if they're different objects. To compare array contents, use memcmp() or write a loop to compare element by element.

11

What is the type of &arr for int arr[5]?

Correct Answer: C) int(*)[5] (pointer to array of 5 ints)

&arr gives the address of the entire array, not just the first element. While it has the same numerical value as &arr[0], it has different type: pointer to array of 5 ints (int(*)[5]) vs pointer to int (int*). This affects pointer arithmetic: &arr + 1 jumps sizeof(arr) bytes, while &arr[0] + 1 jumps sizeof(int) bytes.

12

Can array size be negative?

Correct Answer: D) Both B and C

For regular arrays, negative size is a compilation error. For VLAs (Variable Length Arrays in C99), if the size expression evaluates to negative at runtime, the behavior is undefined (could crash, produce weird results, etc.). The C standard says VLA size must be > 0. Some compilers may reject negative VLA sizes at compile-time.

13

What is the maximum size of an array in C?

Correct Answer: D) All of the above

The maximum array size is constrained by multiple factors: available memory (physical+virtual), SIZE_MAX (maximum value of size_t), implementation limits (stack size for local arrays), and integer overflow. The C standard guarantees arrays up to 65535 bytes, but implementations typically support much larger. Dynamic arrays via malloc() have fewer restrictions than stack arrays.

14

How are 2D arrays passed to functions?

Correct Answer: D) Both B and C

For 2D arrays, the first dimension can be omitted in function parameters because it decays to pointer, but all other dimensions must be specified for pointer arithmetic to work. int arr[][N] and int (*arr)[N] are equivalent in function parameters. The compiler needs to know N to calculate arr[i][j] = *(arr + i*N + j).

15

Can we have arrays of void type?

Correct Answer: B) No, void is incomplete type

void is an incomplete type that cannot be completed. You cannot declare variables, arrays, or functions returning arrays of void. void has no size, so sizeof(void) is invalid. However, you can have arrays of void* (pointers to void), which are commonly used for generic containers as void* can point to any data type.

Previous C Arrays - 1D & 2D Next