Ch 3

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

What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10; a. -10 b. 0 c. 10 d. 20 e. Illegal expression

a. -10

What is the output from System.out.println((int)Math.random() * 4)? a. 0 b. 1 c. 2 d. 3 e. 4

a. 0

Analyze the following code. int x = 0;i f (x > 0); { System.out.println("x"); } a. The symbol x is always printed. b. The value of variable x is always printed. c. Nothing is printed because x > 0 is false. d. The symbol x is always printed twice.

a. The symbol x is always printed.

The binary operator + is left-associative. a. true b. false

a. true

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

a. true

What is (int)Math.random()? a. 0 b. 1 c. 0.5 d. 1.1

a.0

Analyze the following code: if (x < 100) && (x > 10)System.out.println("x is between 10 and 100"); a.The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses. b.The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println(...) statement must be put inside a block. c.The statement compiles fine. d.The statement compiles fine, but has a runtime error.

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

Which of the following expression is equivalent to (x > 1). a. x >= 1 b. !(x <= 1) c. !(x = 1) d. !(x < 1)

b. !(x <= 1)

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

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

Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10). a. 9 b. 10 c. 11

b. 10

Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)? a. 9 b. 10 c. 11

b. 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; } a. 1 b. 2 c. 3 d. 4 e. 0

b. 2

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"); a. I b. II c. III d. IV

b. II

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"); } a. no output b. Income is greater than 3000 c. Income is greater than 3000 followed by Income is greater than 4000 d. Income is greater than 4000 e. Income is greater than 4000 followed by Income is greater than 3000

b. Income is greater than 3000

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

b. false

You can always convert an if statement to a switch statement. a. true b. false

b. false

Assume x = 4 and y = 5, Which of the following is true? a. !(x == 4) ^ y != 5 b. x != 4 ^ y == 5 c. x == 5 ^ y == 4 d. x != 5 ^ y != 4

b. x != 4 ^ y == 5

Given |x - 2| >= 4, Which of the following is true? a. x - 2 >= 4 && x - 2 <= -4 b. x - 2 >= 4 || x - 2 <= -4 c. x - 2 >= 4 && x - 2 < -4 d. x - 2 >= 4 || x - 2 < -4

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

Analyze the following code: // Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <= 0) System.out.println(number); a. The if statement is wrong, because it does not have the else clause; b. System.out.println(number); must be placed inside braces; c. If number is zero, number is displayed; d. If number is positive, number is displayed.e. number entered from the input cannot be negative.

c. If number is zero, number is displayed;

The __________ method immediately terminates the program. a. System.terminate(0); b. System.halt(0); c. System.exit(0); d. System.quit(0); e. System.stop(0);

c. 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; } a. The program has a compile error because the required break statement is missing in the switch statement. b. The program has a compile error because the required default case is missing in the switch statement. c. The switch control variable cannot be double. d. No errors.

c. The switch control variable cannot be double.

Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true. a. if (isPrime = true) b. if (isPrime == true) c. if (isPrime) d. if (!isPrime = false) e. if (!isPrime == false)

c. if (isPrime)

The conditional operator ? : is a ______ a. unary operator b. binary operator c. ternary operator

c. ternary operator

Given |x - 2| <= 4, Which of the following is true? a. x - 2 <= 4 && x - 2 >= 4 b. x - 2 <= 4 && x - 2 > -4 c. x - 2 <= 4 && x - 2 >= -4 d. x - 2 <= 4 || x - 2 >= -4

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

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. y is 0. b. y is 1 because x is assigned to y first. c. y is 2 because x + 1 is assigned to x and then x is assigned to y. d. The program has a compile error since x is redeclared in the statement int y = x = x + 1.

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

Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0)even = true; elseeven = false; Code 2: int number = 45; boolean even = (number % 2 == 0); a. Code 1 has compile errors. b. Code 2 has compile errors. c. Both Code 1 and Code 2 have compile errors. d. Both Code 1 and Code 2 are correct, but Code 2 is better.

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

Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); } a. The program has a compile error. b. The program has a runtime error. c. The program runs fine, but displays nothing. d. The program runs fine and displays It is even!.

d. The program runs fine and displays It is even!.

Which of the following operators are right-associative. a. * b. + (binary +) c. % d. && e. =

e. =

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: Code 2:even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; a. Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. b. Code 3 has a compile error, because you attempt to assign number to even. c. All three are correct, but Code 1 is preferred.d. All three are correct, but Code 2 is preferred. e. All three are correct, but Code 3 is preferred.

e. All three are correct, but Code 3 is preferred.


Conjuntos de estudio relacionados

Anatomy & Physiology: Chapter 26 & 27

View Set

TestOut Network Pro v6.0 | 3.1 Copper Cables and Connectors | 3.1.7 Practice Questions

View Set

Biotechnology Exam #3 Ch. 6, CHEM 527 Exams, Biotechnology Exam #2 Ch.18 Plant Biotechnology, Biotechnology Ch. 3 Exam 1, Biotechnology Exam #2 Ch. 5 (Genomics), Biotechnology Ch. 2 Exam #1, Biotechnology Ch. 1 Exam 1, Biotech Test 3, Biotechnology E...

View Set

Chapter 5: Competitive Advantage, Firm Performance, and Business Models

View Set