Midterm Sample Questions With Solutions
Midterm Sample 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
b) 2𝑎
2𝑐 2*a*((2*c) / (a+b))
𝑎+𝑏
−𝑏+(b2 −4ac)
c) (-b+(b*b-4*a*c))/(2*a)
2𝑎
Page 2
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________
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);
0 . 8 9
iii) printf(“%7.2f”, 0.888);
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>
_____#define PI 3.141593____________________________________________
int main()
{
/* declare variables */
____float a, b, V;____________________________________________________
____V=4.0/3*PI*a*a*b;-________________________________________________