Introduction To Programming in C++ Summer II 2002: Midterm Test
Introduction To Programming in C++ Summer II 2002: Midterm Test
IN
IA
ST
may result in severe
VI RGI N
I TUTE
penalties. Failure to
adhere to these
AN
Y
IT directions will not
D
ST U T P R O S I M
S
AT
E U NI V E
R constitute an excuse
or defense.
• Print your name and 5 digit course index number in the space provided below.
• Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.
Code Form A on the Opscan.
• Choose the single best answer for each question — some answers may be partially correct. If you mark
more than one answer, it will be counted wrong.
• Unless a question involves determining whether given C++ code is syntactically correct, assume that it is.
The given code has been compiled and tested, except where there are deliberate errors. Unless a question
specifically deals with compiler #include directives, you should assume the necessary header files have
been included.
• Note that questions about printed values disregard formatting completely.
• In questions/answers which require a distinction between integer and real values, integers will be
represented without a decimal point, whereas real values will have a decimal point, [1044 (integer), 1044.0
(real)].
• When you have completed the test, sign the pledge at the bottom of this page and turn in the test and your
Opscan.
• This is a closed-book, closed-notes examination. No calculators or other electronic devices may be
used during this examination. You may not discuss (in any form: written, verbal or electronic) the
content of this examination with any student who has not taken it. You must return this test form
when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code
violation.
• There are 40 multiple-choice questions and 1 extra credit question.
• Mark your answers on the test form, for future reference, and on the Opscan. The answers you mark on the
Opscan form will be considered your official answers.
Name ID
print name
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
Signature
1. Which of the following represents the correct logic for a read-until-input-failure
loop?
a. while ( input succeeded )
{
// read next data item
// process last data read
}
int main()
{
int x=4,y=3; // line 1
const int i = 10; // line2
x = x + i; // line 3
i = y*2; // line 4
}
a. Variables are not declared before use
b. There cannot be 3 variables in such a simple program
c. There is an error in line 3
d. There is an error in line 4
e. Both c and d
[Questions: 9- 13] If the input file looks as (This is the complete file – the file ends
after this information.)
12.34 GGG23
[Questions: 14,15]
For questions 14 and 15, consider execution of the following switch statement:
int Enter = 10;
cin >> Enter;
switch (Enter)
{
case 1: Enter = -4;
case 2: Enter = -6;
case 4: break;
case 6: Enter = -8;
break;
default: Enter = -1;
}
14. What would the value of Enter be after execution of this code if the value read for
Enter were 4?
a. –4
b. –6
c. –8
d. –1
e. 10
f. None of these
15. What would the value of Enter be after execution of this code if the value read for
Enter were 1?
a. –4
b. –6
c. –8
d. –1
e. 10
f. None of these
16. What is the logical condition under which the following while loop will
terminate?
int Beta = 5;
while (Beta >= 0 && Beta < 10)
{
cout << Beta << endl;
cin >> Beta;
}
17. When designing a specification for an input file, which of the following would be
a poor choice for a sentinel value?
a. a value of 29 for voter ages
b. a value of 1025 for SAT scores
c. a value of -1 for student heights
d. a value of "No one" for student names
e. All of them
f. a and b only
g. a and c only
h. b and c only
i. c and d only
j. None of these
[Questions 18-21] Which of the following can be used to describe the following
identifiers?
22. Any line in your program beginning with a # symbol is information given to a ___
a. Compiler
b. Operating system
c. Preprocessor
d. RAM
e. None of the above
24. What is the value of temp1 and temp2 after executing the function sum?
void sum(int,int&);
int main()
{
int temp1=5, temp2 = 23;
sum(temp1,temp2);
}
[Questions 25 - 28] The following code fragment uses a function sum to calculate the
sum of two numbers.
sum(a,b,result);
}
void sum(int a, const int& b, int& c)
{
c = a+b;
}
29. For the following code fragment, how many times does sum get incremented? In
other words how many times is line X executed in the following program?
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
sum++; // line X
}
}
a. 4
b. 3
c. 9
d. 2
e. None of the above
31. What is the value printed for variable delta if the following code is executed?
a. 0
b. 1
c. 2
d. 6
e. 4
f. 7
while(true)
{
cout << i;
if(i==5)
break;
i++;
}
a. Forever
b. 5 times
c. 10 times
d. 0 times
e. None of the above
b. ***
***
c. ******
d. *
**
***
a. True
b. False
c. Junk
d. None of the above
36. If I wanted to execute some piece of code based on what range my angle variable
is, which of the following statements would I use?
a. if statement
b. switch statement
c. while statement
d. for statement
e. None of the above
c = a++ + ++b;
a. 13
b. 14
c. 15
d. 12
e. None of the above