0% found this document useful (0 votes)
45 views5 pages

Chapter 3 Selections: X 2 False True

1. This document contains code snippets and explanations of Java concepts like operators, if/else statements, switch statements, and formatting strings. 2. Key concepts covered include boolean operators and their precedence, using switch statements to select different code blocks based on a variable's value, and using format specifiers like %d and %f to format numeric output. 3. Examples show how to check conditions, perform math operations conditionally, select and break from switch statement cases, and display confirmation dialogs in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

Chapter 3 Selections: X 2 False True

1. This document contains code snippets and explanations of Java concepts like operators, if/else statements, switch statements, and formatting strings. 2. Key concepts covered include boolean operators and their precedence, using switch statements to select different code blocks based on a variable's value, and using format specifiers like %d and %f to format numeric output. 3. Examples show how to check conditions, perform math operations conditionally, select and break from switch statement cases, and display confirmation dialogs in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 3 Selections

1. <, <=, ==, !=, >, >=


2. No. Boolean values cannot be cast to other types.
3.
If number is 3,
!a" #isplays
3 is even
3 is o##
!b" #isplays
3 is even
If number is 3$,
!a" #isplays
3$ is o##
!b" #isplays
3$ is o##
%. Note& else matches the first if clause. No output if x = 3 and y = 2. 'utput
is () is *+ if if x = 3 and y = 4. 'utput is (, is 2+ if if x = 2 and y = 2.

, > 2

false true
-ystem.out.println!., is . / ,"0
y > 2

false
true
int ) = , / y0
-ystem.out.println!.) is . / )"0

$. a, c, an# # are the same. !B" an# !1" are correctly in#ente#.
2. No output if x = 2 and y = 3. 'utput is (, is 3+ if x = 3 and y = 2.
'utput is () is 2+ if x = 3 and y = 3.
*. 3es.
4. .$, ., .23%
9. (int)(Math.random() * 20)
10 + (int)(Math.random() * 10)
10 + (int)(Math.random() * 41)

1.
if (y > 0)
x = 1;
11.
!a"
if (score > 90)
pay *= 1.03;
!b"
if (score > 90)
pay *= 1.03;
else
pay *= 1.01;
12.
1onsi#er score is 5, 6hat 6ill be the 7ra#e8
13.
newLine = (count % 10 == 0);
1%. (true) && (3 > 4)
false
!(x > 0) && (x > 0)
false
(x > 0) || (x < 0)
true
(x != 0) || (x == 0)
true
(x >= 0) || (x < 0)
true
(x != 1) == !(x == 1)
true
1$. (x > 1) && (x < 100)
12. ((x > 1) && (x < 100)) || (x < 0)
1*. x > y > 0
incorrect
x = y && y
incorrect
x /= y
correct
x or y
incorrect
x and y
incorrect
14.
a. , is 2.
b. , is 1.
15.
If ch is 9:9, the e,pression is true0
If ch is 9p9, the e,pression is false0
If ch is 9;9, the e,pression is true0
If ch is 9$9, the e,pression is false0
2.
2.0
3.0
6.0
(x < y && y < z) is tue
(x < y !! y < z) is tue
"(x < y) is fa#se
(x $ y < z) is tue
(x $ y < z) is tue
21. a7e > 13 << a7e < 14
22.
wei%&t ' (0 !! &ei%&t ' 160.
23.
wei%&t ' (0 && &ei%&t ' 160.
2%.
wei%&t ' (0 ) &ei%&t ' 160.
2$. -6itch variables must be of char, byte, short, or int #ata types. If a break
statement is not use#, the ne,t case statement is performe#. 3ou can al6ays convert a
switch statement to an e=uivalent if statement, but not an if statement to a switch
statement. >he use of the switch statement can improve rea#ability of the pro7ram in
some cases. >he compile# co#e for the switch statement is also more efficient than its
correspon#in7 if statement.
22. y is 2.
2*. switch (a) {
case 1: x += 5; break;
case 2: x += 10; break;
case 3: x += 16; break;
case 4: x += 34;
}

a is 1
, /= $0 brea?
, /= 10 brea?
a is 2
, /= 120 brea?
a is 3
, /= 3%0 brea?
a is %
24. switch (day) {
case 0: dayName = .-un#ay.; break;
case 1: dayName = .@on#ay.; break;
case 2: dayName = .>ues#ay.; break;
case 3: dayName = .Ae#nes#ay.; break;
case 4: dayName = .>hur#ay.; break;
case 5: dayName = .Bri#ay.; break;
case 6: dayName = .-atur#ay.; break;
}
25.
-ystem.out.print!!count C 1 == " 8 count / .Dn. & count / . ."0
3.
pay = ( te*+eatue ' ,0 ) - +ay . 1.( / +ay . 1.1;
31.
0&e s+ecifies fo out+uttin% a 1oo#ean 2a#ue3 a c&aacte3 a
deci*a# inte%e3 a f#oatin%4+oint nu*1e3 and a stin%
ae %13 %c3 %d3 %f3 and %s.
32.
!a" the last item 3 #oes not have any specifier.
!b" >here is not enou7h items
!c" >he #ata for Cf must a floatin7Epoint value
33.
!a" amount is 32.32 3.233e/1
!b" amount is 32.32 3.233e/1
!c" Ffalse GG F #enote a space
!#" FFHava GG F #enote a space
!e" falseFFFFFHava
!f" FfalseHava
3%. Ise the -trin7.format metho# to create a formatte# strin7.
3$. >he prece#ence or#er for boolean operators is J, <<, an# KK
true !! true && false is true
true && true !! false is true
32. >rue
3*. both are false
34. 3es. 3es. 3es.
35. >o #isplay a confirmation #ialo7 bo,, invo?e H'ptionLane.sho61onfirmMialo7!null,
(Lromptin7 messa7e+". >he metho# returns an int value& if the 3es button is clic?e#, 1
if the No button is clic?e#, an# 2 if the 1ancel button is clic?e#,

You might also like