0% found this document useful (0 votes)
48 views

Chapter 3 Selections: True False False True True True

The document summarizes key concepts about selection statements, operators, and data types in Java including: 1) Comparison operators like <, <=, ==, etc and examples of their usage. 2) If-else conditional statements and examples of using them to check conditions and assign values. 3) Switch statements for selecting blocks of code to execute based on different case values. 4) Logical operators like &&, ||, ! and precedence order when combining them. 5) Formatting output with printf and specifying format specifiers.

Uploaded by

vem
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Chapter 3 Selections: True False False True True True

The document summarizes key concepts about selection statements, operators, and data types in Java including: 1) Comparison operators like <, <=, ==, etc and examples of their usage. 2) If-else conditional statements and examples of using them to check conditions and assign values. 3) Switch statements for selecting blocks of code to execute based on different case values. 4) Logical operators like &&, ||, ! and precedence order when combining them. 5) Formatting output with printf and specifying format specifiers.

Uploaded by

vem
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 3 Selections

1.

<, <=, ==, !=, >, >=

2.
true
false
false
true
true
true

3.

No. Boolean values cannot be cast to other types.

4.
if(y>0)
x=1;

5.
if(score>90)
pay*=1.03;

6.
if(score>90)
pay*=1.03;
else
pay*=1.01;

7.

If number is 30,
(a) displays
30 is even
30 is odd

(b) displays
30 is even

If number is 35,
(a) displays
35 is odd

(b) displays
35 is odd

8.
Note: else matches the first if clause. No output if x = 3 and y = 2.
Output is z is 7 if if x = 3 and y = 4. Output is x is 2 if if x = 2 and y =
2.

true

y>2

x>2

false

false

System.out.println("x is " + x);

true
int z = x + y;
System.out.println("z is " + z);

9.
No output if x = 2 and y = 3. Output is x is 3 if x = 3 and y =
2. Output is z is 6 if x = 3 and y = 3.

10.
Consider score is 90, what will be the grade?

11. a, c, and d are the same. (B) and (C) are correctly indented.

12.

newLine = (count % 10 == 0);

13.

Both are correct. (b) is better.

14.for (a) if number is 14, the output is

14 is even
if number is 15, the output is
15 is multiple of 5
if number is 30, the output is
30 is even
30 is multiple of 5

for (b) if number is 14, the output is


14 is even
if number is 15, the output is
15 is multiple of 5
if number is 30, the output is
30 is even

15.

0.5, 0.0, 0.234

16.

(int)(Math.random() * 20)

10 + (int)(Math.random() * 10)

10 + (int)(Math.random() * 41)

17.

Yes

18.

(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

19.

(x>1)&&(x<100)

20.

((x>1)&&(x<100))||(x<0)

21.

x>y>0
incorrect

x=y&&y
incorrect

x/=y

correct

xory
incorrect

xandy
incorrect

22.
a. x is 2.
b. x is 1.

23.
If ch is 'A', the expression is true;
If ch is 'p', the expression is false;
If ch is 'E', the expression is true;
If ch is '5', the expression is false;

24.

(x < y && y < z) is true


(x < y || y < z) is true
!(x < y) is false
(x + y < z) is true
(x + y < z) is true

25. age > 13 && age < 18

26.

weight > 50 || height > 60.


27.

weight > 50 && height > 60.


28.

weight > 50 ^ height > 60.


29.
Switch variables must be of char, byte, short, or int data types. If a
break statement is not used, the next case statement is performed. You can
always convert a switch statement to an equivalent if statement, but not
an if statement to a switch statement. The use of the switch statement
can improve readability of the program in some cases. The compiled code for
the switch statement is also more efficient than its corresponding if
statement.

30.

y is 2.

31.

x is 17

switch(a){
case1:x+=5;break;
case2:x+=10;break;
case3:x+=16;break;
case4:x+=34;

a is 1

x += 5;

break

x += 10;

break

x += 16;

break

x += 34;

break

a is 2

a is 3

a is 4

32.

switch(day){
case0:dayName="Sunday";break;
case1:dayName="Monday";break;
case2:dayName="Tuesday";break;
case3:dayName="Wednesday";break;
case4:dayName="Thurday";break;
case5:dayName="Friday";break;
case6:dayName="Saturday";break;
}

33. Sorted

34.
(A) ticketPrice = (ages >= 16) ? 20 : 10;
(B) System.out.print((count % 10 == 0) ? count + "\n" : count + " ");

35.
(A)
if(x>10)
score=3*scale;
else
score=4*scale;
(B)
if(income>10000)
tax=income*0.2;
else
tax=income*0.17+1000;
(C)
if(number%3==0)
System.out.println(i);
else
System.out.println(j);

36.

The specifiers for outputting a boolean value, a character, a


decimal integer, a floating-point number, and a string are
%b, %c, %d, %f, and %s.

37.
(a) the last item 3 does not have any specifier.
(b) There is not enough items
(c) The data for %f must a floating-point value

38.
(a) amount is 32.320000 3.233000e+01
(b) amount is 32.3200 3.2330e+01
(c) *false // * denote a space
(d) **Java // * denote a space

(e) false*****Java
(f) *falseJava

39.

The precedence order for boolean operators is ^, &&, and ||

true || true && false

is true

true && true || false

is true

40.True
41.both are false
42.Yes. Yes. Yes.
43.To display a confirmation dialog box, invoke
JOptionPane.showConfirmDialog(null, Prompting message). The method
returns an int value: 0 if the Yes button is clicked, 1 if the No button is
clicked, and 2 if the Cancel button is clicked,

You might also like