2020 Midterm Solution.
2020 Midterm Solution.
Registration No.______________________
Instructions:
Attempt all questions
Calculators are not allowed
You might use extra sheets for working but please try to write the final answer in the space
provided for it
General Hints:
1. Comparison of double/float type variables is exactly similar to int type variables
2. For arithmetic operators if one of the variable/value type is double/float and one is int then the
answer is floating point value.
SHORT QUESTIONS
1. The following program segment has been written to print a simple message in case the input
number is a positive integer and contains exactly 5-digits (not less than 5 or more than 5-
digits). What condition should be written within the parenthesis so that the program segment
works correctly? Please remember that you are not allowed to use any other variables or add
any new instruction in the program segment. Just write the condition using the already declared
variable(s). [3 Points]
int num = 0;
cin >> num;
if(_______________________________________________)
cout<< " It is a five digit positive number";
else
cout<< " It is not a five digit positive number ";
1/10
Midterm Programming Fundamentals Fall 2020
2. For each of the following program segments, specify the output it will produce when executed.
[8 = (1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) Points]
Program Segment Output
int z = 0; z=
z = 27 / 4; 6
cout << "z = " << endl<<z;
int num = 4;
if ( num % 2 == 0 )
cout << num / 2; 2
else
cout << num * 4;
2/10
Midterm Programming Fundamentals Fall 2020
int i, j, k;
i = 5; j = 3; k = 7;
if (i < j) {
if (j < k)
i = j; i = 7 j = 3 k=7
else
j = k;
}
else
{
if (j > k)
j = i;
else
i = k;
}
cout << "i = " << i << " j = " << j << " k = "
<< k << endl;
3/10
Midterm Programming Fundamentals Fall 2020
3. Following program has been written to compute and display average of three numbers but it is
not producing correct output. For example when we input numbers 1, 2 and 3 the program
display 4 whereas the actual average is 2.
Suggest a simple change in a single line of the program that will correct this logical error.
You must also rewrite the corrected line (only) in the space provided below [3 Points]
#include<iostream>
int main(){
4. Following program has some syntax errors. Identify all the syntax errors and write each line
correctly so that the program compiles without errors [6 Points]
#include<iostream> #include<iostream>
International travelers often need to convert some amount of money from currency
into another. For example, a Pakistani traveler to Malaysia got his amount
converted into US Dollars and Ringgit Malaysia. In Malaysia he needed more
Ringgits so he got some of his US Dollars converted into Ringgits. The calculations
for conversions are really simple but travelers are not typically good at math.
In this problem you are required to write a simple program that will take two inputs
i) the amount to be converted ii) the rate of conversion, and then display the amount
that the traveler must receive after conversion. The formula for conversion is
Write a program in the space provided below where the marks will be awarded for
5/10
Midterm Programming Fundamentals Fall 2020
#include<iostream>
int main()
{
double Amount, Rate, NewAmount;
6/10
Midterm Programming Fundamentals Fall 2020
The BMI (Body Mass Index) is calculated using height (measured in meters) and weight
(measured in Kg) of a person. A person with BMI of 25.0 or more is considered
overweight, while the healthy/normal range is 18.5 to 24.9 and a person with BMI less
than 18.5 is considered underweight. The formula for computing BMI is
Write a program that can be used to compute the BMI of a person. Your program
must input weight in Kilograms and Height in centimeters. Your program must
convert the height into meters (1 Meter = 100 centimeters) and then use it to
compute the BMI of the person using formula described above. The program must
display a message indicating if the person is underweight, overweight or has
normal weight. Marks will be awarded for
7/10
Midterm Programming Fundamentals Fall 2020
#include<iostream>
using namespace std;
int main()
{
double Weight, Hight_Centimeters, Hight_Meters, BMI;
cout<<"Enter Weight in Kilograms ";
cin >> Weight;
}
else
{
cout<<"Invalid Height or Weight: Positive Values Only Please";
}
return 0;
}
8/10
Midterm Programming Fundamentals Fall 2020
9/10