Comprehensive Question Bank

Important Q&A Bank

Programming for Problem Solving (ESCS201) · Curated Top-Frequency Exam Questions

Module 1: C Fundamentals & Storage Classes Core Foundations

1. Explain the four storage classes in C (auto, static, extern, register) with their default initial values, scope, and lifetime. [ 10 Marks ]

Storage Class Storage Location Default Initial Value Scope (Visibility) Lifetime
auto RAM Call Stack Garbage Value (random uninitialized bytes) Local (within the enclosing function/block only) Destroyed automatically upon block exit.
static RAM Data Segment Zero (0) Local to the declaring function/block (or file if global) Persists across function calls for entire program runtime.
extern RAM Data Segment Zero (0) Global (accessible across all translation units/files) Entire program execution runtime.
register CPU Registers (if available) Garbage Value Local to block Destroyed upon block exit. Note: Cannot take address &reg.

2. Differentiate between type casting and type conversion in C with examples. [ 5 Marks ]

Type Conversion (Implicit Conversion): Automatically performed by the C compiler when operands of different data types are mixed in an expression. Smaller types are automatically promoted to larger types (e.g., int to float) to prevent data loss.
Type Casting (Explicit Conversion): Manually performed by the programmer using the cast operator (data_type)expr to forcibly convert an expression from one data type to another.

int x = 10; float y = 5.5; float res1 = x + y; // Implicit conversion: x is automatically promoted to 5.0f int res2 = (int)3.14159; // Explicit casting: truncates float to integer 3
Module 2: Arrays, Pointers & Dynamic Memory Memory Management

3. Explain dynamic memory allocation functions (malloc, calloc, realloc, free) in C with examples. [ 10 Marks ]

Dynamic memory allocation functions (defined in <stdlib.h>) allocate heap memory at runtime:

  • malloc(size_t size): Allocates a single contiguous block of memory of specified size in bytes. Contains uninitialized garbage values.
    int *ptr = (int*)malloc(10 * sizeof(int));
  • calloc(size_t num, size_t size): Allocates memory for an array of num elements of size bytes each, and automatically initializes all bytes to zero.
    int *arr = (int*)calloc(10, sizeof(int));
  • realloc(void *ptr, size_t new_size): Resizes an already allocated memory block without losing previously stored data (expanding or shrinking).
    ptr = (int*)realloc(ptr, 20 * sizeof(int));
  • free(void *ptr): Deallocates heap memory previously allocated by malloc/calloc/realloc, returning it to the operating system to prevent memory leaks.
    free(ptr); ptr = NULL;

4. What is array of pointers and pointer to an array? Explain with syntax. [ 5 Marks ]

1. Array of Pointers (int *arr[5];): This is an array containing 5 pointer elements. Every element arr[i] stores the memory address of an integer variable.
2. Pointer to an Array (int (*ptr)[5];): Notice the parentheses around *ptr. This declares a single pointer variable that points to an entire 1-dimensional array of 5 integers. When incremented (ptr++), its memory address jumps by \(5 \times \text{sizeof(int)} = 20\) bytes!

Module 3: Strings, Structures & File Handling Data & I/O

5. Write a C program to copy one text file to another using file handling functions. [ 8 Marks ]

#include <stdio.h> #include <stdlib.h> int main() { FILE *sourceFile, *destFile; char ch; // Open source file in read mode sourceFile = fopen("source.txt", "r"); if (sourceFile == NULL) { printf("Error: Cannot open source.txt for reading!\n"); return 1; } // Open destination file in write mode destFile = fopen("destination.txt", "w"); if (destFile == NULL) { printf("Error: Cannot open destination.txt for writing!\n"); fclose(sourceFile); return 1; } // Copy character by character until End-Of-File (EOF) while ((ch = fgetc(sourceFile)) != EOF) { fputc(ch, destFile); } printf("File copied successfully from source.txt to destination.txt!\n"); // Close both file streams fclose(sourceFile); fclose(destFile); return 0; }

6. Explain self-referential structures in C with an example of a Linked List node. [ 5 Marks ]

A self-referential structure is a structure definition that includes one or more pointer members pointing to a structure of the exact same type. They form the foundational building blocks for dynamic data structures like Linked Lists, Trees, and Graphs.

struct Node { int data; // Data field holding integer value struct Node *next; // Pointer pointing to the next Node of identical structure! };