Midterm Exam - Solution
Midterm Exam - Solution
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.
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;
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;
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 << number << " has " << k << " digits" << endl;
return 0;
}