[go: up one dir, main page]

0% found this document useful (0 votes)
32 views13 pages

M22ucs101-M22uca101-Answer Key

Uploaded by

pride1149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views13 pages

M22ucs101-M22uca101-Answer Key

Uploaded by

pride1149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

MAHENDRA ARTS & SCIENCE COLLEGE

(AUTONOMOUS)

UG/PG - END SEMESTER EXAMINATIONS – Nov/Dec – 2024

Sub. Code : M22UCS101/ M22UCA101


Sub Title : PROGRAMMING IN C
ANSWER KEY
Time: 3.00 Hrs Maximum : 75 Marks
PART – A CHOOSE THE BEST ANSWER (10 * 1 =10 Marks)
(2 Questions from each Unit)
1. What is the correct syntax to include a standard library in C?
Ans: a) #include <stdio.h>
2. Which data type can store a single character in C?

Ans: b) char

3. Which of the following is a loop control statement in C?

Ans: b) while

4. Which of the following is used to declare a variable in C?

Ans: b) int x

5. Which of the following keywords is used to declare an external variable?

Ans: d) extern

6. In recursion, a function:
Ans : a) Calls itself

7. What is the index of the first element of an array in C?


Ans: b) 0

8. Which of the following is a valid bitwise operation in C?


Ans: d) All of the above

9. Which operator is used to access the value stored at the address of a pointer in C?

Ans: b) *

10. What is the keyword used to defined a structure in C

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

PART – C ANSWER ALL THE QUESTIONS (5 * 5 =25 Marks)


16 (a) Data types are used to specify the type of a variable. In c, the data types are
. classified into 3 category.
They are, Primary or Built-in : int , char, float
Derived : array, enum, pointer
User defined : function, structure
(OR)
(b) A variable in C is a named location in memory that stores data values, such as numbers
and characters. Variables can be defined with different data types, such as int, float, or
char.
Here are some things to know about variables in C:
S syntax
The syntax for declaring a variable in C is <type> <variable_name> = <value>.
Da data types
Different data types consume different amounts of memory, so it's important to define
the variable type.
Va variable names
Variable names must start with a letter or underscore, and cannot start with a
digit. They also cannot contain special characters like #, @, or $.
Va variable initialization
Variable initialization is the process of providing a value to the variable based on its
data type.
Da data type changes
C is a strongly typed language, so the data type of a variable cannot be changed after it
has been defined.
Va variable types
Some examples of variable types include int, float, char, bool, and double.
Here are some examples of variables in C:
int num = 3: In this example, int is the data type, num is the variable name, and 3 is the
variable value.
char c, ch: This is an example of a valid declaration.
int p, q, r: This declaration defines and declares the variables p, q, and r as integers.

17 (a) write a c programming for


. (i)largest of three numbers
write a c programming for
(i)largest of three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
Int a,b,c;
clrscr();
Printf(“\n largest of the three numbers”);
Printf(“\n enter three numbers”);
Scanf(“%d%d%d”,&a,&b,&c);
If((a>b)&&(a>c))
Printf(“\n%d id largest”,a);
elseif((b>a)&&(b>c))
Printf(“\n%d is largest”,b);
else
printf(“\n %d is the largest”,c);
getchar();
#include<stdio.h>
#include<conio.h>
void main()
{
Int a,b,c;
clrscr();
Printf(“\n largest of the three numbers”);
Printf(“\n enter three numbers”);
Scanf(“%d%d%d”,&a,&b,&c);
If((a>b)&&(a>c))
Printf(“\n%d id largest”,a);
elseif((b>a)&&(b>c))
Printf(“\n%d is largest”,b);
else
printf(“\n %d is the largest”,c);
getchar();
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
Printf(“\n largest of the three numbers”);
Printf(“\n enter three numbers”);
Scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
Printf(“\n%d id largest”,a);
elseif((b>a)&&(b>c))
Printf(“\n%d is largest”,b);
else
printf(“\n %d is the largest”,c);
getchar();
}
(OR)
(b) Parameters Break Statement in C Continue Statement in C

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;

Example : int *p; char *p;


Where, * is used to denote that ―p is pointer variable
#include int main()
{ int i; int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as
int*p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%d", *p);
p++;
retrn 0;
}
(OR)
(b) #include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}

PART – D ANSWER ANY THREE QUESTIONS (3 * 10 =30 Marks)


21). 1. Arithmetic Operators

These operators are used to perform basic arithmetic operations.


Operator Description Example
+ Addition a + b (if a=3, b=5, result=8)
- Subtraction a - b (if a=3, b=5, result=-2)
* Multiplication a * b (if a=3, b=5, result=15)
/ Division a / b (if a=10, b=5, result=2)
% Modulus (remainder) a % b (if a=10, b=3, result=1)

2. Relational (Comparison) Operators


These operators are used to compare two values. They return either true (1) or false (0).
Operator Description Example
== Equal to a == b (true if a equals b)
!= Not equal to a != b (true if a is not equal to b)
> Greater than a > b (true if a is greater than b)
< Less than a < b (true if a is less than b)
>= Greater than or equal a >= b (true if a is greater than or equal to b)
<= Less than or equal a <= b (true if a is less than or equal to b)

3. Logical Operators

These operators are used to perform logical operations.


Operator Description Example
&& Logical AND (a > b) && (c < d)
|| Logical OR (a > b) || (c < d)
! Logical NOT !(a == b)

4. Assignment Operators

These operators are used to assign values to variables.


Operator Description Example
= Assign a = b assigns value of b to a
+= Add and assign a += b (equivalent to a = a + b)
-= Subtract and assign a -= b (equivalent to a = a - b)
*= Multiply and assign a *= b (equivalent to a = a * b)
/= Divide and assign a /= b (equivalent to a = a / b)
%= Modulus and assign a %= b (equivalent to a = a % b)

5. Increment and Decrement Operators

These operators are used to increment or decrement the value of a variable by 1.


Operator Description Example
++ Increment by 1 a++ (post-increment) or ++a (pre-increment)
-- Decrement by 1 a-- (post-decrement) or --a (pre-decrement)

6.Bitwise Operators

These operators work on bits and perform bit-by-bit operations.


Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
Operator Description Example
~ Bitwise NOT ~a
<< Left shift a << 1
>> Right shift a >> 1

7. Conditional (Ternary) Operator

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

This operator returns the size, in bytes, of its operand.


Operator Description Example
sizeof Returns size sizeof(int) returns 4 in most systems

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)

10. Pointer Operators

These are used with pointers in C.


Operator Description Example
* Dereference operator *ptr accesses the value pointed to by ptr
& Address-of operator &a gets the address of variable a

22. Types of Loops in C:


 for loop
 while loop
 do-while loop
1. “for loop“
The for loop in C provides a concise way to iterate over a block of code using an
initializer, a condition, and an iterator. We commonly use it when the number of
iterations is known beforehand.
It has three main parts:
 Initialization: Executed once at the beginning of the loop. This step allows you to
declare and initialize any loop control variables.
 Condition: Evaluated before each iteration. The loop body is executed if the
condition evaluates to true (non-zero). If it evaluates to false (zero), the loop is
terminated.
 Update: Executed after each iteration. It’s typically used to update loop control
variables.
Syntax :
for (initialization; condition; update)
{
// code to be executed in each iteration of the loop;
}

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 :

23. Storage Classes in C


Storage classes in C are used to determine the lifetime, visibility, memory location,
and initial value of a variable. There are four types of storage classes in C

o Automatic
o External

Storage Storage Default Value Scope Lifetime


classes Place

Auto RAM Garbage Value Local Within function

extern RAM Zero Global Till the end of the main


program Maybe declared
anywhere in the program

Static RAM Zero Local Till the end of the main


program, Retains value
between multiple
functions call

Register Register Garbage Value Local Within the function

24. What is Array?


An array is a group of similar elements or data items of the same type collected at
contiguous memory locations. In simple words, we can say that in computer
programming, arrays are generally used to organize the same type of data.

Declaration and initialization of array


An array is a variable that can store multiple values.
For example, if you want to store 100 integers, you can create an array for it.
dataType arrayName[arraySize];
int data[100];
It is possible to initialize an array during declaration.
For example,
int mark[5] = {19, 10, 8, 17, 9};
initialize an array
int mark[] = {19, 10, 8, 17, 9};
Accessing array
Array can be accessed using array-name and subscript variable written inside pair of
square brackets [].
arr[3] = Third Element of Array
arr[5] = Fifth Element of Array

25. File operation

1. Creating a new file

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;

Opening a file is performed using the library function in the "stdio.h"


header file: fopen().
The syntax for opening a file in standard I/O is: ptr = fopen("fileopen","mode")
r Open for reading. If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
w Open for writing. If the file exists, its contents are overwritten. If the file does not
exist, it will be created.
wb Open for writing in binary mode. If the file exists, its contents are overwritten. If
the file does not exist, it will be created.
a Open for append. i.e, Data is added to end of file. If the file does not exists, it will be
created.
ab Open for append in binary mode. i.e, Data is added to end of file. If the file does
not exists, it will be created

You might also like