Test Questions & Answers Ch.3 Decision Structures Gaddis - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.

Test Questions & Answers Ch.3 Decision Structures Gaddis

Starting Out with Java: From Control Structures through Objects 7e (Gaddis)

Chapter 3 Decision Structures

TRUE/FALSE

1. Programs never need more than one path of execution.

2. The if-else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false.

3. In a switch statement, if two different values for the CaseExpression would result in the same code being executed, you must have two copies of the code, one after each CaseExpression.

4. All it takes for an OR expression to be true is for one of the subexpressions to be true.

5. All it takes for an AND expression to be true is for one of the subexpressions to be true.

6. When two strings are compared using the String class's compareTo method, the comparison is not case sensitive.

7. When testing for character values, the switch statement does not test for the case of the character.

8. If the expression on the left side of the && operator is false, the expression on the right side will not be checked.

9. Unicode is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets.

10. A local variable's scope always ends at the closing brace of the block of code in which it is declared.

11. When testing for character values, the switch statement does not test for the case of the character.

12. In a switch statement, each of the case values must be unique.

13. The String.format method works exactly like the System.out.printf method, except that it does not display the formatted string on the screen.

14. The System.out.printf method formats a string and displays it in the console window.

MULTIPLE CHOICE

1. In an if-else statement, if the boolean expression is false then

a.

no statements or blocks are executed

b.

the statement or block following the else is executed

c.

the first statement or block is executed

d.

all the statements or blocks are executed

2. The switch statement is a

a.

multiple alternative decision structure

b.

nested decision structure

c.

sequence structure

d.

test expression

3. __________ operators are used to determine whether a specific relationship exists between two values.

a.

Assignment

c.

Logical

b.

Arithmetic

d.

Relational

4. If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?

a.

str1 = str2

c.

str1.equals(str2)

b.

str1 && str2

d.

str1 += str2

5. Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2?

a.

str1.equalsIgnoreCase(str2)

b.

str1.equalsInsensitive(str2)

c.

str1 != str2

d.

str1 || str2

6. A block of code is enclosed in a set of

a.

braces, { }

c.

brackets, [ ]

b.

parentheses, ( )

d.

double quotes, " "

7. The boolean expression in an if statement must evaluate to

a.

degrees or radians

c.

positive or negative

b.

true or false

d.

left or right

8. A flag may have the values

a.

defined or undefined

c.

of any range of integers

b.

true or false

d.

of any Unicode character

9. What will be the values of ans, x, and y after the following statements are executed?

int ans = 35, x = 50, y = 50;

if (x >= y)

{

ans = x + 10;

x -= y;

}

else

{

ans = y + 10;

y += x;

}

a.

ans = 60, x = 0, y = 50

b.

ans = 45, x = 50, y = 0

c.

ans = 45, x = 50, y = 50

d.

ans = 60, x = 50, y = 100

10. What will be the value of x after the following statements are executed?

int x = 75;

int y = 60;

if (x > y)

x = x - y;

a.

60

b.

75

c.

15

d.

135

11. What will be the value of bonus after the following statements are executed?

int bonus, sales = 10000;

if (sales < 5000)

bonus = 200;

else if (sales < 7500)

bonus = 500;

else if (sales < 10000)

bonus = 750;

else if (sales < 20000)

bonus = 1000;

else

bonus = 1250;

a.

750

b.

1250

c.

500

d.

1000

12. What will be the value of discountRate after the following statements are executed?

double discountRate = 0.0;

int purchase = 100;

if (purchase > 1000)

discountRate = 0.05;

else if (purchase > 750)

discountRate = 0.03;

else if (purchase > 500)

discountRate = 0.01;

a.

0.0

b.

0.05

c.

0.03

d.

0.01

13. What will be the value of discountRate after the following statements are executed?

double discountRate = 0.0;

int purchase = 1250;

char cust = 'N';

if (purchase > 1000)

if (cust == 'Y')

discountRate = 0.05;

else

discountRate = 0.04;

else if (purchase > 750)

if (cust == 'Y')

discountRate = 0.04;

else

discountRate = 0.03;

else

discountRate = 0.0;

a.

0.0

b.

0.04

c.

0.05

d.

0.03

14. What will be the value of x after the following statements are executed?

int x = 10;

switch (x)

{

case 10:

x += 15;

case 12:

x -= 5;

break;

default:

x *= 3;

}

a.

30

b.

20

c.

25

d.

5

15. What will be the value of discountRate after the following statements are executed?

double discountRate;

char custType = 'B';

switch (custType)

{

case 'A':

discountRate = 0.08;

break;

case 'B':

discountRate = 0.06;

case 'C':

discountRate = 0.04;

default:

discountRate = 0.0;

}

a.

0.08

b.

0.06

c.

0.04

d.

0.0

16. What will be the value of ans after the following statements are executed?

int x = 40;

int y = 40;

if (x = y)

ans = x + 10;

a.

30

c.

50

b.

80

d.

The code contains an error and will not compile.

17. What will be displayed after the following statements are executed?

int ans = 10;

int x = 65;

int y = 55;

if (x >= y)

{

int ans = x + y;

}

System.out.println(ans);

a.

10

c.

100

b.

120

d.

The code contains an error and will not compile.

18. What will be displayed after the following statements are executed?

int y = 10;

if (y == 10)

{

int x = 30;

x += y;

System.out.println(x);

}

a.

40

c.

20

b.

30

d.

The code contains an error and will not compile.

19. What will be the value of pay after the following statements are executed?

int hours = 45;

double pay, payRate = 10.00;

pay = hours <= 40 ? hours * payRate :

40 * payRate + (hours - 40) *payRate * 1.5;

a.

400.00

c.

465.00

b.

450.00

d.

475.00

20. Which of the following expressions determines whether the char variable, chrA, is not equal to the letter 'A'?

a.

chrA == 'A'

c.

chrA || 'A'

b.

chrA != 'A'

d.

chrA.notEquals(A)

21. A __________ is a boolean variable that signals when some condition exists in the program.

a.

sentinel

c.

block

b.

flag

d.

case

22. The __________ statement is used to create a decision structure which allows a program to have more than one path of execution.

a.

block

c.

null

b.

if

d.

flag

23. In Java, when a character is stored in memory, it is actually the __________ that is stored.

a.

Unicode number

c.

floating-point value

b.

ASCII code

d.

letter, symbol, or number

24. Java requires that the boolean expression being tested by an if statement be enclosed in

a.

a set of parentheses

c.

a set of double quotes

b.

a set of braces

d.

a set of brackets

25. Which of the following statements determines whether the variable temp is within the range of 0 through 100 (inclusive)?

a.

if (temp >= 0 && temp <= 100)

b.

if (temp > 0 && temp < 100)

c.

if (temp >= 0 || temp <= 100)

d.

if (temp > 0 || temp < 100)

26. Which of the following expressions will determine whether x is less than or equal to y?

a.

x <= y

c.

x >= y

b.

x => y

d.

x =< y

27. Which of the following is the not equal operator?

a.

<>

b.

NOT

c.

*&

d.

!=

28. If you prematurely terminate an if statement with a semicolon, the compiler will

a.

not display an error message

b.

assume you are placing a null statement there

c.

both (a) and (b)

d.

none of these

29. What would be the value of bonus after the following statements are executed?

int bonus, sales = 1250;

if (sales > 1000)

bonus = 100;

if (sales > 750)

bonus = 50;

if (sales > 500)

bonus = 25;

else

bonus = 0;

a.

100

b.

500

c.

25

d.

0

30. What would be the value of bonus after the following statements are executed?

int bonus, sales = 85000;

char dept = 'S';

if (sales > 100000)

if (dept == 'R')

bonus = 2000;

else

bonus = 1500;

else if (sales > 75000)

if (dept == 'R')

bonus = 1250;

else

bonus = 1000;

else

bonus = 0;

a.

2000

b.

1500

c.

1250

d.

1000

31. Which of the following is the correct boolean expression to test for: int x being a value between, but not including, 500 and 650, or int y not equal to 1000?

a.

((x >= 500 && x <= 650) && (y != 1000))

b.

((x > 500 AND x < 650) OR !(y.equal(1000)))

c.

((x > 500 && x < 650) || (y != 1000))

d.

((x < 500 && x > 650) || !(y == 1000))

32. Which of the following is the correct boolean expression to test for: int x being a value less than or equal to 500 or greater than 650, or int y not equal to 1000?

a.

((x >= 500 && x < 650) && (y != 1000))

b.

((x <= 500 OR x > 650) AND !(y.equal(1000)))

c.

((x >= 500 || x < 650) || (y != 1000))

d.

((x <= 500 || x > 650) && !(y == 1000))

33. ________ works like this: If the expression on the left side of the && operator is false, the expression the right side will not be checked.

a.

short-circuit evaluation

c.

Boolean logic

b.

reverse logic

d.

relational evaluation

34. What will be printed when the following code is executed?

double x = 45678.259;

System.out.printf("%,.2f", x);

a.

45678.259

c.

45,678.26

b.

0,045,678.26

d.

45,678.3

35. Which of the following will format 12.78 to display as 12.8%?

a.

System.out.printf("%2.1d%", 12.78);

b.

System.out.printf("%.2f%%", 12.78);

c.

System.out.printf("%1.2d%", 12.78);

d.

System.out.printf("%.1f%%", 12.78);

36. An expression tested by an if statement must evaluate to

a.

0 or 1

c.

true or false

b.

+1 or -1

d.

t or f

37. What is the value of x after the following code has been executed?

int x = 75;

int y = 90;

if (x != y)

x += y;

a.

75

b.

90

c.

15

d.

165

38. What is the value of ans after the following code has been executed?

int x = 35;

int y = 20, ans = 80;

if (x < y)

ans += y;

a.

80

b.

100

c.

35

d.

55

39. What is the value of charges after the following code has been executed?

double charges, rate = 7.00;

int time = 180;

charges = time <= 119 ? rate * 2 :

time / 60.0 * rate;

a.

7.00

b.

14.00

c.

21.00

d.

28.00

40. What would be the value of discountRate after the following statements are executed?

double discountRate = 0.0;

int purchase = 1250;

if (purchase > 1000)

discountRate = .05;

if (purchase > 750)

discountRate = .03;

if (purchase > 500)

discountRate = .01;

else

discountRate = 0;

a.

.05

b.

.03

c.

.01

d.

0

41. What does the following code display?

int d = 9, e = 12;

System.out.printf("%d %d\n", d, e);

a.

%d %d

b.

9 12

c.

%d 9

d.

%9 %12

42. What does the following code display?

double x = 12.3798146;

System.out.printf("%.2f\n", x);

a.

123798146

c.

%12.38

b.

1238

d.

12.38

43. What is the value of ans, x, and y after the following statements are executed?

int ans = 0, x = 15, y = 25;

if ( x >= y)

{

ans = x + 10;

x -=y;

}

else

{

ans = y + 10;

y += x;

}

a.

ans = 0, x = 15, y = 25

b.

ans = 25, x = -10, y = 25

c.

ans = 35, x = 15, y = 40

d.

ans = 25, x = 15, y = 40

44. Which of the following will format 12.7801 to display as $12.78?

a.

System.out.printf("$%,.2f", 12.7801);

b.

System.out.printf("%f", 12.7801);

c.

System.out.printf("%.2f$$", 12.7801);

d.

System.out.printf("$d", 12.7801);

Document Information

Document Type:
DOCX
Chapter Number:
3
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 3 Decision Structures
Author:
Tony Gaddis

Connected Book

Java Control Structures 7e Test Bank

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party