Chapter 5
C# Programming: From Problem Analysis to Program Design · 20 exercises
Problem 1
The result of the expression if (avalue \(==10\) ) is: a. true or false b. 10 c. an integer value d. aValue e. determined by an input statement
3 step solution
Problem 2
Which expression is evaluated first in the following statement? if ( a > b && c == d || a == 10 && b > a + 7)? a. a>b b. b && c c. d || a d. a+7 e. none of the above
3 step solution
Problem 3
What is the output for total after the following segment of code executes? int num = 4, total = 0; switch (num) { case 1: case 2: total = 5; break; case 3: total = 10; break; case 4: total = total + 3; break; case 8: total = total + 6; break; default: total = total + 4; break; } Console.WriteLine("The value of total is " + total); a. 0 b. 3 c. 13 d. 28 e. none of the above
4 step solution
Problem 4
What is displayed when the following code executes? score = 40; if (score > 95)a. This is a test question! b. Congratulations! That’s a high score! This is a test question! c. That’s a high score! This is a test question! d. Congratulations! That’s a high score! e. none of the above Console.WriteLine("Congratulations!"); Console.WriteLine("That’s a high score!"); Console.WriteLine("This is a test question!");
4 step solution
Problem 5
allows you to do the following: properly check the variable code to determine whether it contains the character \(C,\) and if it does, display "This … # Which statement in C# allows you to do the following: properly check the variable code to determine whether it contains the character \(C,\) and if it does, display "This is a check" and then advance to a new line? a. if code is equal to C Console.WriteLine("This is a check"); b. if (code = "C") Console.WriteLine("This is a check"); c. if (code == 'C') Console.WriteLine("This is a check"); d. if (code == C) Console.WriteLine("This is a check"); e. none of the above
6 step solution
Problem 6
What will be displayed from executing the following segment of code? You may assume testScore has a value of 90. int testScore; if (testScore < 60); // Note the semicolon. Console.WriteLine("You failed the test!"); if (testScore > 60) Console.WriteLine("You passed the test!"); else Console.WriteLine("You need to study for " \+ "the next test!"); a. You failed the test? b. You passed the test! c. You failed the test! You passed the test! d. You failed the test! You need to study for the next test! e. none of the above
4 step solution
Problem 7
The ___________ operator represents the logical AND. a. ++ b. || c. && d. @ e. none of the above
3 step solution
Problem 10
What does the following program segment display? int f = 7, s = 15; f = s % 2; if (f != 1) { f = 0; s = 0; } else if (f == 2) { f = 10; s = 10; } else { f = 1; s = 1; } Console.WriteLine(" " + f + " " + s); a. 7 15 b. 0 0 c. 10 10 d. 1 1 e. none of the above
6 step solution
Problem 13
Consider the following if statement, which is syntactically correct, but uses poor style and indentation: \\[ \text { If }(x>=y) \text { if }(y>0) x=x^{*} y ; \text { else if }(y<4) x=x-y; \\] Assume that \(x\) and \(y\) are int variables containing the values 9 and \(3,\) respectively, before execution of the preceding statement. After execution of the statement, what value does \(x\) contain? a. 9 b. 1 c. 6 d. 27 e. none of the above
5 step solution
Problem 14
After execution of the following code, what will be the value of inputValue? int inputValue = 0; if (inputValue > 5) inputValue += 5; else if (inputValue > 2) inputValue += 10; else inputValue += 15; a. 15 b. 10 c. 25 d. 0 e. 5
4 step solution
Problem 16
Given the following segment of code, what will be the output? int x = 5; if (x == 2) Console.WriteLine("Brown, brown, run aground."); else Console.WriteLine("Blue, blue, sail on through."); Console.WriteLine("Green, green, nice and clean."); a. Brown, brown, run aground. b. Blue, blue, sail on through. c. Brown, brown, run aground. Blue, blue, sail on through. d. Blue, blue, sail on through. Green, green, nice and clean. e. none of the above
4 step solution
Problem 17
What is the result of the following conditional expression when aValue = 100 and bValue = 7? result = aValue > bvalue + 100 ? 1000 : 2000; a. 0 b. 1000 c. 2000 d. 7 e. none of the above
5 step solution
Problem 18
Given the switch statement, which of the following would be the first if statement to replace the first test in the switch? switch (control) { case 11 : Console.WriteLine("eleven"); break; case 12 : Console.WriteLine("twelve"); break; case 16 : Console.WriteLine("sixteen"); break; } a. if (case = 11) b. if (case == 11) c. if (control == 11) d. if (switch == 11) e. none of the above
5 step solution
Problem 19
Which of the following statements about logical operators is correct? a. Logical AND yields true if and only if both of its operands are either true or false. b. Logical OR yields true if either or both of its operands are true. c. Logical OR is represented in C# by && . d. Logical NOT is represented in C# by | . e. none of the above
5 step solution
Problem 20
The string data type can be used: a. as an operand for the == or != b. as an expression in the switch statement to be evaluated c. as an operand for the > or < operator d. a and b are correct e. all of the above
4 step solution
Problem 21
Assuming a is 5, b is 6, and c is 8, which of the following is false? a. a == 5; b. 7 < = (a + 2); c. c < = 4; d. (1 + a) != b; e. c > = 8; f. a > = 0; g. a < = (b * 2);
7 step solution
Problem 22
Could a switch statement be designed logically to perform the same tests as the following nested if statement? If so, explain how it could be done. if (aValue == 100) Console.WriteLine("Value is 100"); else if (aValue < 100) Console.WriteLine("Value is less than 100");
4 step solution
Problem 23
Rewrite the following switch statement as a nested if statement using a series of else...if statements: string birdName; switch (birdName) { case "Pelican": Console.WriteLine("Lives near water."); break; case "Cardinal": Console.WriteLine("Beautiful in the snow."); break; case "Owl": Console.WriteLine("Night creature."); break; case "Eagle": Console.WriteLine("Keen vision"); break; case "Flamingo": Console.WriteLine("Pretty and pink."); break; default: Console.WriteLine("Can fly."); break; }
6 step solution
Problem 24
Rewrite the following compound expression as nested if statements: if ((aValue > bValue) && (bValue == 10)) Console.WriteLine("Test complete");
3 step solution
Problem 25
Write conditional expressions to perform the following tests: a. When amountOwed is greater than 1000.00, display an overdue message. b. When amountOfRain is greater than 5 inches, add 5 to total. When it is between 3 and 5 inches, add 3 to total. When it is less than 3 inches, add 1 to total. c. When middleInitial is equal to the character z, display message ‘‘You’re one in a thousand’’; otherwise, check to see if it is equal to the character ‘a’. When it is equal to the character a, display the message ‘‘You have a common initial’’. d. When balance > 100 and transaction < 50, subtract transaction from balance. When balance is not greater than 100, add transaction to balance.
4 step solution