C Arrays Quiz and Interview – C Programming Course With Examples

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 31

C Arrays Quiz and Interview

Test your knowledge, predict outputs and rehearse the answers interviewers expect.

You have finished the arrays chapter of this free C programming course, so it is time to practise. This lecture collects C arrays interview questions and a quiz on everything from 3D arrays to binary search. It is aimed at students preparing for embedded C programming roles, and it works as a quick revision before an interview. Try each question on paper first. Open the answer only after you have committed to one.

What You Will Learn

  • Review the key ideas of lectures 25 to 30 in one table
  • Answer 13 multiple choice questions
  • Predict the output of five short programs
  • Rehearse 13 interview questions with model answers
  • Solve four coding challenges with hints

Prerequisites

Lectures 25 to 30 of this free embedded C course. Have a compiler ready so you can check your predictions.

Chapter Recap in One Table

LectureTopicOne thing to remember
253D arraysLast subscript changes fastest. Offset = i*R*C + j*C + k
26Passing 2D arraysOnly the leftmost size may be omitted; the function receives a pointer to a row
27StringsA string is a char array ending in a zero byte; n characters need n+1 bytes
28String input and outputGive every input function the buffer size; use fgets, never gets
29Base conversionDivide, collect remainders, reverse; use do-while so 0 works
30Binary searchNeeds sorted data; low <= high; at most floor(log2 n) + 1 steps
Revision map for the arrays chapter
Layout3D arrays→row-major→offset formula
Functions2D parameters→column count needed→pointer to row
Textstrings→null terminator→fgets and puts
Algorithmsbase conversion→binary search→bsearch()

C Arrays Interview Questions: 13-Question Quiz

Write your answers first, then click Show answer under each question.

Q1. Which sizes may be left empty in the parameter int m[][5]?

  1. Both
  2. Only the first
  3. Only the last
  4. None
Show answer

B, only the first. The compiler needs the row length (5) to compute where each row starts. The number of rows is not needed for the address.

Q2. What is the ASCII value of the null terminator?

  1. ‘0’, which is 48
  2. 0
  3. 1
  4. 255
Show answer

B, 0. The terminator is the byte with value zero. The digit character ‘0’ has value 48.

Q3. What does sizeof give for char s[] = "ARM";?

  1. 3
  2. 4
  3. 5
  4. Undefined
Show answer

B, 4. Three letters plus the terminator. The compiler sizes the array to fit them.

Q4. A program runs scanf("%s", name) and you type New Delhi. What is stored in name?

  1. New Delhi
  2. New
  3. Delhi
  4. Nothing
Show answer

B, New. %s stops at the first white space. The text after the space stays in the input buffer.

Q5. Which function was removed from the C standard in C11?

  1. fgets
  2. puts
  3. gets
  4. scanf
Show answer

C, gets. gets cannot be told the size of the destination, so any long input overflows the buffer.

Q6. For int a[2][3][4], what is the offset of a[1][2][3]?

  1. 12
  2. 20
  3. 23
  4. 24
Show answer

C, 23. 1*(3*4) + 2*4 + 3 = 12 + 8 + 3 = 23, the last element.

Q7. What is the one requirement for binary search?

  1. The array length is even
  2. The array is sorted
  3. The array holds unique values
  4. The array is global
Show answer

B, sorted. Without order the decision to discard half of the data is wrong.

Q8. About how many comparisons does binary search need at most for 1000 sorted elements?

  1. 10
  2. 100
  3. 500
  4. 1000
Show answer

A, 10. floor(log2 1000) + 1 = 10, because 2 to the power 10 is 1024.

Q9. Which expression for the middle index cannot overflow?

  1. (low + high) / 2
  2. low + (high – low) / 2
  3. (low * high) / 2
  4. high / 2
Show answer

B. high – low never exceeds the array length, so adding half of it to low is safe.

Q10. What does int a[3][4] = {0}; do?

  1. Sets only a[0][0] to zero
  2. Sets every element to zero
  3. Is a compile error
  4. Leaves other elements random
Show answer

B, sets every element to zero. Elements without an explicit initialiser are zero once at least one initialiser is present.

Q11. What does fgets keep that the old gets removed?

  1. Leading spaces
  2. The newline
  3. The terminator
  4. Nothing
Show answer

B, the newline. fgets stores the newline if the line fits. Strip it with strcspn.

Q12. What is 255 in hexadecimal?

  1. EF
  2. FE
  3. FF
  4. 100
Show answer

C, FF. 15 x 16 + 15 = 255.

Q13. char d[4] = {'P','u','n','e'}; then strlen(d). What happens?

  1. Returns 4
  2. Returns 3
  3. Undefined behaviour
  4. Compile error
Show answer

C, undefined behaviour. There is no terminator, so strlen reads past the end of the array.

C Arrays Interview Questions: Predict the Output

Each snippet is a complete program fragment. Write down what it prints, then check the answer. All answers below were confirmed by compiling and running the code.

Question A: partial initialisation

int a[2][3] = { {1, 2, 3}, {4, 5} };
printf("%d %d %d\n", a[0][2], a[1][1], a[1][2]);
Show answer

Output: 3 5 0. The missing element a[1][2] is zero.

Question B: sizeof versus strlen

char s[10] = "India";
printf("%zu %zu\n", sizeof s, strlen(s));
Show answer

Output: 10 5. The array reserves 10 bytes; the text is 5 characters long.

Question C: cutting a string short

char t[] = "embedded";
t[5] = '\0';
printf("%s %zu %zu\n", t, strlen(t), sizeof t);
Show answer

Output: embed 5 9. The new terminator at index 5 ends the string, but the array still occupies 9 bytes.

Question D: 3D array layout

int c[2][2][2] = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };
printf("%d %d\n", c[0][1][1], c[1][0][1]);
Show answer

Output: 4 6. The offsets are 0*4 + 1*2 + 1 = 3 and 1*4 + 0*2 + 1 = 5, so the values at those positions in 1 to 8 are 4 and 6.

Question E: array parameter size

void f(int m[][4]) { printf("%zu\n", sizeof m); }
/* in main: int g[3][4]; f(g); */
Show answer

Output: 8 on a 64-bit machine. Inside the function m is a pointer to a row, not an array. On a 32-bit microcontroller it prints 4.

C Arrays Interview Questions with Model Answers

Click a question to see the answer. Read the answer aloud, because interviews are spoken.

1. Why must a 2D array parameter include the column count?

To find m[i][j] the compiler computes the start of row i, which means skipping i whole rows. The length of a row is the column count times the element size, so it must be known. The number of rows is not needed for the address, which is why only the leftmost size may be omitted.

2. Is an array copied when you pass it to a function?

No. The array name converts to a pointer to its first element, and only the pointer is passed. The function works on the caller’s memory, so changes are visible after the call. That is also why sizeof inside the function gives the pointer size.

3. What is the difference between sizeof and strlen?

sizeof is a compile-time operator that returns the bytes occupied by the array, including unused room and the terminator. strlen is a run-time function that counts characters before the first zero byte. For char b[8] = "Pune" they return 8 and 4.

4. What is the null terminator and why does C need it?

It is a byte with value 0 that marks the end of a string. C arrays carry no length, so every string function reads until it finds that byte. A string of n visible characters therefore needs n + 1 bytes.

5. Explain the difference between char a[] = "abc" and char *p = "abc".

The array holds its own writable copy of the bytes and has size 4. The pointer points to a string literal, which normally lives in read-only memory, so writing through it is undefined behaviour. The pointer’s sizeof is the size of an address, not of the text. Declare it const char *p to let the compiler enforce read-only use.

6. Why is gets dangerous and what replaces it?

It receives no size, so any input longer than the buffer overwrites adjacent memory. It was removed in C11. Use fgets(buf, sizeof buf, stdin), then strip the newline with buf[strcspn(buf, "\n")] = '\0'.

7. How does a 3D array sit in memory, and why does loop order matter?

In row-major order: the last subscript varies fastest. The offset of a[i][j][k] in a[P][R][C] is i*R*C + j*C + k. Looping with the last subscript innermost reads memory sequentially, which suits caches, prefetchers and DMA.

8. What is the difference between int a[3][4] and int *a[3]?

The first is one contiguous block of 12 ints. The second is an array of three pointers; each pointer may point to a row somewhere else, and rows may have different lengths. They are indexed the same way with a[i][j] but they are not interchangeable as function arguments.

9. How would you convert a number to a binary string?

Divide by 2 repeatedly, store each remainder as a character, and reverse the digits. Use a do-while loop so that zero produces the digit 0, use an unsigned type, and check that the output buffer has room for the digits and the terminator. A 32-bit value needs 33 bytes.

10. Describe binary search, its requirement and its complexity.

Keep a window with low and high, compare the key with the middle element, and discard the half that cannot contain it. The data must be sorted. The worst case is about log2 n steps, so 20 for a million items. Compute the middle as low + (high - low) / 2 to avoid overflow, and stop when low > high.

11. How do you read a line safely in C?

Use fgets with the buffer size, check for a NULL return, and remove the trailing newline. For single words use scanf with a width one smaller than the array size, such as %15s for a 16-byte array.

12. How would you search a calibration table stored in flash?

Keep the table sorted and const so it stays in flash. Use binary search to find the two entries that surround the measured value, then interpolate between them. This costs a handful of comparisons and no RAM for the table.

13. What can go wrong when you index an array with a value from outside the device?

C does not check bounds. A bad index from a UART command or network packet reads or writes outside the array, which can corrupt variables or a return address. Validate the index against the real size before using it.

C Arrays Interview Questions: Coding Challenges

Solve each one on your own machine, compile with -Wall -Wextra, and test with unusual input such as an empty string.

ChallengeTaskHint
1Reverse a string in place without a second array.Swap characters from both ends until the indices meet.
2Count vowels and consonants in a line read with fgets.Use a switch or a small lookup string and ignore non-letters.
3Write void transpose(int rows, int cols, int in[rows][cols], int out[cols][rows]).out[c][r] = in[r][c]; test with a 2 x 3 matrix.
4Find the first occurrence of a key in a sorted array with duplicates.After a hit, keep searching the left half: set high = mid – 1 and remember mid.

Self-check habit

After each challenge, write down the memory picture: which bytes exist, where the terminator is, and which index is the last valid one. Interviewers often ask for exactly that picture.

Interview Tips for Embedded C Candidates

  • Say the memory layout out loud before you write code. It shows that you think like the compiler.
  • Mention bounds and the terminator whenever you handle strings.
  • Compile with warnings and treat them as hints. Many interview bugs are exactly the warnings gcc prints.
  • If you do not know, reason from first principles. Interviewers value clear thinking over a memorised sentence.
  • Prepare one small project story that uses arrays for real, for example a sensor ring buffer or a lookup table.

Summary and Key Takeaways

  • Arrays are contiguous memory; every subscript is an offset calculation.
  • Functions receive a pointer, so sizes must travel separately.
  • Strings need a terminator and bounded input functions.
  • Binary search is fast but only on sorted data.
  • C arrays interview questions reward people who can picture memory. Predicting outputs by hand is the fastest way to find gaps.

Conclusion

Congratulations on finishing the arrays chapter of the free embedded C course. If you can answer these C arrays interview questions without looking, you are ready for most C array questions in an embedded interview. The natural next step is pointers, because every behaviour you met here, from array decay to string functions, is explained by how pointers work. Keep practising, keep compiling with warnings on, and keep the memory picture in your head.

Frequently Asked Questions

Which C arrays interview questions do embedded interviews ask most?

Expect questions on array decay when passed to functions, sizeof versus strlen, the null terminator, 2D array parameters, row-major layout, off-by-one and out-of-bounds errors, and a coding task such as reversing a string or searching a sorted array.

How should I prepare for C coding rounds?

Write each program by hand, compile it with warnings enabled, and predict the output before you run it. The programs in this free C programming course are written for exactly that habit.

Are string questions common in embedded interviews?

Yes, because strings expose pointers, memory layout and buffer safety in a few lines. Be ready to explain the difference between a char array and a pointer to a string literal.

Should I memorise the answers?

No. Interviewers follow up with a variation. Understand the memory picture behind each answer and you can answer any follow-up.

What comes after the arrays chapter in this course?

The free embedded C course continues with pointers, which explain why arrays behave the way they do in function calls.

Ready for Pointers?

Continue with the free C programming course on EmbeddedPathashala. Every chapter is free, practical and aimed at embedded C programming.

<div

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 *