Quiz Answer for Exercise3 PDF

Title Quiz Answer for Exercise3
Course Introduction to Computer Science
Institution University of Waikato
Pages 26
File Size 465.7 KB
File Type PDF
Total Downloads 61
Total Views 172

Summary

Quiz Answer for Exercise 3
You can get answers for this document....


Description

问题 1 1. Which of the following statements are true?

A.(x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B.(x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) C.(x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0) D.(x > 0 || x < 10) is same as ((x > 0) || (x < 10)) 10 分

问题 2 1. Which of the Boolean expressions below has incorrect syntax?

A.(x != 0) || (x = 0) B.(true) && (3 > 4) C.!(x > 0) && (x > 0) D.(x > 0) || (x < 0) 10 分

问题 3 1. 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"); A. The first fragment has a syntax error. B. Both fragments compile, but produce different results. C. Both fragments produce the same output. D. The second fragment has a syntax error.

10 分

问题 4

1.

The equal comparison operator in Java is __________. A.== B.!= C. D.^= 10 分

1.

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

A.=< B. 0) ? 10 : -10; A.20 B.Illegal expression C.10 D.-10 E.0

10 分

1.

问题 9 Analyze the following code.

boolean even = false; if (even) { System.out.println("It is even!"); } A.The code displays It is even! B.The code is wrong. You should replace if (even) with if (even == true). C.The code displays nothing. D.The code is wrong. You should replace if (even) with if (even = true).

10 分

1.

问题 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; A.Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. B.All three are correct, but Code 1 is preferred. C.Code 3 has a compile error, because you attempt to assign number to even. D.All three are correct, but Code 2 is preferred. E.All three are correct, but Code 3 is preferred.

10 分

问 题 11 1. _____ are short-circuit operators.

A.|| B.^ C.&& D.! 10 分

1.

问题 12 What is 1 + 1 + 1 + 1 + 1 == 5?

A.false B.true C.There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true. 10 分

问题 13 1. The not equal comparison operator in Java is __________.

A. B.^= C.!= = D.!= 10 分

问题 14 1. Including a space between the relational operators >=, '2' B.'A' > 'z' C.34 > 34 D.'a' > 'A' 10 分

1.

问题 23 Which of the following is a possible output from invoking Math.random()?

A.1.0 B.0.5 C.3.43 D.0.0 10 分

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

A.&&, ||, ^, *, + B.*, +, ^, &&, || C.*, +, &&, ||, ^ D.*, +, ^, ||, && E.^, ||, &&, *, + 10 分

问题 25 1. The assignment operator = is left-associative.

A.false B.true 10 分

问题 26 1. 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.Income is greater than 3000 B.no output C.Income is greater than 4000 followed by Income is greater than 3000 D.Income is greater than 3000 followed by Income is greater than 4000 E.Income is greater than 4000

10 分

问题 27 1. __________ are the boolean operators.

A.&& B.^ C.>

D.! 10 分

1.

问题 28 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.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. D.y is 1 because x is assigned to y first.

10 分

问题 29 1. 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"); A.I V B.I C.II D.II I

10 分

问题 30 1. 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 = false) B.if (isPrime) C.if (!isPrime == false) D.if (isPrime = true) E.if (isPrime == true) 10 分

问题 31 1. What is the output of the following code?

boolean even = false; System.out.println((even ? "true" : "false")); A.true false B.true

C.nothin g D.false

10 分

问题 32 1. You can always convert an if statement to a switch statement.

A.true B.false 10 分

问题 33 1. 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!");A A.The program displays "It is odd!" B.The program has a syntax error C.The program has a runtime error D.The program displays "It is even!"

10 分

问题 34 1.

What is x after evaluating

x = (2 > 3) ? 2 : 3; A. 5 B. 3 C. 2 D.

4

10 分

1.

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

A.10 B.11 C.9

10 分

问题 36 1. System.exit(0) can be used to terminate the program.

A.false B.true 10 分

问题 37 1. A break statement is required for a switch-case statement in Java.

A.false B.true 10 分

问题 38 1. You can cast a Boolean value to an int, or an int to Boolean.

A.false B.true 10 分

1.

问题 39 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 }

A.1 B.1 C.0 D. 2

10 分

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

A.true B.false 10 分

问题 41 1. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) && (1 > x++).

A.0 B.1 C.-1 10 分

问题 42 Suppose you write the code to display "Cannot get a 1. 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 V B.I C.II I D.II

10 分

问题 43 1. Assume x = 14 and y = 15, Which of the following is true?

A.x % 2 == 0 && y % 2 == 0 B.x % 2 == 0 || y % 2 == 0 C.x % 2 != 0 && y % 2 != 0 D.x % 2 == 0 && y % 2 == 1 10 分

问题 44 1. Which of the following are so called short-circuit operators?

A.& B.&& C.|| D.|

10 分

问题 45 1. What is the output from System.out.println((int)Math.random() * 4)? A.2 B.3 C.1 D.0 E.4 10 分

1.

问题 46 What is the possible output from System.out.println((int)(Math.random() * 4))? A.0 B.2 C.4 D.1

E.3 10 分

问题 47 1. Assume x = 4 and y = 5, which of the following is true?

A.x > 5 && y > 5 B.x < 5 || y < 5 C.x > 5 || y > 5 D.x < 5 && y < 5 10 分

问题 48 1. Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x++ > 10).

A.10 B.11 C.9 10 分

问题 49 1. The __________ method immediately terminates the program.

A.System.halt(0); B.System.quit(0); C.System.exit(0); D.System.terminate(0); E.System.stop(0); 10 分

问题 50 Analyze the following code. 1.

int x = 0; if (x > 0); { System.out.println("x"); } A.Nothing is printed because x > 0 is false. B.The value of variable x is always printed. C.The symbol x is always printed. D.The symbol x is always printed twice.

10 分

问题 51 1. The exclusive or (^) of true and true is true.

A.true B.false 10 分

问题 52 Analyze the following program fragment: 1.

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

10 分

问题 53 Analyze the following code: 1.

boolean even = false; if (even = true) { System.out.println("It is even"); } A.The program has a runtime error. B.The program runs fine and displays It is even. C.The program has a compile error. D.The program runs fine, but displays nothing.

10 分

问题 54 1. Which of the following is equivalent to x != y?

A.x >= y || x y && x < y C.x > y || x < y D.! (x == y) 10 分

1.

问题 55 What is 1.0 + 1.0 + 1.0 == 3.0?

A.true B.false C.There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true. 10 分

问题 56 1. What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?

A.false B.true C.There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true. 10 分

问题 57 1. Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10).

A.9 B.10 C.11 10 分

问题 58 1. Assume x = 4 and y = 5, which of the following is true?

A.x != 5 ^ y != 4 B.x != 4 ^ y == 5 C.!(x == 4) ^ y != 5 D.x == 5 ^ y == 4 10 分

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

A.11 B.10 C.9 10 分

问题 60 1. What is (int)Math.random()?

A.1.1 B.0.5 C.1 D.0 10 分

问题 61 1. The binary operator + is left-associative.

A.true B.false 10 分

1.

问题 62 Analyze the following fragment.

double double switch case 5: case 6: }

x = 0; d = 1; (d + 4) { x++; --x;

A.The switch control variable cannot be double. B.The required break keyword is missing in the switch statement. C.The required default keyword is missing in the switch statement. D.d + 4 should be replaced by 5.

10 分

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

A.true B.false

10 分

问题 64 1. Which of the following expression is equivalent to (x > 1).

A.!(x = 1) B.!(x = 1 D.!(x < 1) 10 分

1.

问题 65 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 10) System.out.println("x is between 10 and 100"); A.The statement compiles fine. B.The statement compiles fine, but has a runtime error. C.The statement has compile errors because (x 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block. D.The statement has compile errors because (x 10) must be enclosed inside parentheses.

10 分

问题 67 1. The ________ operator can be used to compare two values.

A.boolean B.casting C.relational D.numerical 10 分

问题 68 Suppose x = 1, y = -1, and z = 1. What is the 1. 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"); A.x < 0 and z < 0; B.x > 0 and y > 0; C.no printout. D.x < 0 and z > 0;

10 分

问题 69 1. Given |x - 2| 1)) || (x < 0) C.((x < 100) && (x > 1)) && (x < 0) D.(1 > x > 100) || (x < 0) 10 分

1.

问题 73 Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true?

if (cond1 || cond2) ... A.in case cond1 is false and cond2 is true B.in case cond1 is true and cond2 is true

C.in case cond1 is true and cond2 is false D.in case cond1 is false and cond2 is false

10 分

问题 74 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.((x < 100) && (x > 1)) || (x < 0) B.(1 > x > 100) || (x < 0) C.((x < 100) && (x > 1)) && (x < 0) D.1 < x < 100 && x < 0 10 分

问题 75 1. The conditional operator ? : is a ______

A.binary operator B.unary operator C.ternary operator 10 分

问题 76 Analyze the following code. 1.

int x = 0; int y = ((x < 100) && (x > 0)) ? 1: -1; A.y becomes -1 after the code is executed. B.y becomes 1 after the code is executed.

10 分

问题 77 1. Analyze the following code:

Code 1:

int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0); A.Code 2 has compile errors. B.Code 1 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.

10 分

问题 78 The following code displays ___________. 1.

double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature = 4, which of the following is true? A.x - 2 >= 4 || x - 2 = 4 && x - 2 < -4 C.x - 2 >= 4 || x - 2 < -4 D.x - 2 >= 4 && x - 2 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x = 7"); else System.out.println("x...


Similar Free PDFs