Academic Resource • BCA Semester 1 Examination Guide
Home / C Programming / Exam & Viva Guide

BCA C Programming Exam Guide: Top 10-Mark Questions, Memory Trace & Lab Viva

📖 University Exam Blueprint ⚡ 25 Lab Viva Q&As 🧠 Memory Trace Tables 🎓 Curated by Mr. Vivek Saxena

1. University Exam Answer Strategy: The 5-Mark & 10-Mark Formula

In BCA university examinations, examiners grade theory answers based on strict step-marking schemes. Even if your final code is correct, submitting bare code without theoretical grounding results in significant mark deductions.

📋 The High-Scoring 5-Mark Blueprint
  1. Clear Definition (1 Mark): Write a formal 2-line definition with key terminology (e.g., contiguous allocation, stack frame, address-of operator).
  2. Syntax & Rules (1 Mark): State the exact compiler syntax and parameter breakdown.
  3. Mini Flowchart or Trace Table (1 Mark): Visual diagram showing execution path or memory cell values.
  4. Minimal Working C Code (1.5 Marks): 10–15 lines of clean, well-commented C code with #include <stdio.h> and return 0;.
  5. Edge Cases or Time Complexity (0.5 Mark): Mention time complexity or base termination condition.

2. Solved Model Question: Recursion vs. Iteration (10 Marks)

University Problem: "Explain recursion in C. Compare recursion with iteration in terms of stack memory overhead, performance, and termination conditions. Write a program to compute the factorial of a number using recursion and illustrate its stack frame activation trace."

Core Theoretical Comparison

Parameter Recursion Iteration (Loop)
Definition A function calling itself repeatedly until a base condition is met. Repeated execution of a code block controlled by a loop condition.
Memory Overhead High: Each recursive call creates a new Stack Activation Record storing local variables and return address. Low: Constant memory (O(1)) as CPU reuses the same local registers across loop cycles.
Execution Speed Slower due to function call overhead, register saving, and stack push/pop operations. Faster as CPU executes loop jump instructions directly without stack reallocation.
Risk Factor Missing base condition causes Stack Overflow runtime crash. Missing update statement causes an Infinite Loop (process hangs, but stack is safe).

Complete Working Implementation with Base Guard

#include <stdio.h>

// Recursive function to compute factorial
long long factorial(int n) {
    // BASE CASE: Halts recursive descent
    if (n <= 1) {
        return 1;
    }
    // RECURSIVE STEP: n * (n - 1)!
    return n * factorial(n - 1);
}

int main(void) {
    int num = 4;
    printf("Factorial of %d is %lld\n", num, factorial(num));
    return 0;
}

Dry-Run Stack Activation Record Trace (Computing factorial(4))

Call Level Input (n) Stack Status Expression Evaluated Returned Value
Level 1 n = 4 Frame 1 pushed 4 * factorial(3) (Suspended) Waiting...
Level 2 n = 3 Frame 2 pushed 3 * factorial(2) (Suspended) Waiting...
Level 3 n = 2 Frame 3 pushed 2 * factorial(1) (Suspended) Waiting...
Level 4 (Base) n = 1 Frame 4 pushed Base condition reached (n <= 1) Returns 1 (Frame 4 popped)
Unwinding Level 3 n = 2 Frame 3 resumes 2 * 1 Returns 2 (Frame 3 popped)
Unwinding Level 2 n = 3 Frame 2 resumes 3 * 2 Returns 6 (Frame 2 popped)
Unwinding Level 1 n = 4 Frame 1 resumes 4 * 6 Returns 24 to main()

3. Solved Model Question: Parameter Passing (10 Marks)

University Problem: "Differentiate between Call by Value and Call by Reference in C with memory diagrams. Why cannot values of variables in main() be swapped using standard call by value? Explain with code."

💡 Why Call by Value Fails to Swap in main()

In C, function arguments are passed by value by default. When variables a and b are passed to a function, the compiler allocates separate memory locations in the callee's stack frame and copies only the contents. Any mutation modifies local copies and vanishes when the function returns.

#include <stdio.h>

// CALL BY REFERENCE: Pointers hold memory addresses of actual arguments
void swapByRef(int *x, int *y) {
    int temp = *x;  // Dereference: read value at address x
    *x = *y;        // Assign value at address y to address x
    *y = temp;      // Assign temp to address y
}

int main(void) {
    int a = 10, b = 20;
    printf("Before Swap: a = %d, b = %d\n", a, b);
    
    // Pass memory addresses using address-of operator (&)
    swapByRef(&a, &b);
    
    printf("After Swap:  a = %d, b = %d\n", a, b);
    return 0;
}

Memory Address & Dereference Dry-Run Trace

Variable Scope Memory Address Initial Value After Dereference (*x = *y)
a main() 0x7ffee100 10 20 (Modified via pointer *x)
b main() 0x7ffee104 20 10 (Modified via pointer *y)
x swapByRef() 0x7ffee080 0x7ffee100 Unchanged (still holds address of a)
y swapByRef() 0x7ffee088 0x7ffee104 Unchanged (still holds address of b)

4. Structures vs. Unions: Byte-Level Memory Layout (10 Marks)

University Problem: "What is a structure? How does it differ from a union in terms of keyword, memory allocation, and member accessibility? Calculate and explain the output of sizeof() for both."

#include <stdio.h>

// Structure: Each member receives its own dedicated memory slot
struct StudentStruct {
    int id;        // 4 bytes
    char grade;    // 1 byte (+ 3 padding bytes for alignment)
    float marks;   // 4 bytes
}; // Total Size: 12 bytes

// Union: All members share the SINGLE largest memory slot
union StudentUnion {
    int id;        // 4 bytes
    char grade;    // 1 byte
    float marks;   // 4 bytes
}; // Total Size: 4 bytes (Largest member: float/int)

int main(void) {
    printf("sizeof(struct StudentStruct) = %zu bytes\n", sizeof(struct StudentStruct));
    printf("sizeof(union StudentUnion)   = %zu bytes\n", sizeof(union StudentUnion));
    return 0;
}
⚠️ Essential Exam Insight: Union Overwriting

In a union, because all members share the same base address (offset 0), assigning a value to grade corrupts any previous value stored in marks or id. Only one member can hold valid data at any given instant.

5. Dynamic Memory Allocation: malloc() vs calloc() & Memory Leaks (10 Marks)

University Problem: "Explain the functions malloc(), calloc(), realloc(), and free() defined in <stdlib.h>. What is a memory leak and how is it prevented?"

Function Syntax Initialisation Return on Failure
malloc() void* malloc(size_t size); Contains garbage values NULL
calloc() void* calloc(size_t n, size_t size); Initialises all bytes to zero (0) NULL
realloc() void* realloc(void *ptr, size_t new_size); Preserves existing data; new area is uninitialised NULL (original block preserved)
free() void free(void *ptr); Releases allocated heap memory back to OS None (void)
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 5;
    // Step 1: Allocate memory on the heap
    int *arr = (int *)malloc(n * sizeof(int));
    
    // Step 2: MANDATORY EXAM CHECK - verify allocation succeeded
    if (arr == NULL) {
        fprintf(stderr, "Heap allocation failed!\n");
        return 1;
    }

    // Step 3: Use allocated memory
    for (int i = 0; i < n; i++) {
        arr[i] = (i + 1) * 10;
    }

    // Step 4: PREVENT MEMORY LEAK - release heap memory
    free(arr);
    
    // Step 5: PREVENT DANGLING POINTER - reset pointer to NULL
    arr = NULL;

    return 0;
}

6. Top 25 BCA Lab Viva Voce Questions & Examiner-Ready Answers

These questions represent the most frequently asked viva voce inquiries during external university practical examinations.

Q1. What is the difference between a Compiler and an Interpreter?

Answer: A compiler translates the entire source code into machine/object code at once (producing an executable file), whereas an interpreter translates and executes the code line-by-line. C is a compiled language.

Q2. What is the difference between #include <stdio.h> and #include "stdio.h"?

Answer: Angle brackets <> instruct the preprocessor to search for the header file only in standard system library directories. Quotation marks "" instruct it to search the current working directory first before falling back to system paths.

Q3. What is a Pointer, and what is its size in memory?

Answer: A pointer is a variable that stores the memory address of another variable. Its size depends on the machine architecture, not the data type: 4 bytes on a 32-bit system, and 8 bytes on a 64-bit system.

Q4. What is a Dangling Pointer and how do you prevent it?

Answer: A dangling pointer points to a memory location that has already been deallocated using free(). It is prevented by immediately setting the pointer to NULL after freeing: free(ptr); ptr = NULL;.

Q5. What is a NULL Pointer versus a Void Pointer?

Answer: A NULL pointer points to nothing (address 0) and represents an invalid or unallocated reference. A void pointer (void*) is a generic pointer that can hold the address of any data type, but must be typecast before dereferencing.

Q6. What causes a Segmentation Fault (SIGSEGV) in C?

Answer: A segmentation fault occurs when a program attempts to access a memory address it does not have permission to read or write (e.g., dereferencing a NULL pointer, accessing an array out of bounds, or stack overflow from infinite recursion).

Q7. What is the difference between i++ (Post-increment) and ++i (Pre-increment)?

Answer: Post-increment uses the current value in the surrounding expression first and then increments i by 1. Pre-increment increments i first and then yields the updated value to the expression.

Q8. What are the four Storage Classes in C?

Answer: auto (default for local variables, stack memory), register (stored in CPU register for fast access), static (preserves value across function calls, data segment), and extern (global visibility across multiple files).

Q9. Why does scanf("%d", num); crash without an ampersand (&)?

Answer: scanf expects a memory address where it can store user input. Without &, the numerical value stored inside num is treated as an address, causing an unauthorized memory write and immediate segmentation fault.

Q10. What is the difference between while and do-while loops?

Answer: A while loop is an entry-controlled loop (condition is tested before executing the body; may execute 0 times). A do-while loop is an exit-controlled loop (body executes at least once before testing the condition).

Q11. What is the null character '\0' in strings and what is its ASCII value?

Answer: '\0' is the string terminator used by standard C library functions (like strlen, printf) to detect the end of a character sequence. Its ASCII decimal value is 0.

Q12. What is the difference between break and continue?

Answer: break terminates the nearest enclosing loop or switch block immediately. continue skips only the remaining statements of the current iteration and jumps to the next iteration loop evaluation.

Q13. What is an Array, and why is array indexing zero-based in C?

Answer: An array is a collection of homogeneous elements stored in contiguous memory locations. Indexing is zero-based because the index represents an offset from the base address: Address(arr[i]) = BaseAddress + (i * sizeof(element)).

Q14. What is the difference between malloc() and calloc()?

Answer: malloc() takes one argument (total bytes) and leaves allocated memory uninitialised (garbage). calloc() takes two arguments (count, size per element) and clears all allocated bytes to zero.

Q15. What is a Memory Leak and how does it affect long-running servers?

Answer: A memory leak occurs when dynamically allocated heap memory is no longer referenced but never deallocated via free(). Over time, the program consumes all available RAM and the OS kills the process.

7. 10 Common Compiler Traps & Bugs in BCA Practicals

External practical examiners regularly inspect student code for these classic structural and logical mistakes:

Trap 1: The Semicolon After Condition/Loop

if (x == 5);  // <-- ERROR: Semicolon terminates if immediately!
{
    printf("x is 5\n"); // Always executes regardless of x!
}

Trap 2: Assignment Operator (=) Instead of Equality (==)

if (status = 1) { // <-- ERROR: Assigns 1 to status; expression always evaluates to TRUE!
    // Executes unconditionally
}

Trap 3: Off-By-One Array Index Bounds

int arr[5];
for (int i = 0; i <= 5; i++) { // <-- ERROR: Index 5 is outside bounds (0 to 4)!
    arr[i] = 0; // Overwrites adjacent stack memory!
}

Trap 4: Uninitialized Pointer Dereferencing (Wild Pointer)

int *ptr; // Points to random garbage address!
*ptr = 100; // <-- CRASH: Writing to random operating system memory!

Trap 5: Integer Division Truncation

int a = 5, b = 2;
float result = a / b; // <-- Result is 2.000000, NOT 2.5! Must cast: (float)a / b;