Name:
Student ID:
DUE: 10 PM Sunday, October 25. Email instructor a text
file that contains your answers. Use "EE261 Quiz2" as subject of your
email.
1. What are the values of the two variables anInt and aChar right after the following code
snippet accepts the given input? [1%]
int anInt=0;
char aChar='A';
cin >> anInt >> aChar
>> anInt;
input: 1_23_4
Note: '_' represents a blank space.
Answer:
anInt=
aChar=
2. The following code snippet is meant to count the number of blanks
contained in a text file named "data.in", but it always prints 0. What
is wrong with it?
It is known that inFile is NOT in fail state. [1%]
1 ifstream inFile;
2 inFile.open("data.in");
3 int count=0;
4 char c;
5 inFile >> c;
6 while ( !inFile ){
7 if (c == ' ') {
8
++count;
9 }
10 inFile >> c;
11 }
12 cout << count << endl;
The file "data.in" contains the following content
=======BEGIN of data.in===========
To Be or Not to Be, This is the
Question.
========END of data.in============
Answer:
3. [1%]
a. In C++, arithmetic expressions such as a+b can be used anywhere a logical
expression is expected. True or
False
b. In C++, the condition of an IF statement of the form A && B can always be
rewritten as B && A
without changing the meaning of the statement. True or False
c. In C++, !(boolA || boolB)
can be rewritten equivalently as !boolA
&& !boolB. True
or False
d. In C++, one should never compare two floating values for
equality with ==. True or False
4. Given the following definition
string word1="Tremendous",
word2="small";
what is the value of each of the following relational expressions? [1%]
a. word1==word2
b. word1>word2
c. word1<"Tremble"
d. word1<"Tremend"
e. word2=="Small"
5. The four code snippets below all define a boolean variable isLeapYear, whose value should be
true at the end when the value of year indeed represents a leap year.
Which of the four is/are correct? [1%]
Note: the definition of a leap year can be found on Page 33 of the book.
A.
int year;
cin >> year;
bool isLeapYear;
if (year % 4)
isLeapYear = false;
else if (year % 400)
isLeapYear = true;
else if (year % 100)
isLeapYear = false;
else
isLeapYear = true;
B.
int year;
cin >> year;
bool isLeapYear;
isLeapYear = false;
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 ==
0)
isLeapYear
= true;
else
isLeapYear = true;
C.
int year;
cin >> year;
bool isLeapYear;
isLeapYear = false;
if (year % 4 == 0)
if (year % 100) {
if (year % 400 ==
0)
isLeapYear
= true;
}
else
isLeapYear
= true;
D.
int year;
cin >> year;
bool isLeapYear;
isLeapYear = year % 4 ==0 &&
year % 100 !=0 || year % 400 == 0;
=====================================================
Bonus question. [1%]
Given five integer variables i1, i2,
i3, i4, and i5, write a
short code snippet to assign the maximum among these five into another
integer variable max. Your
code is prohibited from changing the value of any of the five
variables. No arrays or loops are allowed.
Write your code below. It should not be more than 15 lines.