0% found this document useful (0 votes)
11 views9 pages

C Paper Makaut

The document is an examination paper for a programming course at Maulana Abul Kalam Azad University of Technology, covering various topics in C programming. It includes questions of varying types, such as very short answer, short answer, and long answer questions, focusing on concepts like pointer arithmetic, recursion, and control structures. The paper consists of multiple groups with specific instructions on answering and marking criteria.

Uploaded by

proyc06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

C Paper Makaut

The document is an examination paper for a programming course at Maulana Abul Kalam Azad University of Technology, covering various topics in C programming. It includes questions of varying types, such as very short answer, short answer, and long answer questions, focusing on concepts like pointer arithmetic, recursion, and control structures. The paper consists of multiple groups with specific instructions on answering and marking criteria.

Uploaded by

proyc06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS/B.

TECH(N)/EVEN/SEM-2/2008/2024-2025/
MAULANA ABUL KALAM AZAD UNIVERSITY OF TECHNOLOGY, WEST BENGAL
Paper Code : ESCS201 Programming for Problem Solving
UPID : 002008

Time Allotted : 3 Hours Full Marks :70


The Figures in the margin indicate full marks.
Candidate are required to give their answers in their own words as far as practicable

Group-A (Very Short Answer Type Question)


1. Answer any ten of the following : [ 1 x 10 = 10 ]
(I) Point out the difference between strcmp() and strncmp()
Ans:- The difference between strcmp() and strncmp() in C lies in the number of characters
compared. strcmp() compares the entire strings, while strncmp() compares only the first n characters.
However, any other correct explanation should be given full credit..
(II) What do you mean by pointer arithmetic in C?
Ans:- Pointers are variables that store memory addresses of other variables. Pointer arithmetic refers to performing
operations like addition, subtraction, increment, and decrement on pointers, allowing them to traverse memory
locations based on the size of the data type they point to.
(III) Write the main of the differences between Structure and Union?
Ans:- The main differences between structures and unions in C are as follows: 1. Memory allocation: In a structure,
each member variable is allocated separate memory locations, whereas in a union, all members share the same
memory location. 2. Access: In a structure, all members can be accessed simultaneously, while in a union, only
one member can be accessed at a time. 3. Size: The size of a structure is the sum of the sizes of all its members,
whereas the size of a union is the size of its largest member.
(IV) Find the correct output:
#include<stdio.h>
main()
{
for(int i=0; i<=5;i++);
{
printf(“%d”,i);
}
}
Ans:- There is a semicolon (;) after the for loop statement and the printf() statement is outside the loop.
Hence the output is 6.
However, the given code will give error, since i cannot be declared within the loop statement. Further, i in the
printf() statement is outside scope of the declaration.
Full credit should be given if the answer is given as 6 or as error.

Note: 0123456 is a wrong answer.


(V) What is the main difference between call by value and call by address in C?
Ans:- In C call-by-value passes a copy of the arguments' value, while call-by-reference passes a reference (or address)
to the original argument, allowing the called function to modify the original variable.
However, any other correct explanation should be given full credit.
(VI) Why is scope of variable necessary in function?
Ans:- In C, scope of a variable defines where a variable is accessible. Scope is important for code
organization, preventing naming conflicts, and ensuring modularity, especially within functions.
However, any other correct explanation should be given full credit.
(VII) #include
main()
{
int i=3;
i=i++;
printf(”%d”,i);
}
1/9
What will be the output of the above code?
Ans:- Since the include statement is incomplete, the code will give error. However, if the header file is assumed to be
correctly declared, the code will run. But the statement i = i++ is ambiguous and not advisable to use in C.
Different compilers will behave differently depending on how they handle post-increment. If the incremented
value is not stored then the output will be 3, otherwise the output may be 4 or some garbage value.
Hence, any answer, error, 3, 4 or garbage with proper explanation should be given credit.
(VIII) Why do we use header files?
Ans:- Header files are used to declare / prototype functions, macros, and types across single / multiple C files.
(IX) Write the output of the following Code:
#include
int main()
{
char x;
x = 'a';
printf("%d\n", x);
}
Ans:- Since the include statement is incomplete, the code will give error. However, if the header file is assumed to be
correctly declared, the output will be 97.
(X) Define recursion.
Ans:- Recursion is a programming technique where a function calls itself with a sub problem until a base condition is
met. This allows the solution of complex problems by breaking them down into simpler instances of the same
problem. However, any other correct definition / explanation should be given credit.
(XI) Arrangement of playing cards is an example of which sorting technique?
Ans:- An examinee cannot answer this question without having a clear idea about pack of playing cards. Further, there
may be various variants of playing cards, like UNO cards, TAROT cards, etc.
With proper enumeration, any sorting technique can be used.
Hence, mention of any technique should be given credit.
(XII) Explain Ackerman function.
Ans:- The Ackermann function is a recursive function defined as:
A(m,n)= n+1, if m=0
= A(m−1,1), if m>0 and n=0
= A(m−1,A(m,n−1)), if m>0 and n>0
It is a non-premitive recursive function that grows very fast.​

1/2 marks may be given for partially correct answer.

Group-B (Short Answer Type Question)


Answer any three of the following : [ 5 x 3 = 15 ]
2. Differentiate between the switch and nested-if statement in C with proper example. [5]
Ans:- Both switch and nested-if statements are used for decision-making, but they differ in their
structure and use cases.
Switch Statement:

Used for selecting one block of code to execute from multiple choices based on the value of a
single variable or expression.
It checks for equality against constant values (cases).
Generally more efficient than nested-if when dealing with multiple discrete choices.
Uses break to prevent fall-through to the next case.
Has an optional default case for handling values not explicitly matched.

Nested-If Statement:

Used for making complex decisions involving multiple conditions.


Allows for checking a range of values or complex logical expressions.
More flexible than switch for non-discrete conditions.
Can become harder to read and maintain with deep nesting.

2/9
Example:

#include
int main() {
int choice = 2;
// Switch statement example
switch (choice) {
case 1:
printf("Choice 1 selected\n");
break;
case 2:
printf("Choice 2 selected\n");
break;
case 3:
printf("Choice 3 selected\n");
break;
default:
printf("Invalid choice\n");
}
int x = 15;
// Nested-if statement example
if (x > 10) {
if (x < 20) {
printf("x is between 10 and 20\n");
if(x%2 ==0){
printf("x is even\n");
} else {
printf("x is odd\n");
}
} else {
printf("x is greater than or equal to 20\n");
}
} else {
printf("x is less than or equal to 10\n");
}
return 0;
}
3. Write a C program to count the number of even and odd numbers from an 1-D array. [5]
Ans:- int main() {
int arr[100], n, i;
int even = 0, odd = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n; i++) {
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Number of even numbers: %d\n", even);
printf("Number of odd numbers: %d\n", odd);
return 0;
}
Note: The necessary header files must be included.

3/9
Any other correct program should be given credit. Part marking to be done for partially correct
answer.
4. Write a program in C to find the area and circumference of a circle using function [5]
Ans:- #define PI 3.14159
float calcArea(float radius) {
return PI * radius * radius;
}
float calcCircum(float radius) {
return 2 * PI * radius;
}
int main() {
float radius, area, circum;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = calcArea(radius);
circum = calcCircum(radius);
printf("Area of the circle = %f\n", area);
printf("Circumference of the circle = %f\n", circum);
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
5. Write a C program to find the length of an array using pointer arithmetic. [5]
Ans:- int main() {
int arr[] = {10, 20, 30, 40, 50};

int *start = arr;


int *end = arr + sizeof(arr) / sizeof(arr[0]);

int length = end - start;


printf("Length of the array = %d\n", length);
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
6. Write a C program to perform bit wise left shift operation on a number taken from the user. Write the [5]
output with an example.
Ans:- int main() {
int num, shift, result;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Enter number of positions to left shift: ");
scanf("%d", &shift);
result = num << shift;
printf("\nOriginal number = %d", num);
printf("\nAfter left shifting by %d positions = %d\n", shift, result);
return 0;
}

Output
Enter an integer: 5
Enter number of positions to left shift: 2
Original number = 5
After left shifting by 2 positions = 20
4/9
Note: Necessary header files must be included.
Any other correct program should be given credit. Part marking to be done for partially correct
answer.

Group-C (Long Answer Type Question)


Answer any three of the following : [ 15 x 3 = 45 ]
7. (a) Draw a flowchart to evaluate factorial of a number. [5]
Ans:- Flowchart must be drawn with proper conventions.
Part marking to be given for partially correct answers.
(b) Write a C program to generate Fibonacci Series upto n-terms using loop. [5]
Ans:- int main() {
int n, i;
int first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
return 0;
}
(c) Explain preprocessor directive with suitable example. [5]
Ans:- Preprocessor directives in C are special instructions that begin with a hash symbol (#) and are
processed by the preprocessor before the actual compilation of your code. They don't translate into
executable code themselves but rather control the compilation process.
Common directives include #include for inserting the content of header files, #define for creating
macros (textual substitutions), and conditional compilation directives like #ifdef, #ifndef, #if, #else, and
#endif to selectively compile parts of the code based on defined conditions.
Preprocessor directives are powerful tools for code organization, creating symbolic constants, and
making code more portable and configurable.
They help in managing dependencies and adapting code to different environments before the compiler
even sees the main C code.

8. (a) Define a recursive function to calculate factorial of a number and call this function to calculate [8]
S = 1! + 2! + ...n! ,where n is user input.
Ans:- long factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
int main() {
int n, i;
long sum = 0;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if(n < 1) {
printf("Please enter a positive integer greater than 0.\n");
return 0;
}
// Calculate S = 1! + 2! + ... + n!
for(i = 1; i <= n; i++) {
sum += factorial(i);
}

5/9
printf("Sum of factorials: %ld\n", sum);
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
(b) Write a C program to swap the content of two variables using Call by Value. [7]
Ans:- In C, when we use call by value, the actual values of the arguments are passed to the function. This
means that any changes made to the parameters inside the function do not affect the original
variables.
So, if we try to swap two variables using call by value, the swap will take place inside the called
function, not in the calling function.
The following code will print the swapped values only in function swap(), not in main()
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside swap function: a = %d, b = %d\n", a, b);
}
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(x, y);
printf("After swap (in main): x = %d, y = %d\n", x, y);
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
9. (a) Write a C program to list all the prime numbers for a given range of numbers. [8]
Ans:- int isPrime(int num) {
if (num < 2)
return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return 0;
}
return 1;
}
int main() {
int start, end;
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);
if (start > end) {
printf("Invalid range. Start should be less than or equal to end.\n");
return 1;
}
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}

6/9
printf("\n");
return 0;
}

Note: Necessary header files must be included.


Any other correct program with or without separate function for prime testing should be given
credit. Part marking to be done for partially correct answer.
(b) Write a C program to check the entered character is a consonant or vowel. If it is not an [7]
alphabet return 'wrong input'.
Ans:- int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch); //The space before %c ignores any previous newline
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
char lower = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
printf("Vowel\n");
}
else {
printf("Consonant\n");
}
}
else {
printf("Wrong input\n");
}
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
10. (a) Describe string with example. [5]
Ans:- In C, a string is an array of characters terminated by a null character '\0'. This null character signals
the end of the string.

Example:

char str1[] = "Hello";


char str2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Both the above declare the string "Hello".
(b) How strings are represented in memory? [4]
Ans:- In C, a string is represented as arrays of characters ending with a special null character '\0'. This
null character is important since it marks the end of the string.
char str[] = "HELLO";
The above will create a string "HELLO" of size 6 byte.
(c) Write a C program to check the entered string is a palindrome or not. [6]
Ans:- int main() {
char str[100];
int i, len, isPalindrome = 1;
printf("Enter a string: ");
scanf("%s", str); // reads single word (no spaces)
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
isPalindrome = 0;
break;

7/9
}
}
if (isPalindrome)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}

Note: Necessary header files (stdio.h, string.h) must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
11. (a) Create a recursive C function to find the GCD of two numbers. [5]
Ans:- int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));
return 0;
}

Note: Necessary header files must be included.


Any other correct program should be given credit. Part marking to be done for partially correct
answer.
(b) Write a C program to sort names of students in a class. [ 10 ]
Ans:- int main() {
int i, j, n;
char names[50][100], temp[100];
printf("Enter number of students: ");
scanf("%d", &n);

printf("Enter the names of the students:\n");


for(i = 0; i < n; i++) {
gets(names[i]);
}
// Sort names using bubble sort
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) {
if(strcmp(names[i], names[j]) > 0) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
// Output sorted names
printf("\nSorted list of names:\n");
for(i = 0; i < n; i++) {
printf("%s\n", names[i]);
}
return 0;
}
8/9
Note: Necessary header files must be included.
Any other correct program with any sorting algorithm should be given credit. Part marking to be
done for partially correct answer.

*** END OF PAPER ***

9/9

You might also like