NUCES Fall 2020 Midterm Paper
NUCES Fall 2020 Midterm Paper
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 5digits). 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]
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 = 27 / 4;
cout << "z = " << endl<<z;
2/10
Midterm Programming Fundamentals Fall 2020
if( num % 7 ){
cout<<"Number is divisible by 7\n";
} else{
cout<<"Number is not divisible by 7\n";
}
int i, j, k;
i = 5; j = 3; k = 7;
if (i < j) { if (j < k)
i = j; 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> using
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]
PROGRAM LINES WITH ERROERS PROGRAM WITHOUT ERROR
#include<iostream>
int main()
{
integer TC , FH;
cin << T;
F << 9\5.0* T + 32
4/10
Midterm Programming Fundamentals Fall 2020
Return 0;
}
WRITING C++ PROGRAMS
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
6/10
Midterm Programming Fundamentals Fall 2020
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
Correct indentation. [2 Points]
Using number of and correct type of variables. [2 Points]
Correct conversions and calculations [4 Points]
Correctly showing the answers and messages. [5 Points]
7/10
Midterm Programming Fundamentals Fall 2020
8/10
Midterm Programming Fundamentals Fall 2020
9/10
Midterm Programming Fundamentals Fall 2020
10/10