0% found this document useful (0 votes)
24 views6 pages

Midterm Exam - Solution

Uploaded by

shahdalalfy23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Midterm Exam - Solution

Uploaded by

shahdalalfy23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

College of Sciences

Department of Computer Science


Programming for Engineers (1411113)
Mid-Term Exam, Summer 2014/2015
DATE: Sunday June 28, 2015; TIME: 14:00 - 15:00

Student Name: ___________________________________ ID#: _______________________


Please circle the name of your instructor:

Ahmed Abdullah

INSTRUCTIONS:
 Closed Books, Closed Notes,
 No Calculators, No E-Dictionaries, No Mobiles.
 Answer ALL the questions in each section.

Marking Scheme:

Score Weight Question


2 Q. 1
Part I
3 Q. 2
5 Q. 1
5 Q. 2 Part II
5 Q. 3
20 Total

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

The following is a legal C++ identifier: _x1 T 1.

Suppose x = 8. After the execution of the statement y = x++; 2.


F
y is 9 and x is 9.

Consider the following statements. 3.


int score;
string grade;
cin >> score; F

grade = (score > 60 ? "pass" : "fail");

If score is equal to 60, the value of grade is "pass".


The expression in the following if statement evaluates to 4.
true only if the value of month is 1.
F
if (month = 1)
monthName = “January”;

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;

▊. 55.680 476.859 23.82 c. 55.680 476.860 23.82


b. 55.690 476.860 23.82 d. 55.680 476.859 23.821

3. Which of the following operators has the lowest precedence?


c &&
a. !
.
▊. || d. ==
2
4. When one control statement is located within another, it is said to be ____.
a. blocked ▊. nested
b
compound d. closed
.

5. What is the output of the following code?


if (6 >= 60)
cout << "Line 1 ";
cout << "Line 2 ";
cout << "Line 3";

a. Line 1 Line 2 Line 3 ▊. Line 2 Line 3.


b Line 3
d. Error
.

6. What is the output of the following C++ code?


int x = 2;
int y = 10;

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:

Every second cost 0.001 and every minute cost 0.03.


If the hours is less than 3 the every hour cost 2.2 otherwise every hour cost 2.

Sample input / output:

#include <iostream>

using namespace std;

int main( )
{
int totalSeconds, seconds, minutes, hours;
float cost;

cout << "Enter the total number of seconds: ";


cin >> totalSeconds;

hours = totalSeconds / 3600;


totalSeconds = totalSeconds % 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;

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

Sample input / output:

#include <iostream>

using namespace std;

int main( )
{
int N, i;
float sum=0;

cout << "Enter how mant terms: ";


cin >> N;

cout << "\nThe sum of the series:\n\n1";


for(i=1; i<N; i++)
{
sum = sum + static_cast<float>(i)/(i+1);
cout << " + " << i << "/" << i+1;
}

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).

Sample input / output:

#include <iostream>
#include <cmath>

using namespace std;

int main( )
{
int N, num, k=0, digit, sum=0, final, extra;

cout << "Enter a positive integer: ";


cin >> N;
num = N;

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;
}

You might also like