ESA Programming With C Paper 1 12
ESA Programming With C Paper 1 12
1.a. Explain the switch construct in C with a coding snippet. (4.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)
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.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)
about:blank 3/7
11/8/24, 12:59 PM ESA - DEC 2023 - UE22CS151B (set- 1)
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.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)
about:blank 7/7
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)
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)
4.a. Create a student structure and add a neat diagram to describe array of
structures (4.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)
1.a. Describe the phases involved in the Program Development Life Cycle of a C
program with a neat diagram. (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;
}
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
(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;
}
#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)
#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.
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)
#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)
1.a. i. Given a C code named test.c, answer the below questions by writing the
appropriate commands.
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;
}
(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.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;
}
}
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.d. i) List any two error handling functions related to file handling.
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;
}
(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 }
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()
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)
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.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;
}
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;
FILE *fptr;
fptr = _____________________;
if(__________________)
printf("Error!");
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)
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)
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)
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)
about:blank 4/7
3/25/25, 4:08 PM ESA -Dec 2024 - UE22CS151B (set- 1)
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)
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.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)
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.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)
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)
about:blank 5/7
3/25/25, 3:53 PM ESA - Dec 2024 - UE23CS151B(set- 1)
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.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)
about:blank 7/7
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)
1.b. List three types of loops in C along with its syntax. (6.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;
}
(5.0 Marks)
about:blank 2/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)
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)
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;
}
}
about:blank 5/8
3/27/25, 11:46 AM ESA June 2024 - UE23CS151B (set- 2)
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)
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.
about:blank 8/8
PES University, Bengaluru
(Established under Karnataka Act 16 of 2013)
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.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)
4.a. Write a C program to copy contents of one file to another file using fgetc() and
fputc(). (5.0 Marks)
int main()
{
printf("Result=%d\n", what(1, 3, add));
printf("Result=%d\n", what(4, 2, div));
return 0;
} (5.0 Marks)
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)
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.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.
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)
#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?
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
}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.
5.a. Give brief notes on the following keywords with suitable code snippets:
i) volatile
ii) extern (4.0 Marks)
#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)
#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)
1.a. Draw a clear picture that shows the Program Development Life Cycle (PDLC) of
a C Program. (4.0 Marks)
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.
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)
#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.
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
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.
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)
#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)
#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)