Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Page
1 of 14
Version
1
Student Name:
_________________________
Student Number:
_________________________
Instructions
1.
2.
3.
4.
5.
6.
Question
Maximum
Points
Your
Points
Question
Maximum
Points
10
12
12
10
15
11
14
8
YOUR TOTAL POINTS
Your
Points
Page 2 of 14
(5 points)
Which of the following are legal identifier names in C++? Mark the correct answers
with Y and incorrect answers with N in the space provided.
2.
a) AT&T
____________
b) Good-luck
____________
c) Ms_Word
____________
d) 7pm
____________
e) WHILE
____________
(10 points)
int a = 2, b = 3, c = 4;
double d = 5.1, e = 5.0, s = 15.3, t = 27.3;
int x = 16, y = 10, z = 27;
What is the value of each of the following expressions? (Each expression should be
evaluated with the same set of initial values as given above.)
a) e / y
____________________________
b) a * b / c + d
____________________________
c) t / b + a / b * z
____________________________
d) z / b * a / b * b
____________________________
e) z % b * a / b * z
____________________________
f) (int)(s / e)
____________________________
g) y / (double)b
____________________________
h) a * (b + 1) % c
_______________________________
i)
x++ + y
_______________________________
j)
x-- + y % ++b
_______________________________
Page 3 of 14
(8 points)
The following C++ program contains some syntax errors. For each erroneous line,
rewrite the corrected line in the space provided. A single line may contain zero or
more errors.
#include
(iostream.h)
________________________
_____________
int Main ( ) ;
________________________
_____________
{
________________________
_____________
integer a;
________________________
_____________
cout << 'a = ' <<
a;
________________________
_____________
cout endl;
________________________
_____________
return 0
________________________
_____________
}
/* end of program
________________________
_____________
4.
(4 points)
What is the value of variable count after the following code is executed?
int count, number;
bool done;
count = 1;
number = 2;
done = false;
while (!done) {
count++;
number = number * 2;
done = true;
Answer: ____________________
Page 4 of 14
Page 5 of 14
(6 points)
What is the value of variable a after executing each of the following code segments?
a)
int a = 2, b = 3, c = 4;
if (a >= b)
c = a + 2;
else if (c >= b)
b = a * 3;
else if (a <= b)
a += b;
Answer: ____________________
b)
int a = 2, b = 4, c = 3;
if (a < b)
if (b < c)
a += c;
else
a += b * c;
Answer: ____________________
c)
int a = 2, b = 3, c = 4;
if
(a >= b)
a += 1;
if (a/b == b/c)
a += b;
if (a <= b + c)
a += c;
Answer: ____________________
(8 points)
2
3
4
5
6
3 4 5 6 7 8 9
5 7 9
7
9
a)
limit = 10;
for (a = 1; a <= limit; a++) {
for (b = a; b >= 1; b--)
cout << " " << b;
cout << endl;
}
cout << endl;
b)
limit = 10;
for (a = 1; a <= limit; a++) {
for (b = limit; b >= a; b--)
cout << " " << b;
cout << endl;
}
cout << endl;
c)
limit = 10;
for (a = 1; a <= 5; a++) {
for (b = 1; b < limit; b = b + a)
cout << " " << b;
cout << endl;
}
cout << endl;
d)
limit = 10;
for (a = 1; a <= limit; a++) {
Answer: ____________________
Page 6 of 14
Page 7 of 14
(6 points)
What is printed to the screen after executing each of the following loop statements? An empty
space should be written as .
a)
Answer:
b)
int x = 0;
while (++x < 10)
if (x % 5)
cout << x << " ";
Answer:
c)
int x = 1;
do {
if (x % 3)
cout << x << " ";
} while (++x < 10);
Answer:
Page 8 of 14
(12 points)
What is printed to the screen after executing the following program? An empty space
should be written as .
#include <iostream.h>
void one(int a, int& b, int c) {
int d;
a = 15;
b = 14;
c = 13;
d = 12;
cout << a << " " << b << " " << c << " " << d << endl;
}
void two(int& a, int b, int& d) {
int c = 0;
d = a * b;
a = 7;
b = 99;
cout << a << " " << b << " " << c << " " << d << endl;
}
int main() {
int a = 10, b = 20, c = 30, d = 40;
one(a, b,
cout << a
two(a, b,
cout << a
return 0;
}
Answer:
c);
<< " " << b << " " << c << " " << d << endl;
d);
<< " " << b << " " << c << " " << d << endl;
Page 9 of 14
(12 points)
This pattern
*
*
*
* *
*
*
*
*
* *
*
is printed to the screen when the following program is executed. This solution uses one function
call which moves the beginning and end markers from one row to the next one. Complete the
missing parts in the program.
#include <iostream.h>
void move_markers(int& begin_pos, int& begin_direction,
int& end_pos, int& end_direction);
int main() {
const int EDGE_LEN = 5;
// length of each edge
int begin_pos = EDGE_LEN;
// position of begin marker
// in a row
int end_pos = EDGE_LEN;
// position of end marker
// in a row
int begin_direction = -1;
// direction of change of
// begin marker from one
// row to the next
// (left: -1; right: +1)
int end_direction = +1;
// direction of change of
// end marker from one
// row to the next
// (left: -1; right: +1)
int col_count;
// column counter
do {
for (_______________________; col_count <= end_pos; col_count+
+)
if (col_count == begin_pos || col_count == end_pos)
cout << ________________________________________;
else
cout << ________________________________________;
cout << endl;
move_markers(begin_pos, begin_direction, end_pos,
end_direction);
} while (________________________________________);
Page 10 of 14
return 0;
}
void move_markers(int& begin_pos, int& begin_direction,
int& end_pos, int& end_direction) {
// Update positions of begin and end markers, and the
// directions they move
if (begin_pos == 1) {
begin_direction = ____________________;
end_direction = ____________________;
}
// Compute position of begin marker for the next row
begin_pos = begin_pos + begin_direction;
// Compute position of end marker for the next row
end_pos = end_pos + end_direction;
}
Page 11 of 14
(15 points)
This output
*987654321
**87654321
***7654321
****654321
*****54321
******4321
*******321
********21
*********1
is printed to the screen when the following program is executed. Complete the program to make it
work properly.
#include <iostream.h>
int main() {
int row;
Page 12 of 14
Page 13 of 14
(14 points)
A void function called rotate takes three integers and rotates them in a counter-clockwise
direction. That is, the third integer is moved to the second place, the second to the first place, and
the first to the last place (i.e., third place). For example, if integer variables number1, number2,
and number3 have values 10, 20, and 30, respectively, then their values will become 20, 30, and
10, respectively, after executing the function call rotate(number1, number2, number3).
Give the complete function definition for rotate. There is no need for you to write the main
function and other code.
THE END
Page 14 of 14