Skip to content
Tech Interview Prep
Technical interview guide

Embedded C/C++ & Memory Constraints

Why systems programming on bare metal with kilobytes of RAM surfaces correctness issues that rarely bite on a general-purpose OS.

Read
55 min
Practice MCQs
25
Interview Q&A
25
Edition
v3
Editorial status
Reviewed

Scope: GCC current C/C++ implementation and optimization guidance; CMSIS 6 startup/linker/compiler control; Arm ABI and SEI CERT guidance reviewed 2026-09-04.

Curated: · Written: · Reviewed:

25 questions

MCQ-3A firmware team ships an image for a Cortex-M4 with 256 KB of flash and 64 KB of SRAM. The linker map shows `.data` at 12 KB, `.bss` at 20 KB, and `.text`/`.rodata` at 90 KB. After a release build, the flash usage report reads 122 KB and the SRAM usage report reads 32 KB. A reviewer claims the SRAM figure is wrong because `.data` is initialized data and therefore lives in flash, not RAM. Which statement correctly describes what happens to the 12 KB of `.data` at startup and why the SRAM report is right?
MCQ-5Your team ships firmware for an STM32F411 (512 KB flash, 128 KB RAM) built with arm-none-eabi-gcc 13.2. `arm-none-eabi-size -A` on the release ELF reports: ``` section size addr .text 0x1F000 0x08000000 .rodata 0x0B400 0x0801F000 .data 0x00400 0x20000000 .bss 0x03000 0x20000400 ``` The linker script places .data's load address immediately after .rodata, and the sections are contiguous with no alignment padding between them. How many bytes of flash does this image occupy?
MCQ-4A Cortex-M application declares `static uint32_t seq;` at file scope and increments it once at the end of main. Its startup code clears .bss before calling main. A developer adds a soft-restart path that calls main() directly, without going through the reset vector. What value does seq hold when the second run of main returns?
MCQ-1A firmware image is built with `arm-none-eabi-gcc -ffreestanding -nostdlib -O2` and linked with a custom linker script; no C library is linked. The source never names `memcpy`, but the link fails with `undefined reference to 'memcpy'` from the object file holding a function that assigns a 96-byte struct by value. What is the cause?
MCQ-23On a 32-bit Arm target complying with the Arm ABI, consider a struct declared with members `uint8_t a; uint32_t b; uint16_t c; uint8_t d;`. How does member reordering reduce the `sizeof` footprint without compiler-specific packing attributes?
MCQ-25An 8-bit memory-mapped control register at address `0x40001000` is declared as `volatile uint8_t * const reg = (volatile uint8_t *)0x40001000;`. An application must clear bit 3 (mask `1U << 3`) and assert bit 5 (mask `1U << 5`) while leaving all other bits intact. Which read-modify-write expression correctly executes this atomic bitwise update in C?
MCQ-2A driver polls a memory-mapped status register through a pointer declared `volatile uint32_t * const reg`. The loop `while ((*reg & READY) == 0) {}` works at -O2, issuing one load per iteration. A teammate refactors the poll into a helper, `static uint32_t poll(uint32_t *p) { return *p; }`, and calls it as `poll((uint32_t *)reg)`. At -O2 the loop now hangs. Which statement explains the hang?
MCQ-18A packet parser receives a frame in a `uint8_t` buffer whose base address is 0x2000. The header is 3 bytes, then a 4-byte sequence number. The parser does `uint32_t seq = *(uint32_t *)(buf + 3);` and runs on a Cortex-M0 (ARMv6-M), where unaligned word loads are not supported. What does the CPU do when it executes that load?
MCQ-15A firmware build writes a record to flash with `fwrite(&rec, sizeof rec, 1, f)`, where `rec` is: ```c struct rec { uint8_t tag; uint32_t seq; uint16_t len; }; ``` The writer build uses the target's default alignment, so `seq` sits at offset 4 and `sizeof rec` is 12. It stores tag = 0x11, seq = 0x22334455, len = 0x6677. A second build declares the same fields in a packed struct (`__attribute__((packed))`, no padding, `sizeof rec` is 7) and reads the same bytes back from the start of the record with `fread(&rec, sizeof rec, 1, f)`. Both builds are little-endian. What does the second build's `rec` hold?
MCQ-16A team defines a wire header for a network protocol as a C struct using bit fields: ```c struct hdr { unsigned version : 3; unsigned flags : 5; unsigned length : 16; }; ``` They compile with GCC on x86-64 and with MSVC on Windows, then send the struct's bytes directly over the wire. The receiver, built with the other compiler, parses the header and reads `version` as 0 and `length` as a value that does not match what was sent, even though both sides agree on the field widths and the struct is 4 bytes on both. What is the most likely cause?
MCQ-17A sensor driver keeps raw readings in a `uint32_t` ring buffer. A helper reinterprets each slot as a float with `float f = *(float *)&buf[i];`. Debug builds (`-O0`) and the unit tests return the right values, but the `-O2` release build returns stale values for slots that were written earlier in the same function. The buffer's declared type cannot change, and the code must stay portable across GCC and Clang. What is the correct diagnosis and fix?
MCQ-22A 32-bit MCU runs a 1 kHz timer ISR that does `ticks++` on a `volatile uint64_t ticks`. The main loop reads `ticks` to timestamp log entries. About one read in 10^5 returns 0x00000001FFFFFFFF, a value the counter never held: it went from 0x00000000FFFFFFFF to 0x0000000100000000. What is the cause, and what fixes it?
MCQ-12Consider this C code compiled for a typical 32-bit platform where int is 32 bits: ```c uint8_t x = 0xFF; int y = x << 4; ``` What is the value of y?
MCQ-13On a platform where int is 32 bits, short is 16 bits, and signed integers use two's complement, this code is compiled and run: ```c unsigned short a = 0x8000; unsigned char b = 0x01; int c = 1; unsigned int d = 1u; int e = -1; int r1 = a << 16; int r2 = b << 24; int r3 = c << 30; unsigned int r4 = d << 31; int r5 = e >> 1; ``` Which of these five shifts has undefined behavior?
MCQ-11A C parser on a platform where `int` is 32 bits reads two `uint32_t` header fields and computes `size_t body = payload_len - header_len;`, then calls `memcpy(dst, src, body)`. A fuzz input sets `payload_len = 20` and `header_len = 60`. The subtraction is well-defined unsigned arithmetic. What is the correct assessment?
MCQ-10A validator has to answer whether x + 1 is still representable, so it leans on one comparison instead of a range check: ```c #include <limits.h> #include <stdio.h> int room(int x) { return x + 1 > x; } int main(void) { printf("%d\n", room(INT_MAX)); return 0; } ``` The target is x86-64 Linux with 32-bit int, and INT_MAX is 2147483647. Built with `gcc -O2`, what does the program print?
MCQ-14A telemetry bus carries 1,000 messages per second. The sender stamps each message with a 32-bit sequence number: ```c static uint32_t seq = 0; hdr.seq = seq++; ``` The receiver keeps the last accepted sequence number in a `uint32_t last_seq` and drops any message whose sequence is not greater (the first message is accepted by a separate startup path): ```c if (hdr.seq <= last_seq) { drop(); return; } last_seq = hdr.seq; ``` The bus runs correctly for about 50 days, then the receiver starts dropping every message. What is the cause?
MCQ-9A 32-bit service reads a record count from an untrusted header into a `uint32_t count`, then runs `struct rec *p = malloc(count * sizeof(struct rec));` followed by a loop that fills `count` records. On this platform `size_t` is 32 bits wide and `sizeof(struct rec)` is 24. A header arrives with `count = 178,956,972`. What happens when the code runs?
MCQ-24An MCU core lacks a hardware Memory Protection Unit (MPU) or MMU to raise an immediate fault on out-of-bounds pointer writes. Which mechanism detects stack overflow with the lowest latency before corrupting adjacent static data sections?
MCQ-6A team sets the main task stack to 4 KB in the linker script, runs the full bench suite with no overflow, and ships. Units then crash only when a sensor ISR fires during a deep JSON parse. Which statement explains why the 4 KB was never proven sufficient?
MCQ-7A Cortex-M4 firmware task with a 4 KB stack overflows inside a recursive JSON serializer. The fault handler runs on the faulting thread's stack, and a watchdog resets the device 200 ms after the fault. Which practice should the handler avoid?
MCQ-8A bare-metal Cortex-M7 runs a 1 kHz motor-control loop with a 200 µs worst-case compute budget and no MMU. The same task parses a 10 Hz telemetry packet inline; the protocol caps a packet at 512 bytes, and the parser mallocs the buffer and frees it after parsing. After six hours of soak testing the loop starts missing its 1 ms deadline, and the misses cluster immediately after telemetry packets. A heap dump taken at that moment shows 40 free blocks, the largest 240 bytes, totalling 9 KB free. What is causing the misses?
MCQ-19A C++17 firmware module has this function: ```cpp #include <cstring> const char* mode_name(int mode) { char buf[16]; switch (mode) { case 0: std::strcpy(buf, "idle"); break; case 1: std::strcpy(buf, "run"); break; default: std::strcpy(buf, "fault"); break; } return buf; } ``` It is called from two interrupt handlers. The build uses `-O2`; AddressSanitizer reports `stack-use-after-return` at the callers. The project forbids heap allocation after initialization. Which change fixes the lifetime bug while preserving reentrancy and the no-heap rule?
MCQ-21To keep the firmware image under 512 KB, a team builds `libsensor.a` with `-fno-rtti -fno-exceptions` and links it into an application built with default flags. `Sensor` is polymorphic; its first non-inline virtual function, the destructor, is defined out-of-line in `sensor.cpp` inside the library. The application calls `dynamic_cast<PressureSensor*>(sensorPtr)`. The link fails with `undefined reference to 'typeinfo for Sensor'`. What is the cause?
MCQ-20A C++17 service defines `Registry g_registry;` in registry.cpp and `Plugin g_plugin;` in plugin.cpp; Plugin's constructor calls `g_registry.registerPlugin(...)`. It ran for months, then a build change moved plugin.o ahead of registry.o on the link line. Now the process segfaults before main prints its first line, and the debugger shows g_registry's vtable pointer is null inside Plugin's constructor. What is the correct explanation and fix?