CS-205 End Exam 2024 Key
CS-205 End Exam 2024 Key
TELANGANA
DIPLOMA EXAMINATION (C-21)
C21-END-APR-24
SEMESTER II , SEMESTER END EXAM
AA/AI/AU/BM/CCB/CCP/CE/CH/CPS/CS/EC/EE/EI/ES/EV/HS/LF/LG/ME/MN/MT/PK/PT/TT
12005
CS-205
Programming In C
Answer Key
PART-A
2. What is the difference between reading a string using scanf() and gets()?
Ans.
The scanf() function is used to read the string input until it encounters a
whitespace, newline, or End of File (EOF). Whereas the gets() function
reads the string input even with the spaces so that we can give multiple
words.
5. What is a function?
Ans.
A function is a block of code that performs a specific task.
7. Define File.
Ans.
A file represents a sequence of bytes on the disc where a group of related
data is stored. A collection of data that is stored on a secondary device, like
a hard disc, is known as a file.
data_type member1;
data_type member2;
data_type member3;
};
PART-B
Characteristics:
1. It should be simple.
2. It should be clear without any ambiguity.
3. It should lead to a unique solution to the problem.
4. It should involve a finite number of steps to arrive at a solution.
5. It should have the capability to handle some unexpected situations that
may occur during the execution of a problem.
----- OR ----
9(b). Define the scope,visibility, and lifetime of variables in functions.
Ans.
● The scope of a variable is the range of program statements that can
access that variable.
● The lifetime of a variable is the interval of time in which storage is bound
to the variable.
● A variable is visible within its scope and invisible or hidden outside it.
10(a). What are the nested-for loops in the C language? Give an example.
Ans.
Nested loop means a loop statement inside another loop statement. That is
why nested loops are also called “loop inside loops.“.
Example:
// C program that uses nested for loop
to print a 2D matrix
#include <stdio.h>
#include <conio.h>
#define ROW 3
#define COL 3
// Driver program
int main ()
{
int i, j;
// Declare the matrix
int matrix[ROW][COL] = { {1, 2, 3}, {4, 5, 6}, {7, 8,
9} };
printf ("Given matrix is \n");
// Print the matrix using nested loops
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
printf ("%d ", matrix[i][j]);
printf ("\n");
}
return 0;
}
Output:
Given matrix is
1 2 3
4 5 6
7 8 9
----- OR ----
10(b). Give example for declaration of getc(), putc(), fprintf(). Mention their use.
Ans.
Declaration: int getc(FILE *fp)
putc(char, fp);
11(a).
Write a C program to find the factorial of a number using recursion.
Ans.
/* Factorial program by recursion in C */
#include
int fact(int);
int main()
{
int num,f;
printf("Enter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
----- OR ----
11(b).
Write a C program to find the GCD of two numbers using recursion.
Ans.
/* C Program to find GCD of given Numbers using Recursion
*/
#include <stdio.h>
int gcd (int, int);
int main ()
{
int a, b, result;
printf ("Enter the two numbers to find their GCD:");
scanf ("%d%d", &a, &b);
result = gcd (a, b);
printf ("The GCD of %d and %d is %d \n", a, b, result);
return 0;
}
if (a == b)
return a;
else if (a > b)
return (gcd (a - b, b));
else
return (gcd (a, b - a));
}
12(a). Write the syntax for declaration of functions: fseek(), ftell(), rewind()
Ans.
int fseek (FILE *fp, long int offset, int position)
----- OR ----
12(b). What is nested structure and what is its syntax?
Ans.
A structure inside another structure is called a nested structure.
struct <struct1_name>
{
member_1 of structure1;
member_2 of structure2;
}
struct <struct2_name>
{
member1 of structure2;
member2 of structure2;
structure1 variable;
} structure2 variable;
Struct test {
int a;
int b;
char c;
Struct test2 {
int k;
test nested_structure;
};
In the above example, the structure test is defined in the test2 structure.
We can access the members of the test structure in the test2 structure
using( . ) or (->) operators.
PART-C
operator meaning
3) Logical operators
The logical operator are used to combine two or more
conditions (i.e.,arithmetic expressions) and make decisions.
There are three logical operators:
operators meaning
&& logical AND
|| logical OR
! logical NOT
4) Assignment operators
Assignment operators are uesd to assign a value to a
variable.All assignment operators are used to construct
assignment expression,which assign the result of an expression to a
variable.
a)simple assignment operator =
b)compound /shorthand assignment operator +=,-
=,*=,/=,%=
6) Conditional operator
The conditional operator to construct conditional expression.This
operator is represented by the ? and : symbols.It is known as
ternary operator because it operates upon three operands rather than one
or two.The operands together with the conditional operator form a
conditional expression.
----- OR ----
13(b). Explain: i) function prototype; ii) function call; and iii) function definition.
Ans.
Function Prototype or Declaration
The syntax:
e.g. The prototype of the function to add two integer number and return the
value as integer is
variable= function_name(arg1,arg2,…,argn);
e.g.
This calls the function sum with two integer arguments, num1 and num2,
and the value returned after executing the sum is assigned to the result.
Function Definition
The syntax:
/* statements; */
int total;
total = x+y;
return total;
14(a). Write a C program to find whether the given number is prime or not.
Ans.
Program to check prime number or not:
#include
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
----- OR ----
14(b). Write a C program to demonstrate how to open a file, write data into it, and
close the file.
Ans.
# include <stdio.h>
# include <conio.h>
int main( )
{
// Declare the file pointer
FILE *filePointer ;
// Get the data to be written in file
char dataToBeWritten[50]
= "A SBTET Portal for question bank";
// Open the existing file Test.c using fopen()
// in write mode using "w" attribute
filePointer = fopen("Test.c", "w") ;
// Check if this filePointer is null
// which maybe if the file does not exist
if ( filePointer == NULL )
{
printf( "Test.c file failed to open." ) ;
}
else
{
printf("The file is now opened.\n") ;
// Enter the data to be written into the file.
if ( strlen ( dataToBeWritten ) > 0 )
{
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer) ;
fputs("\n", filePointer) ;
}
// Closing the file using fclose()
fclose(filePointer) ;
printf("Data successfully written in file Test.c\n");
printf("The file is now closed.") ;
}
return 0;
}
15(a).
Write a C program to pass one-dimensional array to function?
Ans.
Passing arrays to functions
Like simple variables ,it is also possible to pass array values to function.
For example:
-------------
-------------
#include
void main()
int i ;
int marks[10];
for(i=0;i<10;i++)
scanf(“%d”,&marks[i]);
int i;
for(i=0;i
printf(“%d”,a[i]);
----- OR ----
15(b).
List any five differences between local variables and global variables
Ans.
Differences between local variables and global variables are given
below:
16(a). Write a C program to demonstrate the memory allocation for structure and
union.
Ans.
#include <stdio.h>
union unionJob
{
// defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main ()
{
printf ("size of union = %ld bytes", sizeof (uJob));
printf ("\nsize of structure = %ld bytes", sizeof
(sJob));
return 0;
}
----- OR ----
16(b). Create a student structure with name, rollno and marks of 3 subjects as
fields. Write a program to read details of 5 sudents and display student
details, based on their total marks, in descending order.
Ans.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int rollno;
int sub[3];
int total;
};
void main()
{
int i, n, j;
struct student st[20], temp;
clrscr();
printf("Enter number of students data you want to
enter:\n");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("Enter name of student
%d\n",(i+1));
scanf("%s",&st[i].name);
printf("Enter Roll No of student
%d\n",(i+1));
scanf("%d",&st[i].rollno);
printf("Enter marks for 3 subjects of
student %d\n",(i+1));
scanf("%d%d%d",&st[i].sub[0],&st[i].sub[1],&st[i].sub[2])
;
st[i].total =
(st[i].sub[0]+st[i].sub[1]+st[i].sub[2]);
printf("Total Marks of %d student =
%d\n",(i+1), st[i].total);
}
for(i=0;i < (n-1);i++)
{
for(j=0;j < (n-i-1);j++)
{
if(st[j].total < st[j+1].total)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
printf("\n\n\n\t\t******Sorted in descending
order******");
for(i=0; i < n;i++)
{
printf("\nName of student:
%s",st[i].name);
printf("\nRoll No of student:
%d",st[i].rollno);
printf("\nTotal of student:
%d\n",st[i].total);
}
getch();
}