Quiz-Pass by Value Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Course Online

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

MODULE 8 · LESSON 8.2 · PRACTICE

Pass by Value Interview Questions for Embedded C: Quiz and Code Challenges

57 questions: multiple choice, write-the-prototype blanks, a parameter-style table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for pass by value interview questions.

57
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0
Correct
0
Wrong
0
Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.

Section 1: Multiple Choice Questions — Pass by Value and by Pointer

These pass by value interview questions appear in nearly every embedded C screening round. Click an answer; explanations always appear.

1. How does C pass arguments to a function?

2. Which call lets a function change the caller’s int x?

3. What does a swap(int a, int b) that swaps its parameters do to the caller?

4. An array name used as an argument is passed as:

5. What does sizeof(arr) give inside a function whose parameter is int arr[10]?

6. Which parameter type promises a function will not modify the buffer?

7. What does int *const p mean?

8. Why pass a large struct by pointer?

9. How do you modify a caller’s pointer inside a function?

10. What does the XOR swap *a ^= *b; *b ^= *a; *a ^= *b; do if a and b point to the same variable?

11. A function writes a result through an int *out parameter. What should it also do?

12. Which parameter is correct for a function that fills a caller-owned buffer?

13. What is the effect of the C99 keyword restrict on a pointer parameter?

14. Which type should a function use to read a hardware register that changes by itself?

15. Passing NULL to a function that dereferences the pointer without a check leads to:

Section 2: Write or Evaluate (Fill in the Blanks)

Type the declaration or call for the first three items and a short value for the rest.

1. Write the prototype of a swap function taking two int pointers (parameter names a and b, returning nothing):

2. Write a prototype for copy that reads a const char pointer src and writes a char pointer dst, returning void:

3. Write the call that passes the address of count to inc:

4. void f(int x) { x = 99; }. After int a = 1; f(a); the value of a is

5. void g(int *p) { *p = 99; }. After int a = 1; g(&a); the value of a is

6. On a 32-bit Cortex-M4, sizeof(int *) is bytes

7. To let a function change a caller’s char *s, the parameter type is char s (write the stars)

8. Inside void h(int a[8]) the type of a is int (write the stars)

Section 3: Which Parameter Style Fits?

Choose the best parameter style for each need, as asked in pass by value interview questions about API design.

NeedYour answerResult
Read-only access to caller dataconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Return a second resultconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Change the caller’s pointerconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Avoid copying a big structconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Fill a caller buffer safelyconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Read a changing hardware registerconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Promise no overlap between buffersconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const
Keep the pointer fixedconst T *T *out parameterT **pointer to structpointer plus sizevolatile T *restrictT *const

Section 4: True or False

Statements typical of pass by value interview questions.

1. C has true pass by reference.

2. A function can modify the caller’s variable if it receives its address.

3. Assigning to a pointer parameter changes the caller’s pointer.

4. sizeof on an array parameter returns the array size.

5. Passing a struct by value copies all its members.

6. A const int * parameter may be passed a pointer to a non-const int.

7. Passing const int * to an int * parameter compiles without a warning.

8. Two pointer parameters may refer to the same object.

Section 5: Code Snippet Challenges

Trace each program on paper first; the real output follows. These challenges are the code-reading half of pass by value interview questions.

Challenge 1 — Output?: Value versus pointer
#include <stdio.h> static void bump(int x) { x++; } static void bump_p(int *p) { (*p)++; } int main(void) { int a = 5; bump(a); bump_p(&a); bump_p(&a); printf(“%d\n”, a); return 0; }

What is printed?

Output:
7
The first call changes only a copy; the two pointer calls add 2 to a: 5 + 2 = 7.
Challenge 2 — Output?: Reassigning a pointer parameter
#include <stdio.h> static void set(int *p) { int v = 7; p = &v; } int main(void) { int a = 1; set(&a); printf(“%d\n”, a); return 0; }

What is printed?

Output:
1
The function only re-points its local copy of the pointer (to a local that dies anyway); a stays 1.
Challenge 3 — Output?: sizeof on an array parameter
#include <stdio.h> static void show(int arr[4]) { printf(“%d\n”, (int)(sizeof(arr) == sizeof(int *))); } int main(void) { int data[4] = {1, 2, 3, 4}; printf(“%d\n”, (int)sizeof(data)); show(data); return 0; }

What is printed?

Output:
16
1
In main sizeof gives 16 (4 ints of 4 bytes). Inside show the parameter is a pointer, so the comparison is true (1).
Challenge 4 — Output?: Filling a caller array
#include <stdio.h> static void fill(int *a, int n) { for (int i = 0; i < n; i++) a[i] = i * i; } int main(void) { int sq[5]; fill(sq, 5); printf(“%d %d %d\n”, sq[0], sq[2], sq[4]); return 0; }

What is printed?

Output:
0 4 16
The function writes the squares 0, 1, 4, 9, 16 into the caller’s array through the pointer.
Challenge 5 — Find the bug: Writing through a pointer to const
#include <stdio.h> static void zero(const int *p) { *p = 0; } int main(void) { int a = 3; zero(&a); printf(“%d\n”, a); return 0; }

What does GCC report?

Compiler result:constwrite.c: In function ‘zero’: constwrite.c:4:8: error: assignment of read-only location ‘*p’ 4 | *p = 0; | ^The parameter promises not to modify the data; the compiler rejects the assignment. Remove const only if writing is really intended.
Challenge 6 — Output?: Pointer to pointer
#include <stdio.h> static void next(const char **cur) { (*cur)++; } int main(void) { const char *s = “ABC”; next(&s); next(&s); printf(“%c\n”, *s); return 0; }

What is printed?

Output:
C
The function advances the caller’s pointer twice, from A to C.

Section 6: Pass by Value Interview Questions for Embedded C (with Detailed Answers)

Pass by value interview questions test pointer parameters, const correctness, aliasing and safe buffer APIs. Click a question for a model answer.

Q1. Does C support pass by reference?Basic+

No. C passes every argument by value: the callee gets a copy. Passing an address gives the effect of pass by reference, because the callee can write through the copied address, but the pointer itself is still a copy. C++ has real references.

Q2. Why does a swap function with int parameters not work?Basic+
swap_iq.c
#include <stdio.h> static void bad(int a, int b) { int t = a; a = b; b = t; } static void good(int *a, int *b) { int t = *a; *a = *b; *b = t; } int main(void) { int x = 1, y = 2; bad(x, y); printf(“%d %d\n”, x, y); good(&x, &y); printf(“%d %d\n”, x, y); return 0; }
Output (verified by compiling and running)
1 2 2 1

The first version swaps copies. The second swaps the originals.

Q3. How is an array passed to a function?Basic+

The array name decays to a pointer to its first element, so no elements are copied and the function can modify the caller’s array. The parameter forms int a[], int a[8] and int *a are identical, and sizeof(a) inside the function is the pointer size. Pass the element count as a separate parameter.

Q4. What is the difference between const int *p, int *const p and const int *const p?Intermediate+
DeclarationData modifiable?Pointer reassignable?
const int *pNoYes
int *const pYesNo
const int *const pNoNo

Read the declaration right to left: p is a pointer to const int. For API design, the first form is the one you use most for read-only inputs.

Q5. How do you return more than one result with pointers?Intermediate+

Pass output pointers and return a status code: int parse(const char *s, int *value) returns 0 on success and writes the value only then. Callers check the status before using the output. Document which outputs may be NULL, and never leave an output uninitialised on the error path without saying so.

Q6. Why should you pass the buffer size with a pointer?Intermediate+

A pointer carries no length. Without a size parameter the function cannot prevent a write past the end of the caller’s array, which on a microcontroller silently corrupts neighbouring variables. Passing (buf, size) lets the function truncate or return an error; this is why safer APIs such as snprintf take a size.

Q7. How can you modify a pointer in the caller?Intermediate+
pp_iq.c
#include <stdio.h> static void skip_space(const char **cur) { while (**cur == ‘ ‘) (*cur)++; } int main(void) { const char *s = ” go”; skip_space(&s); printf(“[%s]\n”, s); return 0; }
Output (verified by compiling and running)
[go]

Pass the address of the pointer. The parameter is a T **; the function updates *cur. Tokenisers and parsers use this pattern.

Q8. When would you pass a struct by value instead of by pointer?Intermediate+

When it is small (up to about two registers), when you want an independent copy that the callee may change freely, or when the immutable result is clearer in code. For anything larger, or for a struct that is updated in place such as a driver handle, use a pointer, usually const for read-only access. On Cortex-M4 a struct of up to four words is still copied into r0–r3 under the AAPCS, but larger structs go on the stack or via a hidden pointer.

Q9. What problem does pointer aliasing cause and how does restrict help?Advanced+

If two pointer parameters may refer to overlapping memory, the compiler must assume a store through one could change what the other reads, so it reloads values and cannot vectorise or reorder. Declaring them restrict promises there is no overlap, so it can keep values in registers. If you break the promise (for example, passing overlapping buffers to memcpy-like code) the behavior is undefined; use memmove for overlapping data.

Q10. Why can *a ^= *b; *b ^= *a; *a ^= *b; fail?Advanced+

When a and b point to the same variable, the first step sets it to x ^ x = 0, and every later step keeps it at 0, so the value is lost. A temporary-based swap works for aliased pointers. The XOR swap saves one variable but is rarely faster on modern cores and is a common trick question.

Q11. How should a hardware register be passed to a helper function?Advanced+

Use a pointer to a volatile object, for example void set_bits(volatile uint32_t *reg, uint32_t mask). Without volatile, the compiler may cache the register value, merge accesses or remove them. Keep the helper static inline so the access compiles to the same load-modify-store sequence as a direct write, and remember that read-modify-write is not atomic against interrupts; use BSRR where the hardware provides it.

Q12. What checks should a function perform on pointer parameters?Advanced+

At public API boundaries: NULL checks for required pointers, size checks for buffers, and alignment checks where the hardware demands them (a 32-bit access at an odd address faults on some cores). Inside private helpers the contract can be asserted instead. Decide on a policy, document it in the header, and be consistent: crashing early in development with assert and returning an error code in production are both defensible.

Study Plan: Using Your Score on These Pass by Value Interview Questions

ScoreMeaningNext step
90–100%ExcellentContinue to Lesson 8.3: scope, linkage and recursion.
70–89%GoodRedo Examples 1, 4 and 7 of the examples page.
Below 70%ReviseRe-read the theory sections on output parameters and const, then retake the quiz.
Habit to build: Mark every input pointer const and every register pointer volatile; the compiler then enforces your intent.
💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →