Download introduction-to-java-programming-brief-8th-edition

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
1) Suppose a Scanner object is created as follows:
1) _______
Scanner input = new Scanner(System.in);
What method do you use to read an int value?
A) input.int();
C) input.integer();
Answer: B
B) input.nextInt();
D) input.nextInteger();
2) The following code fragment reads in two numbers: (Choose all that apply.)
2) _______
Scanner input = new Scanner(System.in);
int i = input.nextInt();
double d = input.nextDouble();
What are the correct ways to enter these two numbers?
A) Enter an integer, a space, a double value, and then the Enter key.
B) Enter a numeric value with a decimal point, a space, an integer, and then the Enter key.
C) Enter an integer, an Enter key, a double value, and then the Enter key.
D) Enter an integer, two spaces, a double value, and then the Enter key.
Answer: A, C, D
3) If you enter 1 2 3, when you run this program, what will be the output?
3) _______
import java.util.Scanner;
public class Test1 {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
System.out.println(average);
}
}
A) 2.0
Answer: A
B) 4.0
4) Every letter in a Java keyword is in lowercase.
A) true
Answer: A
C) 1.0
D) 3.0
4) _______
B) false
5) Which of the following is a valid identifier? (Choose all that apply.)
A) 8+9
B) class
C) 9X
D) radius
Answer: D, E
5) _______
E) $343
6) Which of the following are correct names for variables according to Java naming conventions?
(Choose all that apply.)
A) RADIUS
B) findArea
C) Radius
D) FindArea
E) radius
Answer: B, E
6) _______
7) Which of the following are correct ways to declare variables? (Choose all that apply.)
A) int length; width;
B) int length, width;
C) int length; int width;
D) int length, int width;
Answer: B, C
7) _______
8) ________ is the Java assignment operator.
A) =:
B) =
Answer: B
8) _______
9) To assign a value 1 to variable x, you write
A) x = 1;
B) 1 = x;
C) x := 1;
Answer: A
D) ==
C) :=
9) _______
D) 1 := x;
E) x == 1;
10) Which of the following assignment statements is incorrect? (Choose all that apply.)
A) i = 1; j = 1; k = 1;
B) i = j = k = 1;
C) i == j == k == 1;
D) i = 1 = j = 1 = k = 1;
Answer: C, D
10) ______
11) To declare a constant MAX_LENGTH inside a method with value 99.98, you write
A) final double MAX_LENGTH = 99.98;
B) double MAX_LENGTH = 99.98;
C) final MAX_LENGTH = 99.98;
D) final float MAX_LENGTH = 99.98;
Answer: A
11) ______
12) Which of the following is a constant, according to Java naming conventions? (Choose all that
apply.)
A) Test
B) COUNT
C) MAX_VALUE
D) read
E) ReadInt
Answer: B, C
12) ______
13) To improve readability and maintainability, you should declare ________ instead of using literal
values such as 3.14159.
A) variables
B) constants
C) methods
D) classes
Answer: B
13) ______
14) Which of these data types requires the most amount of memory?
A) byte
B) int
C) long
Answer: C
14) ______
D) short
15) To declare an int variable number with initial value 2, you write
A) int number = 2l;
B) int number = 2.0;
C) int number = 2;
D) int number = 2L;
Answer: C
15) ______
16) What is the result of 45 / 4?
16) ______
A) 10
Answer: D
B) 12
C) 11.25
D) 11
17) Which of the following expressions will yield 0.5? (Choose all that apply.)
A) 1 / 2.0
B) (double) 1 / 2
C) 1.0 / 2
D) 1 / 2
E) (double) (1 / 2)
Answer: A, B, C
17) ______
18) Which of the following expression results in a value 1?
A) 2 % 1
B) 15 % 4
C) 37 % 6
Answer: C
18) ______
19) 25 % 1 is ________.
A) 1
Answer: E
D) 25 % 5
19) ______
B) 2
C) 3
D) 4
E) 0
20) -25 % 5 is ________.
A) 1
Answer: E
20) ______
B) 2
C) 3
D) 4
E) 0
21) 24 % 5 is ________.
A) 1
Answer: D
B) 2
C) 3
D) 4
E) 0
21) ______
22) -24 % 5 is ________.
A) -1
Answer: D
22) ______
B) -2
C) -3
D) -4
E) 0
23) -24 % -5 is ________.
A) 3
Answer: D
B) -3
C) 4
D) -4
E) 0
23) ______
24) To add a value 1 to variable x, you write (Choose all that apply.)
A) 1 + x = x;
B) x = 1 + x;
C) x += 1;
D) x := 1;
Answer: B, C, E
24) ______
E) x = x + 1;
25) To add number to sum, you write (Note: Java is case-sensitive) (Choose all that apply.)
A) sum = sum + number;
B) number = sum + number;
C) number += sum;
D) sum += number;
E) sum = Number + sum;
Answer: A, D
25) ______
26) Suppose x is 1. What is x after x += 2?
A) 0
B) 1
Answer: D
26) ______
27) Suppose x is 1. What is x after x -= 1?
C) 2
D) 3
E) 4
27) ______
A) 0
Answer: A
B) 1
C) 2
D) -1
E) -2
28) What is x after the following statements?
int x = 1;
int y = 2;
x *= y + 1;
A) x is 1.
Answer: B
B) x is 3.
28) ______
C) x is 2.
D) x is 4.
29) What is x after the following statements?
int x = 1;
x *= x + 1;
A) x is 2.
Answer: A
B) x is 4.
30) Math.pow(2, 3) returns ________.
A) 9.0
B) 8.0
Answer: B
29) ______
C) x is 3.
D) x is 1.
C) 9
D) 8
30) ______
31) The ________ method returns a raised to the power of b.
A) Math.exponent(a, b)
B) Math.power(a, b)
C) Math.pow(b, a)
D) Math.pow(a, b)
Answer: D
31) ______
32) Analyze the following code.
32) ______
public class Test {
public static void main(String[ ] args) {
int month = 09;
System.out.println("month is " + month);
}
}
A) The program displays month is 9
B) The program displays month is 09
C) The program displays month is 9.0
D) The program has a syntax error, because 09 is an incorrect literal value.
Answer: D
33) 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.
33) ______
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.
Answer: B
34) What is i printed?
34) ______
public class Test {
public static void main(String[ ] args) {
int j = 0;
int i = ++j + j * 5;
System.out.println("What is i? " + i);
}
}
A) 5
Answer: B
B) 6
C) 1
D) 0
35) What is i printed in the following code?
35) ______
public class Test {
public static void main(String[ ] args) {
int j = 0;
int i = j++ + j * 5;
System.out.println("What is i? " + i);
}
}
A) 5
Answer: A
B) 6
C) 0
D) 1
36) What is y displayed in the following code?
36) ______
public class Test {
public static void main(String[ ] args) {
int x = 1;
int y = x++ + x;
System.out.println("y is " + y);
}
}
A) y is 4.
Answer: B
B) y is 3.
37) What is y displayed?
public class Test {
public static void main(String[ ] args) {
int x = 1;
int y = x + x++;
System.out.println("y is " + y);
}
}
C) y is 1.
D) y is 2.
37)
______
A) y is 1.
Answer: C
B) y is 3.
C) y is 2.
D) y is 4.
38) To assign a double variable d to a float variable x, you write
A) x = (float)d;
B) x = (long)d
C) x = d;
Answer: A
38) ______
D) x = (int)d;
39) What is the printout of the following code:
39) ______
double x = 5.5;
int y = (int)x;
System.out.println("x is " + x + " and y is " + y);
A) x is 5.5 and y is 5
B) x is 5 and y is 6
C) x is 5.5 and y is 5.0
D) x is 6 and y is 6
E) x is 6.0 and y is 6.0
Answer: A
40) Which of the following assignment statements is illegal? (Choose all that apply.)
A) int t = 4.5;
B) short s = 10;
C) int t = 23;
D) int t = (int)false;
E) float f = -34;
Answer: A, D
40) ______
41) What is the value of (double)5/2?
A) 2
B) 2.5
Answer: B
C) 3.0
D) 3
E) 2.0
41) ______
42) What is the value of (double)(5/2)?
A) 3
B) 2
Answer: D
C) 3.0
D) 2.0
E) 2.5
42) ______
43) The expression (int)(76.0252175 * 100) / 100 evaluates to ________.
A) 76.02
B) 76.03
C) 76.0252175
Answer: D
43) ______
D) 76
44) If you attempt to add an int, a byte, a long, and a double, the result will be a ________ value.
A) long
B) byte
C) int
D) double
Answer: D
44) ______
45) Which of the following is the correct expression of character 4?
A) "4"
B) 4
C) '\0004'
Answer: D
45) ______
D) '4'
46) A Java character is stored in ________.
A) three bytes
B) two bytes
Answer: B
D) one byte
46) ______
C) four bytes
47) Suppose x is a char variable with a value 'b'. What is the printout of the statement
System.out.
println(+ 47)
+x)?
A) c
Answer: A
___
___
B) b
C) a
D) d
48) Which of the following statement prints smith\exam1\test.txt?
A) System.out.println("smith\"exam1\"test.txt");
B) System.out.println("smith\\exam1\\test.txt");
C) System.out.println("smith"\exam1"\test.txt");
D) System.out.println("smith\exam1\test.txt");
Answer: B
48) ______
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((int)i);
B) System.out.println(i);
C) System.out.println((char)i);
D) System.out.println(i + " ");
Answer: C
49) ______
50) The Unicode of 'a' is 97. What is the Unicode for 'c'?
A) 99
B) 97
C) 98
Answer: A
50) ______
51) Will System.out.println((char)4) display 4?
A) Yes
Answer: B
B) No
52) What is the printout of System.out.println('z' - 'a')?
A) 25
B) z
Answer: A
C) a
D) 96
51) ______
52) ______
53) An int variable can hold ________. (Choose all that apply.)
A) "120"
B) "x"
C) 120
Answer: C, D
D) 26
53) ______
D) 'x'
E) 120.0
54) Which of the following assignment statements is correct? (Choose all that apply.)
A) char c = "100";
B) char c = 'd';
C) char c = 100;
D) char c = "d";
Answer: B, C
54) ______
55) The expression "Java " + 1 + 2 + 3 evaluates to ________.
A) java 123
B) Java123
C) Java 123
D) Java6
E) Illegal expression
Answer: C
55) ______
56) Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
A) A1
B) 66
C) B
D) Illegal expression
Answer: A
56) ______
57) Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
57) ______
A) 66
C) B
Answer: A
B) A1
D) Illegal expression
58) The System.currentTimeMillis() returns ________.
A) the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time)
B) the current time in milliseconds since midnight
C) the current time in milliseconds
D) the current time in milliseconds since midnight, January 1, 1970
E) the current time
Answer: A
58) ______
59) Programming style is important, because ________. (Choose all that apply.)
A) good programming style can make a program run faster
B) good programming style makes a program more readable
C) a program may not compile if it has a bad style
D) good programming style helps reduce programming errors
Answer: B, D
59) ______
60) According to Java naming convention, which of the following names can be variables? (Choose
all that apply.)
A) findArea
B) class
C) TOTAL_LENGTH
D) totalLength
E) FindArea
Answer: A, D
60) ______
61) If a program compiles fine, but it produces incorrect result, then the program suffers ________.
A) a runtime error
B) a compilation error
C) a logic error
Answer: C
61) ______
62) The ________ method displays an input dialog for reading a string. (Choose all that apply.)
A) String string = JOptionPane.showMessageDialog(null, "Enter a string", "Input Demo",
JOptionPane.QUESTION_MESSAGE);
B) String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo",
JOptionPane.QUESTION_MESSAGE);
C) String string = JOptionPane.showInputDialog("Enter a string");
D) String string = JOptionPane.showInputDialog(null, "Enter a string");
E) String string = JOptionPane.showInputDialog("Enter a string", "Input Demo",
JOptionPane.QUESTION_MESSAGE);
Answer: B, C, D
62) ______
63) The ________ method parses a string s to an int value.
A) Integer.parseInt(s);
B) integer.parseInteger(s);
C) Integer.parseInteger(s);
D) integer.parseInt(s);
Answer: A
63) ______
64) The ________ method parses a string s to a double value.
A) Double.parseDouble(s);
B) Double.parsedouble(s);
C) double.parseDouble(s);
D) double.parse(s);
Answer: A
64) ______
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,
"Enter an integer", "Input",
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 cannot compile because i does not have an initial value when it is used in i = i
+ 4;
C) The program cannot compile because j is not initialized.
D) The program compiles and runs fine.
Answer: B
65) ______
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)
23)
24)
25)
26)
27)
28)
29)
30)
31)
32)
33)
34)
35)
36)
37)
38)
39)
40)
41)
42)
43)
44)
45)
46)
47)
48)
49)
50)
51)
B
A, C, D
A
A
D, E
B, E
B, C
B
A
C, D
A
B, C
B
C
C
D
A, B, C
C
E
E
D
D
D
B, C, E
A, D
D
A
B
A
B
D
D
B
B
A
B
C
A
A
A, D
B
D
D
D
D
B
A
B
C
A
B
52)
53)
54)
55)
56)
57)
58)
59)
60)
61)
62)
63)
64)
65)
A
C, D
B, C
C
A
A
A
B, D
A, D
C
B, C, D
A
A
B