🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Arrays Quiz and Interview
Chapter 7, Lecture 5 of the free C programming course: test yourself with multiple-choice questions, output prediction, coding challenges and real embedded C interview questions.
You have met the vocabulary, the declaration rules, the index and bounds rules, and the loop patterns. Now it is time to prove it. This final lecture of the chapter is a practice lab for the free C programming course: a C arrays quiz, trace-the-output puzzles, small coding challenges and the questions that interviewers actually ask in embedded C programming roles. Treat it as a free C tutorial exam, and keep a compiler open, because running your own answers is the fastest way to learn in this free embedded C course.
What You Will Learn
- How to check your understanding of array declaration, indexing and bounds
- How to trace array code by hand and predict its output
- Four practice programs that reinforce the loop patterns from Lecture 4
- The ten array questions most often asked in C and embedded interviews, with model answers
Prerequisites
- Lectures 1 to 4 of this chapter
- GCC or any C11 compiler on your machine
- Pen and paper for tracing; do not skip this, it is half of the skill
C Arrays Quiz Recap of Chapter 7
| Topic | Rule to remember |
|---|---|
| Declaration | data_type name[size]; the size should be a compile-time constant |
| Memory | Elements sit in consecutive memory; total bytes = count x element size |
| Indexing | Valid indices run from 0 to size minus 1 |
| Bounds | C never checks; an invalid index is undefined behaviour |
| Looping | for (i = 0; i < SIZE; i++) visits every element once |
| Element count | sizeof arr / sizeof arr[0], in the scope where arr is declared |
Part A: C Arrays Quiz (Multiple Choice)
Assume a 32-bit platform where int and float each occupy four bytes. Choose one answer per question, then open the answer to compare.
Q1. For the declaration int arr[8]; what is the last valid index?
- A) 7
- B) 8
- C) 9
- D) 0
Show answer
A. Eight elements occupy indices 0 to 7, so the upper bound is size minus 1.
Q2. How many bytes does float temp[10]; occupy?
- A) 10
- B) 14
- C) 40
- D) 100
Show answer
C. Ten elements times four bytes each is 40 bytes.
Q3. Which array declaration is valid at file scope in every C standard?
- A)
int n = 4; int a[n]; - B)
#define N 4followed byint a[N]; - C)
int a[-4]; - D)
int a[2.5];
Show answer
B. The size must be an integer constant expression. A variable (A) is rejected at file scope, a negative size (C) is invalid, and a fractional size (D) is not an integer.
Q4. What does the C language guarantee when your program writes to arr[10] and arr has 5 elements?
- A) The compiler stops with an error
- B) The program stops with a run-time error message
- C) Nothing is guaranteed; the behaviour is undefined
- D) The write wraps around to arr[0]
Show answer
C. There is no automatic bounds checking and no wrap-around. The write may corrupt other data or crash, or appear to work.
Q5. Which expression gives the number of elements of int data[N]; when evaluated in the same scope?
- A)
sizeof data - B)
sizeof data[0] - C)
sizeof data / sizeof data[0] - D)
sizeof(int) * N
Show answer
C. Total bytes divided by bytes per element is the element count. Option A gives bytes, B gives one element’s size, and D gives bytes again.
Q6. A loop for (i = 0; i <= 5; i++) is used on int a[5];. What is wrong?
- A) Nothing; it visits all five elements
- B) It makes six passes and uses the invalid index 5 on the last one
- C) It never starts
- D) It skips a[0]
Show answer
B. The test i <= 5 lets i reach 5, but the valid indices stop at 4. Use i < 5.
Q7. Which of these is NOT allowed as an array subscript?
- A) An
intvariable - B)
i + j - C) A function that returns
int - D) A
floatvariable
Show answer
D. The subscript must have an integer type. Convert a float to an integer first.
Q8. What values does an uninitialised local array of int contain?
- A) All zeros
- B) All ones
- C) Indeterminate leftover values
- D) The index numbers
Show answer
C. Only file-scope and static arrays start as zeros. A local array holds whatever was in that stack memory.
Q9. Elements of an array are stored in memory…
- A) In scattered locations linked by pointers
- B) In consecutive locations, element 0 at the lowest address
- C) In reverse order, last element first
- D) Only in registers
Show answer
B. Contiguous storage is what makes index arithmetic cheap and predictable.
Q10. What is the value of sizeof a / sizeof a[0] for int a[6];?
- A) 1
- B) 4
- C) 6
- D) 24
Show answer
C. The array is 24 bytes, one element is 4 bytes, and 24 divided by 4 is 6.
Part B: Predict the Output
Trace each program on paper first. Then compile it and compare. For loops, write the value of the counter at every step.
Puzzle 1: Counting Down
#include <stdio.h>
int main(void)
{
int a[4] = {3, 6, 9, 12};
int i;
for (i = 3; i >= 0; i--)
printf("%d ", a[i] - i);
printf("\n");
a[1] += a[2];
a[3]++;
printf("%d %d\n", a[1], a[3]);
return 0;
}
Show output and explanation
9 7 5 3
15 13
The loop visits indices 3, 2, 1, 0 and prints each element minus its own index: 12-3=9, 9-2=7, 6-1=5, 3-0=3. Then a[1] becomes 6+9=15 and a[3] becomes 12+1=13.
Puzzle 2: Side Effects in the Subscript
#include <stdio.h>
int main(void)
{
int v[5] = {2, 4, 6, 8, 10};
int i = 1;
printf("%d ", v[i++]);
printf("%d ", v[i]);
printf("%d\n", v[++i]);
printf("i = %d\n", i);
return 0;
}
Show output and explanation
4 6 8
i = 3
The first call uses i=1 (value 4), then i becomes 2. The second call prints v[2], which is 6. The third call increments i to 3 first (pre-increment) and prints v[3], which is 8. Each statement is separate, so there is no ambiguity about evaluation order. Never put two changes to the same variable inside one expression.
Part C: Programming Challenges
Write each program yourself, compile it with -Wall -Wextra, and compare with the expected output. Open the hint only if you are stuck.
| # | Challenge | Test data | Expected output |
|---|---|---|---|
| 1 | Print an array in reverse order | {3, 6, 9, 12, 15} | 15 12 9 6 3 |
| 2 | Copy one array into another, then print the copy | src = {5, 10, 15, 20, 25} | 5 10 15 20 25 |
| 3 | Print the index and value of the first maximum | {14, 92, 37, 92, 8} | max 92 at index 1 |
| 4 | Count the elements above the average | {10, 20, 30, 40, 50} | average 30.00, above: 2 |
Hint for challenge 1
Start the counter at the upper bound, SIZE minus 1, and loop while it is greater than or equal to 0. If you use an unsigned counter, rethink the condition, because an unsigned value is never negative.
Hint for challenge 2
Declare both arrays with the same named size constant. Copy element by element inside a loop. The plain statement dst = src; does not compile, because an array cannot be assigned as a whole.
Hint for challenge 3
Keep two variables, the largest value so far and its index. Update both only when an element is strictly greater, so that the first maximum wins.
Hint for challenge 4
You need two loops: one to compute the sum (and therefore the average, with a float cast), and a second to count elements greater than that average.
Stretch goal
Modify challenge 4 so it reads the data from the keyboard and rejects invalid input, using the scanf() return-value check from Lecture 4. Then add an explicit bounds check for any index you compute.
Part D: Embedded C Interview Questions on Arrays
The C arrays quiz above checks recall; interviews go one step further and ask you to justify your answers with reasons from memory layout, compiler behaviour and hardware limits.
These are the array questions that appear again and again in embedded and C interviews. Say your answer aloud before opening the model answer; interviewers listen for clear reasoning, not just the final word.
1. Why do C arrays start at index 0?
Show answer
The index is an offset from the start of the array. The first element is zero elements away, so its index is 0. The address of element i is the start address plus i times the element size, which needs no extra subtraction.
2. What happens if you access an array outside its bounds?
Show answer
The behaviour is undefined. C does no automatic bounds checking, so the access may read or overwrite neighbouring data, crash, or seem to work. Prevent it by validating indices that come from outside the code, using named size constants, and testing with sanitisers on a PC.
3. How do you find the number of elements in an array?
Show answer
Use sizeof arr / sizeof arr[0] in the scope where the array is declared. This fails for a function parameter, because a parameter declared with array syntax is really a pointer, so sizeof returns the pointer size. Pass the length as a separate argument.
4. Is an array the same as a pointer?
Show answer
No. An array is a block of elements with a fixed size. In most expressions its name converts to a pointer to the first element, which is why the two look similar, but sizeof on the array gives the full size and the array name cannot be assigned to. The pointer chapters of this course cover the details.
5. Can you copy one array into another with the assignment operator?
Show answer
No. Arrays are not assignable as a whole. Copy element by element in a loop, or use memcpy() with the correct byte count, which is the element count times the element size.
6. Where is an array stored on a microcontroller?
Show answer
It depends on the declaration. A non-static array at file scope or a static array lives in static RAM, in the initialised-data or the zero-initialised region. A local array lives on the stack. A const table can be placed in flash, depending on the toolchain and linker settings, which saves RAM.
7. Why are variable-length arrays discouraged in embedded code?
Show answer
They allocate from the stack at run time, so an unexpectedly large size can overflow a small stack with no warning. They also make worst-case stack use impossible to prove, are optional since C11, and are banned by coding standards such as MISRA C.
8. Can an array have a size of zero?
Show answer
Not in standard C; the size must be greater than zero. Some compilers, for example GCC, accept zero-length arrays as an extension, mainly at the end of a structure, but portable code should not rely on it.
9. How do you keep an index inside a fixed-size buffer when data keeps arriving, like a UART receive buffer?
Show answer
Use a circular (ring) buffer and wrap the index after each write, for example head = (head + 1) % BUF_SIZE. If BUF_SIZE is a power of two you can use head = (head + 1) & (BUF_SIZE - 1) instead. Also track the count or a tail index so you detect overflow instead of overwriting unread data.
10. Why prefer an unsigned type for an array index?
Show answer
An index can never be negative, so an unsigned type matches its meaning and lets a single comparison against the size protect the array. The trade-off is that decrementing an unsigned counter below zero wraps to a huge number, so loops that count down need a careful condition.
Common Mistakes in the C Arrays Quiz and Interviews
Whether it is a classroom C arrays quiz or a screening call for an embedded role, the same handful of slips cost people marks. Check yourself against this list after every attempt.
Do not guess the output. Candidates lose marks by skipping the trace. Write the counter and every changed variable on paper, line by line.
- Saying C throws an exception on a bad index; it does not
- Using
sizeofon a function parameter and expecting the array size - Claiming arrays can be assigned with
= - Mixing up the size of an array with its upper bound
- Forgetting that an uninitialised local array holds garbage
Best Practices Recap
- One named constant for each array size, used in the declaration and every loop
- Validate every index that is not a compile-time constant
- Compile with warnings enabled; test on a PC with sanitisers before flashing hardware
- Keep loops short and single-purpose, and trace tricky ones on paper
- Know where your arrays live, and budget RAM and stack before you declare them
Summary and Key Takeaways
Chapter 7 so far
- An array groups same-typed values under one name in consecutive memory
- Declare it with a constant size; total bytes equal count times element size
- Valid indices are 0 to size minus 1, and C does not enforce that for you
- A for loop with
i < SIZEis the safe, standard way to process an array - Strong array habits are among the most frequently tested skills in embedded C interviews, which is why every C arrays quiz and screening test returns to them
If you scored well on this C arrays quiz, you already have the foundation most entry-level firmware roles expect; if you missed several questions, reread the matching lecture and retry the quiz after a day. You have now covered one-dimensional array basics from declaration to processing. The remaining parts of this chapter build on this foundation with initialisation, passing arrays to functions and multi-dimensional arrays, all inside the same free C programming course.
Frequently Asked Questions
Where can I practise C arrays for free?
Work through the quiz, puzzles and challenges on this page with GCC on your own computer. This free C programming course is designed so every example can be built and run without paid tools.
How should I prepare for embedded C interview questions on arrays?
Be able to explain indexing, bounds, memory layout, sizeof behaviour and ring buffers aloud, and practise tracing small programs on paper.
Is this free embedded C course enough to start firmware work?
It builds the language foundation. Pair it with hands-on practice on a development board and the later chapters on pointers, structures and bit manipulation.
What is the most common array bug in C?
The off-by-one error: using the size as the last index or looping one step too far.
Should I use arrays or dynamic memory in embedded systems?
Fixed-size arrays are preferred in most firmware because memory use is known at build time and nothing can fail at run time because the heap ran out.
How do I check my answers to the output puzzles?
Compile the program with gcc -Wall -Wextra -std=c11 and run it. If your trace and the real output differ, find the first line where they diverge.
Keep Building with the Free C Tutorial Series
You finished the array fundamentals. Continue the free embedded C course with the next part of Chapter 7, then move on to strings and pointers.

2 Comments