C Multi-Topic Interview Questions and Answers: Model Answers with Worked Traces

Learn how to answer mixed C interview prompts by classifying behaviour first, then tracing arrays, pointers, static state, strings and dynamic memory safely.

KnowledgeGate Team

Exam prep & CS education

Updated 3 Dec 20266 min read
C Multi-Topic Interview Questions and Answers: Model Answers with Worked Traces

A multi-topic C prompt may begin with an array, turn into a pointer and parameter question, then finish with storage duration, ownership or undefined behaviour. Memorised definitions fail because every answer depends on the same objects and values.

The same prompt can move from an array trace to ownership and undefined behaviour. A reliable response names the rule, traces the values, and says when no numeric output is defined. Employers may mix definitions, output prediction, debugging and design judgement.

What makes a C interview question multi-topic?

How should I start when one snippet tests several C concepts? Check its syntax and constraints. If valid, classify the behaviour as defined, unspecified, implementation-defined or undefined. Then trace types, lifetimes, side effects and output. Never calculate a number for code with no defined numeric result.

Use four layers: (1) declarations and types, (2) conversions and parameter adjustment, (3) storage duration, bounds and ownership, and (4) control flow and value changes. Passing v + 1 combines array-to-pointer conversion, pointer arithmetic, a copied pointer argument and an explicit count.

Treat a multi-topic prompt as a chain of related decisions rather than a separate C chapter. A strong answer gives a claim, the language reason and a trace or counterexample. “Compiler dependent” is not a blanket answer.

Model answers connecting arrays, pointers, functions and storage

Is an array a pointer? No. An array object converts to a pointer to its first element in most expressions. sizeof v and &v are non-decay cases. For int v[] = {3, 4, 5, 8};, sizeof v / sizeof v[0] is exactly 4 in its declaring scope, without assuming int size.

What does v + 1 point to? It points to v[1], initially 4; a[i] means *(a + i). A one-past pointer cannot be dereferenced.

Does C pass arrays by reference? No. int a[] adjusts to int *a, whose value C copies. The function can modify caller elements through it, but reassigning local a cannot reassign the array. sizeof a gives pointer size, not array length, so pass nseparately.

What does a block-scope static variable do? It has block scope and static storage duration, so it is initialised once and retains its value across calls. An ordinary local is created on each entry.

Worked trace: array decay, pointer arithmetic and persistent state

What does this print, and why does call 2 start differently?

c
#include <stddef.h>
#include <stdio.h>

int transform(int a[], size_t n) {
    static int bias = 1;
    int total = bias;

    for (size_t i = 0; i < n; ++i) {
        if (a[i] % 2 == 0) {
            total += a[i] / 2;
        } else {
            total += a[i] * 2;
        }
    }

    ++bias;
    return total;
}

int main(void) {
    int v[] = {3, 4, 5, 8};

    printf("%d ", transform(v, sizeof v / sizeof v[0]));
    v[1] = 10;
    printf("%d\n", transform(v + 1, 3));
    return 0;
}

Call 1 gets n = 4 from sizeof v / sizeof v[0] before array conversion. From {3, 4, 5, 8}, contributions 6, 2, 10, 4 give totals 1 -> 7 -> 9 -> 19 -> 23 with bias = 1. Then bias = 2; return 23.

After v becomes {3, 10, 5, 8}, v + 1 points at 10 and n = 3 selects {10, 5, 8}. Starting at persistent bias = 2, contributions 5, 10, 4 give 2 -> 7 -> 17 -> 21. Then bias = 3; return 21.

Output is 23 21. The pointer has no hidden length; transform only reads the selected elements.

Diagram traces two calls to transform: array values and contributions produce totals 23 and 21 after v[1] changes from 4 to 10.

Worked ownership answer: build and free "C!" safely

Who owns returned dynamic memory, and why are three bytes needed for two visible characters?

c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *tag(const char *s) {
    size_t n = strlen(s);
    char *p = malloc(n + 2);

    if (p == NULL) return NULL;
    memcpy(p, s, n);
    p[n] = '!';
    p[n + 1] = '\0';
    return p;
}

int main(void) {
    char *msg = tag("C");

    if (msg != NULL) {
        printf("%s %zu\n", msg, strlen(msg));
        free(msg);
    }
    return 0;
}

strlen("C") = 1, so malloc(1 + 2) requests three bytes. memcpy writes p[0] = 'C', then p[1] = '!' and p[2] = '\0'. Since strlen excludes the terminator, visible length is 2 and stdout is C! 2. Every initially uninitialised byte is assigned before reading.

On success, tag transfers ownership to the caller; on failure it returns NULL. The caller checks before dereferencing and frees msg once. sizeof msg gives pointer size, not allocation size or string length. See Dynamic Memory in C: malloc, calloc, realloc Bugs.

Flow diagram shows tag("C") allocating three bytes for C, !, and the null terminator, returning C! 2 or NULL on failure.

When there is no defined output

Sometimes the correct response classifies the code instead of predicting a value.

Category

Example

Correct conclusion

Constraint violation

int k = 2; switch (k) { case k: break; }

A case label needs an integer constant expression. Request a diagnostic, not output.

Undefined behaviour

After int i = 1;, call printf("%d %d\n", i++, i++);

The arguments modify i twice without sequencing. Neither 1 2 nor 2 1 is valid.

Unspecified behaviour

combine(left(), right())

C does not choose which independent argument is evaluated first. Multiple orders are allowed if side effects do not conflict on one scalar object.

Implementation-defined property

After char c = -1;, code depends on c < 0

Plain char may be signed or unsigned, and the implementation documents its choice.

State the category, rule and conclusion. Do not call these cases “garbage value”. When a result depends on type width or another implementation characteristic, state the assumption instead of silently choosing 32-bit int or 64-bit pointers.

Debugging realloc without losing the old allocation

What is wrong with numbers = realloc(numbers, 5 * sizeof *numbers);? Suppose numbers owns {4, 7, 9} and count = 3. Failure returns NULL while the old block remains allocated, so direct assignment loses its only pointer. Success may keep or move the block, after which the old pointer must not be used.

Use a temporary pointer:

c
int *tmp = realloc(numbers, 5 * sizeof *numbers);
if (tmp != NULL) {
    numbers = tmp;
    numbers[3] = 0;
    numbers[4] = 0;
    count = 5;
}

Success produces {4, 7, 9, 0, 0} and count = 5; realloc does not clear new bytes. On failure, numbers still points to {4, 7, 9} and count remains 3, so the program can continue or free it. For a general requested count, also prevent count-to-byte multiplication overflow.

How interviews probe mixed C concepts

Use three rungs, not a fixed employer pattern. Fundamentals: array decay, pass-by-value, static duration, termination and free. Trace: reproduce 1 -> 7 -> 9 -> 19 -> 23, then 2 -> 7 -> 17 -> 21, explaining why call 2 starts at v[1]. Depth: classify invalid or undefined code, preserve ownership on failure, and make interfaces explicit.

Use four spoken passes: (1) state the rule, (2) identify type, lifetime and owner, (3) trace defined operations, and (4) handle failure and portability before improvements. Include n = 4, then n = 3, not only the output.

Single-topic guides own the deep dives: C Language Concepts Interview Questions and Answers explains the concept-by-concept sequence; Relationship Between Arrays and Pointers Interview QuestionsThe integrated trace then adds ownership and behaviour classification.

Multi-topic C answers in 60 seconds, then the next step

The checksum: arrays are not pointers but often convert; array parameters adjust to pointers; arguments pass by value; counts carry length; static state persists; strings need \0; classify behaviour, ownership and failure before predicting output.

Reproduce 23 21; totals 1 -> 7 -> 9 -> 19 -> 23 and 2 -> 7 -> 17 -> 21; cells 'C', '!', '\0'; C! 2; and {4, 7, 9, 0, 0}. Use the C Language Course for structured practice, then Resume & Interview Preparation for the wider route.