Notes: a) Please answer the questions only in the provided space after each question.
b) Duration is 100 minutes.
c) Closed-book, closed-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)
a) (1 point) Write the cout statement (not the whole program; just one statement) that
displays your first name, last name, your ID number and your SU email address on the screen.
b) (2 points) What does the function return type of void mean? Please circle the correct
answer.
c) (2 points) One of the statements below is syntactically correct, but the others are wrong.
Which one is the correct one? Assume that the variables m and n are defined as int
variables.
a) 3 = m; b) n = 3; c) m + n = 3; d) 3 = m + n;
NAME, LASTNAME:
#include <iostream>
using namespace std;
int doSomethingElse(int y)
{
y++;
return y;
}
int main()
{
int x = 1, y = 8;
cout << "In main: Before: x=" << x << " and y=" << y << endl;
int z = doSomething(x+1, y);
cout <<"In main: After: x="<<x<<" y=" <<y<<" and z="<<z<<endl;
return 0;
}
int i, j = 5, k = 0, count = 0;
2 2 1 3
NAME, LASTNAME:
3)
a) (6 points) What is the output of the following program piece?
string s="ANA";
string t="5";
int i;
for (i=5; i>=1; i--)
{
s = s.substr(0, s.length()/2);
s = s + s;
t = t + t;
}
b) (3 points) In the following piece of code, num is an integer variable. Fill in the table
below to specify the output for different values of num.
if (2 <= num <= 5)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
2*7%(2-4-(3-8))+(3*(5+4-3))
-5 9
-2
6
3 18
14
20
NAME, LASTNAME:
4) (18 points)
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box
below and secure 4 points. If you sign the “not attempted” box below, you accept that you did
not answer this question and you will receive 4 points. In this case, your answer will not be
graded even if you write something as solution.
Not attempted
Write a function that takes two string parameters (lets name them source and sub). This
function:
− must return 1 if source does not contain sub.
− must return 2 if source contains sub, but source does not start with sub and does not end
with sub.
− must return 3 otherwise.
For example, if source is "borabora" and sub is "ra", then the function returns 3 since
"borabora " contains "ra", but it ends with "ra".
As another example, if source is "borabora" and sub is "bo", then the function returns 3 since
"borabora " contains "bo", but it starts with "bo".
As another example, if source is "borabora" and sub is "abo", then the function returns 2 since
"borabora" contains "abo", but it does not start with "abo" and does not end with "abo".
As another example, if source is "borabora" and sub is "cs", then the function returns 1 since
"borabora " does not contain "cs".
begin = source.find(sub);
end = source.rfind(sub);
if (begin == string::npos)
return 1;
else if (begin != 0 && end != source.length() - sub.length())
return 2;
else
return 3;
}
NAME, LASTNAME:
5)
a) (7 points) Fill in the box in the following program piece with an appropriate boolean
expression (condition) so that the output becomes "Bingo" if only two of the input numbers
are equal but the other one is different.
Do not add, delete or update anything else other than the expression that you will write in the
box.
int a, b, c;
cin >> a >> b >> c;
{
cout << "Bingo" << endl;
} Alternative Solutions (Paranthesis may be omitted here and in actual solution)
(a == b && b != c) || (a == c && c != b) || (b == c && c != a)
b) (7 points) What should be the initial value of the variable x in order for the following piece
of code to display 48 as the output? Please give your answer by writing in the box within the
code below.
int a, b;
int sum=0;
int x= 6 ;
int x = 4;
int y = 7;
bool b = false;
6) (16 points) The piece of program below is a partial solution for the following problem:
Write a function that reads 100 real numbers from keyboard and returns true if all of
these input numbers are non-negative (i.e. positive or zero). This function should
return false otherwise.
As soon as the function encounters a negative input number, it should not read the rest
of the numbers.
However, the program is incomplete. Complete this program by filling out the boxes with
appropriate expressions 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.
bool read_and_check ()
{ i<100 is also OK instead of i<=99
double input; b == true is also OK instead of b
int i;
bool b = true ;
7) (16 points) Write a program that creates a robot at position (7,0) facing east. Then, the
program should read two integer numbers (say X, Y). After that, your program will then move
the robot to (X, Y) position.
You may assume that X and Y values are entered as non-negative numbers, so you do not need
to make an input check against negative coordinates.
The direction of the robot at the destination is not important. In other words, your robot's
direction may be anything when it reaches (X, Y).
The explanations about GetInput (which is used to read values in Robot applications)
and Robot member functions are given in the next page for your convenience.
(7,0)
#include "Robots.h"
using namespace std;
int main ()
{
int X, Y;
Robot cs (7, 0, east); //east may be omitted
GetInput ("Please enter the X coordinate of destination", X);
GetInput ("Please enter the Y coordinate of destination", Y);
if (X>7)
{
cs.Move(X-7);
cs.TurnRight();
cs.TurnRight();
cs.TurnRight();
cs.Move(Y);
}
else
{
cs.TurnRight();
cs.TurnRight();
cs.Move(7-X);
cs.TurnRight();
cs.Move(Y);
}
return 0;
}
NAME, LASTNAME: