Array Basics MCQs in C: 11 Solved Questions with Explanations

Solve 11 array basics questions from GATE, ISRO, UPPSC and UGC NET. Each answer explains the C rule or trace that rules out the strongest distractor.

KnowledgeGate Team

Exam prep & CS education

Updated 23 Jul 202611 min read

Array questions in GATE, ISRO, UPPSC and UGC NET almost never ask what an array is. They hand you a declaration with a subtle syntax error, an address to compute, a loop that writes into the array it is reading, or a function whose loop condition has been blanked out. The C rules behind all four are small and fixed: an initializer list takes curly braces, a two-dimensional array is stored one full row at a time, an array argument arrives as the address of element 0, and every cell you overwrite is visible to the next iteration.

Each question below is a past paper, answered with the rule or trace that settles it and with the option most people pick instead ruled out. Work each one on paper before you read the answer.

The four shapes array-basics questions take

Syntax questions test whether you can separate a valid initializer from something that merely looks like one. Memory questions give you a base address and an index pair and expect the row-major formula. Trace questions run a short loop that writes into the array it is reading, so the order of the writes decides the answer. Algorithm questions hide an invariant in a loop condition, a swap pattern or a midpoint update, and the options only separate once you have named it.

Spotting the shape in the first ten seconds tells you whether to reach for a rule, a formula, a written trace or an invariant. The same four habits carry straight into Data Structures MCQs.

Declaring, initializing and passing arrays: 2 solved questions

Q1. UPPSC 2022

Which of the following is the correct way to initialize an array?

  • (A) int num[6] = (2, 4, 12, 5, 45, 5)

  • (B) int num[ ] = {2, 4, 12, 5, 45, 5}

  • (C) int num{6} = {2, 4, 12}

  • (D) int num(6) = {2, 4, 12, 5, 45, 5}

Answer: (B). C initializes an array from a curly-braced list, and with the size left blank the compiler counts the six values for you. (A) is the trap: parentheses make that a comma expression rather than an initializer, so it does not compile. Neither (C) nor (D) is C syntax for stating a size. Practise this initialization question.

Q2. UGC NET 2014

When we pass an array as an argument to a function, what actually gets passed ?

  • (A) Address of the array

  • (B) Values of the elements of the array

  • (C) Base address of the array

  • (D) Number of elements of the array

Answer: (C). An array argument decays to a pointer to element 0, so the function receives that base address and nothing about the length, which is why sizeof inside the function cannot recover the count, ruling out (D), and why (B) is wrong: no elements are copied. (A) is the near miss worth understanding, because &arr is an address too, but its type is pointer-to-array, not the pointer-to-element the parameter actually receives. Practise this array-passing question.

Arrays in memory: the address formula and row-major layout

C stores a two-dimensional array one complete row at a time, so element (i, j) sits Base + (i * columns + j) * element_size bytes in. The column count sets the stride; the row count never appears.

Q3. GATE 2002

Consider the following declaration of a two-dimensional array in C: char a[100][100]; Assuming that main memory is byte-addressable and that the array starts at memory address 0, the address of a[40][50] is:

  • (A) 4040

  • (B) 4050

  • (C) 5040

  • (D) 5050

Answer: (B). Row-major storage lays down all of row 0, then all of row 1, so a[40][50] sits after 40 full rows of 100 chars plus 50 more. A char is 1 byte, giving (40 * 100 + 50) * 1 = 4050. (C) 5040 is the column-major address (50 * 100 + 40), which is not how C stores arrays; (A) and (D) reuse one index twice. Practise this address question.

A 100 x 100 grid labelled char a[100][100] with row 40 highlighted; below it the same storage unrolled into one row-major strip starting at address 0, with tick marks at 0 (a[0][0]), 4000 (a[40][0]) and the cell a[40][50] shaded at address 4050, annotated "address = (40 * 100 + 50) * 1 = 4050"; a small side note "row-major: finish row 0, then row 1, ...".

Q4. GATE 2015

Consider the following two C code segments. Y and X are one- and two-dimensional arrays of size n and n x n respectively, where 2 <= n <= 10. Assume that in both code segments, elements of Y are initialized to 0 and each element X[i][j] of array X is initialized to i+j. Further assume that when stored in main memory all elements of X are in the same main memory page frame. Code segment 1: for (i=0; i<n; i++) Y[i] += X[0][i]; Code segment 2: for (i=0; i<n; i++) Y[i] += X[i][0]; Which of the following statements is/are correct? S1: Final contents of array Y will be same in both code segments. S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory. S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory.

  • (A) Only S2 is correct

  • (B) Only S3 is correct

  • (C) Only S1 and S2 are correct

  • (D) Only S1 and S3 are correct

Answer: (C). Every X[i][j] holds i+j, so segment 1 writes X[0][i] = i into Y[i] and segment 2 writes X[i][0] = i into the same slot: the final Y is identical, so S1 holds. Segment 1 walks row 0, one contiguous run in row-major order; segment 2 walks column 0, jumping a whole row each step, so S3 fails. S1 and S2 survive, which rules out (A), (B) and (D). Practise this contiguity question.

Q5. GATE 2022

What is printed by the following ANSI C program?

#include<stdio.h> int main(int argc, char *argv[]) { int a[3][3][3] = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26, 27}}; int i = 0, j = 0, k = 0; for( i = 0; i < 3; i++ ){ for(k = 0; k < 3; k++ ) printf("%d ", a[i][j][k]); printf("\n"); } return 0; }
  • (A) 1 2 3 10 11 12 19 20 21

  • (B) 1 4 7 10 13 16 19 22 25

  • (C) 1 2 3 4 5 6 7 8 9

  • (D) 1 2 3 13 14 15 25 26 27

Answer: (A). Each inner brace fills one complete 3x3 block, so a[0] holds 1 to 9, a[1] starts at 10 and a[2] at 19. The loop pins j to 0 and moves only i and k, so it prints the first row of each block. (C) is block 0 printed whole, which a pinned i would give; (B) moves j with k fixed, and (D) moves i and j together. Practise this 3D array trace.

Trace-the-output programs: 3 solved questions

All three of these edit the array they are reading. Write the eight cells out and update them one write at a time, because a value read on the third pass may have been written on the first, and a redeclared variable can quietly shadow the one you are tracking.

Q6. ISRO 2015

The output of the following program is main() { static int x[] = {1,2,3,4,5,6,7,8}; int i; for (i=2; i<6; ++i) x[x[i]]=x[i]; for (i=0; i<8; ++i) printf("%d", x[i]); }

  • (A) 1 2 3 3 5 5 7 8

  • (B) 1 2 3 4 5 6 7 8

  • (C) 8 7 6 5 4 3 2 1

  • (D) 1 2 3 5 4 6 7 8

Answer: (A). Read the assignment as: take the value sitting at x[i] and use it as the index to write that value into. At i=2 the value is 3, so x[3] becomes 3; at i=4 the value is 5, so x[5] becomes 5. The passes at 3 and 5 then read what was just written and change nothing, leaving 1 2 3 3 5 5 7 8. (B) is the array untouched, which is what reading the statement as x[i] = x[i] would give. Practise this self-indexing trace.

A strip of 8 boxes indexed 0-7 holding 1,2,3,4,5,6,7,8, repeated in four rows for i = 2, 3, 4, 5; row i=2 shows an arrow from x[2]=3 to box 3 with the write "x[3] = 3", row i=3 labelled "x[3] already 3, no change", row i=4 shows an arrow from x[4]=5 to box 5 with "x[5] = 5", row i=5 labelled "x[5] already 5, no change"; final strip at the bottom reads 1 2 3 3 5 5 7 8.

Q7. GATE 2008

Consider the C program given below. What does it print?

#include <stdio.h> int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i] + 1; i++; } i--; for (j = 7; j > 4; j--) { int i = j/2; a[i] = a[i] - 1; } printf ("%d, %d", i, a[i]); }
  • (A) 2, 3

  • (B) 2, 4

  • (C) 3, 2

  • (D) 3, 3

Answer: (C). The first loop increments i twice per pass, so it touches only a[0] and a[2] and exits with i=4; the i-- leaves the outer i at 3. The second loop declares its own i, so the outer one is untouched: j=7 and j=6 both give 3 and take a[3] from 4 down to 2. (A) and (B) let the inner i leak out; (D) misses the second decrement. Practise this scope trace.

Q8. GATE 2007

Consider the C program given below :

#include <stdio.h> int main () { int sum = 0, maxsum = 0, i, n = 6; int a [] = {2, -2, -1, 3, 4, 2}; for (i = 0; i < n; i++) { if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) { if (sum > maxsum) maxsum = sum; sum = (a [i] > 0) ? a [i] : 0; } else sum += a [i]; } if (sum > maxsum) maxsum = sum ; printf ("%d\n", maxsum); }

What is the value printed out when this program is executed?

  • (A) 9

  • (B) 8

  • (C) 7

  • (D) 6

Answer: (C). The loop restarts the running sum whenever an element is negative or smaller than its predecessor, so what it measures is the best rising run. Across the array the sum reads 2, 0, 0, 3, 7, 2, and maxsum picks up 2 at the first reset and 7 at the last. (A) 9 adds every positive element, 2 + 3 + 4, which the reset at the first negative value makes unreachable. Practise this maxsum trace.

Arrays driving a small algorithm: 3 solved questions

These three hand you working code and ask what it preserves. Say the invariant out loud in one sentence first, then test each option against it.

Q9. GATE 2016

The following function computes the maximum value contained in an integer array p[ ] of size n (n >= 1). int max (int *p,int n) { int a = 0, b=n-1; while (__________) { if (p[a]<= p[b]) {a = a+1;} else {b = b-1;} } return p[a]; } The missing loop condition is:

  • (A) a != n

  • (B) b != 0

  • (C) b > (a + 1)

  • (D) b != a

Answer: (D). The function keeps two candidates, a and b, and each pass discards whichever endpoint is smaller. The maximum is never the smaller endpoint, so it survives every discard and is the only element left once the indices meet, which is exactly where b != a stops. (C) halts one step early with two candidates still standing, and (A) and (B) let an index walk past the meeting point and out of the array. Practise this loop-condition question.

Q10. GATE 2015

Let swap() be a function that swaps two elements using their addresses. Consider the following C function.

void fun (int arr [], int n ) { for (int i = 0; i < n; i+=2) { if (i>0 && arr[i-1] > arr[i]) swap(&arr[i], &arr[i-1]); if (i<n-1 && arr[i] < arr[i+1]) swap(&arr[i], &arr[i+1]); } }

If an array {10, 20, 30, 40, 50, 60, 70, 80} is passed to the function, the array is changed to

  • (A) {20, 10, 40, 30, 60, 50, 80, 70}

  • (B) {10, 30, 20, 40, 60, 50, 80, 70}

  • (C) {10, 20, 30, 40, 50, 60, 70, 80}

  • (D) {80, 70, 60, 50, 40, 30, 20, 10}

Answer: (A). The loop steps i by 2, stopping at 0, 2, 4 and 6. The first test compares with the element before, already in order here, so it never fires; the second swaps the pair whenever arr[i] < arr[i+1]. The input is strictly ascending, so all four pairs swap. (C) is the array unchanged, which needs the second test never to fire, and (B) swaps pairs starting one index late. Practise this pairwise swap question.

Q11. GATE 2014

Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.

int ProcessArray(int *listA, int x, int n) { int i, j, k; i = 0; j = n-1; do { k = (i+j)/2; if (x <= listA[k]) j = k-1; if (listA[k] <= x) i = k+1; } while (i <= j); if (listA[k] == x) return(k); else return -1; }

Which one of the following statements about the function ProcessArray is CORRECT?

  • (A) It will run into an infinite loop when x is not in listA

  • (B) It is an implementation of binary search

  • (C) It will always find the maximum element in listA

  • (D) It will return -1 even when x is present in listA

Answer: (B). Each pass sets k to the midpoint of [i, j] and shrinks the range: x <= listA[k] pulls j to k-1, and listA[k] <= x pushes i to k+1. At least one always fires, so the range strictly shrinks and the loop must end, ruling out (A). When x is present both fire and the closing listA[k] == x check returns its index, so (D) cannot happen; (C) is wrong because the search is steered by x, not by size. Practise this binary search question.

The pattern behind all 11

Bucket

What it tests

Five-second check

Syntax

Initialization

Curly braces; inferable size

Memory

Layout

Row-major address formula

Traces

Mutation and scope

Re-read changed values

Algorithms

Preserved rule

Name the invariant

Write the formula, the trace or the invariant down before you look at the options. More subject-wise practice sits in GATE CS Preparation.

The short version and where to practise next

Around 40 Array Basics questions are open for practice, and the per-question links above each open the full worked solution.

If you would rather learn C in order than question by question, the C Programming Course teaches it end to end, and the shorter C Language Course is enough if you only need the syntax and the trace practice. Solved sets across the other topics are in Coding & CS Fundamentals.

Braces for initializers, row-major for addresses, a written trace for anything that mutates, a named invariant for anything that loops. Four habits, and array questions stop being guesswork.