public class Test1 {
public static void main(String[ ] args) { int x = 1;
int y = x = x + 1;
System.out.println(\ } }
A)y is 1 because x is assigned to y first.
B)y is 2 because x + 1 is assigned to x and then x is assigned to y. C)y is 0.
D)The program has a compile error since x is redeclared in the statement int y = x = x + 1. 34)What is i printed? public class Test {
public static void main(String[ ] args) { int j = 0;
int i = ++j + j * 5;
System.out.println(\ } }
A)0 B) 1 C) 6 D) 5
35)What is i printed in the following code? public class Test {
public static void main(String[ ] args) { int j = 0;
int i = j++ + j * 5;
System.out.println(\ } }
A)5 B) 6 C) 1 D) 0
36)What is y displayed in the following code? public class Test {
public static void main(String[ ] args) { int x = 1;
int y = x++ + x;
System.out.println(\ } }
A)y is 4. B) y is 2. C) y is 3. D) y is 1. 37)What is y displayed? public class Test {
public static void main(String[ ] args) { int x = 1;
int y = x + x++;
System.out.println(\
- 6 -
} }
A)y is 1. B) y is 2. C) y is 4. D) y is 3.
38)To assign a double variable d to a float variable x, you write _____ A)x = (float)d; B) x = (int)d; C)x = (long)d D) x = d; 39)What is the printout of the following code: double x = 5.5; int y = (int)x;
System.out.println(\ A)x is 5.5 and y is 5.0 B)x is 6.0 and y is 6.0 C)x is 5.5 and y is 5 D)x is 5 and y is 6 E)x is 6 and y is 6
40)Which of the following assignment statements is illegal? (Choose all that apply.) A)float f = -34; B)int t = (int)false; C)short s = 10; D)int t = 4.5; E)int t = 23;
41)What is the value of (double)5/2? A)2.5 B) 3 C) 3.0 D) 2.0 E) 2
42)What is the value of (double)(5/2)? A)3 B) 2.0 C) 2.5 D) 2 E) 3.0
43)The expression (int)(76.0252175 * 100) / 100 evaluates to ________. A)76 B) 76.02 C)76.03 D) 76.0252175
44)If you attempt to add an int, a byte, a long, and a double, the result will be a ________ value. A)int B) byte C) double D) long
45)Which of the following is the correct expression of character 4? A)'4' B) \
46)A Java character is stored in ________. A)three bytes B) two bytes C)one byte D) four bytes
47)Suppose x is a char variable with a value 'b'. What is the printout of the statement System.out.println(++x)? A)b B) d C) c D) a
48)Which of the following statement prints smith\\exam1\\test.txt? A)System.out.println(\ B)System.out.println(\ C)System.out.println(\ D)System.out.println(\
49)Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
A)System.out.println(i); B) System.out.println(i + \
- 7 -
C)System.out.println((char)i); D) System.out.println((int)i); 50)The Unicode of 'a' is 97. What is the Unicode for 'c'? A)96 B) 99 C) 97 D) 98
51)Will System.out.println((char)4) display 4? A)Yes B) No
52)What is the printout of System.out.println('z' - 'a')? A)25 B) z C) a D) 26
53)An int variable can hold ________. (Choose all that apply.) A)120.0 B) 120 C) \
54)Which of the following assignment statements is correct? (Choose all that apply.) A)char c = \ B) char c = 'd'; C)char c = 100; D) char c = \ 55)The expression \
A)Java123 B)Java 123 C)java 123 D)Java6 E)Illegal expression
56)Note that the Unicode for character A is 65. The expression \A)B B) A1 C)66 D) Illegal expression
57)Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________. A)A1 B) 66 C)B D) Illegal expression
58)The System.currentTimeMillis() returns ________. A)the current time in milliseconds
B)the current time in milliseconds since midnight, January 1, 1970
C)the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time) D)the current time in milliseconds since midnight E)the current time
59)Programming style is important, because ________. (Choose all that apply.) A)good programming style can make a program run faster B)a program may not compile if it has a bad style
C)good programming style helps reduce programming errors D)good programming style makes a program more readable
60)According to Java naming convention, which of the following names can be variables? (Choose all that apply.)
A)TOTAL_LENGTH B)totalLength C)class D)findArea E)FindArea
61)If a program compiles fine, but it produces incorrect result, then the program suffers ________.
A)a logic error B)a compilation error C)a runtime error
62)The ________ method displays an input dialog for reading a string. (Choose all that apply.) A) String string = JOptionPane.showInputDialog(\a string\\Demo\JOptionPane.QUESTION_MESSAGE);
B) String string = JOptionPane.showInputDialog(null, \a string\\Demo\JOptionPane.QUESTION_MESSAGE);
C) String string = JOptionPane.showInputDialog(null, \ D) String string = JOptionPane.showInputDialog(\
E) String string = JOptionPane.showMessageDialog(null, \a string\\Demo\JOptionPane.QUESTION_MESSAGE);
63)The ________ method parses a string s to an int value.
- 8 -
A)integer.parseInt(s); B)Integer.parseInteger(s); C)integer.parseInteger(s); D) Integer.parseInt(s); 64)The ________ method parses a string s to a double value. A)Double.parsedouble(s); B) double.parse(s);
C)double.parseDouble(s); D) Double.parseDouble(s); 65)Analyze the following code. import javax.swing.*; public class ShowErrors {
public static void main(String[ ] args) { int i; int j;
String s = JOptionPane.showInputDialog(null, \
JOptionPane.QUESTION_MESSAGE); j = Integer.parseInt(s); i = (i + 4); } }
A)The program compiles but has a runtime error because i does not have an initial value when it is used in i = i + 4;
B)The program compiles and runs fine.
C)The program cannot compile because i does not have an initial value when it is used in i = i + 4; D)The program cannot compile because j is not initialized.
1)C 2)A, B, C 3)B 4)A 5)A, B 6)B, E 7)A, D 8)B 9)B 10)A, D 11)C 12)A, B 13)A 14)A 15)A 16)A 17)C, D, E 18)D 19)E 20)E 21)D 22)D 23)D 24)A, D, E 25)B, D 26)D 27)A 28)B29)A 30)C 31)C 32)D 33)B 34)C 35)A 36)C 37)B 38)A 39)C 40)B, D 41)A 42)B 43)A 44)C 45)A46)B 47)C 48)B 49)C 50)B 51)B 52)A 53)B, E 54)B, C 55)B 56)B 57)B 58)C 59)C, D 60)B, D 61)A 62)B, C, D 63)D 64)D 65)C
Chapter3
1)The \ A)<< B) != C) =< D) <= E) <
2)The equal comparison operator in Java is ________. A)!= B) <> C) ^= D) ==
3)What is 1 + 1 + 1 + 1 + 1 == 5?
A)true B)false C)There is no guarantee that 1 + 1 + 1 + 1 + 1 == 5 is true. 4)What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? _______
A)true B)false C)There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. 5)In Java, the word true is ________.
A)same as value 0 B) a Boolean literal C)a Java keyword D) same as value 1 6)________ is the code with natural language mixed with Java code.
A)A flowchart diagram B) Java program C)A Java statement D) Pseudocode 7)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);
- 9 -
C)if (radius >= 0) System.out.println(radius * radius * 3.14159); D)if (radius != 0) System.out.println(radius * radius * 3.14159);
8)Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? if (x > 0) if (y > 0)
System.out.println(\else if (z > 0)
System.out.println(\
A)x > 0 and y > 0; B) x < 0 and z < 0; C)x < 0 and z > 0; D) no printout. 9)Analyze the following code: boolean even = false; if (even = true) {
System.out.println(\}
A)The program has a runtime error.
B)The program runs fine, but displays nothing. C)The program has a compile error.
D)The program runs fine and displays It is even!. 10)Analyze the following code. boolean even = false; if (even) {
System.out.println(\}
A)The code is wrong. You should replace if (even) with if (even == true) B)The code is wrong. You should replace if (even) with if (even = true) C)The code displays nothing. D)The code displays It is even!
11)The following code displays ________. double temperature = 50; if (temperature >= 100)
System.out.println(\else if (temperature <= 40)
System.out.println(\else
System.out.println(\
A)too cold B) just right C)too hot too cold just right D) too hot 12)Analyze the following code: Code 1:
boolean even;
if (number % 2 == 0) even = true; else
even = false; Code 2:
- 10 -