Deck 4: Decision Structures
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
Play
Full screen (f)
Deck 4: Decision Structures
1
What will be displayed after the following code has been executed?
Int x = 65;
Int y = 55;
If (x >= y)
{
Int ans = x + y;
}
System.out.println(ans);
A) 10
B) 120
C) 100
D) The code contains an error and will not compile.
Int x = 65;
Int y = 55;
If (x >= y)
{
Int ans = x + y;
}
System.out.println(ans);
A) 10
B) 120
C) 100
D) The code contains an error and will not compile.
D
2
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
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
A
3
What will be the value of x after the following code is executed?
Int x = 75;
Int y = 60;
If (x > y)
X = x - y;
A) 60
B) 75
C) 15
D) 135
Int x = 75;
Int y = 60;
If (x > y)
X = x - y;
A) 60
B) 75
C) 15
D) 135
C
4
Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 012.8?
A) "###.#"
B) "000.0"
C) "##0.0%"
D) "$#0.00"
A) "###.#"
B) "000.0"
C) "##0.0%"
D) "$#0.00"
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following strings could be passed to the DecimalFormat constructor to display 12.78 as 12.8%?
A) "##0.0%"
B) "###.##%"
C) "000.0%"
D) "#0.00%"
A) "##0.0%"
B) "###.##%"
C) "000.0%"
D) "#0.00%"
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
________ operators are used to determine whether a specific relationship exists between two values.
A) Assignment
B) Arithmetic
C) Logical
D) Relational
A) Assignment
B) Arithmetic
C) Logical
D) Relational
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
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
A) str1.equalsIgnoreCase(str2)
B) str1.equalsInsensitive(str2)
C) str1 != str2
D) str1 || str2
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
The boolean expression in an if statement must evaluate to
A) degrees or radians.
B) true or false.
C) positive or negative.
D) left or right.
A) degrees or radians.
B) true or false.
C) positive or negative.
D) left or right.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
What is the value of ans after the following code has been executed?
Int x = 40;
Int y = 40;
Int ans = 0;
If (x = y)
Ans = x + 10;
A) 30
B) 80
C) 50
D) The code contains an error and will not compile.
Int x = 40;
Int y = 40;
Int ans = 0;
If (x = y)
Ans = x + 10;
A) 30
B) 80
C) 50
D) The code contains an error and will not compile.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
What will be displayed when the following code is executed?
Double x = 45678.259;
DecimalFormat formatter = new
DecimalFormat("#,###,##0.00"); JOptionPane.showMessageDialog(null,formatter.format(x));
A) 45,678.26
B) 45,678.3
C) 0,045,678.26
D) 45678.259
Double x = 45678.259;
DecimalFormat formatter = new
DecimalFormat("#,###,##0.00"); JOptionPane.showMessageDialog(null,formatter.format(x));
A) 45,678.26
B) 45,678.3
C) 0,045,678.26
D) 45678.259
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
What would 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
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
What will be displayed when the following code is executed?
Double x = 45678.259;
DecimalFormat formatter = new DecimalFormat("#,##0.0"); JOptionPane.showMessageDialog(null,formatter.format(x));
A) 45,678.26
B) 45,678.3
C) 45,678.259
D) 45678.259
Double x = 45678.259;
DecimalFormat formatter = new DecimalFormat("#,##0.0"); JOptionPane.showMessageDialog(null,formatter.format(x));
A) 45,678.26
B) 45,678.3
C) 45,678.259
D) 45678.259
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
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
B) str1 && str2
C) str1.equals(str2)
D) str1 + str2
A) str1 == str2
B) str1 && str2
C) str1.equals(str2)
D) str1 + str2
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
What would be the value of x after the following statements were 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
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
What will be the value of bonus after the following code is 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
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
In an if-else statement, if the boolean expression is false
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 statements or blocks are executed.
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 statements or blocks are executed.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
A flag may have the values
A) defined or undefined.
B) of any range of numbers.
C) true or false.
D) of any Unicode character.
A) defined or undefined.
B) of any range of numbers.
C) true or false.
D) of any Unicode character.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
What will be displayed when the following code is executed?
Int y = 10;
If (y == 10
{
Int x = 30;
X += y;
System.out.println(x);
}
A) 40
B) 30
C) 20
D) The code contains an error and will not compile.
Int y = 10;
If (y == 10
{
Int x = 30;
X += y;
System.out.println(x);
}
A) 40
B) 30
C) 20
D) The code contains an error and will not compile.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
A block of code is enclosed in a set of
A) parentheses.
B) brackets.
C) braces.
D) double quotes.
A) parentheses.
B) brackets.
C) braces.
D) double quotes.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
What would 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
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
Enclosing a group of statements inside a set of braces creates
A) an expression.
B) a block of statements.
C) a relational operator.
D) an if-else statement.
A) an expression.
B) a block of statements.
C) a relational operator.
D) an if-else statement.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
If the expression on the left side of the && operator is false, the expression on the right side will not be checked.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following statements determines whether temp is within the range of 0 through 100?
A) if (temp >= 0 && temp <= 100)
B) if (temp > 0 && temp < 100)
C) if (temp >= 0 || temp <= 100)
D) if (temp > 0 || temp < 100)
A) if (temp >= 0 && temp <= 100)
B) if (temp > 0 && temp < 100)
C) if (temp >= 0 || temp <= 100)
D) if (temp > 0 || temp < 100)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
The ________ statement is used to create a decision structure, which allows a program to have more than one path of execution.
A) block
B) if
C) null
D) flag
A) block
B) if
C) null
D) flag
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following expressions determines whether the char variable chrA is not equal to the letter 'A'?
A) chrA == 'A'
B) chrA != 'A'
C) chrA || 'A'
D) chrA.notEquals('A')
A) chrA == 'A'
B) chrA != 'A'
C) chrA || 'A'
D) chrA.notEquals('A')
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
An important style rule you should adopt for writing if statements is to write the conditionally executed statement on the line after the if statement.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
When a character is stored in memory, it is actually the ________ that is stored.
A) Unicode number
B) ASCII code
C) floating-point value
D) letter, symbol, or number
A) Unicode number
B) ASCII code
C) floating-point value
D) letter, symbol, or number
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
Java requires that the boolean expression being tested by an if statement be enclosed in
A) a set of parentheses.
B) a set of braces.
C) a set of double quotes.
D) a set of brackets.
A) a set of parentheses.
B) a set of braces.
C) a set of double quotes.
D) a set of brackets.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
What would 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
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
A local variable's scope always ends at the closing brace of the block of code in which it is declared.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
A ________ is a boolean variable that signals when some condition exists in the program.
A) sentinel
B) flag
C) block
D) case
A) sentinel
B) flag
C) block
D) case
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
Unicode is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
When two strings are compared using the String class's compareTo method, the comparison is case-insensitive.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Which of the following expressions will determine whether x is less than or equal to y?
A) x <= y
B) x => y
C) x >= y
D) x =< y
A) x <= y
B) x => y
C) x >= y
D) x =< y
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
When testing for character values, the switch statement does not test for the case of the character.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
What will be displayed after the following statements are executed?
Int hours = 30;
Double pay, payRate = 10.00;
Pay = hours <= 40 ? hours * payRate : hours * payRate * 2.0;
System.out.println(pay);
A) 300.0
B) 600.0
C) 400.0
D) The code contains an error and will not compile.
Int hours = 30;
Double pay, payRate = 10.00;
Pay = hours <= 40 ? hours * payRate : hours * payRate * 2.0;
System.out.println(pay);
A) 300.0
B) 600.0
C) 400.0
D) The code contains an error and will not compile.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
All it takes for an OR expression to be true is for one of the subexpressions to be true.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
The DecimalFormat class is part of the Java API, but it is not automatically available to your programs.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck