CS 31 Bridge – Summer 08                                                          Homework 2
 
Name: ______________________                     Group: _________________________________            Hours taken: ___
 
You can work in group of 2-3 people. You can talk and discuss among your group. However, write your own answers for the homework. Do NOT copy each other. Make your answers CLEAR and SHORT.
 
True False:

 

1.      Given the declaration

int x = 0;

The following expression causes a divide by zero error:

(x !=0) || (2/x < 1);

 

2.      You want to determine whether time has run out. The following code correctly implements this.

            !time > limit

 

3.      The value of count is 0; limit is 10. Evaluate:

count == 0 && limit < 20

 

4.      In a while loop, the Boolean_Expression is executed before each execution of the loop body.

 

 

Free Form Questions:

 

1.      Assume variables first, second, and max of type double are declared and initialized. Write a sequence of lines of code that put the larger value between first and second into max.

 

 

 

 

 

 

 

 

 

2.      Write multiway/compound if-else statements for which the output is "Alarm: Boiler Pressure: TOO HIGH" if the value of the variable boiler_pressure is greater than 1000 (psi), and the output is  "Boiler Pressure: TOO LOW" if the value of boiler_pressure is below 100(psi), otherwise the output is "Boiler Pressure: within normal limits."

 

 

 

 

 

 

 

 

 

3.      Write Boolean expressions that represent the given English expressions. Assume any variables used have been declared and initialized.

a)      alpha is greater than 1

b)      x is odd

c)      x and y are odd

d)      ch is an upper case alphabetic character (between 'A' and 'Z').

4.      What is the output from each of the following loops?

a)   while ( 0 )

cout << ‘X’;

cout << endl;

b)   do

cout << ‘X’;

while ( y != y );

cout << endl;

c)   int i = 1;

while (i < 9)

{

cout i;

i++;

}

cout << endl;

 

5.      For each of the following situations, tell which type loop (while, do-while, or for) would be best in that situation:

a)      Reading a list of an unknown number of homework grades for a single student.

b)      Summing a series such as 1 +1/2 +1/(22) + 1/(23) + ... + 1/(28)

c)      Testing a newly coded library function to see how it performs for different values of its arguments.

d)      Reading in the number of days of vacation taken by an employee.

 

 

Multiple Choice:

 

There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.

 

1.      Which control construct repeats a sequence of statements zero or more times?

a)      while statement

b)      do-while statement

c)      for statement

d)      switch statement

e)      if-else statement

 

2.      Which of the following is true of the && operator?

a)      It has two operands.

b)      It can have one operand.

c)      It uses short circuit evaluation.

d)      It is the logical AND operator.

e)      It returns true if either operand is true.

 

3.      Which of the following determines the operator that is processed prior to another operator?

a)      Operator associativity

b)      Operator precedence

c)      Whether the operator is an arithmetic operator

d)      None of these determine the order in which operators are processed.

 

4.      The following program purports to sum all entered int values that are greater than 5. It compiles without any error message, and it executes without error message, but nevertheless is wrong. Name all the errors.

// Display the sum of all entered int values

// greater than 5

#include <iostream>

int main()

{

using namespace std;

int x, sum;

while (x < 10)

{

cin >> x;

if (x > 5);

sum = sum +x;

}

cout << "The sum is values > 5 is " << sum << endl;

}

a)      The while header needs a semicolon at the end of its line.

b)      The semicolon at the end of the if statement is an error that the compiler should catch.

c)      The semicolon at the end of the if statement causes all entered values to be summed.

d)      The sum variable is not initialized, so the output value is most likely garbage.

 

5.      A switch statement must have

a)      a default case

b)      more than one non-default case

c)      a break statement

d)      none of the above

e)      all of the above

 

6.      If this code fragment were executed in an otherwise correct and complete program, what would the output be? Explain.

int a = 3, b = 2, c = 5

if (a > b)

a = 4;

if ( b > c)

a = 5;

else

a = 6;

cout << a < endl;

a)      3

b)      4

c)      5

d)      6

e)      None of the above, the cout statement belongs to the else and so is skipped.

 

 

Bonus Questions:

 

1.      Will you laugh at us when we keep repeating those trivial "do-or-die" advices of "always start early" and "develop incrementally", and keep begging you to take them seriously?

 

2.      Will you point and laugh at the guy who laughed at us because he thought those advices were way too trivial, and then failed the class because he took our trivial advices too lightly?