2D Arrays Quiz and Interview- C Programming Tutorials For Beginners

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Free C Programming Course · Arrays · Lecture 24

2D Arrays Quiz and Interview

Test yourself on 2D array initialization, matrix addition, multiplication, transpose and 3D arrays, then practice the interview questions asked in embedded C programming roles.

Free C Programming Course: 2D Array Practice

This final lecture of the array pages in our free C programming course is a practice lab. Reading a free C tutorial is useful, but you only own a concept when you can predict output, spot a bug and explain it out loud. Below you will find 10 multiple-choice questions, 4 output-prediction problems, 4 programming challenges with one full solution, and 14 interview questions with model answers, all tuned for embedded C programming and for anyone using this free embedded C course to prepare for a firmware job.

What You Will Learn

  • How to check your understanding of 2D array initialization and layout
  • How to predict the output of short array programs
  • How to write row sums, column sums, trace and symmetry checks
  • How to answer the array questions interviewers actually ask

Prerequisites

Lectures 19 to 23 of this chapter: 2D array initialization, matrix addition, multiplication, transpose and 3D arrays. Try each question before opening its answer.

Quick Revision Table

TopicOne-line rule
InitializationMissing values become 0; first dimension optional, others required
Memory layoutRow-major; a[i][j] is at offset i*COLS + j
Matrix additionSame order; sum[i][j] = a[i][j] + b[i][j]
Matrix multiplication(M x N) times (N x P) = (M x P); c[i][j] += a[i][k] * b[k][j]
Transposet[j][i] = m[i][j]; M x N becomes N x M
3D arraya[D1][D2][D3]; offset = i*D2*D3 + j*D3 + k

Part 1: Multiple-Choice Quiz

Assume a 4-byte int. Open each answer after you have decided.

Question 1

For int a[2][3] = {1, 2, 3, 4}; what is the value of a[1][1]?

  1. 4
  2. 0
  3. 2
  4. Garbage value
Show answer

B (0). The flat list fills row 0 with 1 2 3 and starts row 1 with 4, so a[1][0] is 4 and the remaining elements of row 1 become 0.

Question 2

Which declaration is invalid in C?

  1. int a[][3] = {{1,2,3},{4,5,6}};
  2. int a[2][3] = {0};
  3. int a[3][] = {{1,2},{3,4},{5,6}};
  4. int a[2][2] = {{1},{2}};
Show answer

C. Only the first dimension may be omitted. The compiler needs the column count as the row stride.

Question 3

What is sizeof(int[4][5]) on a platform where int is 4 bytes?

  1. 20
  2. 40
  3. 80
  4. 9
Show answer

C (80). 4 x 5 = 20 elements, times 4 bytes each.

Question 4

Matrix A is 3 x 2 and matrix B is 2 x 4. What is the size of A x B?

  1. 2 x 2
  2. 3 x 4
  3. 4 x 3
  4. The product is not defined
Show answer

B (3 x 4). The inner sizes (2 and 2) match and disappear, leaving rows of A by columns of B.

Question 5

How many multiplications does the standard triple loop perform for the product in Question 4?

  1. 12
  2. 14
  3. 24
  4. 6
Show answer

C (24). M x N x P = 3 x 2 x 4.

Question 6

In an in-place transpose of a square matrix, what happens if the inner loop starts at j = 0 instead of j = i + 1?

  1. The matrix is transposed twice as fast
  2. The matrix ends unchanged, because each pair is swapped twice
  3. The diagonal is lost
  4. Compile error
Show answer

B. Every mirrored pair is swapped once from each side, which restores the original positions.

Question 7

For int a[2][3][4], at what flat offset does a[1][2][1] sit?

  1. 19
  2. 20
  3. 21
  4. 23
Show answer

C (21). 1 x (3 x 4) + 2 x 4 + 1 = 12 + 8 + 1.

Question 8

Which statement is true about an uninitialized local 2D array?

  1. All elements are 0
  2. All elements are -1
  3. Elements have indeterminate values
  4. The compiler refuses to compile it
Show answer

C. Only static and global arrays are zeroed automatically.

Question 9

Which expression is element-wise multiplication, not matrix multiplication?

  1. c[i][j] += a[i][k] * b[k][j];
  2. c[i][j] = a[i][j] * b[i][j];
  3. c[i][j] = a[i][k] + b[k][j];
  4. c[j][i] = a[i][j];
Show answer

B. It multiplies matching cells and has no summation over k.

Question 10

Why should a large 3D array in an MCU program usually be static or global?

  1. Local arrays cannot be 3D
  2. Large locals can overflow the small stack
  3. Static arrays are faster to write
  4. Global arrays cannot be overwritten
Show answer

B. The stack on a microcontroller is small, and an oversized local array can corrupt memory or crash without a clear error.

Part 2: Predict the Output

Problem 1

int m[2][3] = { {1, 2, 3}, {4, 5, 6} };
printf("%d %d\n", m[1][2], *(*(m + 1) + 1));
Show answer

Output: 6 5. The expression m[1][2] is the last element. In the second expression m + 1 points at row 1, dereferencing gives that row, adding 1 moves to column 1, and the final dereference reads 5.

Problem 2

int d[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int t = 0;
for (int i = 0; i < 3; i++)
    t += d[i][i];
printf("%d\n", t);
Show answer

Output: 15. The loop adds the main diagonal 1 + 5 + 9, which is called the trace.

Problem 3

int t[4][5];
printf("%zu %zu\n", sizeof(t) / sizeof(t[0]),
                    sizeof(t[0]) / sizeof(t[0][0]));
Show answer

Output: 4 5. This is the sizeof trick for the row count and the column count.

Problem 4

int m[2][3] = { {1, 2, 3}, {4, 5, 6} };
int row[2] = {0}, col[3] = {0};
for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++) {
        row[i] += m[i][j];
        col[j] += m[i][j];
    }
printf("%d %d | %d %d %d\n", row[0], row[1], col[0], col[1], col[2]);
Show answer

Output: 6 15 | 5 7 9. Row sums are 1+2+3 and 4+5+6; column sums are 1+4, 2+5 and 3+6.

Part 3: Programming Challenges

#TaskHint
1Print the sum of every row and every column of a 3×4 matrixTwo accumulator arrays, as in Problem 4
2Find the trace (sum of the main diagonal) of an N x N matrixOne loop, index [i][i]
3Check whether a square matrix is symmetric (equals its transpose)Compare m[i][j] with m[j][i] for j greater than i
4Check whether a square matrix is an identity matrixDiagonal must be 1, every other cell 0

Worked Solution: Symmetric Matrix Check

A square matrix is symmetric when m[i][j] equals m[j][i] for every pair. We only need to test the cells above the diagonal. The function below uses a C99 variable-length array parameter, which lets the function accept any square size, and it returns 1 or 0.

/* symmetric.c */
#include <stdio.h>

static int is_symmetric(int n, int m[n][n])
{
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (m[i][j] != m[j][i])
                return 0;
    return 1;
}

int main(void)
{
    int s1[3][3] = { {1, 7, 3}, {7, 4, -5}, {3, -5, 6} };
    int s2[3][3] = { {1, 7, 3}, {2, 4, -5}, {3, -5, 6} };
    printf("symmetric s1=%d s2=%d\n", is_symmetric(3, s1), is_symmetric(3, s2));
    return 0;
}
$ gcc -std=c11 -Wall -Wextra -o symmetric symmetric.c
$ ./symmetric
symmetric s1=1 s2=0

Try the other three challenges yourself before looking anything up. A good self-check: run them on a matrix whose answer you can compute by hand.

Note on variable-length array parameters: the form int m[n][n] with a size parameter declared earlier is standard C99 and optional in C11 and newer. gcc and clang support it. Some embedded compilers do not, so check your toolchain before relying on it. A fixed #define size always works.

Part 4: Interview Questions on Arrays

These come up in embedded C programming interviews, from fresher campus rounds to firmware engineer screens. Try answering aloud in two or three sentences first.

1. Why must the second dimension be specified in a 2D array declaration?

Because the compiler needs the column count to compute the address of a[i][j] as base + (i * COLS + j) * sizeof(element). The row count is not part of that formula, which is why the first dimension can be omitted.

2. How is a 2D array stored in memory?

In row-major order as one contiguous block: all of row 0, then row 1, and so on, with no padding between rows of the same type.

3. What is the difference between a 2D array and an array of pointers?

A 2D array is one contiguous block with fixed row length. An array of pointers holds separate addresses, each of which can point to a different row allocated anywhere, with different lengths. They are indexed the same way but are different types with different memory use.

4. How do you pass a 2D array to a function?

Declare the parameter with the column size, such as int m[][COLS], or in C99 use int rows, int cols, int m[rows][cols]. The function receives a pointer to the first row, so it modifies the caller’s data and cannot use sizeof to find the original size.

5. What does sizeof(arr) / sizeof(arr[0]) give you?

The number of rows of a 2D array, as long as arr is still an array in that scope and has not decayed to a pointer.

6. What is row-major versus column-major order?

Row-major stores each row contiguously (C, C++). Column-major stores each column contiguously (Fortran, MATLAB). It matters when exchanging data with libraries from other languages and when choosing a cache-friendly loop order.

7. How does a partially initialized 2D array behave?

Elements without an initializer are set to zero, row by row when nested braces are used.

8. Where does a const 2D array live on a microcontroller?

Typically in read-only flash memory, which saves RAM. The exact placement depends on the compiler and linker script, so verify it in the map file.

9. What is the time complexity of matrix multiplication with the three-loop method?

O(M x N x P), which is O(n cubed) for square matrices of side n.

10. How would you transpose a square matrix in place?

Loop i from 0 to n-1 and j from i+1 to n-1, swapping m[i][j] with m[j][i] using a temporary variable. Starting j at i+1 prevents swapping each pair twice.

11. Why can an out-of-range array index be dangerous in firmware?

C performs no bounds checking, so the access silently reads or overwrites neighbouring variables, which can cause intermittent bugs, crashes or security vulnerabilities. Validate indices that come from outside the program.

12. Give the flat offset for a[i][j][k] in int a[D1][D2][D3].

offset = i * (D2 * D3) + j * D3 + k. The first dimension D1 is not needed to calculate it.

13. What is a good way to avoid wrap-around when adding 8-bit matrix data?

Widen to a larger type before adding, then clamp (saturate) to the maximum value if the result exceeds it, as done in the brightness example of the matrix addition lecture.

14. Which values do uninitialized local and global arrays hold?

Local (automatic) arrays hold indeterminate values. Global and static arrays are zero-initialized before main starts.

Common Mistakes Checklist

CheckWhy it matters
Did you size the result array correctly?Multiplication gives M x P, transpose gives N x M
Is the accumulator zeroed before the k loop?Garbage in means garbage out
Do all loop limits use the right dimension names?Prevents out-of-bounds access
Are scanf return values checked?Stops garbage matrices
Is large data static instead of a big local?Protects the small embedded stack

Summary and Key Takeaways

  • A 2D array is a contiguous block in row-major order, and the column count is the row stride.
  • Matrix addition is cell by cell, multiplication is row times column, and transpose swaps the indices.
  • 3D arrays add a third index with offset i*D2*D3 + j*D3 + k.
  • Interviewers favour questions on layout, initialization, passing arrays to functions and bounds safety.

Conclusion

If you could answer most of the questions above without peeking, you have a solid base in multi-dimensional arrays and a good start for any embedded C programming interview. Rewrite the programs from this chapter from memory, change the sizes, and watch what breaks; that habit teaches more than any amount of reading. Keep going through the free embedded C course, because every later chapter builds on the array skills you practiced here.

Frequently Asked Questions

Is this C course really free?

Yes. The free C programming course on EmbeddedPathashala has no fee, and every lecture includes examples, quizzes and interview questions.

Is this free embedded C course useful for beginners?

Yes. It starts from the basics, and each lecture adds embedded-specific notes on memory, data types and hardware use.

How should I practice with this quiz?

Answer on paper first, run the programs in a compiler to confirm, then open the answers and compare your reasoning, not just the result.

Which compiler should I use for the examples?

Any C11 compiler such as gcc or clang. Compile with -Wall -Wextra to catch common mistakes early.

Do embedded interviews really ask about 2D arrays?

Often. Questions on memory layout, array parameters, bounds safety and small matrix routines are common in firmware screening rounds.

Finished the Arrays Chapter?

Great work. Continue with the free C programming course on EmbeddedPathashala and keep building real embedded C programming skills.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *