Java ch 3 check point and self test

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

&& first and then || both are true

Suppose, when you run the following program, you enter the input 2 3 6 from the console. What is the output? public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println("(x < y && y < z) is " + (x < y && y < z)); System.out.println("(x < y || y < z) is " + (x < y || y < z)); System.out.println("!(x < y) is " + !(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y > z) is " + (x + y > z)); } }

(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 false

The order of the precedence (from high to low) of the operators binary +, *, &&, ||, ^ is:

*, +, ^ , &&,||

What is the output from System.out.println((int)Math.random() * 4)?

0

What is the possible output from System.out.println((int)(Math.random() * 4))?

0,1,2,3

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

0.1,0.5,0.234

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

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

How many days in the February of a leap year? Which of the following is a leap year? 500, 1000, 2000, 2016, and 2020?

29 days 2000,2016,2020

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

45- false 67- true 101- false

List six relational operators.

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

The "less than or equal to" comparison operator in Java is __________.

<=

Which of the following operators are right-associative.

=

The equal comparison operator in Java is __________.

==

Which of the following statements are true? A. (x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B. (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) C. (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) D. (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0)

A,B,and C

Which of the following is a possible output from invoking Math.random()? A. 3.43 B. 0.5 C. 0.0 D. 1.0

B and C

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

False False

Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best?

I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");

if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000");

Income is greater than 3000

What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; }

Since there are no if statements y = 2

The __________ method immediately terminates the program.

System.exit(0);

Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; }

The switch control variable cannot be a double

What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?

There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true. because the involves floating point number. Floating point numbers are approximated

True or false? All the binary operators except = are left associative.

True

In Java, the word true is ________.

a Boolean literal

Which of the following statements are equivalent? Which ones are correctly indented? (a) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (b) if (i > 0) { if (j > 0) x = 0; else if (k > 0) y = 0; } else z = 0; (c) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (d) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;

a, c, d are equivalent c is correctly indented

(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.

a.) (num > 1) && (num < 100) b.) (num > 1) && (num < 100) || (num < 0)

(a) Write a Boolean expression for |x - 5| < 4.5. (b) Write a Boolean expression for |x - 5| > 4.5.

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

What is the output of the following code if number is 14, 15, or 30? (a) if (number % 2 == 0) System.out.println (number + " is even"); if (number % 5 == 0) System.out.println (number + " is multiple of 5"); (b) if (number % 2 == 0) System.out.println (number + " is even"); else if (number % 5 == 0) System.out.println (number + " is multiple of 5");

a.) if the number is 14 the output is: 14 is even if the number is 15 the output is: 15 is multiple of 5 if the number is 30 the output is : 30 is even 30 is multiple of 5 b.) if the number is 14 the output is: 14 is even if the number is 15 the output is: 15 is multiple of 5 if the number is 30 the output is: 30 is even

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 ); or Random rand = new Random(); rand. b.) (int)(Math.random()*10+10); c.) (int)(Math.random()* 41 +10); d.) (int)(Math.random()*2);

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

age > 13 && age < 18

Are the following statements correct? Which one is better? (a) if (age < 16) System.out.println ("Cannot get a driver's license"); if (age >= 16) System.out.println ("Can get a driver's license"); (b) if (age < 16) System.out.println ("Cannot get a driver's license"); else System.out.println ("Can get a driver's license"); Both are correct. (b) is better.

both statements are correct b is better

Which of the following code displays the area of a circle if the radius is positive. A. if (radius != 0) System.out.println(radius * radius * 3.14159); B. if (radius >= 0) System.out.println(radius * radius * 3.14159); C. if (radius > 0) System.out.println(radius * radius * 3.14159); D. if (radius <= 0) System.out.println(radius * radius * 3.14159);

c

What data types are required for a switch variable? If the keyword break is not used after a case is processed, what is the next statement to be executed? Can you convert a switch statement to an equivalent if statement, or vice versa? What are the advantages of using a switch statement?

char, byte, short, int, or string types the next case statement you can convert a switch statement to an if statement but an if statement cannot be converted to a switch statement switch statements are easy to read, more efficient than an if statement

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

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

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

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

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

What is the output of the code in (a) and (b) if number is 30? What if number is 35? (a) if (number % 2 == 0) System.out.println(number + " is even."); System.out.println(number + " is odd."); (b) if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");

if number is 30 a displays 30 is even 30 is odd if number is 35 a displays 35 is odd if number is 30 b displays 30 is even if number is 35 b displays 35 is odd

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

if x = 2 and y = 3 no output if x = 3 and y = 2 output:x is 3 if x=3 and y = 3 output: z is 6

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

if x =3 and y =2 there is no output if x =3 and y =4 the output is: z is 7 if x = 2 and y =2 the output is: x is 2

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

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

Rewrite the switch statement in Listing 3.8 using an if-else statement.

int remainder = year % 12; if (remainder == 0) System.out.println("monkey"); else if (remainder == 1) System.out.println("rooster"); else if (remainder == 2) System.out.println("dog"); else if (remainder == 3) System.out.println("pig"); else if (remainder == 4) System.out.println("rat"); else if (remainder == 5) System.out.println("ox"); else if (remainder == 6) System.out.println("tiger"); else if (remainder == 7) System.out.println("rabbit"); else if (remainder == 8) System.out.println("dragon"); else if (remainder == 9) System.out.println("snake"); else if (remainder == 10) System.out.println("horse"); else System.out.println("sheep");

What is 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");

it is backwards

The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right");

just right

Rewrite the following statement using a Boolean expression: if (count % 10 == 0) newLine = true; else newLine = false;

newLine = (count % 10 == 0)

Can the following conversions involving casting be allowed? Write a test program to verify it. boolean b = true; i = (int)b; int i = 1; boolean b = (boolean)i;

no. boolean values cannot be cast as types

What happens if you enter an integer as 05?

same as 5

What is x after the following if-else statement is executed? Use a switch statement to rewrite it and draw the flowchart for the new switch statement. 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;

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

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("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; }

What is wrong in the following code? if radius >= 0 { area = radius * radius * PI; System.out.println("The area for the circle of " + " radius " + radius + " is " + area); }

the parenthesis is required around the if statement

Multiple-Choice Question 3.2.3 What is 1 + 1 + 1 + 1 + 1 == 5?

true

What is the value of the following expression? true || true && false

true

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

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

Assume x is an integer. Write a simple expression that returns true if x is even or false if x is odd.

x % 2 == 0

Assume that x and y are int type. Which of the following are legal Java expressions? x > y > 0 x = y && y x /= y x or y x and y (x != 0) || (x = 0)

x > y > 0 incorrect because x > y is evaluated to a boolean value, which cannot be compared to 0 x = y && y -> cannot conver boolean types x /= y correct x or y incorrect x and y incorrect (x != 0) || (x = 0) incorrect x = 0 is wrong it should be x == 0

What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } }

y is 2 because x + 1 is assigned to x and then x is assigned to y.

(a) if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income - 10000) * 0.15; (b) if (income <= 10000) tax = income * 0.1; else if (income > 10000 && income <= 20000) tax = 1000 + (income - 10000) * 0.15;

yes

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

yes

Is x > 0 && x < 10 the same as x > 0 && x < 10? Is x > 0 || x < 10 the same as x > 0 || x < 10? Is (x > 0 || x < 10) && y < 0 the same as (x > 0 || (x < 10 && y < 0))?

yes yes yes


Conjuntos de estudio relacionados

Quiz 5: Using information ethically

View Set

Chapter 48 Skin Integrity and Wound Care

View Set