🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Arrays Quiz and Interview Questions
Ten quiz questions, four output puzzles, coding challenges with verified solutions and twelve interview questions on C arrays. The practice lab of our free C tutorial for embedded C programming.
Reading about arrays is one thing; proving you understand them is another. This closing lab of the arrays chapter in our free C programming course tests everything from passing arrays to functions to initializing two dimensional arrays. Work through the quiz, predict the program outputs before running them, and attempt the coding challenges. If you are preparing for a firmware interview, the last section covers the array questions that appear again and again in embedded C programming interviews and C programming for embedded systems roles. Every answer and program output shown here was checked by compiling and running the code with GCC.
What You Will Learn
- How to check your understanding of array parameters,
sizeofand 2D layout - How to predict program output by tracing array changes by hand
- How to write small array functions from scratch
- How to answer the classic array interview questions clearly and briefly
Prerequisites
- All previous lectures of the arrays chapter, especially the last four on functions and 2D arrays
- GCC installed, so you can run the challenge solutions:
gcc -std=c17 -Wall -Wextra -o prog prog.c
Arrays Chapter Quick Recap
| Topic | Rule to remember |
|---|---|
| Array argument | The function receives the address of element 0, so changes persist in the caller |
| Array parameter | int v[10], int v[] and int *v are the same |
| Length | Pass it separately; compute it with sizeof(a) / sizeof((a)[0]) in the declaring scope |
| 2D declaration | type name[ROWS][COLS], row first, indices from 0 |
| 2D memory | Row-major: offset is (r * COLS + c) * size |
| 2D parameter | Column count is required; row count is optional |
| Initialization | Values fill in memory order; missing values become zero |
Arrays Quiz: 10 Multiple Choice Questions
Choose an answer first, then open the answer panel to check yourself.
Question 1. Inside void f(int v[8]), what is v?
- An array of 8 integers
- A pointer to int
- A copy of the caller’s array
- A constant integer
Show answer
B. The parameter is rewritten to int *v; the 8 is ignored.
Question 2. In the function that declares int a[12];, which expression gives the number of elements?
sizeof(a)sizeof(a) * sizeof(a[0])sizeof(a) / sizeof(a[0])sizeof(&a)
Show answer
C. Total bytes divided by bytes per element gives 12.
Question 3. A function has the parameter const uint8_t buf[]. Which statement inside it fails to compile?
uint8_t x = buf[0];buf[0] = 5;if (buf[1] == 0) { }const uint8_t *p = buf;
Show answer
B. The elements are read-only through this parameter, so assignment is an error.
Question 4. For int g[4][6];, how many elements are there and what is the last one?
- 10 elements, last is
g[4][6] - 24 elements, last is
g[4][6] - 24 elements, last is
g[3][5] - 10 elements, last is
g[3][5]
Show answer
C. 4 times 6 is 24, and the largest valid indices are one less than each dimension.
Question 5. Which function parameter declaration is invalid?
int t[][5]int t[3][5]int t[][]int (*t)[5]
Show answer
C. The column size is needed to locate rows, so only the first dimension may be omitted.
Question 6. Given int m[2][3] = {{1, 2}, {3}};, what is m[0][2]?
- 1
- 2
- 3
- 0
Show answer
D. Row 0 has only two values listed, so its third element is zero-filled.
Question 7. In int a[3][3], which element sits in memory right after a[1][2]?
a[2][0]a[1][3]a[2][2]a[0][0]
Show answer
A. Row-major order places row 2 immediately after the end of row 1.
Question 8. For a 4-column table of 1-byte elements, what is the byte offset of element [2][1]?
- 6
- 8
- 9
- 12
Show answer
C. (2 × 4 + 1) × 1 = 9.
Question 9. What does sizeof report for uint16_t grid[3][4];?
- 7
- 12
- 14
- 24
Show answer
D. 12 elements times 2 bytes each equals 24 bytes.
Question 10. A local variable int buf[4]; is declared inside main with no initializer. What do its elements hold?
- All zeros
- All ones
- Indeterminate values
- The compiler refuses to compile
Show answer
C. Local arrays are not zeroed automatically. Write = {0} if you need zeros.
| Your score | What it means |
|---|---|
| 9 to 10 | Excellent. Move on to the challenges. |
| 6 to 8 | Good. Re-read the lectures behind the questions you missed. |
| 0 to 5 | Revisit the chapter and rerun each demo program. |
Predict the Output of Array Programs
Trace the code by hand, write down what you expect, then open the answer. Here is the complete test program:
#include <stdio.h>
static void f(int x[])
{
x[0] = 99;
x[3] += x[1];
}
int main(void)
{
int a[4] = {1, 2, 3, 4};
int m[2][3] = {{1, 2}, {3}};
int g[2][3] = {1, 2, 3, 4};
int t[3][5];
f(a);
printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
printf("%d %d %d\n", m[0][2], m[1][1], m[1][0]);
printf("%d %d\n", g[1][0], g[1][2]);
printf("%zu\n", sizeof(t) / sizeof(t[0]));
return 0;
}
Puzzle 1. What does the first printf show after f(a)?
Show answer
99 2 3 6. The function edits the caller’s array: element 0 becomes 99, and element 3 becomes 4 + 2 = 6.
Puzzle 2. What does the second printf show?
Show answer
0 0 3. Row 0 never received a third value, row 1 never received a second value, and m[1][0] was set to 3.
Puzzle 3. What does the third printf show?
Show answer
4 0. The flat list fills row 0 with 1, 2, 3 and puts 4 at the start of row 1. The third element of row 1 stays zero.
Puzzle 4. What does the last printf show?
Show answer
3. One row is t[0], so total size divided by row size gives the row count, whatever the element size is.
Compile it with gcc -std=c17 -Wall -Wextra -o predict predict.c. GCC warns about missing braces on the flat initializer of g, a good reminder of the previous lecture. The full run prints:
99 2 3 6
0 0 3
4 0
3
Array Coding Challenges for Embedded C Programming
Try each challenge on your own before looking at hints or solutions.
- Reverse in place. Write
void reverse_in_place(int a[], size_t n)that reverses the array without a second buffer. Hint: swap the first and last, then move inward. - Diagonal sum. Write
int trace3(int m[3][3])that returns the sum of the main diagonal. - Row helper. Write a function that sums one row of a table when given the column count and one row.
- Frame validator. Write
int frame_ok(const uint8_t f[], size_t n)that returns 1 when all bytes, including the final checksum byte, add up to zero modulo 256. This pairs with the checksum from the earlier lecture. - Keypad decode. Given a 4 by 4
static const charkey map, writechar key_at(int row, int col)that returns'?'for an out-of-range row or column.
Show solutions to challenges 1, 2 and 3 with output
#include <stdio.h>
#include <stddef.h>
static void reverse_in_place(int a[], size_t n)
{
for (size_t i = 0; i < n / 2; i++) {
int tmp = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = tmp;
}
}
static int trace3(int m[3][3])
{
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += m[i][i];
}
return sum;
}
static int row_sum(int cols, int row[])
{
int s = 0;
for (int c = 0; c < cols; c++) { s += row[c]; }
return s;
}
int main(void)
{
int v[6] = {10, 20, 30, 40, 50, 60};
int m[3][3] = {{2, 0, 0}, {0, 5, 0}, {0, 0, 9}};
reverse_in_place(v, 6);
for (int i = 0; i < 6; i++) { printf("%d ", v[i]); }
printf("\n");
printf("trace = %d\n", trace3(m));
printf("row 1 sum = %d\n", row_sum(3, m[1]));
return 0;
}
60 50 40 30 20 10
trace = 16
row 1 sum = 5
Notice that m[1] is itself a one-dimensional array (a whole row), so it can be handed to a function that expects a plain array. Challenges 4 and 5 follow the same patterns, so write and test them yourself.
C Arrays Interview Questions and Answers
Keep answers short and precise. Interviewers value clarity over length.
1. What is the difference between an array and a pointer in C?
An array is a block of contiguous elements with a size known to the compiler. A pointer is a variable holding an address. An array name converts to a pointer to its first element in most expressions, but the two are different types, which shows in sizeof.
2. Why does sizeof give the pointer size for an array parameter?
Because the parameter is adjusted to a pointer type. Only the declaring scope sees the real array, so compute the length there and pass it in.
3. Is C pass by value or pass by reference?
Always pass by value. For arrays, the value passed is the address of the first element, giving reference-like behaviour.
4. When does an array name not decay to a pointer?
When it is the operand of sizeof or the unary &, and when it is a string literal initializing a character array.
5. What is the difference between a, &a and &a[0] for an array a?
a and &a[0] both give the address of the first element, with type pointer to element. &a has the same address but type pointer to the whole array, so adding 1 to it steps over the entire array.
6. How is a 2D array stored in memory?
Row-major and contiguous: row 0, then row 1, and so on. The offset of [r][c] is (r * COLS + c) elements.
7. Why must the column count appear in a 2D array parameter?
The compiler needs it to compute the address of each row. The row count is not part of that calculation, so it may be omitted.
8. What is the difference between int *a[5], int (*a)[5] and int a[5][5]?
The first is an array of five pointers to int. The second is a pointer to an array of five ints, which is how a 2D array parameter looks after adjustment. The third is a real 5 by 5 block of integers.
9. What happens when you index past the end of an array?
The behaviour is undefined: it may read garbage, corrupt neighbouring variables or crash. C does no bounds checking, so you must validate indices yourself.
10. How do you pass an array to a function without allowing changes?
Declare the parameter const, for example const int a[], and pass the length separately.
11. Are arrays inside structs copied when the struct is passed by value?
Yes. The whole struct, including its array, is copied. Pass a pointer to the struct to avoid the copy.
12. Where does a const lookup table live on a microcontroller?
Typically in flash, because its initial values are constant and it is never written. This saves RAM. Confirm in the linker map for your toolchain.
Common Mistakes and Troubleshooting
| Mistake | Symptom | Fix |
|---|---|---|
| Computing the length inside the callee | Length is 4 or 8 regardless of data | Pass the length from the caller |
| Forgetting zero for local arrays | Random values in output | Initialize with = {0} |
| Mixing up rows and columns in a 2D parameter | Garbled table, wrong results | Match COLS exactly in caller and callee |
| Reading array bounds from untrusted data | Crash or data leak | Check every index and length first |
Best Practices, Performance and Security
Best Practices
- Pair every array with its length and mark input arrays
const. - Use braced rows when initializing 2D arrays.
- Compile with
-Wall -Wextraand read every warning.
Performance Considerations
Pass arrays by address, loop rows on the outside, and keep constant tables in flash. These three habits cover most array performance concerns on small devices.
Security Considerations
Treat every index and length that originates outside your code as hostile until validated. Most memory-safety bugs in embedded C come from this one omission.
Summary and Conclusion
Chapter 7 key takeaways
- Arrays reach functions as addresses, so functions modify the caller’s data.
- Length travels separately;
sizeofbelongs in the declaring scope. - 2D arrays are row-major, and their parameters need the column count.
- Braced rows and
consttables make initialization safe and RAM-friendly.
You have now completed the arrays chapter of this free C programming course: one-dimensional arrays, array functions, two dimensional arrays, processing patterns and initialization. Each concept connects directly to real firmware work, from ADC buffers to keypad maps. Keep the demo programs, modify them, and break them on purpose; fixing your own bugs is the fastest way to build confidence. Strings, which are simply arrays of characters, will feel familiar once you are comfortable with everything here.
Frequently Asked Questions
Where can I practise more C array problems for free?
Re-implement the demos from each lecture with different data, then extend them: add a column, change the element type, or write a new helper function. This free C tutorial series gives complete code to start from.
How many array questions are asked in embedded C interviews?
Array and pointer questions are among the most frequent. Expect the sizeof-in-function question, array versus pointer, and 2D array memory layout almost every time.
Should I memorise syntax or understand layout?
Understand layout first. Once you can picture row-major memory and address passing, the syntax rules follow naturally.
Is this free embedded C course enough for a first firmware job?
It builds the C foundation that firmware roles expect. Pair it with hands-on practice on a development board and the later chapters on pointers, structures and memory.
Do the code examples work on microcontrollers?
The language rules are the same. Replace printf with your UART or debug output, and take care with the sizes of pointers and integer types on your target.
Chapter 7 Complete: What Next?
You have finished the arrays chapter of our free C programming course. Continue to the next chapter of the course, or revisit any lecture to refresh a topic.

2 Comments