0% found this document useful (0 votes)
47 views8 pages

Chp03 CheckPointAnswers

This document contains examples of Java code snippets and questions about Java programming concepts like conditional statements, loops, operators, and methods. Some key points covered include: - Examples of if-else statements, switch statements, ternary operators, and logical operators. - Questions about the output or evaluation of code snippets containing these constructs. - Explanations of differences between if-else and switch statements. - Examples demonstrating order of operations and precedence rules for logical operators.

Uploaded by

Bibek Dhakal
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)
47 views8 pages

Chp03 CheckPointAnswers

This document contains examples of Java code snippets and questions about Java programming concepts like conditional statements, loops, operators, and methods. Some key points covered include: - Examples of if-else statements, switch statements, ternary operators, and logical operators. - Questions about the output or evaluation of code snippets containing these constructs. - Explanations of differences between if-else and switch statements. - Examples demonstrating order of operations and precedence rules for logical operators.

Uploaded by

Bibek Dhakal
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/ 8

Chapter 3 Selections

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

2.

true

false

true

true

false

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 false
x>2

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


false
y>2

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 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. (a) (int)(Math.random() * 20)

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

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

(d) (int)(Math.random() * 2)

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. (a) (x > 1) && (x < 100) (b) (num > 1) && (num < 100) || num < 0

20. (a) (x – 5> < 4.5 && (x – 5) > -4.5 (b) (x – 5> > 4.5 && (x – 5) < -4.5

21. x>y>0

incorrect

x = y && y

incorrect

x /= y
correct

x or y

incorrect
x and y

incorrect

(x != 0) || (x = 0)

Incorrect on x = 0

22.

Yes

23.

If x is 45, the expression is false.

If x is 67, the expression is true.

If x is 101, 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.

x = 3; y = 3;

if (x + 3 == 6) {

y = 1;

y += 1;

31. x is 17

switch (a) {

case 1: x += 5; break;

case 2: x += 10; break;

case 3: x += 16; break;

case 4: x += 34;

a is 1
x += 5; break

a is 2
x += 10; break

a is 3
x += 16; break

a is 4
x += 34; break

32. switch (day) {

case 0: System.out.println("Sunday”); break;

case 1: System.out.println("Monday”); break;

case 2: System.out.println("Tuesday”); break;


case 3: System.out.println("Wednesday”); break;

case 4: System.out.println("Thurday”); break;

case 5: System.out.println("Friday”); break;

case 6: System.out.println("Saturday”); break;

33. Sorted

34.

ticketPrice = (ages >= 16) ? 20 : 10;

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. (int)(Math.random() * 2) == 0 ? -1 : 1.

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

true || true && false is true

true && true || false is true

38. True

39. both are false


40. Yes. Yes. Yes.

You might also like