CH 3 quiz

Ace your homework & exams now with Quizwiz!

All three are correct, but Code 3 is preferred.

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;

The code displays nothing.

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

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

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

The program runs and displays It is even!. Explanation: It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and prints 'It is even!'.

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

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

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

i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.

Analyze the following code: int i = 3434; double d = 3434; System.out.printf("%5.1f %5.1f", i, d);

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

!(x == y) x != y

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

x < 5 || y < 5

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

x != 5

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

Income is greater than 3000

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

if (!isPrime = false)

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

x < 0 and z > 0

Suppose x = 1, y = -1, and z = 1. What will be displayed by 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");

B. 10

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

10Explanation: For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.

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

****123456

The statement System.out.printf("%10s", "123456") outputs ___________. (Note: * represents a space)

1.2e+03

The statement System.out.printf("%3.1e", 1234.56) outputs ___________.

1234.6

The statement System.out.printf("%3.1f", 1234.56) outputs ___________.

123456

The statement System.out.printf("%5d", 123456) outputs ___________.

(ch >= 'A' || ch <= 'Z')

To check whether a char variable ch is an uppercase letter, you write ___________.

true

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

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

What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0?

false

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

F

What is the output of the following code? char ch = 'F'; if (ch >= 'A' && ch <= 'Z') System.out.println(ch);

x is 1

What is the output of the following code? int x = 0; if (x < 4) { x = x + 1; } System.out.println("x is " + x);

true

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

-10

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

2

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

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

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

a

What will be displayed by the following switch statement? char ch = 'a'; switch (ch) { case 'a': case 'A': System.out.print(ch); break; case 'b': case 'B': System.out.print(ch); break; case 'c': case 'C': System.out.print(ch); break; case 'd': case 'D': System.out.print(ch); }

b (because there is no break

What will be displayed by the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); }

Explanation: a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10 < x) && (x < 0)

Which of the Boolean expressions below is incorrect? A. (true) && (3 => 4) B. !(x > 0) && (x > 0) C. (x > 0) || (x < 0) D. (x != 0) || (x = 0) E. (-10 < x < 0)

ABCE

Which of the following are valid specifiers for the printf statement? A. %4c B. %10b C. %6d D. %8.2d E. %10.2e

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

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

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

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

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

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?

=

Which of the following operators are right-associative.

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

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 Boolean Literal

In Java, the word true is ________.

ABCD

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

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

System.out.printf("%3d %4.2f", number, Math.pow(number, 1.5)); The correct answer is C Explanation: Math.pow(number, 1.5) is a floating-point value and must be formatted using the specifier f, not d. number is an int and must be formatted using the specifier d.

Suppse number contains integer value 4, which of the following statement is correct? A. System.out.printf("%3d %4d", number, Math.pow(number, 1.5)); B. System.out.printf("%3d %4.2d", number, Math.pow(number, 1.5)); C. System.out.printf("%3d %4.2f", number, Math.pow(number, 1.5)); D. System.out.printf("%3f %4.2d", number, Math.pow(number, 1.5)); E. System.out.printf("%3f %4.2f", number, Math.pow(number, 1.5));

<=

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

System.exit(0);

The __________ method immediately terminates the program.

==

The equal comparison operator in Java is __________.

just right

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

*, +, &&, ||

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


Related study sets

PSYC&100 General Psychology: Chapter 16

View Set

Church History Trimester 1 Review Ch. 1

View Set

Finance: Ch 3 Institutional Lenders for Real Estate Finance

View Set

NCLEX/ ATI comp: Musculoskeletal (OA & RA)

View Set

Science 500 - Unit 2, Plants - TEST

View Set

Funeral Services NBE Arts Review

View Set

All BABOK Techniques - Advantages & Disadvantages

View Set