1. Suppose we have these declarations,
int x = -1, y = 0, z = 1;
This Boolean expression is correct and it does what the programmer intends.
x < y < z
2. The value of count is 0; limit is 10. Evaluate:
(count != 0)||(limit < 20)
3. In a while loop, the Boolean_Expression is executed before each execution of the loop body.
4. In a do-while loop, a continue statement terminates the loop.
5. A break statement is used in loops only.
1. Assume variables first and second are declared to be double and are initialized. Write a sequence of lines of code that cause the values stored in first and second to be exchanged if the value of first is not less than second.
2. A numeric integer grade is between 50 and 99. Using integer division or otherwise obtain a int value that is 5, 6, 7, 8, or 9 from the numeric grade. Write code using a switch statement using this number in the selector expression that assigns letter grades based on this 10 point scheme:
if the numeric_grade is not less than 90, the letter_grade is an A,
if the numeric_grade is not less than 80, the letter_grade is a B,
if the numeric_grade is not less than 70, the letter_grade is C,
if the numeric_grade is not less than 60, the letter_grade is D,
otherwise the letter_grade is F.
3. What is the output from each of the following loops?
a) char c = 'A';
do
{
cout << c << " ";
c = c + 2;
} while ( c <= 'M' )
cout << endl;
b) int i = 0;
while (i < 50)
{
if ( i < 20 && i != 15 )
cout << 'X';
i++;
}
cout << endl;
4. Write the following do-while statement with a while construct, and maybe some extra code.
x = 10;
do
{
cout << x << endl;
x = x - 3;
} while ( x > 0 );
5. Write a program that reads in exactly 10 integers and outputs the sum.
6. Rewrite the following while loop as for loop:
int i = 1;
while(i<=10)
{
if (i<5 && i !=2)
cout << 'X';
i++;
}
7. Assume year has been previously declared as an int and given a meaningful value. Write
a switch statement that for any value of year,
produces exactly the same output as the following if statement.
if (year == 1992)
cout << "Clinton";
else if (year == 2008)
cout << "Who knows?";
else if (year == 1988 || year == 2000)
cout << "Bush";
else
cout << "Someone";
There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.
1. What is the value of the bool valued expression, 1 < x < 10? Does the value of this depend on the value of x? Explain, and give the expression that the programmer probably meant.
a) This statement is incorrect as it is always false.
b) This statement is correct and its value depends on x.
c) This statement is incorrect and it is always true
d) This statement is incorrect, but it does depend on the value of x.
2. In a switch statement, when a break statement is encountered, an immediate transfer of control is made to
a) the default case of the switch statement
b) a goto statement
c) the else clause
d) the statement beyond the end of the switch statement.
e) none of these
3. Which of the following loop statements is guaranteed to iterate the body of the loop at least once?
a) while(control) body;
b) do body while(control);
c) for (initialize; test; update) body;
d) none of the above
e) all of the above
4. Where is it legal to put a break statement? What does the break do there?
a) A break is placed in a simple (unnested) loop, to terminate the loop.
b) A break is placed in an inner block of nested blocks, to transfer control beyond the end of block the break is within.
c) A break is placed in a loop in nested loops, to transfer control beyond the end of the innermost loop the break is within.
d) A break is placed in a switch statement, to terminate the switch by transferring control beyond the end of the switch.
e) A break is placed in a loop where it restarts the loop.
5. In the expression (j > 0 && j+1 == 10), which operator executes last?
a) >
b) &&
c) +
d) ==
6. The statements int x = 1; int y; y = x++;
a) Assign y the value 2;
b) Change the value of x to 2.
c) Assign y the value 0;
d) Assign y the value 1;
e) The ++ is a postfix operator.
7. This question asks about nesting of if, if-else, switch, while, do-while, and for statements:
a) These constructs may not be nested in at all.
b) These constructs may be nested in any way that meets the needs of algorithms the programmer is coding.
c) Only control constructs of a given kind may be nested (while loops within while loops; if-else within if-else etc.)
d) The question does not make sense in C++.
8. Each of the following has at least one error, either intent, or an error that may be caught by the compiler or both). What is the error? Assume all the variables you see are declared and initialized.
a) for(int i = 0; i <10; i++);
sum = sum +x;
b) if (x > 0)
x = 3
else
x =4;
c) if(x = 0) x = MAXINT;
d) if x > 0 x = 3;
e) if (x>0) then x =3;