Midterm Exam - Solution
Midterm Exam - Solution
Ahmed Abdullah
INSTRUCTIONS:
Closed Books, Closed Notes,
No Calculators, No E-Dictionaries, No Mobiles.
Answer ALL the questions in each section.
Marking Scheme:
Outcomes:
Total Score Question Weight Outcome
II. 1 1
Analyze simple problems, design algorithms and write
II. 2 1 B.
them in pseudo-code or flowcharts.
II. 3 1
II. 1 1
II. 2 1 Develop, test, and debug computer programs. C.
II. 3 1
I. 1 2
I. 2 3 Apply the concepts of variables, data types, input,
II. 1 3 output, expressions, assignment, the processes of D.
II. 2 3 decision-making and repetition.
II. 3 3
Part I:
Q.1 Indicate whether each of the following statements is True or False:
1
Statement TRUE/FALSE
Q.2 Circle the letter of the choice that best completes the statement or answers
the question.
1.
1. Suppose that x and y are int variables, z is a double variable, and the input is:
28 32.6 12
Choose the values of x, y, and z after the following statement executes:
cin >> x >> y >> z;
▊. x = 28, y = 32, z = 0.6
b. x = 28, y = 32, z = 12.0
c. x = 28, y = 12, z = 32.6
d. x = 28, y = 12, z = 0.6
2. Suppose that x = 55.68, y = 476.859, and z = 23.8216. What is the output of the
following statements?
cout << fixed << showpoint;
cout << setprecision(3);
cout << x << ' ' << y << ' ' << setprecision(2) << z << endl;
switch (x)
{
case 0: y = 0;
case 1: y = 1;
case 2: y = x;
case 3: y = y + 3;
case 4: y = y + 4
break;
dafault: y = 10;
}
cout << y << endl;
a. 2 ▊. 9
b. 5 d. 10
Part II:
3
Q.1 Park Meter: Write a program that prompts for the time in seconds, converts
the seconds to hours, minutes, and seconds then calculates the parking fee
based on the following scheme:
#include <iostream>
int main( )
{
int totalSeconds, seconds, minutes, hours;
float cost;
if(hours < 3)
cost = 0.001*seconds + 0.03*minutes + 2.2*hours;
else
cost = 0.001*seconds + 0.03*minutes + 2*hours;
cout << "The total cost is: " << cost << endl;
return 0;
}
4
Q.2 Evaluate the Series: Write a program that prompts the user to enter a positive
integer n. The program evaluates and prints the following series:
1 2 3 N −1
1+ + + +⋯+
2 3 4 N
#include <iostream>
int main( )
{
int N, i;
float sum=0;
cout << "\n\nwith " << N << " terms is: " << sum << endl;
return 0;
}
5
Q.3 Parity Check: Write a program that prompts the user to enter a positive
integer. The program adds at the end of the integer a parity digit such that
the digits sum of the new integer is a multiple of 3. For example if the
integer is 2705 (two thousand, seven hundred, and five) the digits sum is
2+7+0+5 = 14 and for the digits sum to be a multiple of 3 we need to add 1
at the end of the integer to make it 12705 (twelve thousand, seven hundred,
and five).
#include <iostream>
#include <cmath>
int main( )
{
int N, num, k=0, digit, sum=0, final, extra;
while(num > 0)
{
digit = num%10;
num = num/10;
sum = sum + digit;
k++;
}
extra = 3 - sum%3;
final = extra*pow(10, k) + N;
cout << "The new form of " << N << " is " << final << endl;
return 0;
}