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

Midterm Sample Questions With Solutions

.

Uploaded by

TYW GAMEFIRE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Midterm Sample Questions With Solutions

.

Uploaded by

TYW GAMEFIRE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Page 1

Sample Midterm Exam Questions with Solutions

Q.2. (15 points) Design an algorithm OR draw a flowchart for a computer program that finds the
average of 3 input numbers entered by the user and prints “pass”, if average is greater than or
equal to 50, otherwise prints “fail”. At the end it must print computed average as well.

1. BEGIN
2. PRINT “Enter three numbers:”
3. READ num1, num2, num3
4. average = (num1+num2+num3)/3
5. IF average >= 50
PRINT “pass”
ELSE
PRINT “fail”
ENDIF
6. PRINT “average:”, average
7. END

Q.3. (19 points)


a) (10 points) State which of the following C identifier names are valid or invalid. Give reason if
invalid.
Identifier Name Valid or Invalid Reason(s) if invalid
Return valid
_C valid
a.b invalid No special character
2B invalid Cannot start with a number
Midterm Grade invalid No space between the words

b) (9 points) Write down a C expression corresponding to each of the following mathematical


expressions.

Mathematical Expression C Expression


𝑡
𝑡 𝑘
+2 t/k-(t/k+2)/(3*s*s)
a) −
𝑘 3𝑠2

b) 2𝑎
2𝑐 2*a*((2*c) / (a+b))
𝑎+𝑏

−𝑏+(b2 −4ac)
c) (-b+(b*b-4*a*c))/(2*a)
2𝑎
Page 2

Q.4. (20 points)

a) (10 points) Compute the values of the following C expressions assuming that a, b and c are
integer variables and d is a float variable as declared below.
int a=2, b=3, c=4;
float d=5.0;
i) (b+2)/b+2 ______3________

ii) b*c/d ______2.4_______

iii) a/(b/c-1) ______-2_______

iv) b%c*(a/d) ______1.2______

v) ++a+b-- _______6______

b) (10 points) For the following statements, give the corresponding outputs into the boxes on the
right which correspond to different spaces in the output.

2 4
i) printf(“%-4d%3d”, 2, 4);

ii) printf(“%10.3e”, 627.14); 6 . 2 7 1 e + 0 0 2

0 . 8 9
iii) printf(“%7.2f”, 0.888);

iv) printf(“%-5.2f %.2f”, 5.0, 123.4); 5 . 0 0 1 2 3 . 4 0

v) printf(“%f%c%d”, 23.12, ‘+’, 15); 2 3 . 1 2 0 0 0 0 + 1 5


Page 3

Q.5. (20 points)


In the following C program there are total of 10 errors in different lines. In the provided table,
indicate the line numbers which error occurred and write your correction in front:

1: #include (stdio.h)
2: #Define PI 3.14
3: int main
4: {
5: Int rad, base, height;
6: Float area, ci;
7:
8: printf("\nEnter radius of circle: ");
9: scanf("%d", rad);
10:
11: area = PI * rad * rad;
12: printf("\nArea of circle : %d ", area);
13:
14: ci = 2 * PI * rad;
15: printf("\nCircumference : %f ", ci)
16:
17: printf("\nEnter the base of Right Angle Triangle : ");
18: scanf("%d", &base);
19:
20: printf("\nEnter the height of Right Angle Triangle : ");
21: scanf("%d", &height);
22:
23: area = 0.5 * base * height;
24: printf("\nArea of Right Angle Triangle : %f", area);
25: Return 0;
26:

Line Correction
number
1 #include <stdio.h>
2 #define PI 3.14
3 int main()
5 int rad, base, height;
6 float area, ci;
9 scanf("%d", &rad);
12 printf("\nArea of circle : %f ", area);
15 printf("\nCircumference : %f ", ci);
25 return 0;
26 }
Page 4

4
Q.6. (17 points) The formula for the volume of a sphere is 𝑉 = 3 𝜋 𝑎2 𝑏 where a and b are the

half-lengths of the major and minor axes respectively. The following C program reads values for a
and b and then calculates and displays the volume. Complete the missing parts in the program.
Use appropriate variable declarations in the program (do not use any additional variables) and
write only 1 statement on each blank line.

#include <stdio.h>

/*declare the constant value of 𝜋 as 3.141593

_____#define PI 3.141593____________________________________________

int main()
{
/* declare variables */

____float a, b, V;____________________________________________________

/*read the values of a and b from the keyboard*/

____scanf(“%f%f”, &a, &b);___________________________________________

/*compute the volume of the sphere */

____V=4.0/3*PI*a*a*b;-________________________________________________

/*display the result onto the screen*/

_____printf(“Volume = %f”, V); ________________________________________


return 0;
}

You might also like