EST 102 (Model Question Paper) Programming in C (Common To All Programs) - Abhinand TJ
EST 102 (Model Question Paper) Programming in C (Common To All Programs) - Abhinand TJ
TECH DEGREE
EXAMINATION,
Max.Marks:100
(Part A)
Answer:- Processor, also known as a “Microprocessor“. It is a small electronic chip that is placed inside
the computers and other electronics components. Processors can manage all instructions such as
arithmetical, logical, input/output (I/O) and other basic instructions, which are created by hardware or
operating system. Its main role is to obtain input data from input devices, perform computations and then
display accurate results on the output devices. Nowadays, more advanced processors are available in
the market, which are capable of controlling trillions of instructions per second. Processors are used in
PCs as well as they can be used into other electronic devices such as smartphones, tablets, PDA etc.,
● Registers, and
results are sent to the registers, which also store instructions. Caches are small and fast memories that
store copies of data for frequent use, and act similarly to a random access memory (RAM).
The CPU carries out its operations through the three main steps of the instruction cycle: fetch, decode,
and execute.
● Decode: a decoder converts the instruction into signals to the other components of the computer.
● Execute: the now decoded instructions are sent to each component so that the desired operation
can be performed.
Memory:- Memory is just like a human brain. It is used to store data and instructions. Computer
memory is the storage space in the computer, where data is to be processed and instructions
● Cache Memory
● Secondary Memory
Cache memory is a very high speed semiconductor memory which can speed up the CPU. It acts as a
buffer between the CPU and the main memory. It is used to hold those parts of data and programs
which are most frequently used by the CPU. The parts of data and programs are transferred from the
disk to cache memory by the operating system, from where the CPU can access them.
Primary Memory (Main Memory) (volatile memory)
Primary memory holds only those data and instructions on which the computer is currently working. It
has a limited capacity and data is lost when power is switched off. It is generally made up of
semiconductor devices. These memories are not as fast as registers. The data and instruction required
to be processed resides in the main memory. It is divided into two subcategories RAM and ROM.
Secondary Memory
This type of memory is also known as external memory or non-volatile. It is slower than the main
memory. These are used for storing data/information permanently. CPU directly does not access these
memories, instead they are accessed via input-output routines. The contents of secondary memories
are first transferred to the main memory, and then the CPU can access it. For example, disk, CD-ROM,
DVD, etc.
2. What are the differences between compiled and interpreted languages? Give example for each.
(3 Marks)
Answer:
No
typically compilers and not interpreters. instructions directly and freely, without previously
compiling a program into machine-language
instructions.
2 In this language, once the program is In this language, the instructions are not directly
3 There are at least two steps to get from There is only one step to get from source code to
4 In this language, compiled programs run While in this language, interpreted programs can be
5 In this language, compilation errors prevent In these languages, all the debugging occurs at
6 The code of compiled language can be A program written in an interpreted language is not
7 This language delivers better performance. This language delivers relatively slower
performance.
3. Write a C program to read a Natural Number through keyboard and to display the reverse
of the given number. For example, if “3214567” is given as input, the output to be shown is
“7654123”. (3 Marks)
#include<stdio.h>
//#include<conio.h>
main()
int a, b, s=0;
//clrscr();
scanf("%d",&a);
while(a>0)
b=a%10;
s=(s*10)+b;
a=a/10;
return;
Answer: A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled
NOTE − Use of goto statements is highly discouraged in any programming language because it makes
it difficult to trace the control flow of a program, making the program hard to understand and hard to
modify. Any program that uses a goto can be rewritten to avoid them.
Syntax
goto label;
..
label: statement;
Here the label can be any plain text except C keyword and it can be set anywhere in the C program
Flow Diagram
Example:
#include<stdio.h>
int main()
printf("enter a number\n");
scanf("%d", &number);
temp_num=number;
START: digit=number%10;
rev=rev*10+digit;
number=number/10;
if( number > 0)
goto START;
Output:
12345
Input number=12345
Reversed number=54321
5. Explain the different ways in which you can declare & initialize a single dimensional array.
(3 Marks)
Answer: Initializing one dimensional array Assigning some value to the variable that undergoes
processing.
2 4 6 12
In the first eg: the array age is initialized for its 6 elements. The size is 6. In the second eg: the
6. Write a C program to read a sentence through keyboard and to display the count of white spaces
Answer:
#include<stdio.h>
int main()
char a[60];
int i=0,count=0;
printf("Enter the string\n");
gets(a);
puts(a);
while(a[i]!='\0')
if(a[i]==' ')
count++;
i++;
Output:
now a days people have got used to staying in home for long durations
now a days people have got used to staying in home for long durations
Breaking the code in smaller functions keeps the program organized, easy to understand and
makes it reusable.
● The C compiler follows top-to-down execution, so the control flow can be easily managed in case
of functions. The control will always come back to the main() function.
● In case we need to test only a particular part of the program we will have to run the whole program
and figure out the errors which can be quite a complex process. Another advantage here is that
functions can be individually tested which is more convenient than the above mentioned process.
● A function can be used to create our own header file which can be used in any number of
8. With a simple example program, explain scope and lifetime of variables in C. (3 Marks)
Answer: Storage classes specify the scope, lifetime and binding of variables.
To fully define a variable, one needs to mention not only its ‘type’ but also its storage class.
A variable name identifies some physical location within computer memory, where a collection of bits are
Lifetime
The lifetime of a variable defines the duration for which the computer allocates memory for it (the
● Automatic − The auto storage class is the default for variables declared inside a block. A
variable x that has automatic storage is deleted when the block in which x was declared exits.
● Static − A variable is created when the declaration is executed for the first time. It is destroyed
● Dynamic − The variables memory is allocated and deallocated through memory management
functions.
Storage Classes
Class Area
Automatic Memory Till control remains in block Till control remains in block Local Auto
Register CPU Garbage value Till control remains in block Local Register
register
Static Memory Zero Value in between function Local Static
calls
execution
#include<stdio.h>
int main()
printf ("%d\n",i);
printf("%d\n", i);
printf("%d\n", i);
}
Output: 321
#include<stdio.h>
int main()
printf ("%d\n",i);
fun ();
fun ()
Output:
3
9. Write a function in C which takes the address of a single dimensional array (containing finite
sequence of numbers) and the number of numbers stored in the array as arguments and stores
the numbers in the same array in reverse order. Use pointers to access the elements of the array.
(3 Marks)
Answer:
#include <stdio.h>
int main()
int arr[100];
int size;
int *right;
scanf("%d", &size);
right = &arr[size - 1]; // Pointer to arr[size - 1]
scanf("%d", left++);
printArr(arr, size);
*left ^= *right;
*right ^= *left;
*left ^= *right;
left++;
right--;
return 0;
Output:
10. With an example, explain the different modes of opening a file. (3 Marks)
#include<stdio.h>
#include<conio.h>
main()
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
putc(ch,fp);
fclose(fp);
fp = fopen("hello.txt", "r");
printf("%c",ch);
fclose(fp);
Part B
Answer any one Question from each module. Each question carries 14 Marks
11. (a) Draw a flow chart to find the position of an element in a given sequence, using linear
searching technique. With an example explain how the flowchart finds the position of a
Answer:
#include <stdio.h>
void main()
int num;
scanf("%d", &num);
int array[num];
scanf("%d", &array[i]);
}
scanf("%d", &keynum);
if (keynum == array[i] )
found = 1;
break;
if (found == 1)
else
Output:
Flow Chart
(b) Write a pseudo code representing the flowchart for linear searching. (4 Marks)
Answer:
Step 3: Initialize for loop with index variable i=0 and final value of size of array with step increment
Step 6: In function to search for the key element using if condition (keyele==arr[i])
Step 8: if found =1, print the array element is found with the index number or position number.
Step 9: if key element is not found print the element is not found
12. (a) With the help of a flow chart, explain the bubble sort operation. Illustrate with an example. (10
Marks)
#include<stdio.h>
#include<conio.h>
main()
int a[15], i, j, t, n;
//clrscr();
scanf("%d",&n);
printf("enter the numbers \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
Output:
12 3 0 25 7
0 3 7 12 25
Flow Chart:
12. (b) Write an algorithm representing the flowchart for bubble sort. (4 Marks)
Answer:
Step 8: Stop
13. (a) Write a C program to read an English Alphabet through keyboard and display whether
Answer:
#include<stdio.h>
int main()
char ch;
scanf("%c", &ch);
else
return (0);
13. (b) Explain how one can use the builtin function in C, scanf to read values of different data
types. Also explain using examples how one can use the builtin function in C, printf for text formatting.
(8 Marks)
Answer: A function in which a prototype already predefined in the library is called a built in function.
There is no need to write a program for calling library functions or built in functions.
The name of the library function cannot be changed as its prototype is already predefined in the compiler.
Library functions are used in each and every program irrespective of complexity.
The library functions like scanf and printf are already present in the header file #include<stdio.h>
. To use these functions we need to include the header file in our program. For example,
If you want to use the printf() function, which is a formatted output statement, the header file <stdio.h>
should be included.
The printf statements used in different data types used are as given below.
Example:
#include <stdio.h>
int main()
printf("C Programming");
return 0;
Output
C Programming
#include <stdio.h>
int main()
int testInteger = 5;
return 0;
Output
Number = 5
We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the
value of testInteger.
int main()
return 0;
Output
number1 = 13.500000
number2 = 12.400000
To print float, we use %f format specifier. Similarly, we use %lf to print double values.
#include <stdio.h>
int main()
return 0;
Output
character = a
If we try to use printf() without including the stdio.h header file, we will get an error.
scanf()
In C programming, scanf() is one of the commonly used function to take input from the user. The scanf()
function reads formatted input from the standard input such as keyboards.
For the formatted input statement scanf() statement also we need to use the #include<stdio.h> header
file.
#include <stdio.h>
int main()
int testInteger;
printf("Number = %d",testInteger);
return 0;
Output
Enter an integer: 4
Number = 4
In this case also if we use scanf statement without the #include<stdio.h> header file, we get an error.
Here, we have used %d format specifier inside the scanf() function to take int input from the user. When
We have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and
#include <stdio.h>
int main()
float num1;
double num2;
scanf("%lf", &num2);
return 0;
Output
num1 = 12.523000
num2 = 10.200000
We use %f and %lf format specifiers for float and double respectively.
C Character I/O
#include <stdio.h>
int main()
char chr;
return 0;
Output
Enter a character: g
You entered g
When a character is entered by the user in the above program, the character itself is not stored. Instead,
And when we display that value using %c text format, the entered character is displayed. If we use %d to
The printf() lies in its formatting string. That text can be packed with plain text, escape sequences, and
● Arithmetic operators
● Relational operators
● Logical operators
● Bitwise operators
● Assignment operators
● Conditional operators
● Special operators
Arithmetic operators
C supports all the basic arithmetic operators. The following table shows all the basic arithmetic operators.
Operator Description
% remainder of division
Relational operators:
Operator Description
== Check if two operand are equal
> Check if operand on the left is greater than operand on the right
Logical operators
|| Logical OR (a || b) is true
Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits
from right to left. Bitwise operators are not applied to float or double.
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
Assignment Operators
operand
+= adds right operand to the left operand and assign the a+=b is same as
-= subtracts right operand from the left operand and a-=b is same as
/= divides left operand with the right operand and assign a/=b is same as
%= calculate modulus using two operands and assign the a%=b is same
Conditional operator
1. Ternary Operator
2. ? : Operator
It is actually the if condition that we use in C language decision making, but using the conditional
operator, we turn the if condition statement into a short and simple operator.
Explanation:
● If (expression 1) returns true then the expression on the left side of " : " i.e (expression 2) is
executed.
● If (expression 1) returns false then the expression on the right side of " : " i.e (expression 3) is
executed.
Special operator
sizeof Returns the size of an variable sizeof(x) return size of the variable x
variable
14. (b) Explain how characters are stored and processed in C. (4 Marks)
Answer: Computers work in binary . As a result, all characters, whether they are letters, punctuation or
digits, are stored as binary numbers. All of the characters that a computer can use are called as the
ASCII character set. The ASCII character set is a set of numbers from 0 .. 255, where each number in
the set represents a unique character. For example, the number 48 is used to represent the digit '0', 65
ASCII 32, the tab character is ASCII 9 , and the carriage return is represented by 13. When we look at a
text file, every symbol seen and every non-visible formatting technique is represented by an ASCII
character. In what follows the assumption is made that data is being read from the keyboard.
Every keystroke made is recorded in an area of memory called the keyboard buffer. This is where data
is held until needed by a program. When an input command such as getchar or scanf appears in a C
program, the program will first check the keyboard buffer to see if any data is present. If data is present,
the program will extract the data out of the keyboard buffer. In this way the input command is satisfied.
If there is no data in the buffer, the program will pause and wait for the user to type something.
15. (a) Write a function in C which takes a 2-Dimensional array storing a matrix of numbers and
the order of the matrix (number of rows and columns) as arguments and displays the sum
Answer:
#include<stdio.h>
int i, j;
for(i = 0; i < rows; i++)
int Sum = 0;
int main()
scanf("%d", &a[i][j]);
return 0;
Output:
11 22
33 44
Answer: A matrix having non-zero elements only in the diagonal running from the upper left to the lower
#include<stdio.h>
int main()
int row_size,col_size;
scanf("%d",&row_size);
scanf("%d",&col_size);
int matrix[row_size][col_size];
int i,j;
printf("Enter the Matrix Element:\n");
for(i=0;i<row_size;i++)
for(j=0;j<col_size;j++)
scanf("%d",&matrix[i][j]);
int point=0;
for(i=0;i<row_size;i++)
for(j=0;j<col_size;j++)
{
point=1;
break;
if(point==1)
else
Output:
100
020
003
16. (a) Without using any builtin string processing function like strlen, strcat etc., write a
Answer:
#include <stdio.h>
int main()
int i,j;
scanf("%s",first_string);
scanf("%s",second_string);
for(i=0;first_string[i]!='\0';i++);
for(int j=0;second_string[j]!='\0';j++)
first_string[i]=second_string[j];
i++;
first_string[i]='\0';
return 0;
Output:
Kerala
_University
Answer:
#include<stdio.h>
#include<conio.h>
int main()
int a[15], i, j, t, n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
Output:
22 1 9 0 3
0 1 3 9 22
17. a) (a) Write a function namely myFact in C to find the factorial of a given number. Also, write
another function in C namely nCr which accepts two positive integer parameters n and r and
returns the value of the mathematical function C(n,r) = ( n! / ( r! x (n - r)!) ). The function nCr is
Answer:
#include <stdio.h>
int myFact(int);
int nCr(int);
void main()
int n,r,ncr,N;
scanf("%d",&N);
int fact=myFact(N);
scanf("%d",&r);
ncr=nCr(n)/(nCr(r)*nCr(n-r));
int nCr(int n)
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
int myFact(int N)
{
int j,fact=1;
for(j=1;j<=N;j++)
fact=fact*j;
return fact;
Output:
Enter a number n
10
Enter a number r
/*program accepts a number from the keyboard and computes its factorial by recursive function*/
#include<stdio.h>
int main()
scanf("%d", &num);
fact=rec_funct(num);
}
/*recursive function for factorial*/
int rec_funct(N)
int N;
int fct;
return(1);
else
return (fct);
Output:-
Factorial of 5=120
18. (a) With a suitable example, explain the differences between a structure and a union in C (6 Marks)
Answer:-
keyword struct keyword is used to define the union keyword is used to define the
structure union
size When variables or members are When a variable is associated with a
associated with a structure, the compiler union, the compiler allocates the memory
allocates the memory for each member. considering the size of the largest
The size of a structure is equal to or memory. So, the size of the union is the
greater than the sum of sizes of its size of the largest member.
members.
Value altering Altering the value of a member will not Altering the value of any of the member
affect other members of the structure will alter other member values
Accessing Individual members can be accessed at Only one member can be accessed at a
Initialization of Several members of a structure can be Only the first member of a union can be
int semester; };
float total_marks;
};
18. b) Declare a structure namely Student to store the details (roll number, name, mark_for_C)
of a student. Then, write a program in C to find the average mark obtained by the students
in a class for the subject Programming in C (using the field mark_for_C). Use array of
Answer:
/* program accepts the roll no, name and marks obtained in one test of students of a class and displays
#include<stdio.h>
void main()
{
struct stud_rec
int Rollno;
char Name[20];
int mark_for_C;
float avg;
};
int i,n,total=0;
scanf("%d",&n);
scanf("%d%s%d",&s[i].Rollno,s[i].Name,&s[i].mark_for_C);
for(i=0;i<n;i++)
for(i=0;i<n;i++)
total+=s[i].mark_for_C;
s[i].avg=total/n;
Output:
5 Babu 25
3 Ramu 21
4 Binu 20
1 Adil 19
5 Babu 25
3 Ramu 21
4 Binu 20
1 Adil 19
2 Somu 17
19. (a) With a suitable example, explain the concept of pass by reference. (6 Marks)
•Actual values are not passed, instead their addresses are passed.
•If any modification is made to the values in the called function, then original values get changed within
•Thus the data items of the calling program can be accessed by the called program(function)
•No value is copied when pointers are passed as arguments, as in call by value method.
•Unlike call by value method, If the values are changed in the functions, this will modify the original
•When the pointers are passed as arguments, then the following two points must be considered.
1) in the calling program, the function is invoked with the function name and addresses of actual
2) In the called program, parameter list, each and every formal parameters (pointers) must be preceded
by an indirection operator(*).
/* illustrates the call by reference method to interchange the contents of two integer variables*/
#include<stdio.h>
int main()
int temp;
temp= *n1;
*n1 = *n2;
*n2= temp;
Output:
56
88
19. (b) With a suitable example, explain how pointers can help in changing the content of a
Answer:
/*program passes the third element of an array and interchanges with the 4th element and finally prints
#include<stdio.h>
int main()
int x[10];
int i, n;
scanf("%d", &n);
int temp;
temp = *ptr1;
*ptr1=*ptr2;
*ptr2=temp;
Output:
22 555 66 77
Resultant array is
20. (a) Differentiate between sequential files and random access files? (4 Marks)
Answer:
Sequential Access to a data file means that Random Access to a file means that the computer
the computer system reads or writes system can read or write information anywhere in the
information to the file sequentially, starting data file. This type of operation is also called “Direct
from the beginning of the file and proceeding Access” because the computer system knows where
step by step. the data is stored (using Indexing) and hence goes
you access information in the same order all search through it and find the data you need more
the time. Also is faster than random access. easily (using indexing for example). Random Access
Tape Storage devices (used for offline Modern computer hard disks are using Random Access
Sequential drive stores files and data in a A random access drive puts them all over the place
specific order
20. (b) Using the prototypes explain the functionality provided by the following functions. (10 Marks)
i. fseek()
ii. ftell()
iii. fread()
iv. fwrite()
v. rewind()
Answer:
i. fseek():- fseek() is used to move a file pointer associated with a given file to a specific position.
Syntax:
int fseek(FILE *pointer, long int offset, int position)
position defines the point with respect to which the file pointer needs to be moved. It has three values:
#include <stdio.h>
int main()
FILE *fp;
fp = fopen("test.txt", "r");
fseek(fp, 0, SEEK_END);
printf("%ld", ftell(fp));
return 0;
Output:
ii. ftell(): ftell() in C is used to find out the position of file pointer in the file with respect to starting of the
“Someone over there is calling you. We are going for work. Take care of yourself.” (without the quotes)
When the fscanf statement is executed word “Someone” is stored in string and the pointer is moved
#include<stdio.h>
int main()
char string[20];
fscanf(fp,"%s",string);
printf("%ld", ftell(fp));
return 0;
}
Output:
iii. fread():The fread() function is the complementary of fwrite() function. fread() function is commonly
used to read binary data. It accepts the same arguments as fwrite() function does. The syntax of fread()
function is as follows:
The ptr is the starting address of the memory block where data will be stored after reading from the file.
The function reads n items from the file where each item occupies the number of bytes specified in the
second argument. On success, it reads n items from the file and returns n. On error or end of the file, it
int val;
This reads a float value from the file and stores it in the variable val.
This reads an array of 10 integers from the file and stores it in the variable arr.
int arr[10];
This reads 5 integers from the file and stores it in the variable arr.
int arr[10];
This reads 5 integers from the file and stores it in the variable arr.
Example : The following program demonstrates how we can use fread() function.
#include<stdio.h>
#include<stdlib.h>
struct employee
{
char name[50];
char designation[50];
int age;
float salary;
} employee;
int main()
FILE *fp;
fp = fopen("employee.txt", "rb");
if(fp == NULL)
exit(1);
{
printf("Name: %s \n", emp.name);
fclose(fp);
return 0;
Output:
iv) fwrite(): The fwrite() function writes the data specified by the void pointer ptr to the file.
Syntax: size_t fwrite(const void *ptr, size_t size, size_t n, FILE *fp);
ptr: it points to the block of memory which contains the data items to be written.
On success, it returns the count of the number of items successfully written to the file. On error, it returns
a number less than n. Notice that two arguments (size and n) and return value of fwrite() are of type
float *f = 100.13;
#include<stdlib.h>
struct employee
char name[50];
char designation[50];
int age;
float salary;
} employee;
int main()
int n, i, chars;
FILE *fp;
fp = fopen("employee.txt", "wb");
if(fp == NULL)
exit(1);
scanf("%d", &n);
fflush(stdin);
printf("Name: ");
gets(employee.name);
printf("Designation: ");
gets(employee.designation);
printf("Age: ");
scanf("%d", &employee.age);
printf("Salary: ");
scanf("%f", &employee.salary);
}
fclose(fp);
return 0;
v) rewind(): The C library function void rewind(FILE *stream) sets the file position to the beginning of
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
//clrscr();
fp=fopen("file.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
while((c=fgetc(fp))!=EOF){
printf("%c",c);
fclose(fp);
getch();
Output: