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=3               
aChar='2'                


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:

Two errors:

(1) the condition on line 6 for the while loop should have been ( inFile ), meaning that file stream is in valid state.

(2) The extraction operator >> ignores all blanks in cin. As a result, fixing (1) is necessary but still insufficient. Instead of inFile >> c, use cin.get(c) on lines 5 and 10.



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
 
Answer:

a. True
b. False.
(Due to short-circuited evaluation of logical expressions in C++, (divisor!=0 && num/divisor==0) and (num/divisor==0 && divisor!=0) produce different results when divisor equals 0. The former will return false, and the latter will halt the program due to a division-by-zero error.)
c. True
d. False (See the last slide of Chapter 5 lecture)


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"        

Answer: all are false.

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;


Answer:

D
=====================================================

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.



if (i1 >= i2 && i1 >= i3 && i1 >= i4 && i1 >= i5){
   max = i1;
}
else if (i2 >= i1 && i2 >= i3 && i2 >= i4 && i2 >= i5){
   max = i2;
}
else if (i3 >= i1 && i3 >= i2 && i3 >= i4 && i3 >= i5){
   max = i3;
}
else if (i4 >= i1 && i4 >= i2 && i4 >= i3 && i4 >= i5){
   max = i4;
}
else {
  max = i5;
}