0% found this document useful (0 votes)
18 views

C MS1 Key

This document contains the answer key for a mid-semester examination for a C Programming and Problem Solving course. It includes answers to 14 multiple choice or short answer questions covering topics like algorithms, data types, operators, conditional statements, loops, and functions in the C programming language. The answer key provides the concepts and code snippets as answers to the questions.

Uploaded by

kathirveljune10
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)
18 views

C MS1 Key

This document contains the answer key for a mid-semester examination for a C Programming and Problem Solving course. It includes answers to 14 multiple choice or short answer questions covering topics like algorithms, data types, operators, conditional statements, loops, and functions in the C programming language. The answer key provides the concepts and code snippets as answers to the questions.

Uploaded by

kathirveljune10
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/ 9

COIMBATORE INSTITUTE OF TECHNOLOGY

DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

1. An algorithm is a set of well-defined instructions to solve a particular problem. It takes a set


of input(s) and produces the desired output. For example,
An algorithm to add two numbers:
a. Take two number inputs
b. Add numbers using the + operator
c. Display the result
2. Basic Data Types
int
char
float
double
3. Names can contain letters, digits and underscores.
Names must begin with a letter or an underscore (_).
Names are case sensitive (myVar and myvar are different variables).
Names cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words (such as int) cannot be used as names.
4. Keywords are predefined or reserved words that have special meanings to the compiler. These
are part of the syntax and cannot be used as identifiers in the program.
5.

6. a=5 it's an assignment operator - the value 5 will be stored in the variable 'a' and the program
output is 5 for the variable.
A==5 '==' is used for comparison. It means if the value of A is equal to 5 then it will return
true as output.
7. The conditional operator or ternary operator in C is generally used when we need a short
conditional code such as assigning value to a variable based on the condition. The working of
the conditional operator in C is as follows:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

variable = Expression1 ? Expression2 : Expression3;


Step 1: Expression1 is the condition to be evaluated.
Step 2A: If the condition(Expression1) is True then Expression2 will be executed.
Step 2B: If the condition(Expression1) is false then Expression3 will be executed.
Step 3: Results will be returned.
8. The syntax of an if...else statement in C programming language is −
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}
9. An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.

10.

11. a.
Precedence of operators
The precedence of operators determines which operator is executed first if there is more than
one operator in an expression.
An example:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

int x = 5 - 17* 6;
In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first. Then the
expression involving - is evaluated as the precedence of - is higher than that of =.
Operators Precedence & Associativity Table
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

Associativity of Operators
The associativity of operators determines the direction in which an expression is evaluated.
For example,

b = a;
Here, the value of a is assigned to b, and not the other way around. It's because the
associativity of the = operator is from right to left.
Also, if two operators of the same precedence (priority) are present, associativity determines
the direction in which they execute.
example:
1 == 2 != 3
Here, operators == and != have the same precedence. And, their associativity is from left to
right. Hence, 1 == 2 is executed first.
The expression above is equivalent to:
(1 == 2) != 3
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

b.
Algorithm of factorial of a number
Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Step 7: Stop
12. a.

b. Min = (m<n ? (m<p?m:p) ; (n<p?n:p))


#include <stdio.h>
int main() {
int m,n,p,Min;
printf ("Enter three integers: ");
scanf ("%d %d %d", &m,&n,&p);
if (m<n) {
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

if (m<p) {
Min = m;
} else {
Min = p;
}
} else {if (n<p) {
Min = n;
} else {
Min = p;
}}
printf("Min = %d",Min);
return 0;
}
13. a.
There are mainly two types of loops in C Programming:

Entry Controlled loops: In Entry controlled loops the test condition is checked before entering
the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the
loop body. The loop body will execute at least once, irrespective of whether the condition is
true or false. do-while Loop is Exit Controlled loop.
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

b.
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

14. a.
Algorithm to check if a given number is Palindrome or not
Step 1: Start
Step 2: Read the input number from the user
Step 3: Declare and initialize the variable reverse and assign input to a temp variable
tempNum=num
Step 4: Start the while loop until num !=0 becomes false
rem = num % 10
reverse*= 10 + rem
num = num / 10
Step 5 : Check if reverse == tempNum
Step 6: If it’s true then the number is a palindrome
Step 7: If not, the number is NOT a palindrome
Step 8: Stop

b.
C switch Statement
The switch statement allows us to execute one code block among many alternatives.
Syntax of switch...case
switch (expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
.
default:
// default statements
}

The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case constant2:
are executed until break is encountered.
If there is no match, the default statements are executed.
Example:-
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
COIMBATORE INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MECHANICAL ENGINEERING

MID-SEMESTER EXAMINATION-I
Program: B.E. Mechanical Engineering
Group: Common to Group I and Group II
Academic Year: 2023-24 Semester: 01

23ME131/ C PROGRAMMING AND PROBLEM SOLVING


Course Instructor: Dr.P.Manivel & Dr.G.Ramkumar

ANSWER KEY

printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}

Output:
Value is 8
In the given program we have explain initialized a variable num with value 8.
A switch construct is used to compare the value stored in variable num and execute the block
of statements associated with the matched case.
In this program, since the value stored in variable num is eight, a switch will execute the case
whose case-label is 8. After executing the case, the control will fall out of the switch and
program will be terminated with the successful result by printing the value on the output
screen.

You might also like