TU BCA 2080 Regular — C Programming (CACS101)
2080 BS
Subject
Subject Exam
Board / University
Official Examination
Level & Term
Marks & Duration
FM: 60 · PM: 24 · 180m
Exam Instructions:
Candidates are required to give their answers in their own words as far as practicable. Figures in the margin indicate full marks. Group A: Attempt ALL questions (10 x 1 = 10) Group B: Attempt any SIX questions (6 x 5 = 30) Group C: Attempt any TWO questions (2 x 10 = 20)
Group A — Objective Questions (MCQs)
- 1.[1M]Which of the following is an invalid variable name in C? (a) _salary (b) 1st_rank (c) total_sum (d) age2
View Model Solution & Answer
**Correct Answer: (b) 1st_rank** **Explanation:** In C identifier rules, variable names must begin with a letter (uppercase or lowercase) or an underscore (`_`). They cannot begin with a numeric digit. Therefore, `1st_rank` violates C identifier syntax rules. - 2.[1M]What is the return type of the `malloc()` function in C? (a) `int*` (b) `char*` (c) `void*` (d) `float*`
View Model Solution & Answer
**Correct Answer: (c) `void*`** **Explanation:** `malloc()` allocates raw memory on the heap and returns a generic pointer of type `void*`, which can be explicitly typecast to any pointer type (e.g., `(int*)malloc(...)`).
Group B — Short Answer Questions
- 1.[5M]Explain the difference between call by value and call by reference in C with suitable code snippets.
View Model Solution & Answer
### Call by Value vs Call by Reference | Feature | Call by Value | Call by Reference | | :--- | :--- | :--- | | **Parameter Passing** | Passes a copy of the actual argument. | Passes the memory address of the actual argument using pointers. | | **Modification** | Changes made in called function do **not** affect caller variables. | Changes made in called function **directly modify** caller variables. | | **Memory** | Consumes additional memory for parameters. | Memory efficient (operates directly on original memory). | ```c // Call by Value void swapValue(int a, int b) { int temp = a; a = b; b = temp; } // Call by Reference void swapRef(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } ``` - 2.[5M]What is recursion? Write a recursive function in C to calculate the factorial of a positive integer.
View Model Solution & Answer
### Recursion in C Recursion is a programming technique where a function calls itself directly or indirectly to solve smaller sub-problems of the same type until a termination condition (**base case**) is satisfied. ```c #include <stdio.h> long long factorial(int n) { // Base Case if (n <= 1) return 1; // Recursive Case return n * factorial(n - 1); } int main() { int num = 5; printf("Factorial of %d is: %lld\n", num, factorial(num)); return 0; } ```
Group C — Long Answer Questions
- 1.[10M]Explain dynamic memory allocation in C. Differentiate between `malloc()` and `calloc()`. Write a C program to dynamically allocate memory for N integers, sort them in ascending order, and free the memory.
View Model Solution & Answer
### Dynamic Memory Allocation in C Dynamic memory allocation allows programs to request memory from the operating system heap during runtime rather than compile time. #### `malloc()` vs `calloc()` 1. **`malloc(size)`:** Allocates a single contiguous block of memory of specified bytes. Contains **garbage values**. 2. **`calloc(n, size)`:** Allocates `n` blocks of memory, each of `size` bytes, and initializes all allocated bytes to **zero**. ```c #include <stdio.h> #include <stdlib.h> int main() { int n, temp; printf("Enter number of elements: "); scanf("%d", &n); int *arr = (int*) malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } printf("Enter %d integers:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Bubble Sort for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("Sorted array in ascending order:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); free(arr); // Deallocate arr = NULL; return 0; } ```
