Midterm Exam - Solution
Midterm Exam - Solution
Mahir Abdullah
INSTRUCTIONS:
Closed Books, Closed Notes,
No Calculators, No E-Dictionaries, No Mobiles.
Answer ALL the questions in each section.
Marking Scheme:
Weight Score
10 Part I
15 Part II
25 Total
Outcomes:
4 Part I.1
Apply the concepts of variables, data types, input, output, expressions,
E. 8 Part II.1
assignment, the processes of decision-making and repetition.
7 Part II.2
The expression (x >= 0 && x <= 100) evaluates to false if either x < 0 or x >= 7
F
100.
T In a switch statement, the default is optional.. 8
# include <iostream>
int main( )
{
int amount;
float bill;
return 0;
}
3. [3 Marks] Trace the following program and write its output:
2
#include <iostream>
Output
using namespace std;
616 int main( )
{
319 int i = 1, a = 6, b = 4, c = 3;
616 while( i <= 3 )
{
if (a > b){
b = 5 + a;
c = a - 1;
cout << a;
}
else{
b -= 8;
cout << b;
}
c = c + b;
cout << c << endl;
i++;
}
return 0;
}
1. [8 Marks] Miles per Gallon: Drivers are concerned with the mileage obtained by their cars. A
driver likes to know the number of miles driven and the number of gallons used. If the average
3
miles per gallon is more than 20 the car is economical, otherwise it is not economical. Write a
program that prompts the user to enter the number of gallons used and the number of driven miles.
The program calculates and prints the average miles per gallon and also prints a message that the car
is economical or not economical.
2. [7 Marks] Number Divisors: Write a program that prompts the user to enter a positive integer N.
The program reads N positive integers and finds the product of the even integers and the sum of the
odd integers.
#include <iostream>
4
int main( )
{
float gallons, miles, average;
cout << "The miles / gallon is: " << average << endl;
return 0;
}
#include <iostream>
int main( )
5
{
int N, num, even=1, odd=0, k=0;
while(k<N)
{
cout << "Enter a positive integer: ";
cin >> num;
if(num % 2 == 0)
even = even * num;
else
odd = odd + num;
k++;
}
cout << "The product of the even integers is " << even << endl;
cout << "The sum of the odd integers is " << odd << endl;
return 0;
}