Chapter 3 java

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

What is y after the following switch statement? int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1 }

-1

What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10;

-10

Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0;

All three are correct, but Code 3 is preferred.

Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false;

Both Code 1 and Code 2 are correct, but Code 2 is better.

Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); }

The code displays nothing.

Analyze the following two code fragments: (i) int x = 5; if (0 < x) && (x < 100) System.out.println("x is between 1 and 100"); (ii) int x = 5; if (0 < x && x < 100) System.out.println("x is between 1 and 100");

The first fragment has a syntax error.

Analyze the following code: boolean even = ((231 % 2) == 0); if (even = true) System.out.println("It is even!"); else System.out.println("It is odd!");

The program displays "It is even!"

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); }

The program runs fine and displays It is even!

Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");

The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.

Analyze the following fragment: double x = 0; double d = 1; switch (d + 4) { case 5: x++; case 6: --x; }

The switch control variable cannot be double.

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

Analyze the following code: int x = 0; if (x > 0); { System.out.println("x"); }

The symbol x is always printed.

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.

What is 1.0 + 1.0 + 1.0 == 3.0?

There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.

In Java, the word true is ________.

a Boolean literal

Suppose cond1, cond2, and cond3 are Boolean expressions. Which of the following expression is equivalent to cond1 || cond2 && cond3?

cond1 || (cond2 && cond3)

A break statement is required for a switch-case statement in Java.

false

In a switch statement, the default case must appear last among all cases. Otherwise, it would result in a compilation error.

false

The assignment operator = is left-associative.

false

The break keyword must be used in a switch statement; otherwise, a syntax error occurs.

false

The default case must be specified in a switch statement.

false

The exclusive or (^) of true and true is true.

false

What is the output of the following code? boolean even = false; System.out.println((even ? "true" : "false"));

false

You can always convert an if statement to a switch statement.

false

You can cast a Boolean value to an int, or an int to Boolean.

false

Suppose isPrime is a boolean variable. Which of the following is the correct and best statement for testing if isPrime is true?

if (isPrime)

Which of the following code displays the area of a circle if the radius is positive?

if (radius > 0) System.out.println(radius * radius * 3.14159);

Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true? if (cond1 || cond2) ...

in case cond1 is false and cond2 is true in case cond1 is true and cond2 is true in case cond1 is true and cond2 is false

Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true? if (cond1 && cond2) ...

in case cond1 is true and cond2 is true

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

What is the output of the following code? (Please indent the statement correctly first.) int x = 9; int y = 8; int z = 7; if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x <= 9 and z >= 7"); else System.out.println("x <= 9 and z < 7");

none

Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");

x < 0 and z > 0;

Assume x = 4 and y = 5, which of the following is true?

x < 5 || y < 5

Assume x is 0. What is the output of the following statement? if (x > 0) System.out.print("x is greater than 0"); else if (x < 0) System.out.print("x is less than 0"); else System.out.print("x equals 0");

x equals 0

Analyze the following code: int x = 0; int y = ((x < 100) && (x > 0)) ? 1: -1;

y becomes -1 after the code is executed.

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.

________ are short-circuit operators.

|| ^ &&

The ________ method immediately terminates the program.

System.exit(0);

Given |x - 2| <= 4, which of the following is true?

x - 2 <= 4 && x - 2 >= -4

Given |x - 2| >= 4, which of the following is true?

x - 2 >= 4 || x - 2 <= -4

________ are the boolean operators.

! && ^

Which of the following is equivalent to x != y?

! (x == y) x > y || x < y

Which of the following expressions is equivalent to (x > 1)?

!(x <= 1)

The not equal comparison operator in Java is ________.

!=

Which of the following expressions evaluates to true?

'a' > 'A'

Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?

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

Which of the following is not a valid boolean expression?

(1 < x < 100) (x =< 5) && (x>=5) (x = 1) || (x != 1)

Which of the Boolean expressions below is incorrect?

(true) && (3 => 4) (x != 0) || (x = 0) (-10 < x < 0)

Which of the Boolean expressions below has incorrect syntax?

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

Which of the following statements are true?

(x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) (x > 0 && x < 10) is same as ((x > 0) && (x < 10))

Which of the following expressions is equivalent to (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)?

(year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0) year % 4 == 0 && (year % 100 != 0) || year % 400 == 0 year % 4 == 0 && year % 100 != 0 || year % 400 == 0

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

*, +, &, &&, ||

Suppose x=0 and y=0. What is x after evaluating the expression (y > 0) && (1 > x++)?

0

What is (int)Math.random()?

0

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()?

0.0 0.5

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10)?

10

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)?

10

Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x++ > 10)?

10

Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- > 10)?

10

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

2

What is x after evaluating the following?

3

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

==

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 correct? 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 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"); 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");

I II III IV

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

II

Analyze the following code: // Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <= 0) System.out.println(number);

If number is zero, number is displayed.

Suppose income is 4001, what is the output of the following code? 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

Analyze the following code: // Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <= 0) System.out.println(number); System.out.println(number);

number is printed out once if number is positive number is printed out twice if number is negative number is printed out twice if number is zero number is always printed out at least once

The ________ operator can be used to compare two values.

relational

The conditional operator ? : is a ________.

ternary operator

Including a space between the relational operators >=, <=, ==, and != causes a syntax error.

true

System.exit(0) can be used to terminate the program.

true

The binary operator + is left-associative.

true

What is 1 + 1 + 1 + 1 + 1 == 5?

true

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

true

You can always convert a switch statement to an equivalent if statement.

true

Assume x = 4 and y = 5, which of the following is true?

x != 4 ^ y == 5

Assume x = 4, which of the following is true?

x != 5

Assume x = 14 and y = 15, Which of the following is true?

x % 2 == 0 && y % 2 == 1 x % 2 == 0 || y % 2 == 0


Set pelajaran terkait

✬4 Software and Platform Services

View Set

Question 6 Briefly discuss three basic causes of accidents in the workplace

View Set

TEAS M.1.4 Solve equations in one variable

View Set

CMA Exam Part 1 Section B: PLanning, Budgeting, and Forecasting

View Set