Test 1
For the following 3 questions, select the value of the given C++ arithmetic
expression. Note that the presence of a
decimal indicates a double value, rather than an int.
1) 7/4 – 3/4 1 2.5 3 4 none of these
2) 15/3*2+1 1 2 3 4 none of these
3) 4.0 + 5/2 6 6.0 6.5 not allowed none of these
For the next 3 questions, select the value assigned to the relevant variable, given the declarations:
int IntVar;
double
DecVar;
4) IntVar = 19 / 5; 3 3.0 3.8 4 none of these
5) DecVar = 5 + 2.7; 7 7.7 8 8.0 none of these
For the following 4 questions, suppose the (file) input stream In contains the following 5 lines of data (there's one tab character between columns and a newline character immediately after the last character on each line):
55 23 72 40 Gomer
17 30 95 28 Goober
6 34 82 66 Opie
19 62 36 21 Floyd
8 49 45 33 Bea
What is the value of each of the indicated variables after the execution of the following program segment?
int Zero = 0, One = 1, Two = 2, Three = 3, Four =
4;
string
First = "Andy", Second = "Barney";
In
>> Zero >> One >> Two;
In.ignore(100,
'\n');
In
>> Two >> One >> Zero;
In
>> Four;
In.ignore(100,
'\t');
In
>> First;
In.ignore(100,
'\n');
In
>> Second;
1 2 3 4 5
6) Zero 55 72 30 95 none of these
7) Four 40 28 30 17 none of these
8) First "\t" "Goober" "Opie" "6" none
of these
9) Second "Opie" " 6" "6" "34" none of these
For the next 2 questions, assume the input file stream iFile is connected to an input file whose contents are:
47 A321
(There's a single space separating the '7' from the 'A'.) Consider execution of the following code fragment immediately after the file stream has been opened:
int i1 = 0;
char ch1 = 'x', ch2 = 'y';
iFile >> i1;
iFile.get(ch1);
iFile >> ch2 >> i1;
10) The resulting value of the variable ch1 would be:
1) '4'
2) '7'
3) ' ' (a space)
4) 'A'
5) '3'
6) '2'
7) '1'
8) '\n'
9) none of these
11) The resulting value of the variable i1 would be:
1) 4
2) 47
3) 3
4) 32
5) 321
6) ASCII code for 'A'
7) none of these
For the next 2 questions, consider writing a program that must read lines of data formatted in the following way. Each line will contain the name of a team, followed by a tab, followed by a score, followed by a tab, followed by another team name, followed by a tab, followed by another score, followed by a newline. For example:
Washington
Deadskins<tab>21<tab>Dallas Plowboys<tab>27<newline>
Assume that an input stream variable, In, has just been opened on such a file, and that the following variables have been declared:
string
Team1, Team2;
int Score1, Score2;
12) Which of the following statements will correctly read the first team name into the variable Team1?
1) In >> Team1;
2) getline(In, Team1);
3) getline(In, Team1, '\t');
4) 1, 2 and 3
5) 1 and 2 only
6) 1 and 3 only
7) 2 and 3 only
8) none of these
13) Assuming that the first team name, and the tab following it, have been read, which of the following statements will correctly read the first score into the variable Score1?
1) In >> Score1;
2) getline(In, Score1);
3) getline(In, Score1, '\t');
4) 1, 2 and 3
5) 1 and 2 only
6) 1 and 3 only
7) 2 and 3 only
8) none of these
For the next 2 questions, assume the input file stream ifile is connected to an input file whose contents are:
47 A321
(There's a single tab separating the '4' from the '1'.) Consider execution of the following code fragment immediately after the file stream has been opened:
int i1;
char ch1 = 'x',
ch2 = 'y', ch3 = 'z';
ifile >>
ch1 >> i1 >> ch2 >> ch3;
14) The resulting value of the variable i1 would be:
1) 4
2) 47
3) 7
4) 3
5) 32
6) 2
7) 321
8) none of these
15) The resulting value of the variable ch2 would be:
1) '4'
2) '7'
3) ' ' (a space)
4) 'A'
5) '3'
6) '2'
7) '1'
8) '\n'
9) none of these
For the next 3 questions, assume the following variable declarations and initializations:
bool
Burke,
Hare = false;
int a = 0,
b = 1, c = 3;
Determine the value assigned by each of the following statements to the relevant Boolean variable, or if there's something (syntactically) wrong with the expression; choose from the following answers:
1 true 2 false 3 syntax error
16) Burke =
2 + b < c ;
17) Hare =
!( Hare );
18) Burke = (a + b < c
|| a + b == c);
For the next 4 questions, assume the input file stream Fred is connected to an input file whose contents are:
3.14 2.71828 43 0 27
24 1.8 -12 3 5 51 8
45.738 17.9 19 32 91
(There's a newline character at the end of each line, and a single space separating values on the same line.) Consider execution of the following code fragment immediately after the file stream has been opened:
int
anInt1, anInt2, anInt3, anInt4;
float aFloat1, aFloat2;
Fred.setf(ios::floatfield, ios::fixed);
Fred.setf(ios::showpoint);
Fred >> aFloat1 >> anInt1;
cout << aFloat1 <<
anInt1; // A
Fred.ignore(100, '\n');
Fred.ignore( 5, '\n');
Fred >> anInt1 >> aFloat2 >> anInt3;
Fred.ignore( 80, '\n');
cout << anInt1 <<
setprecision(1) << aFloat2 << anInt3; // B
Fred >> aFloat2;
cout << setprecision(2) <<
aFloat2; // C
19) In the statement labeled A, the value printed for the variable anInt1 would be:
1) 3
2) 0
3) 2
4) 24
5) 1
6) 8
7) –12
8) 45
9) none
of these
20) In the statement labeled B, the value printed for the variable aFloat2 would be:
1) 1.8
2) 0.8
3) –12.0
4) 3.0
5) 5.0
6) 51.0
7) 8.0
8) -12
9) none
of these
21) In the statement labeled B, the value printed for the variable anInt3 would be:
1) 43
2) 0
3) 27
4) 24
5) 1
6) 8
7) –12
8) 45
9) none
of these
22) In the statement labeled C, the value printed for the variable aFloat2 would be:
1) 45
2) 45.0
3) 45.7
4) 45.73
5) 45.74
6)
45.738
7) 46
8) 46.0
9) none
of these
23) What is the value of the variable Z after the following code is executed?
int W = 5, X = 9, Y = 5, Z = 1;
if (X % Y >= 2 + W) {
Z++;
if (Y–3*W >= -X)
Z--;
else
Z++;
}
else {
Z = -1;
}
1) –1
2) 0
3) 1
4) 2
5) 3
6) the
code contains a syntax error
7) none
of the above
24) What output will the following program produce? (Be careful this may be tricky.)
#include <iostream>
using namespace std;
void main( ) {
int Score = 87, Rank = 2;
if (Score >= 95)
if (Rank <= 5)
cout << "Nice
job!";
else
cout << "Good job!";
}
1) Nice job!
2) Good job!
3) "Nice job!"
4) "Good job!"
5) both 1 and 2
6) both 3 and 4
7) No output is produced.
8) none of these
25) What
is the value printed for the variable Delta if the following code is
executed?
int Delta = 0, X = 4;
if ( X / 2 == 1 )
Delta = Delta + X;
X--;
if ( X / 2 == 0 )
Delta = Delta + X;
X--;
if ( X / 2 == 0 )
Delta = Delta + X;
cout << "Delta = "
<< Delta << endl;
1) 0
2) 1
3) 2
4) 3
5) 4
6) 5
7) 6
8) None of these
For the next 2 questions, assume the declarations:
string FName = "Mickey",
LName = "Mouse";
int
Age = 74;
26) What would be printed by the
statement: cout << LName + ", " + FName;
1) Mickey Mouse
2) Mickey, Mouse
3) Mouse, Mickey
4) The
statement is not allowed.
5) None
of these
27) Which of the following statements would print
the name Mickey, followed by the value of Age,
so that the value 74 would be right-justified to column 15?
1) cout << FName << Age;
2) cout << FName << setw(15) << Age;
3) cout << FName << setw(15 – FName.length()) << Age;
4) All of these
5) 2 and 3 only
6) None of these
For the next 2 questions, 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;
}
What would the value of Enter be after execution of this code if the value read for Enter were:
Enter 1 2 3 4 5 6
28) 2 -1 -4 -6 -8 10 none of these
29) 5 -1 -4 -6 -8 10 none of these
30) What is the first step in Polya's four-step process?
1) Design a solution.
2) Understand the problem.
3) Test the solution.
4) Implement the solution.
5) Find an excuse.
6) none of these
Key:
Let me know if there is any problem
with this key.