2006 Fall Midterm1 Solutions
2006 Fall Midterm1 Solutions
Notes: a) Please answer the questions only in the provided space after each question.
b) Duration is 100 minutes.
c) Close-book, close-notes, no calculators and computers. One A4 size cheat-note
is allowed.
d) There must be eight pages (including this one). Please check it out!
QUESTIONS
1) (12 points)
Write a function that takes a string parameter (let's name it str) and returns another string. The
returned string must be composed as follows: the first two characters will be the first two
characters of str. To the end of this, str will be appended entirely.
If there are less than 2 characters in str, then the function must return an empty string (i.e., a
string with no characters in it).
#include <iostream>
using namespace std;
int main()
{
int a=1, b=10;
if (a>0)
{
cout << "main first if" << endl;
a--;
}
else if (a==0)
{
cout << "main second if" << endl;
a--;
}
else if (a<0)
cout << "main second if" << endl;
a=7;
b = compute(b,a);
double x= divide (10,4);
cout << "main x: " << x << endl;
cout << "main end: " << a << " " << b << endl;
return 0;
}
main first if
compute begin: 10 7
compute end: 70 76
main x: 2
main end: 7 1
NAME, LASTNAME:
3)
a) (5 points) What is the output of the following program piece?
int k;
int i;
for (k=pow(2,18),i=0;k>5;i++)
{
k/=2;
}
cout << i << " " << k << endl;
16 4
b) (4 points) Fill in the “Message Displayed” column of the table below according to
following program piece. If no message is displayed, put a dash ( ─ ) in the box.. Leaving a
box blank means you do not know the answer for this case!
if (x)
{ x y Message Displayed
cout << "aaa" << endl; aaa
if (y) true true bbb
cout << "bbb" << endl;
} true false aaa
else
cout << "ccc" << endl; ccc
false true
c) (6 points) Write the Boolean test expressions of the following nested if statements so that
the bodies of the statements give correct information about the values of integer variables a
and b and the program does not crash. Give your answer in the boxes below. No extra code
or variable is allowed other than the required boolean expressions.
{
cout << a << " / " << b << " is greater than 2" << endl;
}
This part may be
else if ( b!=0 && a/b <= 2 omitted
)
{
cout << a << " / " << b << " is less than or equal to 2" << endl;
}
else
{
cout << a << " / " << b << " is undefined since denominator is 0" <<endl;
}
NAME, LASTNAME:
4)
a) (1 point) Write the cout statement (not the whole program, just one statement) that
displays your first name, last name and ID number on the screen.
b) (1 point) What is the main difference between a parameter and a local variable?
d) (3 points) Is the following expression syntactically correct? If not, explain the syntax
problem(s). If so, give the result of the expression.
5+-4-+5*-3/+8-(-10)
e) (2 points) Is the following piece of code syntactically correct? If not, explain the syntax
problem(s). If so, give the values of the variables first, second and third.
No syntax errors. The values of three of them are the same and
62675.4 (6.26754e4 is also OK)
f) (2 points) Is the following piece of code syntactically correct? If not, explain the syntax
problem(s). If so, give the value of mychar.
5) (20 points)
Write a function that takes an integer parameter and returns true if the digit “1” occurs
exactly once in this integer; returns false otherwise.
For examples:
If the parameter is 591254, then the function should return true.
If the parameter is 219413, then the function should return false since there are more than one
1's.
If the parameter is 456, then the function should return false, since there are no 1's in this
integer.
Hint: You do not need to use loops in this question. Think of a string based solution!
6) (14 points)
Write a program that reads 20 integer numbers from keyboard and displays the
maximum among them.
However, the program is incomplete. Complete this program by filling out the boxes with
appropriate variables/expressions/arguments so that the program solves the above problem.
You are not allowed to delete or update anything in the program. Moreover, you cannot add
anything other than what you are going to write in the boxes.
#include <iostream>
i--;
}
cout << "max of these numbers is " << maxnum << endl;
return 0;
}
NAME, LASTNAME:
7) (18 points) Write a function that takes a robot and an integer (say n) as parameters. The
function should move the robot n cells towards north if n is a positive even number,
or n cells towards east if n is a positive odd number. During this movement, the robot should
put one thing on each cell visited. If n is not positive, then the robot should not move.
You may assume that there are enough amount of things in the robot's bag. You may also
assume that there are no walls in the robot world.
However, you cannot assume anything about the robot's current direction and position. These
data are not passed to the function as parameters. Your function should work properly no
matter current location and the direction of the parameter robot are.
Robot member functions are listed in the next page for your convenience. You are not
allowed to use any other member functions that we added for homework 5.
The heading of the required function is given below. Your solution should use this heading.
PickThing ()
Gets one thing from the current cell and put it in the bag of the robot object. If the cell
occupied by the robot is not empty, one thing is transferred to the robot’s bag. That means, the
cell content is reduced by one thing and the bag content of the robot is increased by one thing.
If the cell is empty, nothing happens.
PutThing ()
Gets one thing from the bag of the robot object and put it in the current cell. If the robot’s bag
is not empty, one thing is transferred to the cell occupied by the robot. That means, the cell
content is increased by one thing and the bag content of the robot is decreased by one thing. If
the bag is empty, nothing happens.