MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL
Paper Code: BCAC102
Programming for Problem Solving through C
UPID: 1000081
Time Allotted: 3 Hours
Full Marks: 70
The figures in the margin indicate full marks.
Candidates are required to give their answers in their own words as far as practicable.
Group-A (Very Short Answer Type Questions)
Answer any ten of the following: (1 × 10 = 10)
Linker converts source program into machine code. (State True/False)
The expression
-11 % -3evaluates to ______.What will be the output of the following code segment?
int a = 0; switch(a) { case 0: default: printf("default"); case 1: printf("a=%d", a); }
C performs bound checking for arrays. (State True/False)
Size of a pointer is equal to the data type it points to. (State True/False)
By default, any user-defined function returns ______ type of data.
FILEis of type ______.What is the program that translates source code into object code?
*is a ______ operator.What is the final value of
xwhen the codeint x; for(x=0; x<10; x++) {}is executed?Binary search works on sorted arrays. (State True/False)
Combine the following two statements into one:
char *p; p = (char*) malloc(100);
Group-B (Short Answer Type Questions)
Answer any three of the following: (5 × 3 = 15)
2. Passing Parameters to a Function & Computing y = x^n
Write a function that computes y = x^n where x and n are positive integers.
3. fscanf() and fprintf() Functions
Discuss their uses with suitable examples.
4. Number System Conversion
Convert
(29)₁₀to binary.Convert
(1011101)₂to hexadecimal.
5. Bitwise Operators & Swapping Without a Third Variable
What are bitwise operators? Give examples.
Write a program to swap two variables without a third variable.
6. Find the Output
a)
char p[] = "%d\n"; p[1] = 'c'; printf(p, 65);
b)
#include <stdio.h> void main() { int k = 5; int *p = &k; int **m = &p; printf("%d%d%d\n", k, *p, **m); }
Group-C (Long Answer Type Questions)
Answer any three of the following: (15 × 3 = 45)
27. Call by Value vs. Call by Reference
Explain with examples.
Differentiate between storage classes (
auto,static,register,extern).Explain functions with return values and arguments.
28. Loops & Prime Number Check
Differentiate between
breakandcontinue.Differentiate between entry-controlled (
for,while) and exit-controlled (do-while) loops.Write a C program to check if a number is prime.
29. Array & String Operations
Write a program to delete an element from an array.
Write a program to check if a string is a palindrome.
30. Recursion, Type Casting, Embedded C vs. Traditional C
Explain recursive functions with an example.
Differentiate between type casting and type conversion.
Compare Embedded C and Traditional C.
31. File Handling in C
Explain
fseek()andrewind().Write a program to create a file
TEXT, store 10 integers, and read them back.
Analysis and Solutions for BCAC102 Programming for Problem Solving through C
Group-A (Very Short Answer Type Question)
Answer any ten of the following:
i) False - The linker combines object files into an executable, while the compiler converts source code to machine code.
ii) The expression
-11%-3evaluates to -2 (In C, the sign of the result matches the dividend).iii) Output:
defaulta=0
(Since there's no break statement, execution falls through to the next case)
iv) False - C does not perform bound checking for arrays.
v) False - Pointer size depends on the system architecture, not the data type it points to.
vi) By default any user defined function returns int type of data.
vii) FILE is of type structure.
viii) The program that translates source code into object code is a compiler.
ix)
*is a pointer dereference operator (or could be multiplication operator).x) The final value of x is 10 (loop exits when x=10).
xi) True - Binary search requires a sorted array.
xii) Combined statement:
char *p = (char*)malloc(100);
Group-B (Short Answer Type Question)
Passing parameters to a function and computing y=x^n:
#include <stdio.h> int power(int x, int n); // function prototype int main() { int x = 2, n = 3; printf("%d^%d = %d\n", x, n, power(x, n)); return 0; } int power(int x, int n) { int result = 1; for(int i = 0; i < n; i++) { result *= x; } return result; }
fscanf() and fprintf() functions:
fscanf() and fprintf() are file handling functions similar to scanf() and printf() but work with files.
#include <stdio.h> int main() { FILE *fp; int num; // Writing to file fp = fopen("example.txt", "w"); fprintf(fp, "%d %d %d", 10, 20, 30); fclose(fp); // Reading from file fp = fopen("example.txt", "r"); while(fscanf(fp, "%d", &num) != EOF) { printf("%d ", num); } fclose(fp); return 0; }
Number system conversions:
(29)₁₀ to binary:
29 ÷ 2 = 14 remainder 1
14 ÷ 2 = 7 remainder 0
7 ÷ 2 = 3 remainder 1
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
Reading remainders from bottom: 11101
So, (29)₁₀ = (11101)₂(1011101)₂ to hexadecimal:
Group into nibbles: 0101 1101
0101 = 5, 1101 = D
So, (1011101)₂ = (5D)₁₆
Bitwise operators and swapping without third variable:
Bitwise operators perform operations at bit level:
&(AND),|(OR),^(XOR),~(NOT),<<(left shift),>>(right shift)
#include <stdio.h> int main() { int a = 5, b = 7; printf("Before swap: a=%d, b=%d\n", a, b); a = a ^ b; b = a ^ b; a = a ^ b; printf("After swap: a=%d, b=%d\n", a, b); return 0; }
Find the output:
a) Output:
A
(Changed format specifier to %c, so 65 prints as 'A')
b) Output:
555
(All three print the same value of k through different levels of indirection)
Group-C (Long Answer Type Question)
Call by value vs call by reference, storage classes, and functions
a) Call by value vs call by reference:
Call by value: Copies the value of arguments. Changes inside function don't affect original.
Call by reference: Passes address of variable. Changes inside function affect original.
// Call by value void square(int x) { x = x * x; } // Call by reference void squareRef(int *x) { *x = (*x) * (*x); } int main() { int a = 5; square(a); // a remains 5 squareRef(&a); // a becomes 25 return 0; }
b) Storage classes:
auto: Default for local variables (lifetime within block)
register: Suggests storing in register (no address can be taken)
static: Preserves value between function calls
extern: Declares variable defined elsewhere
c) Functions with return and arguments:
// Function with return and arguments float average(int a, int b) { return (a + b) / 2.0; } int main() { float avg = average(10, 20); printf("Average: %.2f\n", avg); return 0; }
Break vs continue, loops, and prime number check
a) Differences:
break: Exits the loop immediately
continue: Skips current iteration and continues with next
Entry controlled (for, while): Condition checked before iteration
Exit controlled (do-while): Condition checked after iteration
b) Prime number check:
#include <stdio.h> #include <stdbool.h> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPrime(num)) { printf("%d is prime\n", num); } else { printf("%d is not prime\n", num); } return 0; }
Array and string operations
a) Delete element from array:
#include <stdio.h> void deleteElement(int arr[], int size, int pos) { if (pos < 0 || pos >= size) { printf("Invalid position\n"); return; } for (int i = pos; i < size - 1; i++) { arr[i] = arr[i + 1]; } printf("Array after deletion:\n"); for (int i = 0; i < size - 1; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr)/sizeof(arr[0]); deleteElement(arr, size, 2); // Delete element at index 2 return 0; }
b) Palindrome check:
#include <stdio.h> #include <string.h> #include <stdbool.h> bool isPalindrome(char str[]) { int left = 0; int right = strlen(str) - 1; while (left < right) { if (str[left] != str[right]) { return false; } left++; right--; } return true; } int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); if (isPalindrome(str)) { printf("%s is a palindrome\n", str); } else { printf("%s is not a palindrome\n", str); } return 0; }
Recursive functions, type casting/conversion, Embedded vs Traditional C
a) Recursive function example (factorial):
#include <stdio.h> int factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n - 1); } int main() { int num = 5; printf("Factorial of %d is %d\n", num, factorial(num)); return 0; }
b) Type Casting vs Type Conversion:
Type Casting: Explicit conversion by programmer (e.g.,
(float)5/2)Type Conversion: Implicit conversion by compiler (e.g.,
5/2.0)
c) Embedded C vs Traditional C:
Embedded C has hardware-specific extensions, limited resources, direct hardware access
Traditional C is for general-purpose computing with standard libraries
File operations
a) fseek() and rewind():
fseek(): Moves file pointer to specific positionrewind(): Moves file pointer to beginning of file
b) File program:
#include <stdio.h> int main() { FILE *fp; int num; // Create file and write 10 integers fp = fopen("TEXT", "w"); for (int i = 1; i <= 10; i++) { fprintf(fp, "%d ", i); } fclose(fp); // Read and print the integers fp = fopen("TEXT", "r"); printf("Numbers in file:\n"); while (fscanf(fp, "%d", &num) != EOF) { printf("%d ", num); } fclose(fp); return 0; }
These solutions cover the key concepts and programming techniques tested in the exam paper. Each answer demonstrates practical implementation of the theoretical concepts asked in the questions.
Thanks
Mr.Siddhan Khata
Director
S.K.Institute

No comments:
Post a Comment