SYCS-135 Introduction to Programming -
Fall 1999-2
Examination II - 100 points
Name:
___________________________________���
ID: _________________________
There are 36 questions on this exam.� The number of points N each question is worth in indicated by [N]
at the beginning of the question narrative.
You should read each question carefully before answering.� If you must erase, do so completely.� The Exam is closed book.
��������������� ____ 1.��������������� [2] An example of a logical
(Boolean) expression is an arithmetic expression followed by a relational �������������� ��������������� operator
followed by an arithmetic expression.
��������������� ��������������� A)��������������� True
��������������� ��������������� B)��������������� False
��������������� ____ 2.��������������� [2] This question is about
short-circuit evaluation of logical expressions. Consider the following ������������� ��������������� ��������������� expression in some imaginary
programming language (not C++):
�������������������������������
������������������������������� � (N > 5) AND (K / N < 12)
�������������������������������
������������������������������� � If N equals 0 when this expression is
evaluated, which of the
������������������������������� � following statements about the expression is
true?
��������������� ��������������� A)��������������� It causes a divide-by-zero error only if the language
does not use short-circuit evaluation.
��������������� ��������������� B)��������������� It causes a divide-by-zero error whether or not the
language uses short-circuit evaluation.
��������������� ��������������� C)��������������� It never causes a divide-by-zero error.
��������������� ��������������� D)��������������� It causes a divide-by-zero error only if the language
uses short-circuit evaluation.
��������������� ____ 3.��������������� [2] The expression� !(n < 5)�
is equivalent to the expression�
n > 5.
��������������� ��������������� A)��������������� True
��������������� ��������������� B)��������������� False
��������������� ____ 4.��������������� [2] Given the module description
�������������������������������
������������������������������� � PRINT AVERAGE (Level 1)
������������������������������� � Print a heading
������������������������������� � Print sumOfValues / countOfValues
�������������������������������
������������������������������� � the following is an appropriate module
precondition:
������������������������������� ��� "sumOfValues is assigned AND
countOfValues does not
������������������������������� ��� equal 0."
��������������� ��������������� A)��������������� True
��������������� ��������������� B)��������������� False
____ 5.�� [2] The code
segment
�������������������������������
��������������� ��������������� if (speed <= 40)
������������� ����������������� ���cout << "Too slow";
��������������� ��������������� if (speed > 40 && speed
<= 55)
������������������������������� �� cout << "Good speed";
��������������� ��������������� if (speed > 55)
������������������������������� �� cout << "Too fast";
�������������������������������
��������������� ��������������� could be written equivalently as
�������������������������������
������������������������������� � if (speed <= 40)
������������������������������� ������ cout << "Too slow";
������������������������������� � else if (speed <= 55)
������������������������������� ����� cout << "Good speed";
������������������������������� � else
������������������������������� ����� cout << "Too fast";
��������������� ��������������� A)��������������� True
B)
False
��������������� ____ 6.��������������� [2] Which of the following is
not a C++ relational operator?
��������������� ��������������� A)��������������� <
��������������� ��������������� B)��������������� !=
��������������� ��������������� C)��������������� ==
��������������� ��������������� D)��������������� >=
��������������� ��������������� E)��������������� &&
____
7.�� [2] If p is a Boolean variable, which
of the following logical expressions always has the value FALSE?
��������������� A)��������������� p && p
��������������� B)��������������� p || p
��������������� C)��������������� p && !p
��������������� D)��������������� p || !p
��������������� E)��������������� b and d above
��������������� ____ 8.��������������� [2] Which logical operator (op)
is defined by the following table?
��������������� ��������������� (T and F denote TRUE and FALSE.)
�������������������������������
������������������������������� � P�
Q�� P op Q
������������������������������� � ------------
������������������������������� � T�
T�� T
������������������������������� � T�
F�� F
������������������������������� � F�
T�� F
������������������������������� � F�
F�� F
��������������� A)��������������� OR
��������������� B)��������������� NOT
��������������� C)��������������� AND
��������������� D)��������������� none of the above
��������������� ____ 9.��������������� [3] What is the output of the
following C++ code fragment?
������������������������������� � (Be careful here.)
�������������������������������
������������������������������� � int1 = 120;
������������������������������� � cin >> int2;���������� // Assume user types 30
������������������������������� � if ((int1 > 100) && (int2 = 50))
������������������������������� ��� int3 = int1 + int2;
������������������������������� � else
������������������������������� ��� int3 = int1 - int2;
������������������������������� � cout << int1 << ' ' <<
int2 << ' ' << int3;
��������������� ��������������� A)��������������� 120 30 150
��������������� ��������������� B)��������������� 120 30 90
��������������� ��������������� C)��������������� 120 50 70
��������������� ��������������� D)��������������� 120 30 70
��������������� ��������������� E)��������������� 120 50 170
��������������� ____ 10.��������������� [3] What is the value of loopCount
after control exits the following loop?
� ������������� ��������������� loopCount
= 1;
������������������������������� � while (loopCount <= 145)
������������������������������� � {
������������������������������� ��� alpha = alpha + 7;
������������������������������� ��� loopCount++;
������������������������������� � }
��������������� ��������������� A)��������������� 146
��������������� ��������������� B)��������������� 1
��������������� ��������������� C)��������������� 145
��������������� ��������������� D)��������������� 144
��������������� ____ 11.��������������� [3] After execution of the
following code, what is the value of length?
������������������������������� � (count and length are of type int.)
�������������������������������
������������������������������� � length = 5;
������������������������������� � count = 4;
������������������������������� � while (count <= 6)
������������������������������� � {
������������������������������� ��� if (length >= 100)
������������������������������� ����� length = length - 2;
������������������������������� ��� else
������������������������������� ����� length = count * length;
������������������������������� ��� count++;
��������������� ��������������� }
������������������������������� �
��������������� ��������������� A)��������������� 100
��������������� ��������������� B)��������������� 600
��������������� ��������������� C)��������������� 98
��������������� ��������������� D)��������������� 20
E)
none of the above
��������������� ____ 12.��������������� [3] What is the output of the
following code fragment? (finished is a Boolean variable, and firstInt ���������� ��������������� ��������������� and secondInt are of type int.)
�������������������������������
������������������������������� � finished = FALSE;
������������������������������� � firstInt = 3;
������������������������������� � secondInt = 20;
������������������������������� ��while
(firstInt <= secondInt && !finished)
������������������������������� ��� if (secondInt / firstInt <= 2)�� // Reminder: integer division
������������������������������� ����� finished = TRUE;
������������������������������� ��� else
������������������������������� ����� firstInt++;
������������������������������� � cout << firstInt << endl;
��������������� ��������������� A)��������������� 5
��������������� ��������������� B)��������������� 9
��������������� ��������������� C)��������������� 8
��������������� ��������������� D)��������������� 3
��������������� ��������������� E)��������������� 7
��������������� 13.� [2] The condition that causes a loop to be
exited is called the ____________________.
��������������� 14.� [2] A(n) ____________________-controlled
loop is an event-controlled loop whose event is the input of a ������ ��������������� special
value.
��������������� 15.� [2] A(n) ____________________-controlled loop
is an event-controlled loop that uses a Boolean variable ��������������� to
record its event.
��������������� 16.� [2] A(n) ____________________ is an input
statement located before the While loop that processes its ������� ��������������� input
data.
��������������� ____ 17.��������������� [2] When parameters are passed
between the calling code and the called function, parameters and ��� ��������������� ��������������� arguments are matched by:
��������������� ��������������� A)��������������� their names
��������������� ��������������� B)��������������� their relative positions in the parameter and
argument lists
��������������� ��������������� C)��������������� whether they are inputs to or outputs from the
function
��������������� ��������������� D)��������������� their data types
��������������� ____ 18.��������������� [2] Parameter passage by
reference is used if a parameter's data flow is
��������������� ��������������� A)��������������� one-way, into the function.
��������������� ��������������� B)��������������� one-way, out of the function.
��������������� ��������������� C)��������������� two-way, into and out of the function.
��������������� ��������������� D)��������������� a and b above
��������������� ��������������� E)��������������� b and c above
��������������� ____ 19.��������������� [2] Given the function prototype
�������������������������������
������������������������������� � void Fix( int&, float );
�������������������������������
��������������� Which of the
following is an appropriate function call? (someInt is of type int, and
someFloat is of type float.)
��������������� A)��������������� Fix(24, 6.85);
��������������� B)��������������� someFloat = 0.3 * Fix(someInt,
6.85);
��������������� C)��������������� Fix(someInt + 5, someFloat);
��������������� D)��������������� a and c above
��������������� E)��������������� none of the above
��������������� ____ 20.��������������� [3] Given the function
definition
�������������������������������
������������������������������� � void Twist( int� a,
������������������������������� ������������� int& b )
������������������������������� � {
������������������������������� ����� int c;
�������������������������������
������������������������������� ����� c = a + 2;
������������������������������� ����� a = a * 3;
������������������������������� ����� b = c + a;
������������������������������� � }
�������������������������������
������������������������������� ��� what is the output of the following code
fragment that invokes Twist?
������������������������������� ��� (All variables are of type int.)
�������������������������������
������������������������������� � r = 1;
������������������������������� � s = 2;
������������������������������� � t = 3;
������������������������������� � Twist(t, s);
������������������������������� � cout << r << ' ' << s
<< ' ' << t << endl;
��������������� ��������������� A)��������������� 5 14 3
��������������� ��������������� B)��������������� 1 14 3
��������������� ��������������� C)��������������� 1 14 9
��������������� ��������������� D)��������������� 1 10 3
��������������� ��������������� E)��������������� none of the above
��������������� ____ 21.��������������� [3] What is the output of the
following code fragment? (All variables are of type int.)
�������������������������������
������������������������������� � alpha = 3;
������������������������������� � beta = 20;
������������������������������� � if (beta > 10)
������������������������������� � {
������������������������������� ����� int alpha = 5;
�������������������������������
������������������������������� ����� beta = beta + alpha;
������������������������������� � ����cout << alpha << ' ' << beta << endl;
������������������������������� � }
������������������������������� � cout << alpha << ' ' <<
beta << endl;
��������������� ��������������� A)��������������� 5 25
������������������������������� ��������������� 3 25
��������������� ��������������� B)��������������� 3 20
��������������� ��������������� C)��������������� 5 25
������������������������������� ��������������� 3 20
��������������� ��������������� D)��������������� 5 25
������������������������������� ��������������� 5 25
��������������� ��������������� E)��������������� 3 25
������������������������������� ��������������� 3 25
��������������� ____ 22.��������������� [2] In the following function,
the declaration of beta includes an initialization.
�������������������������������
������������������������������� � void SomeFunc( int alpha )
������������������������������� � {
������������������������������� ����� int beta = 25;
������������������������������� ����� ...
������������������������������� � }
�������������������������������
��������������� ��������������� Which of the following
statements about beta is false ?
��������������� ��������������� A)��������������� It is initialized once only, the first time the
function is called.
��������������� ��������������� B)��������������� It is initialized each time the function is called.
��������������� ��������������� C)��������������� It cannot be reassigned a different value within the
function.
��������������� ��������������� D)��������������� a and c above
��������������� ��������������� E)��������������� b and c above
��������������� ____ 23.��������������� [2] In the following function,
the declaration of beta includes an initialization.
�������������������������������
������������������������������� � void SomeFunc( int alpha )
������������������������������� � {
������������������������������� ����� static int beta = 25;
������������������������������� ����� ...
������������������������������� � }
�������������������������������
��������������� ��������������� Which of the following
statements about beta is false ?
��������������� ��������������� A)��������������� It is initialized once only, the first time the
function is called.
��������������� ��������������� B)��������������� It is initialized each time the function is called.
��������������� ��������������� C)��������������� It cannot be reassigned a different value within the
function.
��������������� ��������������� D)��������������� a and c above
��������������� ��������������� E)��������������� b and c above
��������������� ____ 24.��������������� [2] Given the function
definition
�������������������������������
������������������������������� � bool IsZip( /* in */ float someFloat )
������������������������������� � {
������������������������������� ����� return (someFloat == 0.0);
������������������������������� � }
�������������������������������
��������������� ��������������� what is the value of the
expression� IsZip(2.4)� ?
��������������� ��������������� A)��������������� true
��������������� ��������������� B)��������������� 2.4
��������������� ��������������� C)��������������� false
��������������� ��������������� D)��������������� "someFloat == 0.0"
��������������� ��������������� E)��������������� 0.0
������� 25.�� [2]
Write the If statement for the following logic:� Only Sophmores (class_code = �S�) who have GPA�s between 2.5 and
3.5 will have their scholarship_amount increased by $500�
�������������������������������
������������������������������� �
__________________________________________________________________________
������������������������������� �
___________________________________________________________________________
������������������������������� � __________________________________________________________________________
������������������������������� ��
___________________________________________________________________________.
��������������� ____ 26.��������������� [2] The function heading
�������������������������������
������������������������������� � float TenToThePower( /* in */ int n )
�������������������������������
is for a function that returns 10.0 raised
to any integer power.Which of the following statements stores into someFloat
the value 10.0 raised to the power someInt?
�������������������������������
��������������� ��������������� A)��������������� TenToThePower(someInt);
��������������� ��������������� B)��������������� TenToThePower(someInt) = someFloat;
��������������� ��������������� C)��������������� someFloat = TenToThePower(someInt);
��������������� ��������������� D)��������������� someInt = TenToThePower(someFloat);
��������������� ��������������� E)��������������� TenToThePower(someFloat, someInt);
27.�
[2] The ____________________ of an identifier is the region of program
code where it is legal to reference that identifier.
28.�
[2] The ____________________ of a variable is the period of time during
program execution when the variable has memory allocated to it.
____
29. [2] If a module is supposed to print a
line of asterisks of a given length, which is more appropriate to use--a
value-returning function or a void function?
��������������� ��������������� A)��������������� a value-returning function
��������������� ��������������� B)��������������� a void function
____
30. [2] Any Switch statement that has a
break statement after each case alternative can be replaced by a nested
If-Then-Else structure.
��������������� ��������������� A)��������������� True
��������������� ��������������� B)��������������� False
____ 31.��������������� [3]
To produce the output� 2 4 6 8 10, what
is the loop condition for the following loop?
�������������������������������
������������������������������� � n = 0;
������������������������������� � do
������������������������������� � {
������������������������������� ����� n = n + 2;
������������������������������� ����� cout << n << ' ';
������������������������������� � } while ( ??? );
�������������������������������
��������������� A)��������������� n < 10
��������������� B)��������������� n > 8
��������������� C)��������������� n <= 10
��������������� D)��������������� n < 8
��������������� E)��������������� n >= 2
��������������� ____ 32.��������������� [3] What is the output of the
following code fragment if the input value is 'G'?
�������������������������������
������������������������������� � cin >> inputChar;
������������������������������� � switch (inputChar)
������������������������������� � {
������������������������������� ����� case 'A' : cout << 1;
������������������������������� ���������������� break;
������������������������������� ����� case 'Q' : cout << 2;
������������������������������� ���������������� break;
������������������������������� ����� case 'G' :
������������������������������� ����� case 'M' : cout << 3;
������������������������������� ���������������� break;
������������������������������� ����� default�
: cout << 4;
������������������������������� �
��������������� A)��������������� 3
��������������� B)��������������� 4
��������������� C)��������������� There is no output.
��������������� D)��������������� 2
��������������� E)��������������� 1
33.� [2] In C++, the ____________________
statement is the looping control structure whose body is certain to execute at
least once.
34.��
[8]� The following code segment
contains logical errors that will yield erroneous results.� Write the correct code to the right of the
given code that will write out all integers between 1000 and 0 inclusive that
are divisible by 4, in descending order (i.e., 1000, 996, 992, ......., 8, 4,
0).
��������������� ��� //INCORRECT CODE���� � |��������������� //CORRECT CODE
������������������������������� ������������������������������� � |
������ �������� int k = 1000;���������������������� � |
������ �������� while (k >= 0)���������������������� � |
���� ���������� �{������������ ������������������������������� � |
��� ����������� ��������������� k =
k - 4; ��������������� ��|
��� ����������� ��������������� cout
<< k;������������ � |
��������������� }������������� ������������������������������� � |
��������������� 35.��������������� [6] What is wrong with each of
the following C=++ function definitions?
��������������� a.��������������� void Test1(int m, int n)
{
������������������������������� ��������������� return 3 * n + m;
��������������� ��������������� �}
�������������������������������
��������������� b.��������������� float Test2 (int i, float x)
��������������� ��������������� {
������������������������������� ��������������� i = i + 7;
������������������������������� ��������������� x = 4.8 + float(i)
��������������� ��������������� }������������� ������������������������������� �������������������������������
��������������� ��������������� a.__________________________________________________________________________
������������������������������� �
___________________________________________________________________________
��������������� ��������������� b.
__________________________________________________________________________
������������������������������� ��
___________________________________________________________________________
36.� [12]�
The following program adds two times together.� Both times are expressed in hours, minutes and seconds. First the
program prompts the user to enter the current time. Then the program prompts
the user to enter the amount of time they wish to add.� The program displays the result and
terminates.
The function addtime
adds the specified hours, minutes and seconds to the current time.� In this problem you must write the function
body of addtime.�
You can assume that the input data is always correct.� The following is a sample of how the program
should work:
�SAMPLE RUN: ����
������������������������������� ��������������� Enter current time(hh mm ss): 12
55 45��� ���������������
������������������������������� ��������������� Enter clock adjustment (hh mm
ss): 1� 10� 20��������������������������� �������������������������������
������������������������������� ��������������� Time reset to� 2: 6: 5������ ������������������������������� �������������������������������
________________________________________________________________________
#include <iostream.h>
void addtime (int&, int&, int&, int, int, int); ��������� ������������������������������� ��������������� ��������������� //*****
function prototype *****/
/*****
main function *****/
int main ()
{
��������������� ��������������� int current_hours, current_mins,
current_secs;������� ��������������� //current clock setting
��������������� ��������������� int inhours, inmins, insecs;������������������ �� ������������������������������� ��������������� // hours, minutes & seconds
needed to
������������������������������� ������������������������������� ������������������������������� ������������������������������� ��������������� // added to current time
��������������� ��������������� while (cin)
��������������� ��������������� {
������������������������������� ��������������� cout << "\nEnter
current time (hh mm ss): ";������������ //
prompt user to enter current time
������������������������������� ��������������� cin >> current_hours
>>current_mins >> current_secs; ��������������� //
get current time
������������������������������� ��������������� cout << setw(2) <<
current_hours << ":"
�� ���������������������������� ��������������� <<
setw(2) << current_mins << current_secs << endl; ����� // mirror the input
������������������������������� ��������������� cout << "\nEnter
clock adjustment (hh mm ss):"; ��������������� //
prompt user for time to add
��������������� ��������������� ��������������� cin
>> inhours >> inmins >> insecs;������������� ��������������� ��������������� // get time to be added
������������������������������� ��������������� cout << setw(2) <<
inhours << ":" << setw(2) << inmins
�� ���������������������������� ��������������� <<
":" << setw(2) << insecs <<endl;����� ��������������� ��������������� // mirror the input
������������������������������� ��������������� addtime (current_hours,
current_mins, current_secs, inhours, inmins, insecs);
������������������������������� ������������������������������� ������������������������������� ������������������������������� ��������������� // function call to the function
you
������������������������������� ������������������������������� ������������������������������� ������������������������������� ��������������� // must write.� Remember that you
������������������������������� ������������������������������� ������������������������������� ������������������������������� ��������������� // should display the results as
������������������������������� ������������������������������� ������������������������������� ������������������������������� ��������������� // shown above in the sample run
��������������� ��������������� }
��������������� ��������������� return 0;
}
/***** USE THE VARIABLES SHOWN *****/
/***** IN THE FUNCTION HEADER BELOW *****/
void addtime(int& hours, ��� ��������������� // IN the current hours, OUT the
total hours
������������������������������� �������� int& minutes, ������ ��������������� //
IN the current minutes, OUT the total minutes
������������������������������� �������� int&, seconds������ ��������������� //
IN the current seconds, OUT the total seconds
������������������������������� �������� int h, ��������������������� ��������������� //
IN: the hours to be added
������������������������������� �������� int m, �������������������� ��������������� //
IN: the minutes to be added
������������������������������� �������� int s)���������������������� ��������������� //
IN: the seconds to be added
/***** WRITE THE FUNCTION BODY HERE *****/
{
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
}