0% found this document useful (0 votes)
49 views

ESA Programming With C Paper 1 12

The document outlines the End Semester Assessment (ESA) for the course UE22CS151B - Problem Solving With C at PES University, Bengaluru, detailing various programming tasks and questions related to C programming concepts. It includes questions on constructs like switch-case, memory management, structures, error handling, and file operations, along with coding tasks such as implementing sorting algorithms and handling strings. The assessment is structured into multiple sections, each focusing on different aspects of C programming, with a total of 100 marks.

Uploaded by

atharvmore2006
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)
49 views

ESA Programming With C Paper 1 12

The document outlines the End Semester Assessment (ESA) for the course UE22CS151B - Problem Solving With C at PES University, Bengaluru, detailing various programming tasks and questions related to C programming concepts. It includes questions on constructs like switch-case, memory management, structures, error handling, and file operations, along with coding tasks such as implementing sorting algorithms and handling strings. The assessment is structured into multiple sections, each focusing on different aspects of C programming, with a total of 100 marks.

Uploaded by

atharvmore2006
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/ 84

11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

PES University, Bengaluru


(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - DEC 2023

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Explain the switch construct in C with a coding snippet. (4.0 Marks)

1.b. Explain different options of gcc to get the output of C code.


(6.0 Marks)

1.c. Separate the keyword/s and the variable/s from the set of identifiers
given below.
Return me i for (4.0 Marks)

about:blank 1/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

1.d. Explain the preprocessing phase of Program development life cycle of


C with a neat diagram. (6.0 Marks)

2.a. Name any four functions available in string.h to play with the
collection of characters. (4.0 Marks)

2.b. Write a function to find the product of all the integers in an array and
return the product. (5.0 Marks)

about:blank 2/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

2.c. Explain an array of integers with an example code. (5.0 Marks)

2.d. Given the client code, fill the implementation of disp_vowels function
to print only the vowles in the string.
#include<stdio.h>
int main()
{ char a[ ] = "hi and hellow"; avoid_vowels(a); return 0; }
void disp_vowles(char str[])
{ // Fill this implementation } (6.0 Marks)

3.a. Given a below structure, if s1 and s2 are structure variable and


pointer to structure variable respectively, write the set of statements to
print the values associated with s1 and s2.
struct student {
int roll_no;
char name[20];
int marks;
}; (4.0 Marks)

about:blank 3/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

3.b. List any four dynamic memory management functions in C. Explain


any one of them. (6.0 Marks)

3.c. List any four characteristics of structures in C. (4.0 Marks)

3.d. struct node


{
int info;
char details[100];
struct node *next;
};
typedef struct node node_t;
node_t what(node_t*);
struct node type and the alias name is given. If there are five nodes
connected using the next pointer of each node to another node, write the
implementation of what function to return the last node. Function takes
the pointer to the first node as an argument. (6.0 Marks)

about:blank 4/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

4.a. Define a student structure and add a neat diagram to describe array
of structures (4.0 Marks)

4.b. Implement the function to sort an array of integers using selection


sort. (6.0 Marks)

4.c. Write a C program to copy the contents of one file to another with a
comma appended at the end of each line. (6.0 Marks)

about:blank 5/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

4.d. Provide brief description and a code snippet about Error handling in
C using errno and strerror. (4.0 Marks)

5.a. What are pre-processor directives? Predict the outputs when the
below code gets executed.
#include<stdio.h>
#define sqr(x) x*x
#define cube(x) sqr(x)*x
#define MAX 5
int main()
{
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
#define MAX 3
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
return 0;
} (5.0 Marks)

5.b. What are Enums in C? Explain with a code snippet. (5.0 Marks)

about:blank 6/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)

5.c. Say True or False


i) Array of bit fields is allowed
ii) Bit fields with a length of 0 must be unnamed
iii) Accessing the Variable length Arguments from the function body
makes use of macros
available in stdarg.h
iv) Storing the symbol of one enum in another enum variable is invalid in
C. (4.0 Marks)

5.d. What is a storage class in C? Explain static with an example code


snippet. (6.0 Marks)

about:blank 7/7
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - May 2024

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Explain different options of gcc to get the output of C code. (4.0 Marks)

1.b. Brief the significance of switch construct in C with a coding snippet. (6.0 Marks)

1.c. Separate the keyword/s and the variable/s from the set of identifiers given
below.
you Return break for (4.0 Marks)
1.d. With a neat diagram, explain the phases involved in the Program development
life cycle of C program. (6.0 Marks)

2.a. Explain any two string related functions available in string.h (4.0 Marks)

2.b. Write a function to find the sum of all the integers in an array which is sent as
argument and return the sum. (5.0 Marks)

2.c. Explain the features of arrays in C with an example code. (5.0 Marks)
2.d. Fill the implementation of disp_vowels function to print only the vowels in the
string. Driver code is given.
#include<stdio.h>
int main()
{ char a[ ] = "hi and hello pes university"; avoid_vowels(a); return 0; }
void disp_vowles(char str[])
{ // Fill this implementation } (6.0 Marks)

3.a. If s1 and s2 are structure variable and pointer to structure variable respectively
for the below structure, write the set of statements to print the attributes
associated with s1 and s2.
struct student {
int rno;
char name[20];
int marks;
}; (4.0 Marks)

3.b. Mention any four dynamic memory management functions in C. Explain any
one of them. (6.0 Marks)

3.c. List the features/characteristics of structures in C. (4.0 Marks)


3.d. struct node
{
int info;
char details[100];
struct node *next;
};
typedef struct node node_t;
node_t what(node_t*);
struct node type and the alias name is given. If there are five nodes connected
using the next pointer of each node to another node, write the implementation of
what function to return the last node. Function takes the pointer to the first node
as an argument. (6.0 Marks)

4.a. Create a student structure and add a neat diagram to describe array of
structures (4.0 Marks)

4.b. Implement selection sort using an array of structures. (6.0 Marks)

4.c. Write a C program to copy the contents of one file to another. (6.0 Marks)
4.d. Provide brief description and a code snippet about Error handling in C using
errno and strerror. (4.0 Marks)

5.a. What are pre-processor directives? Predict the outputs when the below code
gets executed.
#include<stdio.h>
#define sqr(x) x*x
#define cube(x) sqr(x)*x
#define MAX 5
int main()
{
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
#define MAX 3
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
return 0;
} (5.0 Marks)

5.b.
Define Enum. Explain with a code snippet.

(5.0 Marks)

5.c. Say True or False


i) Storing the symbol of one enum in another enum variable is invalid in C.
ii) Bit fields with a length of 0 must be unnamed
iii) Accessing the Variable length Arguments from the function body makes use of
macros
available in stdarg.h
iv) Array of bit fields is allowed (4.0 Marks)
5.d. List any three storage classes in C. Explain static with an example code snippet.
(6.0 Marks)
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - June 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Describe the phases involved in the Program Development Life Cycle of a C
program with a neat diagram. (6.0 Marks)

1.b. i) Categorize the words to its specific box.


Box1 - Variables
Box2 - Types
A) double B) _INT C) float D)DOUBLE_

ii) Say True or False.

A) C is a case insensitive language.


B) pRintf("I love C"); This coding statement results in compile time error.
(6.0 Marks)

1.c. Write a C program to find whether the number entered by the user is even or
odd using the ternary operator. Display appropriate message. (4.0 Marks)
1.d. (i) When the code given below is executed, and if the user enters 5 and 10
separated by a space, predict the output.
#include <stdio.h>
int main()
{
int num1, num2;
int value = scanf("%d %d", &num1, &num2);
int sum = num1+num2;
printf("%d %d", sum,value);
return 0;
}

(ii) What is the output of below code when it is executed?


#include <stdio.h>
int main()
{
int x = 16;
int y = x >> 2;
printf("%d", y);
} (4.0 Marks)

1.e. Explain the working of a switch-case construct in C along with its syntax.

(5.0 Marks)

2.a. Write a C function named bubble_Sort which takes integer array 'arr' and the
number of elements in arr 'n'. The function must sort the integers given in an
array in ascending order using the bubble sort technique.
The function declaration is as follows:
void bubble_Sort(int arr[],int n); (6.0 Marks)
2.b. Give a brief note on any two storage class specifiers used in C. (6.0 Marks)

2.c. i) The 2D Array 'arr' represents the square matrix and this is the parameter to
the given function 'what'. Fill up the blanks with an appropriate expression (using
index notation) so that the function returns the sum of non-principal
diagonal elements of a matrix.

Sample:
If the Matrix is as given below,
[123
456
789]
expected output is: 2 + 3 + 4 + 6 + 7 + 8 = 30

int what(int arr[3][3])


{
int sum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if ( ---------)
sum += ---------;
}
ii.Say True or False

(A) When you pass an array to a function, you are actually passing a pointer to
the first element of the array.
(B) An array of pointers can store the addresses of different types of variables.
(4.0 Marks)
2.d. i.Given int a[5]={10,5,11,56,78}; int n=5;
Consider the following code. What is returned from the function if 'a' and 'n' are
passed as an argument to the function?
int what(int *a, int n) {
int count = 0;
for (int i = 0; i < n; i++) {
if (*(a + i) % 2 != 0) {
count++;
}
}
return count;
}

ii. What is the output of code given below?


enum Color_Code { Orange=5, Yellow=10, Blue=4, Orange=12 };
int main()
{
printf("%d",Orange);
return 0;
} (4.0 Marks)

2.e. i. Define recursion. Which data structure is used to implement recursion in C?


ii. What does the following recursive function do?

#include<stdio.h>
int Find(int num,int result);
int main()
{
int a = 3423;
printf("%d",Find(a,0));
return 0;
}
int Find(int num,int result)
{
if(num==0)
return result;
else
return Find(num/10,result+(num%10));
} (5.0 Marks)

3.a. Explain briefly the following with an example.


( A) calloc (B) realloc (6.0 Marks)
3.b. Consider the structure definition given below and alias for it.
struct inventory
{
char item_code ;
char item_description[100];
int quantity;
float price;
};
typedef struct inventory INVENTORY;

Write the C statements for the following


A) Create a pointer to the structure.
B) Allocate memory for all the members of the structure dynamically
C) Assign values to all the members of the structure through this pointer.
(6.0 Marks)

3.c. i. List any four string manipulation functions.

ii.Predict the output of the code given below.

#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
printf("%d", strcmp(strcat(str1, str2), "HelloWorld"));
return 0;
} (4.0 Marks)

3.d. i) Fill up the blanks in the below code with an appropriate expression so that
the function returns the length of the string.

int my_strlen(const char *str)


{
int len = 0;
while (*str != '\0')
{
-------;
-------;
}
return len;
}

ii.Say True or False.

A) Array of bit fields is allowed.


B) All members of a union share the same memory. (4.0 Marks)
3.e. Consider the following structure for a linked list node:
struct Node
{
int data;
struct Node* next;
};

Create a function called createNode which generate a new node for a linked list.
The function should take an integer parameter representing the data for the new
node. It must return a pointer to the newly created node.
Declaration of the function is as given:
struct Node* createNode(int data); (5.0 Marks)

4.a. Write a C program to copy the contents of one file to another file. Display
appropriate message. (6.0 Marks)

4.b. i.Define callback. How callback is implemented in C language?


ii.Write the declaration of a function pointer 'fp' which takes 2 int values as
parameters and returns back a float.
iii. Write the output of the below code:

#include<stdio.h>
int what(int x, int y, int (*op)(int,int))
{
return op(x, y);
}
int fun1(int x, int y)
{
return x / y;
}
int fun2(int x, int y)
{
return x % y;
}
int main()
{
printf("Result=%d\n", what(6, 3, fun1));
printf("Result=%d\n", what(5, 2, fun2));
return 0;
} (6.0 Marks)
4.c. With an example show that
A ) we can define a macro with an expression
B) we can define a macro with a parameter (4.0 Marks)

4.d. What is conditional compilation? List any four conditional compilation


constructs. (4.0 Marks)

4.e. i) List any four pre-defined macros.

ii) Say True or False

The fgetc function reads a block of data from a file.


(5.0 Marks)
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - June 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. i. Given a C code named test.c, answer the below questions by writing the
appropriate commands.

A) How do you get the output of preprocessing stage on the terminal?


B) How do you preprocess and compile the file test.c?
C) What is the result of preprocessing and compiling test.c file?
D) Is it possible to rename the executable file?

ii. Categorize the following words as variables or keywords.


A) short B) Print C) static D) count (6.0 Marks)

1.b. List three types of loops in C along with its syntax. (6.0 Marks)

1.c. Write a C program to find whether an integer number entered by the user is
even or odd using bitwise operator. Display appropriate message. (4.0 Marks)
1.d. i. Consider the following C code snippet. What will be the output when the
code below is executed?
#include <stdio.h>
int main()
{
int x = 5, y = 7, z;
z = x++ * --y;
printf("%d", z);
return 0;
}

ii. Fill up the blanks in the below code snippet to get the output as 1 2 2 4
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; _______ ;i++) {
for (j = 1; j <= 2; j++) {
printf("%d ", ___________ );
}
}
return 0;
} (4.0 Marks)

1.e. i)What is the output of the program given below if the user inputs the
character 'A' and presses Enter key while executing?
#include <stdio.h>
int main()
{
int result=getc(stdin);
printf("%d",result);
return 0;
}

ii. Mention the outputs of below code snippets separately.


(A) printf("%d", 4 && 3);
(B) printf("%d", -1?0:1);

iii. Say True or False

(A) The getchar function in C can be used to read a string of characters until
a newline character is encountered.
(B) The default case in a switch statement is mandatory.

(5.0 Marks)
2.a. Write the definition of binarySearchRecursive function which takes the
sorted(ascending order) integer array 'arr' as it's first argument, lowest index(0) as
it's second argument, highest index(n-1) as it's third argument where n is the
number of elements in the array and the element to be searched, 'key' as it's last
argument. The function searches for the key element recursively in the given array
and if the search is successful, returns the index of the element in an array,
otherwise returns -1. The function declaration for the same is as follows:
int binarySearchRecursive(int arr[], int left, int right, int key); (6.0 Marks)

2.b. Give a brief note on the following storage class specifiers

A) static B) register (6.0 Marks)

2.c. In the code given below, if the address of arr[0][0] is 5000 and the size of
integer is 4 bytes, find the address of arr[1][0]. Also mention what gets printed.

#include<stdio.h>
int main()
{
int arr[][2] = {2,5,4,7,9,1,90,67,23,75,86};
printf("%d",arr[1][0]);
return 0;
} (4.0 Marks)
2.d. i.What gets printed when the below code runs?
enum days
{ sunday = -3, monday, tuesday };
int main()
{
enum days d;
printf("%d %d",d=monday, tuesday);
}

ii. A 2D array, 'matrix' represents the square matrix and 'n' is the number of
elements in the matrix. Fill up the blanks with an appropriate expression so that
the output would be the principal diagonal elements printed.

Sample:
If the Matrix is as given below,
[178
546
923]

expected output is 1 4 3

include <stdio.h>
void printPrincipalDiagonal(int matrix[3][3], int n) {
for (int i = 0; _____________; i++) {
printf("%d ", _________);
}
printf("\n");
} (4.0 Marks)

2.e. i.Consider the following function definition. What does this function do?

#include <stdio.h>
void processArray(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
*(arr + i) = *(arr + i) * 2;
}
}

ii. Predict the output of the following code when it is run.


#include <stdio.h>
int main()
{
int arr[5] = {11, 21, 13, 44, 25};
int *ptr = arr + 2;
printf("%d", *ptr);
return 0;
} (5.0 Marks)
3.a. Explain briefly the following.
A) realloc B) Priority Queue (6.0 Marks)

3.b. Create a structure called Emp_attendance with the following fields -


Emp_Id(whole number), Emp_name(string), no_days_present(whole number).
Choose appropriate data type for each field. Also provide C statements for each of
these.
A) Give an alias name called EMP_ATTD for the defined structure.
B )Declare a variable to hold maximum 100 employee details.
C) Declare a pointer to the above structure. Store the address of the structure in
this pointer. (6.0 Marks)

3.c. List any two string functions with its syntax.


(4.0 Marks)

3.d. What gets printed when the code below is executed?


#include<stdio.h>
int main()
{
char str[] = "PESU";
int i;
for(i=0; str[i]; i++)
printf("%c %c %c %c\n", str[i], *(str+i),
*(i+str), i[str]+1);
return 0;
} (4.0 Marks)
3.e. What is a linked list? Consider the following structure for the node in a linked
list.

struct Node {
int data;
struct Node* next;
};

Write a function definition for PrintList, which takes the address of the first node as
an argument to print all the nodes in a sequence.
Function declaration is as follows:
void printList(struct Node* node) ; (5.0 Marks)

4.a. Write a C program that counts the total number of lines in a CSV file named
"data.csv". Assume that the file data.csv is available to the program and maximum
number of characters in each line is 200. Display appropriate message.
(6.0 Marks)

4.b. With an example code, show that

A) A string can be defined using macros


B) A macro can be used in another macro (6.0 Marks)
4.c. Define conditional compilation. Write the output of below code when it is run.
#include <stdio.h>
int main()
{
#ifndef CHECK
#define CHECK 10
printf("%d\n", CHECK);
#endif
#ifdef CHECK
printf("%d", CHECK+2);
#endif
printf("\npreprocessing successful");
return 0;
} (4.0 Marks)

4.d. i) List any two error handling functions related to file handling.

ii) Say True or False

A) The fprintf function reads a data from a file.


B) The mode "w" in fopen(), creates a file if it is not existing (4.0 Marks)

4.e. What is callback in C ? Give an example code. (5.0 Marks)


PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - June 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. With a neat diagram, list and explain the different phases involved in the program development life cycle. (7 Marks)
(7.0 Marks)

1.b. i) Consider the following program and write the output of the program. (4 Marks)
#include <stdio.h>
int main() {
int a, b, c, k;
a=20; b=35; c=42;
printf("a=%d, b=%d, c=%d\n", a, b, c);
if (a < b)
{
k=b;b=a; a=k;
}
printf("a=%d, b=%d, c=%d\n", a, b, c);
if (a < c)
{
k=c; c=a; a=k;
}
printf("a=%d, b=%d, c=%d\n", a, b, c);
if (b > c)
{
k=c; c=b; b=k;
}
else if (a < c)
{
k=c; c=a; a=k;
}
printf("a=%d, b=%d, c=%d\n", a, b, c);
return 0;
}

ii) Convert the following for-loop into an equivalent while-loop: (2 Marks)


for(i = 2; i <= sqrt(n); i += 3)
printf("%d ", i);

(6 Marks)
(6.0 Marks)

1.c. Write the output for the following program: [2 + 2 + 2 ]


#include <stdio.h>
int main() {
int w = 0, x = 2.5, y = 5, z = 3, s = 4, t = 5, u = -3;
double a = 2.36, b = 3.19, c = 3.0, d = 2.91726;
printf("Expr_1 = %d\n", (int)(c * y / z + y / z * c));
printf("Expr_2 = %lf\n", x - s * t * - c - u);
printf("Expr_3 = %f\n", (float)(x + y < z + w && a > b - 17 * x || ! x < 5));
return 0;
}
(6
Marks)
(6.0 Marks)
1.d. Write a C program that takes a 4-digit number as input and calculates the sum of the first and last digits of the
given number.
sample input - Enter a 4-digit number: 4563
sample output - The sum of the first and last digit is: 7 (6 Marks) (6.0 Marks)

2.a. Given the client code, write the implementations for read_array and display_sum.
The read_array function must read n elements from the user and store it in an array variable, arr.
The display_sum function must find the sum of all the elements in arr and display the result. (6 Marks)
#include<stdio.h>
void read_array(int *a, int n);
void display_sum(int *a,int n);

int main()
{ int arr[1000];
int n;
printf("Enter n: ");
scanf(“%d”,&n);
read_array(arr, n);
display_sum(arr, n);
return 0;
}
(6.0 Marks)

2.b. What is the difference between call by value and call by reference? Write a c program to swap two integers using
call by reference? (2+4)

(6
Marks) (6.0 Marks)

2.c. Write a function to find the pattern from the given string and display the position of the first character of the
pattern. (7 Marks) (7.0 Marks)
2.d. i) Fill up the blank space using the pointer notation to get the expected output. (1 Mark)
Expected output: 60
#include<stdio.h>
int main()
{
int a[][2] = {34,55,11,17,20,60};
printf("%d", _________________);
return 0;
}
ii) What is the output of below code? (1 Mark)
#include<stdio.h>
int main()
{
char list_of_friends[] = {"THAKSH", "INCHARA", "AADIT", "AADHYA"};
printf("%s",list_of_friends[0]);
return 0;
}
iii) Find the output of the below recursive code. (2 Marks)
#include<stdio.h>
int get_what(int n1,int n2);
int main()
{
int n1 = 12;
int n2 = 10;
printf("%d",get_what(n1,n2));
return 0;
}
int get_what(int n1,int n2)
{
if(n1 == 0 || n2 == 0) return n2;
else {
return get_what(n2-2,n1-1);
}
}
iv) Name the two types of lines required to complete the rules of the make file creation. (2 Marks)
(1+1+2+2=6 Marks)
(6.0 Marks)

3.a. Implement Binary search using recursive method on an array of 100 integer elements which are stored in ascending order.
Display an appropriate message for both successful and unsuccessful searches.
Given the array, int a[ ] = {100,98,76,54,44,43,42,40,31,30};
Write only the binary search function definition.
Note: Client code is not required
(6 Marks) (6.0 Marks)
3.b. Complete the function definition for oddeven_list to segregate the even and odd numbers from the given linked list and
copy those elements to respective arrays. The function must also print both the arrays. The client code is given as below:.
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data; struct node* link; };
typedef struct node NODE;
struct list
{ NODE *head; };
typedef struct list LIST;
void oddeven_list(LIST* li,int *even,int *odd);
int main()
{
NODE *n = (NODE*) malloc(sizeof(NODE));
n->data = 140;
n->link = (NODE*) malloc(sizeof(NODE));
n->link->data = 313;
n->link->link = (NODE*) malloc(sizeof(NODE));
n->link->link->data = 250;
n->link->link->link = (NODE*) malloc(sizeof(NODE));
n->link->link->link->data = 188;
n->link->link->link->link = NULL;
LIST *li;
li->head = n;
int odd[100];
int even[100];
oddeven_list(li,even,odd);
return 0;
}
void oddeven_list(LIST *li,int *even,int *odd)
{ // write code for this implementation }

(6 Marks) (6.0 Marks)

3.c. There is a structure called student and two data members in it such as name and age. Write the code to do the following:.
i) Create a structure variable s
ii) Create a pointer to structure variable which points to s
iii) Display name using pointer
iv) Display age using s
v) Create a structure variable s1 and copy the contents of s to s1.Write the code snippet to compare s1 and s (6 Marks)
(6.0 Marks)

3.d. List all the library functions used for dynamic memory allocation with their syntax and examples (7 Marks) (7.0 Marks)

4.a. What is preprocessor directives? list and explain any 5 preprocessor directives. (3+5 marks)
(8 Marks) (8.0 Marks)
4.b. Distinguish between the following functions: (2+2+2)
1)getc() and fgetc()
2)scanf() and fscanf()
3)printf() and fprintf()

(6 Marks) (6.0 Marks)

4.c. Read the following code and fill in the blanks. The program must write the details of age and name from the age and name
arrays to a data file named emp.txt. A space must separate each row of data from the same indices when copying it to a file. (5
Marks)
#include<stdio.h>
int main()
{
int age[ ] = {34,45,32,54,44};
char name[][50] = {"raj","rajesh","anil", "anitha","rajendra"};
FILE *fp =________________________________
if(______________)
{
for(int i = 0 ; i < 5; i++)
{
fprintf(fp,"__________ \n",age[i],name[i]);
}
}
fclose(fp);
return 0;
} (5.0 Marks)

4.d. List out any four characteristics of macro in C. Also find the output of below code. (4+2)
#include<stdio.h>
#define SUM(a,b) a*b
int main()
{
printf("%d",SUM(5+2,2+1)); return 0;
}
(6 Marks) (6.0 Marks)
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - June 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. List and explain the different types of errors with suitable example (5 Marks)
(5.0 Marks)

1.b. Point out the errors, if any in the following c statements. (5 Marks)
i)_salary=1000;
II) si=p*rate of interest+no-of-years/100;
iii) date=’18 Apr 2024;’
iv) count=count+1; (5.0 Marks)

1.c. Given the client code, fill the implementation of avoid_vowels function to print
only the consonants in the string. (6 Marks)
#include<stdio.h>
int main()
{
char a[ ] = "immunization and health record";
avoid_vowels(a);
return 0;
}
void avoid_vowels(char str[])
{
// Fill this implementation
} (6.0 Marks)
1.d. Explain do-while, for and switch constructs in C with a coding snippet (9 Marks)
(9.0 Marks)

2.a. Write the user defined function to copy one string to another string. Also, test
this function in the client code. (6 Marks) (6.0 Marks)

2.b. Write a C program to find a factorial of a given number using recursion.show


stack frame for function call (6+4=10 Marks) (10.0 Marks)

2.c. Write a function to replicate a given string n times. Assume that the given string
has enough storage space. (4 Marks)
You may use functions in the string library.
void str_replicate(char* s, int n)
{
// write code
} (4.0 Marks)
2.d. List the four types of storage classes in C and explain any one with an example
code snippet. (5 Marks) (5.0 Marks)

3.a. Write 5 differences between structures and unions. (5 Marks) (5.0 Marks)

3.b. Illustrate array of pointers to integers with a code snippet. (6 Marks) (6.0 Marks)

3.c. Write a c program to read and display the details of 3 employee's id and name
using array of structures. (6 Marks) (6.0 Marks)
3.d. Fill in the blank spaces to insert and display the content of the linked list for the
following code snippet. (8 Marks)
NODE* insertFront(NODE* head,int ele)
{
NODE* newNode=createNode(ele);
_______________________;
_______________________;
_______________________;
}
void display(NODE *head)
{
if___________________
printf("Empty List\n");
else
{
NODE *p=_____________;
while(________)
{
printf("%d ",_____________);
_____________;
}
}
} (8.0 Marks)

4.a. Brief about Error handling in C using errno and strerror. Write the C code
snippet for the same.(5 Marks)
(5.0 Marks)
4.b. What is the output?
#include<stdio.h>
#define sqr(x) x*x
#define cube(x) sqr(x)*x
#define MAX 5
int main()
{
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
#define MAX 3
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
#define MAX 2+2
printf("Square is %d\nCube is %d\n",sqr(MAX),cube(MAX));
return 0;
}

(6 Marks) (6.0 Marks)

4.c. Read the below code and fill the blanks. This program reads student name and
marks from the keyboard and write it into a file called "student.txt".
#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;

printf("Enter number of students: ");


scanf("%d", &num);

FILE *fptr;
fptr = _____________________;
if(__________________)
printf("Error!");

for(i = 0; i < num; ++i)


{
printf("For student%d\nEnter name: ", i+1);
_________________________;

printf("Enter marks: ");


_______________________________

fprintf(________________________________________________);;
}

fclose(___________);
return 0;
} (6 Marks) (6.0 Marks)
4.d. What are qualifiers? List the types of Qualifiers. Explain in detail the type
qualifiers. (1+2+5=8 Marks) (8.0 Marks)
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

PES University, Bengaluru


(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Dec 2024

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. #include<stdio.h>
int main()
{
int n = 100; // line #1
int b; // line #2
scanf("%d",&b); // line #3
printf("%d by %d is %d\n",n, b, n/b); //line #4
return 0; //line #5
}
Given the above code, answer these questions.
If user enters 4, what is the output?
If user enters 3, what is the output?
If user enters -4, what is the output?
If user enters 0, what is the output? (4.0 Marks)

1.b. Explain Program development Life Cycle of a C program with a neat


diagram (6.0 Marks)

about:blank 1/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

1.c. Separate the keyword/s and the variable/s from the set of identifiers
given below.
return main as _for (4.0 Marks)

1.d. Explain the switch construct in C with a coding snippet (6.0 Marks)

2.a. If the size of character is 1 byte, what is the output of below code
snippet?
int main()
{
char s[] = {'D', 'A', '\0', 'T', 'A', '\0'};
printf("%lu\n",sizeof(s));
printf("%d\n",strlen(s));
printf("%s\n",s);
printf("%s\n",&s[3]);
return 0;
} (4.0 Marks)

about:blank 2/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

2.b. What is an array in C? Write a code to define an array of 10 integers.


Store 10 numbers in this array by taking numbers from the user. Display
alternate numbers on the terminal. (6.0 Marks)

2.c. Name any four string manipulation functions available in string.h


library. (4.0 Marks)

2.d. What is a pointer in C? Bring the differences between an array and a


pointer. (6.0 Marks)

about:blank 3/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

3.a. Explain malloc and calloc functions of C with it’s syntax. (4.0 Marks)

3.b. struct node


{
int info;
char details[100];
struct node *next;
};
typedef struct node node_t;
node_t what(node_t*);
struct node type and the alias name is given. If there are five nodes
connected using the next pointer of each node to another node, write the
implementation of what function to return the last node. Function takes
the pointer to the first node as an argument. (6.0 Marks)

3.c. Consider the structure PLAYER as below.


#include<stdio.h>
#include<stdlib.h>
struct PLAYER
{ int id; char name[100]; };
i) Create a pointer to PLAYER by allocating memory dynamically.
ii) Assign a name to the PLAYER created above. (4.0 Marks)

about:blank 4/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

3.d. What is a queue? List the operations on Queue. (6.0 Marks)

4.a. struct student


{ int roll_no; char name[100]; int marks; };
Given a struct student type as above, explain compile time initialization of
array of structures. (4.0 Marks)

4.b. Explain any one sorting technique with an example code snippet.
(6.0 Marks)

about:blank 5/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

4.c. What is a callback in C? Explain with a sample code. (4.0 Marks)

4.d. Write a neat diagram to explain array of pointers to array of integers


with the sample code. (6.0 Marks)

5.a. Mention the four storage classes used in C Programming. (4.0 Marks)

about:blank 6/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)

5.b. Write a C program to copy the contents of one file to another with a
colon(:) appended at the end of each line. (6.0 Marks)

5.c. What is file inclusion directives? Which directive is used to do the


same. Mention the types of file inclusion directives.
(4.0 Marks)

5.d. Write the code to demo this – “ Assigning signed value to an Unsigned
variable for which bit fields are specified ” (6.0 Marks)

about:blank 7/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

PES University, Bengaluru


(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Dec 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Expected output og below code is 3 6 9


Fill up the blanks.
#include <stdio.h>
int main()
{
int i, j;
for (i = 3; -------; i++) {
for (j = -------; j <= 3; j++) {
printf("%d ", i*j);
}
}
return 0;
} (4.0 Marks)

1.b. Explain the switch construct in C with a coding snippet (6.0 Marks)

about:blank 1/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

1.c. Separate the keyword/s and the variable/s from the set of identifiers
given below.
chocolates print printf if (4.0 Marks)

1.d. Describe Program development Life Cycle of a C program with a neat


diagram having proper details of input and output for each phase.
(6.0 Marks)

1.e. Write a C program to read the characters from the user using getc
and display them on the screen using putc. The program should stop
reading when the user enters a newline. (5.0 Marks)

about:blank 2/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

2.a. If the size of character is 1 byte, what is the output of below code
snippet?
int main()
{
char s[] = {'D', 'A', '\0', 'T', 'A', '\0'};
printf("%lu\n",sizeof(s));
printf("%d\n",strlen(s));
printf("%s\n",s);
printf("%s\n",&s[3]);
return 0;
} (4.0 Marks)

2.b. What is an array in C? Write a code to define an array of 10 integers.


Store 10 numbers in this array by taking numbers from the user. Display
alternate numbers on the terminal. (6.0 Marks)

2.c. Explain enumerations in C with an example code. (4.0 Marks)

about:blank 3/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

2.d. Explain static storage class with an example code. (6.0 Marks)

2.e. Write the code to demo this – “ Assigning signed value to an Unsigned
variable for which bit fields are specified ” (5.0 Marks)

3.a. Explain malloc and calloc functions of C with it’s syntax. (4.0 Marks)

about:blank 4/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

3.b. struct node


{
int info;
char details[100];
struct node *next;
};
typedef struct node node_t;
node_t what(node_t*);
struct node type and the alias name is given. If there are five nodes
connected using the next pointer of each node to another node, write the
implementation of what function to return the last node. Function takes
the pointer to the first node as an argument. (6.0 Marks)

3.c. Consider the structure PLAYER as below.


#include<stdio.h>
#include<stdlib.h>
struct PLAYER
{ int id; char name[100]; };
i) Create a pointer to PLAYER by allocating memory dynamically.
ii) Assign a name to the PLAYER created above. (4.0 Marks)

3.d. What is a queue? List the operations on Queue.


(6.0 Marks)

about:blank 5/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

3.e. Describe Unions in C. Predict the outputs of below code.


#include <stdio.h>
union Data {
int i;
float f;
};
int main() {
union Data data;
data.i = 10;
printf("1. Integer: %d\n", data.i);
data.f = 3.14;
printf("2. Float: %.2f\n", data.f);
printf("3. Integer after Float assignment: %d\n", data.i);
return 0;
} (5.0 Marks)

4.a. With an example show that


a. a string can be defined using macros
b. a macro can be used in another macro (4.0 Marks)

4.b. Explain any one sorting technique with an example code snippet.
(6.0 Marks)

about:blank 6/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)

4.c. What is a callback in C? Explain with a sample code. (4.0 Marks)

4.d. Write a C program to copy the contents of one file to another with a
colon(:) appended at the end of each line. (6.0 Marks)

4.e. What is file inclusion directive? Which directive is used to do the


same. Mention the types of file inclusion directives.
(5.0 Marks)

about:blank 7/7
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

PES University, Bengaluru


(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - Jan - June 2024

UE23CS151B - Problem Solving With C

Total Marks : 100.0

1.a. i. Given a C code named test.c, answer the below questions by


writing the appropriate commands.

A) How do you get the output of preprocessing stage on the terminal?


B) How do you preprocess and compile the file test.c?
C) What is the result of preprocessing and compiling test.c file?
D) Is it possible to rename the executable file?

ii. Categorize the following words as variables or keywords.


A) short B) Print C) static D) count (6.0 Marks)

1.b. List three types of loops in C along with its syntax. (6.0 Marks)

1.c. Write a C program to find whether an integer number entered by the


user is even or odd using bitwise operator. Display appropriate message.
(4.0 Marks)

about:blank 1/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

1.d. i. Consider the following C code snippet. What will be the output
when the code below is executed?
#include <stdio.h>
int main()
{
int x = 5, y = 7, z;
z = x++ * --y;
printf("%d", z);
return 0;
}

ii. Fill up the blanks in the below code snippet to get the output as 1 2
2 4
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; _______ ;i++) {
for (j = 1; j <= 2; j++) {
printf("%d ", ___________ );
}
}
return 0;
} (4.0 Marks)

1.e. i)What is the output of the program given below if the user inputs the
character 'A' and presses Enter key while executing?
#include <stdio.h>
int main()
{
int result=getc(stdin);
printf("%d",result);
return 0;
}

ii. Mention the outputs of below code snippets separately.


(A) printf("%d", 4 && 3);
(B) printf("%d", -1?0:1);

iii. Say True or False

(A) The getchar function in C can be used to read a string of characters


until a newline character is encountered.
(B) The default case in a switch statement is mandatory.

(5.0 Marks)

about:blank 2/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

2.a. Write the definition of binarySearchRecursive function which takes


the sorted(ascending order) integer array 'arr' as it's first argument,
lowest index(0) as it's second argument, highest index(n-1) as it's third
argument where n is the number of elements in the array and the
element to be searched, 'key' as it's last argument. The function searches
for the key element recursively in the given array and if the search is
successful, returns the index of the element in an array, otherwise returns
-1. The function declaration for the same is as follows:
int binarySearchRecursive(int arr[], int left, int right, int key);
(6.0 Marks)

2.b. Give a brief note on the following storage class specifiers

A) static B) register (6.0 Marks)

2.c. In the code given below, if the address of arr[0][0] is 5000 and the size
of integer is 4 bytes, find the address of arr[1][0]. Also mention what gets
printed.

#include<stdio.h>
int main()
{
int arr[][2] = {2,5,4,7,9,1,90,67,23,75,86};
printf("%d",arr[1][0]);
return 0;
} (4.0 Marks)

about:blank 3/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

2.d. i.What gets printed when the below code runs?

enum days
{ sunday = -3, monday, tuesday };
int main()
{
enum days d;
printf("%d %d",d=monday, tuesday);
}

ii. A 2D array, 'matrix' represents the square matrix and 'n' is the number
of elements in the matrix. Fill up the blanks with an appropriate
expression so that the output would be the principal diagonal elements
printed.

Sample:
If the Matrix is as given below,
[178
546
923]

expected output is 1 4 3

include <stdio.h>
void printPrincipalDiagonal(int matrix[3][3], int n) {
for (int i = 0; _____________; i++) {
printf("%d ", _________);
}
printf("\n");
} (4.0 Marks)

about:blank 4/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

2.e. i.Consider the following function definition. What does this function
do?

#include <stdio.h>
void processArray(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
*(arr + i) = *(arr + i) * 2;
}
}

ii. Predict the output of the following code when it is run.


#include <stdio.h>
int main()
{
int arr[5] = {11, 21, 13, 44, 25};
int *ptr = arr + 2;
printf("%d", *ptr);
return 0;
} (5.0 Marks)

3.a. Explain briefly the following.


A) realloc B) Priority Queue (6.0 Marks)

about:blank 5/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

3.b. Create a structure called Emp_attendance with the following fields -


Emp_Id(whole number), Emp_name(string), no_days_present(whole
number). Choose appropriate data type for each field. Also provide C
statements for each of these.
A) Give an alias name called EMP_ATTD for the defined structure.
B )Declare a variable to hold maximum 100 employee details.
C) Declare a pointer to the above structure. Store the address of the
structure in this pointer. (6.0 Marks)

3.c. List any two string functions with its syntax.


(4.0 Marks)

3.d. What gets printed when the code below is executed?


#include<stdio.h>
int main()
{
char str[] = "PESU";
int i;
for(i=0; str[i]; i++)
printf("%c %c %c %c\n", str[i], *(str+i),
*(i+str), i[str]+1);
return 0;
} (4.0 Marks)

about:blank 6/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

3.e. What is a linked list? Consider the following structure for the node in
a linked list.

struct Node {
int data;
struct Node* next;
};

Write a function definition for PrintList, which takes the address of the
first node as an argument to print all the nodes in a sequence.
Function declaration is as follows:
void printList(struct Node* node) ; (5.0 Marks)

4.a. Write a C program that counts the total number of lines in a CSV
file named "data.csv". Assume that the file data.csv is available to the
program and maximum number of characters in each line is 200.
Display appropriate message. (6.0 Marks)

4.b. With an example code, show that

A) A string can be defined using macros


B) A macro can be used in another macro (6.0 Marks)

about:blank 7/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)

4.c. Define conditional compilation. Write the output of below code when
it is run.
#include <stdio.h>
int main()
{
#ifndef CHECK
#define CHECK 10
printf("%d\n", CHECK);
#endif
#ifdef CHECK
printf("%d", CHECK+2);
#endif
printf("\npreprocessing successful");
return 0;
} (4.0 Marks)

4.d. i) List any two error handling functions related to file handling.

ii) Say True or False

A) The fprintf function reads a data from a file.


B) The mode "w" in fopen(), creates a file if it is not existing (4.0 Marks)

4.e. What is callback in C ? Give an example code. (5.0 Marks)

about:blank 8/8
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - JULY - 2023

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Categorize the following into valid and invalid variables in C language. If invalid,
give reasons for invalidity.
i) num of digits (ii) %avg (iii) double (iv)1stnum (4.0 Marks)

1.b. i) Predict the output of the below code: (2M)


#include<stdio.h>
int main()
{
int i=0;
for(;i<=5;)
{
i++;
printf("%d ", i);
}
return 0;
}
ii) Predict the output of the below code: (2M)
#include<stdio.h>
int main()
{
int h=5;
int b=5*2<h*2?1:0;
printf("%d\n",b);
return 0;
}
iii) State true or false: (1M)
Statements inside a while loop are executed at least once. (5.0 Marks)
1.c. Write a C program to find and display the sum of first n natural numbers
using: i) while loop (ii) for loop (iii) do while loop
Note: Natural numbers are integers beginning from 1. (7.0 Marks)

1.d. Write a C program to read a 3 digit integer through keyboard and check if the
middle digit in it is odd. If it is odd print 1 else print 0. (4.0 Marks)

2.a. What is an array? What the following function does? What is the output of the
following function for the input: a[]={20,50,10,30} and n=4.
int what(int a[],int n)
{
int res=a[0];
for(int i=1;i<n;i++)
{
if(a[i]<res)
res=a[i];
}
return res;
} (4.0 Marks)

2.b. Explain the terms Function declaration, Function definition, Argument,


Parameter with the help of an example. (6.0 Marks)
2.c. An integer sequence is defined as follows: First number in the sequence is 1
and second number in the sequence is 2. Third number onwards the sequence is
obtained by adding the previous 2 numbers. Write a recursive C function to take
the value of n as input and return back the nth number in such a sequence, where
n>=1. (4.0 Marks)
2.d. Predict the output of the below code: (2M * 3 = 6M)
i)
#include <stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6};
int i, j, k;
j=++arr[2];
k=arr[1]++;
i=arr[j++];
printf("i=%d, j=%d, k=%d",i,j,k);
return 0;
}
ii)
#include<string.h>
int main()
{
char str1[] = "University";
char str2[20];
char str3[] = "PES";
int i = strcmp(strcat(str3, strcpy(str2, str1)), "PESUniversity");
printf("%d\n",i);
return 0;
}
iii)
#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "PESUB";
char t;
int i,j=strlen(p)-1;
for(i=0; i<=j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
p[i]='\0';
printf("%s\n",p);
return 0;
} (6.0 Marks)
3.a. Write a C function to subtract 2 square matrices. (4.0 Marks)

3.b. What is a Structure? Give an example.


Create an array of strings named months which can hold the first 3 letters of all the
12 months. (4.0 Marks)

3.c. Explain malloc and free functions in C. (4.0 Marks)


3.d. Consider the following node structure:
typedef struct node
{
int info;
struct node *next;
}NODE;

Implement the following functions:


void displayList(NODE* head);
displayList takes a pointer to the first node if the list is non empty and prints all the
elements in the linked list and returns nothing. If the list is empty then prints
"Empty list".

NODE* destroyList(NODE *head);


destroyList takes a pointer to the first node if the list is non empty and deletes all
the dynamically allocated nodes and returns NULL to set the head pointer to NULL
in the calling function. If the list is empty then just returns back NULL. (8.0 Marks)

4.a. Write a C program to copy contents of one file to another file using fgetc() and
fputc(). (5.0 Marks)

4.b. Write a C function named binarySearchIterative which takes an integer array


'a' sorted in ascending order, the no. of elements 'n' and the element to be
searched 'key'.
Searches for the key element iteratively in the given array and if the search is
successful then it returns back the index of the element in array, otherwise returns
-1.
The function declaration for the same is as follows:
int binarySearchIterative(int a[],int n,int key); (5.0 Marks)
4.c. Write a C function to sort the integer data in descending order using selection
sort. (5.0 Marks)

4.d. What is a callback function? How is it implemented in C language. Write the


declaration of a function pointer 'fp' which takes 2 float values as parameters and
returns back a float.
Write the output of the below code:
#include<stdio.h>
int what(int x, int y, int (*op)(int,int))
{
return op(x, y);
}
int add(int x, int y)
{
return x + y;
}
int div(int x, int y)
{
return x / y;
}

int main()
{
printf("Result=%d\n", what(1, 3, add));
printf("Result=%d\n", what(4, 2, div));
return 0;
} (5.0 Marks)

5.a. i) Mention any two differences between structure and union.


ii) State true or false: enum names are automatically assigned values if no value is
specified.
iii) What is the range of integer values (lowest and highest value) that the member
c of bitFieldDemo structure can hold?
struct bitFieldDemo
{
unsigned int c:4;
}; (5.0 Marks)
5.b. List 4 different storage classes in C. Explain the storage class auto and extern.
(5.0 Marks)

5.c. Predict the output of the below code:


#include<stdio.h>
#define sqr1(x) x*x
#define sqr2(y) (y)*(y)
int main()
{
printf("%d ",sqr1(2+2));
printf("%d\n",sqr2(2+3));
return 0;
} (4.0 Marks)

5.d. i) What is conditional compilation? List any 2 conditional compilation


directives.
Assume size of int is 4 bytes, char is 1 byte, double is 8 bytes.
ii) What is the output of the below code?
#include<stdio.h>
#pragma pack(1)

struct demo
{
int a;
char b;
double d;
};
int main()
{
printf("Size of structure is %lu bytes\n",sizeof(struct demo));
return 0;
} (6.0 Marks)
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - JULY - 2023

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Draw a neat picture and explain each step in the Program Development Life
Cycle (PDLC) of a C program. (6.0 Marks)

1.b. Mention the output of the following code pieces individually.

i) int n=7980; printf("%d", printf("%d", printf("%d", n) ) );


ii) int case = 8; printf(“%d”,case);
iii) printf("%d", -4?0:4);
iv) int a; printf("%d",a = 2 | (7 == 7 == 7) ); (4.0 Marks)

1.c. Write a C Program to count the number of digits in a number taken through
user input and also check if the individual digits are even or odd.
Display appropriate messages.

Sample output:
Enter a number: 12345
5 is odd
4 is even
3 is odd
2 is even
1 is odd
The number of digits in the number is :5 (5.0 Marks)
1.d. i) State True or False :
a) ++ is a unary operator in C.
b) C is an interpreted language.
c) There can be multiple a.exe files in one folder.

ii) How many bytes does sizeof(‘\r’) occupy?

iii) Scanf() instead of scanf() leads to ________ error. (5.0 Marks)

2.a. Write a C function my_strcat() that accepts two strings as arguments, which
are taken as user input, and emulates strcat() in the string.h file.
Test this function with the client code.

Sample Output:
Enter str1:
Exam
Enter str2:
Over
str1 is ExamOver and str2 is Over (6.0 Marks)

2.b. Find the output of the following program.

#include<stdio.h>
int main()
{
char str[] = "PESU";
int i;
for(i=0; str[i]; i++)
printf("%c %c %c %c\n", str[i], *(str+i), *(i+str), i[str]+2);
} (4.0 Marks)
2.c. i)Define Recursion?

ii) Find the output of the following C code.


#include<stdio.h>
int what(int num,int res);
int main()
{
int a = 151;
printf("%d\n",what(a,0));
return 0;
}

int what(int num,int res)


{
if(num==0)
return res;
else
return what(num/10,res+(num%10));
} (4.0 Marks)

2.d. Write a C function that returns the smallest element from an integer array arr
with n elements. In the main function, call the function to the test.

For Example: If the array elements are {9,7,5,3,10,12,5}, the function returns the
smallest element of the array, which is 3 in this case. (6.0 Marks)
3.a. i) Find the output of the below C program. // 2 marks

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p1 = (int*)malloc(sizeof(int));
*p1 = 250;
printf("%d ", *p1);
int *p2 = p1;
printf(" %d ", *p2);
*p2 = 999;
printf("%d ",*p1);
printf("%d ",*p2);
free(p1);
p1 = NULL;
p2 = NULL;
}

ii) If the same pointer variable is allocated memory more than once using the
dynamic memory allocation functions, initially allocated memory space becomes a
_________.
Garbage which has no name and hence no access in turn results in __________ //
2 marks
(4.0 Marks)

3.b. The below C program has errors. Recognise the errors and write a correct
program to produce the output 1001 and XYZ separated by a tab space.

#include<stdio.h>
#include<stdlib.h>

struct Student
{
int roll_no;
char name[100];
};
int main()
{
struct Student s;
s = malloc(1,sizeof(struct Student));
s.roll_no = 1001;
s->name = "XYZ";
printf("%d\t%s\n",s.roll_no,s.name);
return 0;
} (5.0 Marks)
3.c. In ABC company there are 3 salesmen. Each salesman sells 2 items.
Write a C program using two dimensional arrays to display the total sales of
each item.

Sample Output:
Enter the data:
Enter the sales of 2 items sold by the sales man: 0
23 45
Enter the sales of 2 items sold by the sales man: 1
20 40
Enter the sales of 2 items sold by the sales man: 2
10 15
Total sales of each item 0 = 53
Total sales of each item 1 = 100 (5.0 Marks)

3.d. Given the structure declaration and the client code, define the function
insert_front to add nodes to the beginning of the linked list and define the display
function as well to print the data in the nodes.

Sample Output:
Enter the element:
10
Enter the element:
20
Enter the element:
30
30 20 10

typedef struct node


{
int data;
struct node *link;

}node;

int main()
{
int element;
node *head = NULL;
for(int i=0;i<3;i++)
{
printf("Enter the element:\n");
scanf("%d",&element);
head = insert_front(head,element);
}
display(head);
} (6.0 Marks)
4.a. The file test.txt exists with some data. Write a C Program to find the length of
the text file using fseek() and ftell() functions. (5.0 Marks)

4.b. Given a sorted array of integers, write a function which searches for a given
integer using binary search and returns the index of it, returns -1 otherwise.
Use the below function declaration to define the function.

int binary_search(int *array, int size, int value);

array: pointer to the array where the value has to be searched.


value : element to be searched
size : size of the array (6.0 Marks)

4.c. i) Find the output of the below code.


#include <stdio.h>
int main()
{
int arr[6] = {10,7,11,18};
int *arrp[10];
arrp[9] = &arr[3];
printf("%d ",*arrp[9]);
arrp[11] = &arr[5];
printf("%d\n",*arrp[11]);
}

ii) Find the output of the following code


#include <stdio.h>
#include<string.h>
int main()
{
char line[100]="PESU,EC,RR";
printf("%s ",strtok(line,","));
printf("%s ",strtok(NULL,","));
printf("%s ",strtok(line,","));
} (4.0 Marks)
4.d. Define callbacks in C with an example program. (2 marks -Definition, 3
marks - example program) (5.0 Marks)

5.a. Give brief notes on the following keywords with suitable code snippets:
i) volatile
ii) extern (4.0 Marks)

5.b. Find the output of the following program.

#include <stdio.h>
enum marks{Phy=1,Chem=4,Maths,Comp};
int main()
{
enum marks m;
m=Maths;
printf("%d ",Chem);
switch(m)
{
case Phy:printf("Physics");break;
case Chem:printf("Chemistry");break;
case Maths:printf("Maths");break;
case Comp:printf("Computers");break;
}
printf(" %d ",Comp);
printf("%d ",Phy);
} (4.0 Marks)
5.c. Find the output of the following C programs.
i)
#include<stdio.h>
int main()
{ char a = 'z'; char b = 'w'; const char *c = &b; *c = 'x';
printf("%c",*c); return 0;
}

ii)
#include<stdio.h>
int main()
{ int i = 333; int j = 666; int* const p = &i; *p = 555;
printf("%d\n",*p); return 0;}

iii)
#include<stdio.h>
int main()
{ printf("%d",sizeof(long) >= sizeof(int)); return 0; }

iv)
#include<stdio.h>
#include<stddef.h>
union A
{ int x; float y; char z; };
int main()
{ printf("%lu ",offsetof(union A,z));
}

v)
#include<stdio.h>
void fun();
int main()
{ fun(); fun(); return 0; }
void fun()
{ static int a = -1; a--; printf("%d\t",a); } (6.0 Marks)

5.d. i) Mention any 3 differences between unions and structures

ii) Find the output of the following code.

#include<stdio.h>
#define MAX 5
#define fun(a,b) a*b
int main()
{
printf("%d\t",fun(MAX,3+6));
#undef MAX
int MAX = 35;
printf("%d\t",MAX);
#define MAX 22
printf("%d",MAX);
}
(6.0 Marks)
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)

END SEMESTER ASSESSMENT (ESA) - JULY - 2023

UE22CS151B - Problem Solving With C

Total Marks : 100.0

1.a. Draw a clear picture that shows the Program Development Life Cycle (PDLC) of
a C Program. (4.0 Marks)

1.b. Mention the outputs of below code snippets separately.

i) int n=559, a; printf("%d", a = printf("%d", a = printf("%d", n) ) );

ii) int auto = 8; printf("%d",auto);

iii) printf("%d", -10?10:0);

iv) int a; printf("%d",a = 6 | (8 == 8 == 8));

v) int c=11,d7; printf("%d",c);

vi) int a = 100; a == 100 || ++a == 101; printf("%d", a); (6.0 Marks)

1.c. Write a C Program to count the number of digits in a number taken through
user input and also print the reverse of the number. Print the count of digits in the
number as well.

Sample output:
Enter a number: 1234
The reverse of the number is 4321.
The number has 4 digits. (5.0 Marks)
1.d. i) State True or False:
a) ** is an operator in C.
b) C is both compiled and interpreted language.
c) There are multiple a.exe files in one folder.

ii) How many bytes does sizeof(‘\n’) occupy?

iii) Printf() instead of printf() leads to ________ error. (5.0 Marks)

2.a. Write a C function my_strcpy() that accepts two strings as arguments, and
emulates strcpy() in the string.h file. Test this function with the client code

Sample Output:
Enter Str1:
Exam Over
Str1: Exam Over
Str2: Exam Over (6.0 Marks)

2.b. Find the output of the following program.

#include<stdio.h>
int main()
{
char str[] = "BEST";
int i;
for(i=0; str[i]; i++)
printf("%c %c %c %c\n", str[i], *(str+i), *(i+str), i[str]+2);
} (4.0 Marks)
2.c. i) Define pointers with an example program.

ii) Find the output of the following C code


#include<stdio.h>
int what(int num,int res);
int main()
{
int a = 121;
printf("%d\n",what(a,0));
return 0;
}
int what(int num,int res)
{
if(num==0)
return res;
else
return what(num/10,res+(num%10));
} (4.0 Marks)

2.d. Write a C function that returns the biggest element from an integer array arr
with n elements. In the main(), call the function to the test.

For Example: If the array elements are {9,4,5,7,2,3,19,6,1}, the function returns
the biggest element which is 19 in this case. (6.0 Marks)
3.a. i) Find the output of the following code.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p1 = (int*)malloc(sizeof(int));
*p1 = 300;
printf("%d ", *p1);
int *p2 = p1;
printf(" %d ", *p2);
*p2 = 777;
printf("%d ",*p1);
printf("%d",*p2);
free(p1);
p1 = NULL;
p2 = NULL;
}

ii)
To avoid dangling pointers after free() is used, ________ is assigned to the pointer.
Dereferencing a NULL pointer results in __________. (4.0 Marks)

3.b. The expected output is 5001 and Cricket separated by a tab space. Find
the errors in the below program and write a correct version of the program
to get the expected output.

#include<stdio.h>
#include<stdlib.h>

struct SPORT
{
int s_no;
char sport_name[100];
};
int main()
{
struct SPORT sp;
sp = calloc(sizeof(struct SPORT));
sp.s_no = 5001;
sp->sport_name = "Cricket";
printf("%d\t%s\n",sp.s_no,sp.sport_name);
return 0;
} (5.0 Marks)
3.c. In XYZ company there are 3 salesmen. Each salesman sells 2 items. Write a C
program using two dimensional arrays to display the total sales by each
salesman.

Sample output:
Enter the data:
Enter the sales of 2 items sold by the sales man: 0
10 10
Enter the sales of 2 items sold by the sales man: 1
20 20
Enter the sales of 2 items sold by the sales man: 2
30 30
Total sales by salesman 0 = 20
Total sales by salesman 1 = 40
Total sales by salesman 2 = 60 (5.0 Marks)

3.d. Given the structure declaration and the client code, define the function
insert_end to add nodes to the end of the linked list and define the display
function as well to print the data in the nodes.

Sample output:
Enter the element:
30
Enter the element:
20
Enter the element:
10
30 20 10

typedef struct Node


{
int data;
struct Node *link;
}Node;

int main()
{
Node *head = NULL;
int element;
for(int i=0;i<3;i++)
{
printf("Enter the element:\n");
scanf("%d",&element);
head = insert_end(head,element);
}
display(head);
}
(6.0 Marks)
4.a. The file sample.txt exists with some data. Write a C program to find the
number of the characters in the text file using fgetc() function. (5.0 Marks)

4.b. Given a sorted array of integers, write a recursive function which searches for
a given integer using binary search approach and returns the index of it, returns -1
otherwise. Use the below function declaration to define the function.

int binary_search(int *array, int key, int start, int end);

array: pointer to the array where the key has to be searched.


key : element to be searched
start: 0
end : size of the array – 1 (6.0 Marks)

4.c. i) A function opr takes two integer parameters and returns an integer. Write a
valid function pointer declaration that can store the address of opr.

ii)
Find the output of the following code.
#include <stdio.h>
#include<string.h>
int main()
{
char line[100]="Hello,All,Friends";
printf("%s ",strtok(line,","));
printf("%s ",strtok(NULL,","));
printf("%s ",strtok(line,","));
} (4.0 Marks)
4.d. Write short notes on array of pointers to structures with an example program.
(2 marks - Definition, 3 marks - Example program)
(5.0 Marks)

5.a. Give brief notes on the following keywords suitable code snippets.
i) volatile
ii) register (4.0 Marks)

5.b. Find the output of the following.

#include <stdio.h>
enum cars{TATA=1,BMW=4,KIA,MG=7};
int main()
{
enum cars c;
c=KIA;
printf("%d ",KIA);
switch(c)
{
case TATA:printf("TATA");break;
case BMW:printf("BMW");break;
case KIA:printf("KIA");break;
case MG:printf("MG");break;
}
printf(" %d ",TATA);
printf("%d ",MG);
} (4.0 Marks)
5.c. Find the output of the following C programs.
i)
#include<stdio.h>
int main()
{ char a = 'q'; char b = 'p'; const char *c = &b; *c = 'z';
printf("%c",*c); return 0;
}

ii)
#include<stdio.h>
int main()
{ int i = 999; int j = 777; int* const p = &i; *p = j;
printf("%d\n",*p); return 0;}

iii)
#include<stdio.h>
int main()
{ printf("%d",sizeof(short) >= sizeof(int)); return 0; }

iv)
#include<stdio.h>
#include<stddef.h>
union A
{ double x; int y; float z; };
int main()
{ printf("%lu ",offsetof(union A,z));
}

v)
#include<stdio.h>
void fun();
int main()
{ fun(); fun(); return 0; }

void fun()
{ static int a = -5; a--; printf("%d\t",a); } (6.0 Marks)

5.d. i) Mention any three preprocessor directives with its purpose.

ii) Find the output of the following code.

#include<stdio.h>
#define MAX 6
#define fun(a,b) a*b
int main()
{
printf("%d\t",fun(MAX,7+7));
#undef MAX
int MAX = 77;
printf("%d\t",MAX);
#define MAX 9
printf("%d",MAX);
} (6.0 Marks)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy