0% found this document useful (0 votes)
27 views5 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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 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 PDF, TXT or read online on Scribd
You are on page 1/ 5

College of Sciences

Department of Computer Science


Programming for Engineers (1411113)
Mid-Term Exam, Fall 2016/2017
DATE: Thursday November 3rd, 2016; TIME: 4:00-5:00

Student Name: ___________________________________ ID#: _______________________


Please circle the name of your instructor:

Manar Mahir Sohil Abdullah

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

Marking Scheme:
Outcome Question Weight Score Total
A. Identify different phases of problem solving and algorithm design.
4 2
2 1
B. Develop, test, and debug computer programs. 3 1
4 1
C. Use the concepts of variables, data types, input, output, expressions,
and assignment. 2 4
1 4
D. Apply selection and repetition statements. 3 4
4 3
E. Implement modular programming.

F. Use the concept of string and arrays.

Total 20

1
Part I General

1. [4 Marks] Tracing What is the output of the following program? Assume there is no syntax error.
Test your program with the following inputs:
(1) 12 17
(2) 8 19

#include <iostream>
using namespace std;
int main( )
{
int x, y;
cin >> x >> y;
if (x > 10)
{ if (y < 10)
x = x - 2;
else if (y < 20)
y = y + 4; }
else
y = y + 1;
x = x + 3;
cout << x << “\t\t\t” << y <<endl;
return 0;
}

Sample input/output:
(1) Input: 12 17 (2) Input : 8 19
Output Output

15 21 11 20

2
Part II Variables and Assignment

2. [5 Marks] Integer Left-Shift: Write a program that reads a five-digit integer, left-shift the right
most digit to the left of the integer, and prints the new integer. The left shift of 25379 is 92537.

Sample input/output:

#include <iostream>
using namespace std;

int main()
{
int number, digit, newNumber;
cout << "Enter a five-digit number: ";
cin >> number;

digit = number % 10;


newNumber = 10000*digit + (number/10);

cout << "The left-shift of " << number << " is " << newNumber << endl;

return 0;
}

3
Part III Selection

3. [5 Marks] Is it a Palindrome: A palindrome is a number or a text phrase that reads the same
backwards as forwards. For example,: 131, 555, 45754 and 11. Write a program that reads a five-
digit integer and determines whether it is a palindrome or not.

Sample input/output:

#include <iostream>
using namespace std;

int main()
{
int number, digit1, digit2, digit3, digit4, digit5, temp;
cout << "Enter a five-digit number: ";
cin >> number;
temp = number;
digit1 = temp % 10;
temp = temp / 10;
digit2 = temp % 10;
temp = temp / 10;
digit3 = temp % 10;
temp = temp / 10;
digit4 = temp % 10;
digit5 = temp / 10;

if ( digit1 == digit5 && digit2 == digit4 )


cout << number << " is a palindrome" << endl;
else
cout << number << " is not a palindrome" << endl;

return 0;
}

4
Part IV Repetition

4. [6 Marks] Count the Number of Digits in a Given Integers: Write a program that reads integers,
find and print how many digits in each integer, see sample input / output below.

Sample input/output:

#include <iostream>
using namespace std;

int main()
{
int number, temp, k;

cout << "Enter an integer, -1 to stop: ";


cin >> number;
while(number != -1)
{
temp = number;
k = 0;
while(temp > 0)
{
temp = temp / 10;
k++;
}

cout << number << " has " << k << " digits" << endl;

cout << "Enter an integer, -1 to stop: ";


cin >> number;
}

return 0;
}

You might also like