文档库 最新最全的文档下载
当前位置:文档库 › java第三章

java第三章

1. 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);

#
2. Suppose x=0 and y=0 what is x after evaluating the expression (y > 0) && (1 > x++).

a. 1
b. 0
c. -1

#
3. 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");


a. too hot
b. just right
c. too cold
d. too hot too cold just right

#
4. What is the printout of 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);
}


a. a
b. aa
c. abcd
d. ab
e. abc

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

a. &&, ||, *, +
b. *, +, &&, ||
c. *, +, ||, &&
d. ||, &&, *, +

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

#
7. Which of the following are valid specifiers for the printf statement?

a. %8.2d
b. %4c
c. %6d
d. %10.2e
e. %10b

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

a. x != 5
b. x == 5
c. !(x == 4)
d. x != 4

#
9. Which of the Boolean expressions below is incorrect?

a. (x > 0) || (x < 0)
b. (-10 < x < 0)
c. (true) && (3 => 4)
d. (x != 0) || (x = 0)
e. !(x > 0) && (x > 0)

#
10. __________ are the boolean operators.

a. >
b. ^
c. &&
d. !

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

a. 123.4
b. 1234.5
c. 1234.6
d. 1234.56
e. 123.5

#
12. Which of the following expression is equivalent to (x > 1).


a. x >= 1
b. !(x = 1)
c. !(x < 1)
d. !(x <= 1)

#
13. Which o

f the following is not a valid boolean expression.

a. (1 < x < 100)
b. x == 1
c. (x = 1) || (x != 1)
d. (x =< 5) && (x>=5)

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

a. x == 5
b. x != 4
c. !(x == 4)
d. x != 5

#
15. 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. The program has a syntax error
b. The program has a runtime error
c. The program displays "It is odd!"
d. The program displays "It is even!"

#
16. 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. 4
b. 2
c. 3
d. 0
e. 1

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

a. (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)
b. year % 4 == 0 && (year % 100 != 0) || year % 400 == 0
c. year % 4 == 0 && year % 100 != 0 || year % 400 == 0
d. (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)

#
18. What is 1.0 + 1.0 + 1.0 == 3.0?

a. false
b. true
c. There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.

#
19. Analyze the following fragment.

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

a. The switch control variable cannot be double.
b. The required break keyword is missing in the switch statement.
c. d + 4 should be replaced by 5.
d. The required default keyword is missing in the switch statement.

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

a. false
b. true

#
21. Analyze the following code:

// Enter an integer
String numString = JOptionPane.showInputDialog(null,
"Enter a number:",
"Exam Input", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(numString);

if (number <= 0)
System.out.println(number);


a. The if statement is wrong, because it does not have the else clause;
b. If number is positive, number is displayed.
c. number entered from the input cannot be negative.
d. If number is zero, number is displayed;
e. System.out.println(number); must be placed inside braces;

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

a. true
b. false

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

a. false
b. true

#
24. The not equal comparison operator in Java is __________.

a. !=
b. ^=
c. != =
d. <>

#
25. What is 1 - 1.0 - 1.0 - 1.0 - 1.0 - 1.0 == 5.0?

a. There is no guarantee that 1 - 1.0 - 1.0 - 1.0 - 1.0 - 1.0 == 5.0 is true.
b. true
c. false

#
26. The statement System.out.printf("%5d",

123456) outputs ___________.

a. 23456
b. 12345.6
c. 12345
d. 123456

#
27. Which of the following expressions evaluates to true?

a. 34 > 34
b. 'A' > 'z'
c. 2 > '2'
d. 'a' > 'A'

#
28. Analyze the following code:

if (x < 100) && (x > 10)
System.out.println("x is between 10 and 100");

a. The statement compiles fine.
b. The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses.
c. The statement compiles fine, but has a runtime error.
d. 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.

#
29. _____ are short-circuit operators.

a. &&
b. ||
c. !
d. ^

#
30. The conditional operator ? : is a ______

a. binary operator
b. unary operator
c. ternary operator

#
31. The binary operator + is left-associative.

a. false
b. true

#
32. Which of the Boolean expressions below has incorrect syntax?

a. !(x > 0) && (x > 0)
b. (x != 0) || (x = 0)
c. (true) && (3 > 4)
d. (x > 0) || (x < 0)

#
33. 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. IV
b. III
c. II
d. I

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

a. 11
b. 9
c. 10

#
35. 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))

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

a. !=
b. <=
c. <<
d. =<
e. <

#
37. Analyze the following code.

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

a. The code displays nothing.
b. The code is wrong. You should replace if (even) with if (even == true)
c. The code is wrong. You should replace if (even) with if (even = true)
d. The code displays It is even!

#
38. What is the output of the following code?

char ch = 'F';
if (ch >= 'A' && ch <= 'Z')
System.out.println(ch);


a. F f
b. nothing
c. F
d. f

#
39. What is the output of the following code?

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


a. true false
b. true
c. nothing
d. false

#
40. 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)

#
41. 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 followed by Income is greater than 4000
b. Income is greater than 4000
c. Income is greater than 3000
d. no output
e. Income is greater than 4000 followed by Income is greater than 3000

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

a. 11
b. 10
c. 9

#
43. 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");


a. x <= 9 and z < 7;
b. none
c. x <= 9 and z >= 7;
d. x > 9 and y > 8;

#
44. What is x after evaluating

x = (2 > 3) ? 2 : 3;


a. 3
b. 2
c. 4
d. 5

#
45. Analyze the following code:

int i = 3434; double d = 3434;
System.out.printf("%5.1f %5.1f", i, d);


a. i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.
b. The code compiles and runs fine to display 3434 3434.0.
c. The code compiles and runs fine to display 3434.0 3434.0.

#
46. Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x-- > 10).

a. 10
b. 11
c. 9

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

a. true
b. false

#
48. 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);


a. Code 1 has compile errors.
b. Both Code 1 and Code 2 are correct, but Code 2 is better.
c. Code 2 has compile errors.
d. Both Code 1 and Code 2 have compile errors.

#
49. The default case must be specified in a switch statement.

a. true
b. false

#
50. 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. 2
d. 0

#
51. Analyze the following code:

boolean even = false;
if (even = true) {
System.out.println("It is even!");
}

a. The program runs, but displays nothing.
b. The program runs and displays It is even!.
c. The program has a runtime error.
d. The program has a compile error.

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

a. x >= y || x <= y
b. x > y && x < y
c. ! (x == y)
d. x > y || x < y

#
53. 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

#
54. 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 false
c. in case cond1 is true and cond2 is true
d. in case cond1 is false and cond2 is false

#
55. 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");

a. x > 0 and y > 0;
b. x < 0 and z > 0;
c. x < 0 and z < 0;
d. no printout.

#
56. 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
b. IV
c. II
d. III

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

a. true
b. false

#
58. Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true?

if (cond1 || cond2) ...


a. in case cond1 is true and cond2 is true
b. in case cond1 is false and cond2 is true
c. in case cond1 is true and cond2 is false
d. in case cond1 is false and cond2 is false

#
59. The __________ method immediately terminates the program.

a. System.terminate(0);
b. System.stop(0);
c. System.quit(0);
d. System.exit(0);
e. System.halt(0);

#
60. Suppose x=10 and y=10. What

is x after evaluating the expression (y > 10) && (x-- > 10)?

a. 10
b. 11
c. 9

#
61. Analyze the following code.

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.

#
62. Analyze the following code:

String numString = JOptionPane.showInputDialog(null,
"Enter a number:",
"Exam Input", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(numString);
if (number <= 0)
System.out.println(number);
System.out.println(number);


a. number is printed out twice if number is negative;
b. number is printed out twice if number is zero;
c. number is printed out once if number is positive.
d. number is always printed out at least once;

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

a. false
b. true

#
64. The equal comparison operator in Java is __________.

a. <>
b. !=
c. ==
d. ^=

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

a. true
b. false

#
66. The ________ operator can be used to compare two values.

a. numerical
b. casting
c. relational
d. boolean

#
67. Which of the following operators are right-associative.

a. *
b. + (binary +)
c. =
d. %
e. &&

#
68. What is 1 + 1 + 1 + 1 + 1 == 5?

a. There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true.
b. true
c. false

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

#
70. In Java, the word true is ________.

a. a Boolean literal
b. same as value 1
c. same as value 0
d. a Java keyword

#
71. The assignment operator = is left-associative.

a. false
b. true

#
72. 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 == false)
c. if (!isPrime = false)
d. if (isPrime)
e. if (isPrime = true)

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

a. 0.123e+04
b. 0.123456e+04
c. 1.23+03
d. 1.2e+03
e. 0.1e+04

#
74. 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");


a. x equals 0
b. x is greater than 0

c. None
d. x is less than 0

#
75. Analyze the following code.

int x = 0;
if (x > 0);
{
System.out.println("x");
}

a. The symbol x is always printed.
b. The symbol x is always printed twice.
c. Nothing is printed because x > 0 is false.
d. The value of variable x is always printed.

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

a. true
b. false

#
77. What is the printout of 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);
}


a. bcd
b. bb
c. bbb
d. abcd
e. b

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

a. false
b. true

#
79. What is y after the following statement is executed?

x = 0;
y = (x > 0) ? 10 : -10;

a. 20
b. 10
c. -10
d. 0
e. Illegal expression

#
80. 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 == 1
d. x % 2 == 0 && y % 2 == 0

#
81. 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

#
82. 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. Both fragments compile, but produce different results.
b. The first fragment has a syntax error.
c. Both fragments produce the same output.
d. The second fragment has a syntax error.

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

a. false
b. true

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

a. (cond1 || cond2) && cond3
b. cond1 || (cond2 && cond3)

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

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

a. (ch >= 'A' || ch <= 'Z')
b. (ch

>= 'A' && ch >= 'Z')
c. (ch >= 'A' && ch <= 'Z')
d. ('A' <= ch <= 'Z')

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

a. 12345*****
b. ****123456
c. 23456*****
d. 123456****

相关文档
相关文档 最新文档