Chapter 3

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

List six relational operators

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

Assuming that x is 1, show the result of the following Boolean expressions: (x > 0) (x < 0) (x != 0) (x >= 0) (x != 1)

True False True True False

List the precedence order of the Boolean operators. Evaluate the following expressions: true || true && false true && true || false

True, True

Write an if statement that increases pay by 3% if score is greater than 90.

if (score > 90){ pay *= 1.03; }

x > y > 0 x = y && y x /= y x or y x and y (x != 0) || (x = 0)

only x /= y

What is x after the following if-else statement is executed? Use a switch statement to rewrite it. int x = 1, a = 3; if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;

x is 17 switch (a) { case 1: x += 5; break; case 2: x += 10; break; case 3: x += 16; break; case 4: x += 34; }

Suppose x = 2 and y = 3. Show the output, if any, of the following code. if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

x is 2

if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

x is 2

What is y after the following switch statement is executed? Rewrite the code using an if-else statement. x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; }

y = 2. x = 3; y = 3; if (x + 3 == 6) { y = 1; } y += 1;

Are the following two expressions the same? a. x % 2 == 0 && x % 3 == 0 b. x % 6 == 0

yes

if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

z is 6

Suppose x = 3 and y = 4; show the output, if any, of the following code. if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

z is 7, but make sure you declare z as an int

Write conditional expression that returns -1 or 1 randomly.

(int)(Math.random() * 2) == 0 ? -1 : 1;

Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.

( age > 13 ) && (age < 18)

(a) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100. (b) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.

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

Which of the following is a possible output from invoking Math.random()? 323.4 0.5 34 1.0 0.0 0.234

0.5, 0.0, 0.234

Write a Boolean expression that evaluates to true if weight is greater than 50 pounds and height is greater than 60 inches.

(weight > 50) && (height > 60)

Write a Boolean expression that evaluates to true if either weight is greater than 50 pounds or height is greater than 60 inches, but not both.

(weight > 50) ^ (height > 60)

Write a Boolean expression that evaluates to true if weight is greater than 50 pounds or height is greater than 60 inches.

(weight > 50) || (height > 60)

Suppose x = 3 and y = 2. Show the output, if any, of the following code. if (x > 2) { if (y > 2) { int z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

There is no output

Suppose x = 3 and y = 2; show the output, if any, of the following code. if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }

There is no output

What is logically wrong in the following code? if (score >= 60.0) System.out.println("D"); else if (score >= 70.0) System.out.println("C"); else if (score >= 80.0) System.out.println("B"); else if (score >= 90.0) System.out.println("A"); else System.out.println("F");

They need to set a range for each score because if not any input above 60 will be D.

Answer these questions: a. How do you generate a random integer i such that 0 <= i < 20? b. How do you generate a random integer i such that 10 <= i < 20? c. How do you generate a random integer i such that 10 <= i <= 50? d. Write an expression that returns 0 or 1 randomly.

a. (int)(Math.random() * 20) b. 10 + (int)(Math.random() * 10) c. 10 + (int)(Math.random() * 41) d. (int)(Math.random() * 2)

Evaluate the following expressions: 2 * 2 - 3 > 2 && 4 - 2 > 5 2 * 2 - 3 > 2 || 4 - 2 > 5

both false

Assuming that x is 1, show the result of the following Boolean expressions. (true) && (3 > 4) !(x > 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x == 0) (x >= 0) || (x < 0) (x != 1) == !(x == 1)

false false true true true true

What is the value of the expression x >= 50 && x <= 100 if x is 45, 67, or 101?

false true false

Write an if statement that assigns 1 to x if y is greater than 0.

if (y > 0) . { int x = 1; }

Write a switch statement that displays Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6, accordingly

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; }

Rewrite the following if statements using the conditional operator. if (ages >= 16) ticketPrice = 20; else ticketPrice = 10;

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


Set pelajaran terkait

Phenom People Recruiter Certification Exam

View Set

Pharm Chapter 42: Drugs Used to Treat Glaucoma and Other Eye Disorders

View Set

PSYC TEST 3: mod 50_launchpad reading+questions

View Set

abeka vocabulary and spelling poetry V quiz 02B

View Set

Publi Health Unit 1 NU 485 - Chapter quizzes

View Set

Module 14 Review Quiz: Linux installation and configuration

View Set