M22ucs101-M22uca101-Answer Key
M22ucs101-M22uca101-Answer Key
(AUTONOMOUS)
Ans: b) char
Ans: b) while
Ans: b) int x
Ans: d) extern
6. In recursion, a function:
Ans : a) Calls itself
9. Which operator is used to access the value stored at the address of a pointer in C?
Ans: b) *
Ans: a) struct
PART – B ANSWER ALL THE QUESTIONS (5 * 2 =10 Marks)
11. 10
12. getchar() and putchar()
gets() and puts()
scanf() and printf()
13. The four different storage classes in C program are auto, register, extern, and static.
14. ANS : An array is a data structure that store a collection of elements of the same data
type.
Data_type array_name[size];
15. ANS: A variable that stores the memory address of another variable
Loop The break statement allows for an The continue statement does not provide an exit
Construct immediate exit from a loop construct. from a loop construct.
Switch and The break statement can be used The continue statement cannot be used with the
Loop with the switch statement and within switch statement, but it can be used within for, do-
Statement for, do-while, and while loops. This while, and while loops. This means the continue
means the break statement can occur statement can only occur in loops, not switches.
in both loops and switches.
Control Upon encountering the break Upon encountering the continue statement,
statement, control immediately exits control automatically returns to the start of a loop
from a loop construct. statement.
Function The break statement causes a loop or The continue statement does not cause loop
switch to terminate a case at the termination but leads it into its next iteration. This
moment of its execution. This means means a loop will execute all its iterations,
a loop or switch will end abruptly even if it encounters a continue statement. The
upon encountering a break. continue statement is used to skip statements
that appear after the continue in a loop.
Syntax It can be denoted as: It can be denoted as:
break; continue;
18 (a) C functions are written with three components:
. 1. Function Declaration
2. Function Definition
3. Function Call
C Function Syntax
return_type function_name (parameter1_type parameter1_name, parameter2_type
parameter2_name, ...);
return_type: This specifies the type of the value that the function will return. If the
function doesn't return any value, you can use void as the return type.
function_name: This is the name of the function. Typically, the name of your function
reflects its purpose.
parameter1_type, parameter2_type: These are the data types of the parameters that
the function takes. Parameters are variables used to pass values into the function. It
have multiple parameters, but they need to be separated with commas.
parameter1_name, parameter2_name: These are the names of the parameters. Each
parameter has a corresponding name that refers to the values passed into the
function.
Declaring Functions in C
function in C by specifying its name, return type, and parameters. Function
declarations are typically placed at the beginning of your program or in header files to
inform the compiler about the functions that will be defined later in the code.
(OR)
(b) // C program to find factorial of given number
// using recursion
#include <stdio.h>
int factorial( int n)
{
if (n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main() {
printf(“enter number”);
scanf(“%d”,&num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
19 (a) Arrays
. An array is a collection of data items of same data type. Arrays can only be declared
There is no keyword for arrays.
An array cannot have bit fields.
An array name represents the address of the starting element.
Structures.
A structure is a collection of data items of different data types.
Structures can be declared and defined.
The keyword for structures is struct.
A structure may contain bit fields
A structure name is known as tag. It is a Shorthand notation of the declaration
(OR)
(b) 1. The & (bitwise AND) in C takes two numbers as operands and does AND on
every bit of two numbers. The result of AND is 1 only if both bits are 1.
2. The | (bitwise OR) in C takes two numbers as operands and does OR on every bit
of two numbers. The result of OR is 1 if any of the two bits is 1.
3. The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every
bit of two numbers. The result of XOR is 1 if the two bits are different.
4. The << (left shift) in C takes two numbers, the left shifts the bits of the first
operand, and the second operand decides the number of places to shift.
5. The >> (right shift) in C takes two numbers, right shifts the bits of the first
operand, and the second operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
20 (a) Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is
. used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any
of the data type such as int, float, char, double, short etc.
Pointer Syntax : data_type *var_name;
3. Logical Operators
4. Assignment Operators
6.Bitwise Operators
This operator is used to evaluate a condition and return one of two values.
Operator Description Example
? : Conditional condition ? true_value : false_value
8. Sizeof Operator
9. Comma Operator
The comma operator allows you to evaluate multiple expressions, but only the last one is
returned.
Operator Description Example
, Comma operator (a = 1, b = 2, c = a + b)
Flowchart:
2. “while loop”
A while loop in C is a control flow structure that allows a piece of code to be
executed repeatedly as long as a particular condition is true.
Here, the condition is evaluated before the execution of the loop’s body. If the
condition is true, the code inside the loop will run. After the loop body has run, the
condition is evaluated again, and if it’s still true, the body runs again. This process
continues until the condition becomes false. If the condition starts off as false, the loop
body will never execute.
Syntax :
while (condition)
{
// code to be executed as long as condition is true;
}
Flowchart :
3. “do-while loop”
The do-while loop checks the condition after executing the loop body, which ensures
that the loop body is executed at least once, even if the condition is initially false.
Syntax :
do
{
// code to be executed;
} while (condition);
Flowchart :
o Automatic
o External
2. Opening an existing file 3. Closing a file 4. Reading from and writing information
to a file Working with files When working with files, you need to declare a pointer of
type file. This declaration is needed for communication between the file and program.
FILE *fptr;