Here is the complete set of easy, comprehensive five-mark answers for all 51 questions.
Each
response is structured specifically to meet standard exam grading criteria, including definitions,
syntax, examples, and programs where required.
1. What is an array? How to declare and initialize one- and
two-dimensional arrays? (CO1 L1)
● Definition: An array is a collection of variables of the same data type stored in contiguous
memory locations under a single name.
● 1D Array Declaration & Initialization:
○ Syntax: data_type array_name[size];
○ Example: int arr[5] = {10, 20, 30, 40, 50};
● 2D Array Declaration & Initialization:
○ Syntax: data_type array_name[rows][columns];
○ Example: int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
2. Define an Array in C. Write the syntax for declaration and
initialization of a single dimensional array with example. (CO1 L2)
● Definition: An array in C is a derived data type that stores a fixed-size, sequential
collection of elements of the identical data type.
● Syntax for Declaration: data_type array_name[array_size];
● Syntax for Initialization: data_type array_name[size] = {value1, value2, ..., valueN};
● Example Code:
int marks[3] = {85, 90, 95}; // Declares an integer array of size
3 and initializes it
3. Explain Linear Search algorithm with features and time
complexity. (CO1 L2)
● Algorithm: Linear search examines each element of a list sequentially starting from the
first element until a match is found or the end of the list is reached.
● Features:
○ It works on both sorted and unsorted lists.
○ It is simple to implement.
○ It does not require additional memory structure.
● Time Complexity:
○ Best Case: $O(1)$ (Element found at the first position).
○ Worst/Average Case: $O(n)$ (Element found at the last position or not present).
4. What is Linear Search? Write a C program to implement Linear
Search. (CO1 L3)
● Definition: Linear Search is a sequential search algorithm that starts at one end of a
collection and checks every element until the target element is found.
● C Program:
#include <stdio.h>
int main() {
int arr[] = {4, 2, 9, 7, 5};
int target = 7, n = 5, found = -1;
for(int i = 0; i < n; i++) {
if(arr[i] == target) { found = i; break; }
}
if(found != -1) printf("Element found at index %d\n", found);
else printf("Element not found\n");
return 0;
}
5. Explain Binary Search algorithm. Write a C program to search
element 12 using Binary Search method (2, 6, 9, 12, 16). (CO1 L3)
● Algorithm: Binary search splits a sorted array in half repeatedly. It compares the target
with the middle element. If the target is smaller, it searches the left half; if larger, it
searches the right half.
● C Program:
#include <stdio.h>
int main() {
int arr[] = {2, 6, 9, 12, 16};
int low = 0, high = 4, target = 12, mid, found = -1;
while(low <= high) {
mid = low + (high - low) / 2;
if(arr[mid] == target) { found = mid; break; }
if(arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
printf("Element found at index %d\n", found);
return 0;
}
6. Define a function in C. Write the general syntax of function
definition. (CO2 L1)
● Definition: A function is a self-contained block of code that performs a specific,
well-defined task. It promotes code reusability and modularity.
● General Syntax:
return_type function_name(parameter_list) {
// Body of the function
// Statement(s);
return value; // Optional based on return_type
}
● Component Breakdown: return_type indicates the data type of the output value;
parameter_list specifies variables that receive inputs when the function is executed.
7. What is a function prototype? State its purpose. (CO2 L1)
● Definition: A function prototype is a declaration statement that specifies the function's
name, return type, and arguments before its actual implementation.
● Syntax: return_type function_name(data_type1, data_type2, ...);
● Purpose:
○ It informs the compiler about the function structure before it gets invoked.
○ It enables the compiler to perform type-checking on function arguments.
○ It flags mismatched arguments or incorrect return assignments during compilation.
8. Write a recursive C program to find factorial of a number. (CO2
L3)
● C Program:
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
9. Write a recursive program to print Fibonacci series up to n
terms. (CO2 L3)
● C Program:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int terms = 6;
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
10. Explain the working mechanism of recursive functions with
stack concept. (CO2 L2)
● Mechanism: Recursion relies on the system call stack memory. Every time a recursive
function calls itself, a new Activation Record (Stack Frame) is allocated at the top of the
stack.
● Stack Allocation: This frame retains the local variables, parameters, and return address
of that specific call instance.
● Winding Phase: Execution suspends for the caller function and dives deeper into
successive stack allocations until the base case validates.
● Unwinding Phase: Once the base condition returns a value, frames pop off the stack in
Last-In, First-Out (LIFO) order, computing intermediate results back down to the original
caller.
11. Explain the concept of parameter passing in C. (CO2 L2)
● Call by Value:
○ Actual parameter values are copied directly into the formal arguments.
○ Modifications made inside the target function do not affect original caller variables.
○ Example: void modify(int x) { x = 20; }
● Call by Reference (Simulated using pointers):
○ The memory addresses of actual parameters are passed to the formal pointer
arguments.
○ Manipulations alter the stored value at that address directly, mutating the original
variable.
○ Example: void modify(int *x) { *x = 20; }
12. Define a pointer in C. Explain its syntax and basic usage.
(CO3 L1)
● Definition: A pointer is a special variable that stores the memory address of another
variable rather than holding a direct data value.
● Syntax: data_type *pointer_name;
● Basic Usage Example:
int num = 10;
int *ptr = # // '&' obtains address; ptr now holds address of
num
printf("Value of num: %d\n", *ptr); // '*' dereferences ptr to
access value
13. What is a NULL pointer? Explain with example. (CO3 L1)
● Definition: A NULL pointer is a pointer that does not point to any valid memory location. It
is explicitly assigned a value of 0 or NULL to signify it is unlinked.
● Purpose: It prevents unintended references to garbage memory addresses and helps
trace errors.
● Example Code:
#include <stdio.h>
int main() {
int *ptr = NULL; // Initialized to NULL
if (ptr == NULL) {
printf("Pointer is safely NULL and not pointing
anywhere.\n");
}
return 0;
}
14. Explain pointer initialization with suitable example. (CO3 L2)
● Concept: Pointer initialization is the assignment of a specific variable's memory address
to a pointer at declaration. Accessing uninitialized pointers leads to undefined behavior.
● Example Code:
#include <stdio.h>
int main() {
int age = 25;
int *ptr = &age; // Initialization using address-of operator
'&'
printf("Address: %p\n", (void*)ptr);
printf("Value: %d\n", *ptr);
return 0;
}
15. Describe pointer arithmetic with examples. (CO3 L2)
● Concept: Math operations performed on pointer variables scale dynamically according to
the underlying bytes size of the data type it points to.
● Valid Operations: Increment (++), Decrement (--), Addition of an integer (+), Subtraction
(-).
● Examples:
int arr[2] = {10, 20};
int *ptr = arr; // ptr points to arr[0] (e.g., Address 1000)
ptr++; // ptr increments by 4 bytes (for int) to point to
arr[1] (Address 1004)
16. Explain the relationship between arrays and pointers. (CO3
L2)
● Relationship: The name of an array acts as a constant pointer containing the base
address of its first element (&arr[0]).
● Equivalence: Array indexing syntax tracks closely with pointer arithmetic notations:
○ arr[i] matches *(arr + i) exactly.
● Code Example:
int arr[] = {5, 10, 15};
int *ptr = arr;
printf("%d\n", *(ptr + 1)); // Outputs 10, equivalent to arr[1]
17. What is a pointer to pointer? Explain with example. (CO3 L2)
● Definition: A pointer to a pointer is a form of multiple indirection where the first pointer
contains the memory address of a second pointer.
● Syntax: data_type **pointer_name;
● Example Code:
int val = 50;
int *ptr = &val; // Single pointer
int **dptr = &ptr; // Double pointer storing address of ptr
printf("Value via double pointer = %d\n", **dptr); // Outputs 50
18. Define function pointer and explain its syntax. (CO3 L1)
● Definition: A function pointer is a pointer that holds the starting entry address of
executable code blocks inside a function instead of standard data variables.
● Syntax: return_type (*pointer_name)(argument_data_types);
● Usage Example:
int add(int a, int b) { return a + b; }
int (*fptr)(int, int) = add; // Points to function 'add'
int result = fptr(5, 3); // Invoking function via pointer
19. Explain dynamic memory allocation using malloc() and
calloc(). (CO3 L2)
● malloc(): Allocates a single block of raw contiguous memory of a specified size in bytes.
Contents are uninitialized (contain garbage values).
○ Syntax: ptr = (cast_type*) malloc(size);
● calloc(): Allocates multiple blocks of memory of a given element size and initializes all
allocated bytes to zero.
○ Syntax: ptr = (cast_type*) calloc(n, size);
20. Explain the use of realloc() and free() functions. (CO3 L2)
● realloc(): Alters the size of previously allocated dynamic memory blocks on the heap
without losing existing data.
○ Syntax: ptr = realloc(ptr, new_size);
● free(): Releases dynamically allocated memory back to the heap system to prevent
memory leaks.
○ Syntax: free(ptr);
21. Differentiate between pointer and array. (CO3 L2)
Feature Pointer Array
Definition A variable that holds a A collection of uniform data
memory address. elements.
Feature Pointer Array
Mutability Can be reassigned to point Base address pointer is a
elsewhere. fixed constant.
Size Fixed size (typically 4 or 8 Total size equals number of
bytes). elements * type size.
Allocation Can allocate heap Static stack allocation upon
dynamically. declaration.
22. What is a function pointer? Differentiate between call by value
and call by reference (using pointers) with suitable examples.
(CO3 L2)
● Function Pointer: A pointer variable pointing to a block of code defining a function.
● Differences:
// Call By Value Example
void valChange(int a) { a = 100; } // Changes localized copy only
// Call By Reference Example
void refChange(int *a) { *a = 100; } // Permanently modifies
original value
23. Explain dangling pointer with example. (CO3 L2)
● Definition: A dangling pointer is a pointer that continues pointing to a memory location
that has already been freed or deallocated.
● Example:
int *ptr = (int*) malloc(sizeof(int));
*ptr = 25;
free(ptr); // The memory block is freed, but ptr still holds the
address
// ptr is now a Dangling Pointer. Fix by setting: ptr = NULL;
24. Write a C program to swap two numbers using pointers. (CO3
L3)
● C Program:
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
25. Write a program to access array elements using pointers.
(CO3 L3)
● C Program:
#include <stdio.h>
int main() {
int arr[] = {11, 22, 33};
int *ptr = arr;
for(int i = 0; i < 3; i++) {
printf("Element %d = %d\n", i, *(ptr + i));
}
return 0;
}
26. Write a program to demonstrate pointer to pointer. (CO3 L3)
● C Program:
#include <stdio.h>
int main() {
int num = 786;
int *p1 = #
int **p2 = &p1;
printf("Value using num: %d\n", num);
printf("Value using p1: %d\n", *p1);
printf("Value using p2: %d\n", **p2);
return 0;
}
27. What is Dynamic Memory Allocation in C? List different DMA
functions available in C. (CO3 L3)
● Definition: Dynamic Memory Allocation (DMA) is the process of allocating memory
manually from the heap segment at runtime instead of compile-time.
● Functions available in <stdlib.h>:
1. malloc(): Allocates specified uninitialized raw byte size.
2. calloc(): Allocates memory initialized to zero for specified blocks.
3. realloc(): Resizes existing dynamic allocations.
4. free(): Deallocates memory spaces back to the heap.
28. Write a C program to allocate memory for an array using
malloc(). (CO3 L3)
● C Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*) malloc(3 * sizeof(int));
if(arr == NULL) return 1; // Error check
arr[0] = 1; arr[1] = 2; arr[2] = 3;
for(int i=0; i<3; i++) printf("%d ", arr[i]);
free(arr);
return 0;
}
29. Write a program to allocate memory using calloc() and print
values. (CO3 L2)
● C Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*) calloc(3, sizeof(int));
if(ptr == NULL) return 1;
// Prints default values initialized by calloc (all zeros)
for(int i = 0; i < 3; i++) printf("%d ", ptr[i]);
free(ptr);
return 0;
}
30. Write a program to resize memory using realloc(). (CO3 L3)
● C Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*) malloc(2 * sizeof(int));
ptr[0] = 10; ptr[1] = 20;
ptr = (int*) realloc(ptr, 4 * sizeof(int)); // Resizing array
size from 2 to 4
ptr[2] = 30; ptr[3] = 40;
for(int i=0; i<4; i++) printf("%d ", ptr[i]);
free(ptr);
return 0;
}
31. Write a program to demonstrate memory deallocation using
free(). (CO3 L2)
● C Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*) malloc(sizeof(int));
*p = 100;
printf("Before free: %d\n", *p);
free(p); // Memory returned to heap
p = NULL; // Safe practice to avoid dangling pointer
return 0;
}
32. What is a file in C? Explain types of files. (CO4 L1)
● Definition: A file is a named collection of bytes stored permanently on secondary storage
disks (hard drives).
● Types of Files:
○ Text Files (.txt, .c): Store data as standard plain ASCII/UTF characters.
Human-readable and simple to view.
○ Binary Files (.dat, .bin): Store data directly as custom 1s and 0s representations
mirroring internal RAM configurations. Efficient and not human-readable.
33. Define FILE * pointer and its role in file handling. (CO4 L1)
● Definition: FILE is a pre-defined system structure in C defined within <stdio.h>. A FILE *
is a pointer referencing this structure.
● Role:
○ Acts as a descriptor or handle to track active structural streams.
○ Maintains the state buffer, access positions, and error flags of external disk objects.
○ Bridges application instructions to physical target inputs/outputs.
34. Explain different file modes (r, w, a, etc.). (CO4 L2)
● "r" (Read): Opens an existing text file for reading. Fails if the file does not exist.
● "w" (Write): Creates an empty file for writing. Overwrites existing contents if the file
already exists.
● "a" (Append): Opens or creates a file to add data to the end. Retains old content.
● "rb" / "wb" / "ab": Perform identical tasks optimized for raw binary formats.
35. Explain fopen() and fclose() functions with syntax. (CO4 L2)
● fopen(): Opens a file stream and links it to a FILE * handler pointer.
○ Syntax: FILE *fopen(const char *filename, const char *mode);
● fclose(): Closes an active file stream connection and flushes temporary system buffer
streams.
○ Syntax: int fclose(FILE *stream);
36. Differentiate between text file and binary file. (CO4 L2)
Feature Text File Binary File
Content Type Plain ASCII text characters. Raw stream of bits (binary
numbers).
Feature Text File Binary File
Readability Easily readable by humans. Requires special software to
interpret.
Storage Efficiency Less compact; high Highly compact and
translation. computationally faster.
Newline character Converts \n to carriage No character conversion
returns. occurs.
37. Explain fprintf() and fscanf() functions. (CO4 L2)
● fprintf(): Writes formatted string data into a file target instead of printing directly to
standard output screen.
○ Syntax: fprintf(FILE_pointer, "format_string", variables);
● fscanf(): Reads parsed fields matching requested sequence templates out from
designated files.
○ Syntax: fscanf(FILE_pointer, "format_string", &variables);
38. Explain fread() and fwrite() functions. (CO4 L2)
● fwrite(): Writes blocks of raw binary structural segments onto files.
○ Syntax: fwrite(ptr, size, count, FILE_pointer);
● fread(): Reads defined blocks of raw data directly out from files into memory.
○ Syntax: fread(ptr, size, count, FILE_pointer);
● Parameters: ptr (buffer), size (byte size of element), count (number of elements).
39. How do you check errors in file handling? Explain. (CO4 L2)
Errors can be tracked via standard pointer return evaluations and validation utilities:
● NULL Pointer checks: If fopen encounters restricted access or missing targets, it returns
NULL.
● ferror() utility: Checks the error flag for the given file stream. Returns non-zero if an
issue occurred.
● feof(): Determines if a read stream crossed the End-Of-File bounds.
40. Explain file pointer and its usage. (CO4 L1)
● Concept: A file pointer is a variable of type FILE * that keeps track of the file's current
position and manages read/write data transfers.
● Usage Flow:
FILE *fp; // 1. Declaration
fp = fopen("[Link]", "w"); // 2. Linking stream
fprintf(fp, "Hello"); // 3. Target operation
fclose(fp); // 4. Stream cleanup
41. Explain file opening modes with examples. (CO4 L2)
Modes dictate permissions granted during system calls:
● Read Mode ("r"): FILE *f = fopen("[Link]", "r"); (Reads static files).
● Write Mode ("w"): FILE *f = fopen("[Link]", "w"); (Creates blank templates/overwrites).
● Append Mode ("a"): FILE *f = fopen("[Link]", "a"); (Appends logs to the end of a file).
42. Write a program to write data into a file. (CO4 L3)
● C Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("[Link]", "w");
if(fp == NULL) return 1;
fprintf(fp, "Exam Success 2026");
fclose(fp);
return 0;
}
43. Write a program to read data from a file. (CO4 L3)
● C Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("[Link]", "r");
char str[50];
if(fp == NULL) return 1;
if(fscanf(fp, "%s", str) != EOF) {
printf("File data: %s\n", str);
}
fclose(fp);
return 0;
}
44. Write a program to copy contents from one file to another.
(CO4 L3)
● C Program:
#include <stdio.h>
int main() {
FILE *src = fopen("[Link]", "r");
FILE *dest = fopen("[Link]", "w");
char ch;
if(src == NULL || dest == NULL) return 1;
while((ch = fgetc(src)) != EOF) {
fputc(ch, dest);
}
fclose(src); fclose(dest);
return 0;
}
45. Write a program to count number of characters in a file. (CO4
L3)
● C Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("[Link]", "r");
int count = 0; char ch;
if(fp == NULL) return 1;
while((ch = fgetc(fp)) != EOF) {
count++;
}
printf("Total characters: %d\n", count);
fclose(fp);
return 0;
}
46. Write a program for binary file read/write operations. (CO4 L3)
● C Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("[Link]", "wb+");
int write_num = 123, read_num = 0;
fwrite(&write_num, sizeof(int), 1, fp);
rewind(fp); // Go back to start of file
fread(&read_num, sizeof(int), 1, fp);
printf("Binary Read: %d\n", read_num);
fclose(fp);
return 0;
}
47. Explain error handling in file operations with example. (CO4
L2)
● Concept: Error handling prevents program crashes when file operations fail (e.g., file not
found, insufficient disk space). It uses NULL checks and function checks like perror().
● Example Code:
#include <stdio.h>
int main() {
FILE *fp = fopen("ghost_file.txt", "r");
if (fp == NULL) {
perror("Error opening file"); // Prints standard system
error message
return 1;
}
fclose(fp);
return 0;
}
48. Differentiate between fprintf() and fwrite(). (CO4 L2)
Feature fprintf() fwrite()
Output Type Text format. Binary format.
Arguments Formatted string and Block memory reference
Feature fprintf() fwrite()
arguments. addresses.
Target Type Primarily Text Files. Primarily Binary Files.
Human Readable Yes. No.
49. Explain preprocessor directives (#define, #include). (CO4 L1)
● Concept: Directives are source instructions handled by the compiler preprocessor phase
before actual code compilation starts.
● #include: Imports definitions from system header paths into code.
○ Example: #include <stdio.h>
● #define: Replaces specified identifiers with matching code text blocks everywhere in the
program.
○ Example: #define PI 3.1415
50. Explain debugging techniques in C programs. (CO4 L2)
● Source Instrumentation (Print Statements): Inserting trace checks manually (printf) to
monitor tracking paths and state alterations at runtime.
● Using Interactive Debuggers (GDB): Setting breakpoints to step through lines of code,
inspect call stacks, and monitor variables dynamically.
● Compiler Warning Flags: Compiling using diagnostics parameters (like -Wall, -Wextra)
to catch issues early.
51. Explain advantages and applications of file handling. (CO4
L2)
● Advantages:
○ Data Persistence: Data is retained after program execution terminates.
○ High Storage Capacity: Saves heavy arrays to disk memory, saving RAM.
○ Portability: Files can be transferred across computer systems easily.
● Applications:
○ Log Files: Tracking active runtime updates and system errors.
○ Database Management: Storing operational profiles and structure details
permanently.
○ Configuration Profiles: Storing startup parameters for applications.
If you would like to delve deeper into any of these concepts, let me know if I should provide
complete visual trace diagrams for recursion, or run more example checks on the file
handling operations.