0% found this document useful (0 votes)
47 views16 pages

CS-205 End Exam 2024 Key

The document discusses a C programming exam with questions covering topics like variable names, storage classes, functions, file handling functions, recursion, structures, and operators. The exam contains 3 parts - Part A with 8 one mark questions, Part B with 4 three mark questions, and Part C with 4 five mark questions testing various C programming concepts.

Uploaded by

veereshgajwel
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)
47 views16 pages

CS-205 End Exam 2024 Key

The document discusses a C programming exam with questions covering topics like variable names, storage classes, functions, file handling functions, recursion, structures, and operators. The exam contains 3 parts - Part A with 8 one mark questions, Part B with 4 three mark questions, and Part C with 4 five mark questions testing various C programming concepts.

Uploaded by

veereshgajwel
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/ 16

STATE BOARD OF TECHNICAL EDUCATION AND TRAINING

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

Exam Date: 26-04-2024 Session: AN


Duration: 2 Hours [02:00 PM To 04:00 PM] [Total Marks: 40]

PART-A

Instructions: 1. Answer the following questions. 8X1=8


2. Each question carries ONE mark.

1. Write the rules for constructing variable names.


Ans.
● Variable names must begin with a letter or an underscore.
● Variables are case-sensitive.
● They can be constructed with digits or letters.
● No special symbols are allowed other than underscores.

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.

3. List different storage classes in C.


Ans.
There are four storage classes in C.They are given below.

Storage class Keyword


(a) automatic storage class auto
(b) register storage class register
(c) static storage class static
(d) external storage class extern
4. List four white spaces in C-language.
Ans.
● n new line,
● t horizontal tab,
● v vertical tab,
● ? Question mark.

5. What is a function?
Ans.
A function is a block of code that performs a specific task.

6. How many minimum arguments can a function have?


Ans.
Zero

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.

8. Write the syntax of union.


Ans.
union <name of union>

data_type member1;

data_type member2;

data_type member3;

};

PART-B

Instructions: 1. Answer the following questions. 4 X 3 = 12


2. Each question carries THREE marks.
9(a). Define an algorithm and list its characteristics.
Ans.
Algorithm: An algorithm is a self-contained, step-by-step set of operations
to be performed to accomplish a task.

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.“.

Syntax for the Nested For loop:

for ( initialization; condition; increment )

{ for ( initialization; condition; increment )

{ // statement of inside loop }

// statement of outer loop }

// statement of outer loop }

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)

getc functions are used to read a character from a file. In a C program, we


read a character as below:.
getc (fp);

Declaration: int putc(int char, FILE *fp)

The putc function is used to display a character on standard output or to


write to a file. In a C program, we can use putc as below:.

putc(char, fp);

Declaration: int fprintf(FILE *fp, const char *format,


The fprintf() function is used to write formatted data into a file. In a C
program, we use fprinf() as below:.
fprintf (fp, “%s %d”, “var1”, var2);

Where fp is the file pointer to the data type "FILE.”.


var1: string variable
var2: integer variable

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;
}

int gcd (int a, int b)


{

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)

long int ftell (FILE *fp)

void rewind (FILE *fp)

----- OR ----
12(b). What is nested structure and what is its syntax?
Ans.
A structure inside another structure is called a nested structure.

general form or syntax of nested structure is

struct <struct1_name>
{
member_1 of structure1;
member_2 of structure2;
}
struct <struct2_name>
{
member1 of structure2;
member2 of structure2;
structure1 variable;
} structure2 variable;

Here, structure is inside of structure 2;


Example:

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

Instructions: 1. Answer the following questions. 4 X 5 = 20


2. Each question carries FIVE marks.

13(a). Explain two types of operators with examples.


Ans.
1) Arithmetic operator
Arithmethic operators are used in mathemathical calculations.
There are 5 arithmmetic operators they are:

operator meaning

+ addition or unary plus


- subraction or unary minus
* multiplication
/ divion
% modulus
2) Relational operator
A relational operator are used to compare two values and return a
true or false result based upon that comparison.That is ,this are used to test
the relationship between two variables ,are between a variable and a
constant.
There are six relational operators:
operator meaning
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to

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 +=,-
=,*=,/=,%=

5) Increment and decrement operator


C provides special operators ++ and --.The ++ and -- are known as
increment and decrement operators.
operator sample expression
++ prefix increment ++a
++ postfix increment a++
-- prefix decrement --a
-- postfix decrement a--

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

A function prototype provides the compiler with a description of a function


that will be defined and used at a later point in the program. It should be
before the first call of the function. The prototype includes a return type
indicating the type of variable that the function will return. It also includes
the function name, which should describe what the function does. The
prototype also contains the variable types of the arguments (arg type) that
will be passed to the function. Optionally, it can contain the names of the
variables that will be passed. A prototype should always end with a
semicolon.

The syntax:

return_type function_name( arg_type1, arg_type 2,..., arg_type-n);

e.g. The prototype of the function to add two integer number and return the
value as integer is

int sum(int, int );

Calling the function

Calling or invoking the function locates the function in memory, furnishing


it with arguments and causing it to execute. When a function is called, the
control is passed to the function where it is actually defined. The actual
statements are executed, and control is passed again to the calling
program.

variable= function_name(arg1,arg2,…,argn);

The function prototyped above is called:

e.g.

result= sum(num1, num2);

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:

return_type function_name( arg-type name-1,...,arg-type name-n)

/* statements; */

A function's definition is the actual function. The definition contains the


code that will be executed. The first line of a function definition, called the
function header, should be identical to the function prototype, with the
exception of the semicolon. A function header shouldn't end with a
semicolon. In addition, although the argument variable names

were optional in the prototype, they must be included in the function


header. Following the header is the function body, which contains the
statements that the function will perform. The function body should start
with an opening bracket and end with a closing bracket. If the function
return type is anything other than void, a return statement should be
included, returning a value matching the return type. If the function does
not return any value, the keyword void is used as the return type.

e.g., the above sum function is defined as:

int sum(int x,int y)

int total;

total = x+y;

return total;

Function Prototype Examples

double square( double );


void print_line( int , char );

int menu_choice( void );

Function Definition Examples

double square( double number ) /* function header */

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);

for (i = 2; i <= n / 2; ++i) {


// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}

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.

To pass the array to called function, it is sufficient to pass name of array


without subscripts and size of array as arguments to function call.
array name itself gives base address.

For example:

display(marks,5); // function call

This is received by formal parameter array arr in a called function display()

void display(int arr[ ],int count)

-------------

-------------

Rules for Passing 1-D Array to a Function:

● Function call must contain name of array as actual argument.


● Formal parameter in Function definition must be array type.
● It is not required to specify size of an array.

C program to illustrate passing one-dimensional arrays to functions is given


below.

#include

void display(int a[ ],int count);

void main()

int i ;

int marks[10];

printf(“Enter the marks \n”);

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

scanf(“%d”,&marks[i]);

display(marks,10); /* function call */


}

void display(int a[ ],int count)

int i;

printf(“The marks are \n”);

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:

S. Local variable Global variable


No.
1 A local variable is a variable that is A global variable is a variable that is declared
declared inside a function. outside of main().
2 Default initial value: an unpredictable The default initial value is zero.
value, which is often called a garbage
value.
3 Scope: local to the block in which the Scope: Global.
variable is defined.
4 Lifetime: Till the control remains within Lifetime: throughout the program, i.e., as long
the block/function in which the variable is as the program is under execution.
defined.
5 Local variables are also called automatic Global variables are also called external
variables. variables.
6
#include #include
void main() int i = 1; // global variable
{ void main()
auto int i=1; //local {
variable printf(“%d”,i);
printf(“%d”,i); i=i+1;
i=i+1; }
}

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();
}

You might also like