1 points - davidspellman

advertisement
Question 1
1.
Will System.out.println((char)4) display 4?
Answer
Yes
No
1 points
Question 2
1.
The value of a variable can be changed.
Answer
true
false
1 points
Question 3
1.
Which of the following expression results in a value 1?
Answer
25 % 5
2%1
15 % 4
37 % 6
1 points
Question 4
1.
Which of the following is a valid identifier? (choose all that
apply)
Answer
$343
class
radius
9X
8+9
1 points
Question 5
1.
Which of the following is a constant, according to Java
naming conventions?
Answer
read
ReadInt
Test
MAX_VALUE
1 points
Question 6
1.
Suppose i is an int type variable. Which of the following
statements display the character whose Unicode is stored in
variable i?
Answer
System.out.println(i);
System.out.println((int)i);
System.out.println(i + " ");
System.out.println((char)i);
1 points
Question 7
1.
You can define a constant twice in a block.
Answer
true
false
1 points
Question 8
1.
Which of the following is the correct expression of character
a?
Answer
"a"
'\a'
'a'
'\000a'
1 points
Question 9
1.
To add a value 1 to variable x, you write (choose all that
apply)
Answer
x := 1;
x += 1;
x = x + 1;
x = 1 + x;
1 + x = x;
1 points
Question 10
1.
You can change the value of a constant.
Answer
true
false
1 points
Question 11
1.
Which of the following assignment statements is illegal?
(choose all that apply)
Answer
int t = 23;
int t = 4.5;
float f = -34;
short s = 10;
int t = (int)false;
1 points
Question 12
1.
pow is a method in the ________ class.
Answer
Double
System
Math
Integer
1 points
Question 13
1.
What is the result of 45 / 4?
Answer
10
11.25
12
11
1 points
Question 14
1.
-24 % -5 is ________.
Answer
3
-3
4
-4
0
1 points
Question 15
1.
What is the printout of System.out.println('z' - 'a')?
Answer
a
26
25
z
Question 1
1.
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);
}
Answer
a
aa
ab
abcd
abc
1 points
Question 2
1.
The exclusive or (^) of true and true is true.
Answer
true
false
1 points
Question 3
1.
The conditional operator ? : is a ________.
Answer
ternary operator
unary operator
binary operator
1 points
Question 4
1.
________ are the boolean operators. (choose all that apply)
Answer
!
&&
^
>
1 points
Question 5
1.
Suppose x=10 and y=10 what is x after evaluating the
expression (y > 10) && (x++ > 10).
Answer
9
10
11
1 points
Question 6
1.
A break statement is required for a switch-case statement in
Java.
Answer
true
false
1 points
Question 7
1.
In a switch statement, the default case must appear last
among all cases. Otherwise, it would result in a compilation
error.
Answer
true
false
1 points
Question 8
1.
The not equal comparison operator in Java is ________.
Answer
!= =
<>
!=
^=
1 points
Question 9
1.
The ________ operator can be used to compare two values.
Answer
numerical
relational
boolean
casting
1 points
Question 10
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");
Answer
The second fragment has a syntax error.
Both fragments produce the same output.
Both fragments compile, but produce different results.
The first fragment has a syntax error.
1 points
Question 11
1.
________ are short-circuit operators. (choose all that apply)
Answer
^
!
&&
||
1 points
Question 12
1.
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);
(choose all that apply)
Answer
number is printed out once if number is positive.
number is printed out twice if number is negative;
number is printed out twice if number is zero;
number is always printed out at least once;
1 points
Question 13
1.
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;
Answer
All three are correct, but Code 1 is preferred.
Code 3 has a syntax error, because you attempt to assign number to even.
Code 2 has a syntax error, because you cannot have true and false literals in the
conditional expression.
All three are correct, but Code 2 is preferred.
All three are correct, but Code 3 is preferred.
1 points
Question 14
1.
The assignment operator = is left-associative.
Answer
true
false
1 points
Question 15
1.
You can always convert a switch statement to an equivalent if
statement.
Answer
true
false
Question 1
1.
A variable declared in the for loop control can be used after
the loop exits.
Answer
true
false
1 points
Question 2
1.
Analyze the following fragment:
double sum = 0;
double d = 0;
while (d != 10.0) {
d += 0.1;
sum += sum + d;
}
Answer
The program may not stop because of the phenomenon referred to as numerical
inaccuracy for operating with floating-point numbers.
After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
The program does not compile because sum and d are declared double, but assigned
with integer value 0.
The program never stops because d is always 0.1 inside the loop.
1 points
Question 3
1.
Which of the following expression yields an integer between 0
and 100, inclusive?
Answer
(int)(Math.random() * 100 + 1)
(int)(Math.random() * 101)
(int)(Math.random() * 100)
(int)(Math.random() * 100) + 1
1 points
Question 4
1.
How many times will the following code print "Welcome to
Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
Answer
9
8
10
11
0
1 points
Question 5
1.
Suppose cond1 is a Boolean expression. When will this while
condition be true?
while ( !cond1) ...
Answer
in case cond1 is false
in case cond1 is true
always false
always true
1 points
Question 6
1.
What the output of the following code:
for ( ; ; )
System.out.println("Welcome to Java");
Answer
prints out Welcome to Java one time.
prints out Welcome to Java forever.
prints out Welcome to Java two times.
does not print anything.
1 points
Question 7
1.
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
Answer
12
10
9
11
1 points
Question 8
1.
Analyze the following code:
public class Test {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}
(choose all that apply)
Answer
The program has a syntax error because of the semicolon (;) on the for loop line.
The program compiles despite the semicolon (;) on the for loop line, and displays 4.
The for loop in this program is same as for (i = 0; i < 10; i++) { };
System.out.println(i + 4);
The program compiles despite the semicolon (;) on the for loop line, and displays
14.
1 points
Question 9
1.
How many times will the following code print "Welcome to
Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
} while (++count < 10);
Answer
0
10
9
11
8
1 points
Question 10
1.
What is 1.0 + 1.0 + 1.0 == 3.0?
Answer
true
false
There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.
1 points
Question 11
1.
What is the output of the following fragment?
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
System.out.println(j);
Answer
64
16
8
32
4
1 points
Question 12
1.
Is the following loop correct?
for (; ; );
Answer
Yes
No
1 points
Question 13
1.
A break statement can be used only in a loop.
Answer
true
false
1 points
Question 14
1.
A continue statement can be used only in a loop.
Answer
true
false
1 points
Question 15
1.
What the output of the following code:
for ( ; false ; )
System.out.println("Welcome to Java");
Answer
prints out Welcome to Java one time.
prints out Welcome to Java forever.
does not print anything.
prints out Welcome to Java two times.
Question 1
1.
The selectionSort method is defined in this section. What is
list1 after executing the following statements?
double[] list1 = {3.1, 3.1, 2.5, 6.4};
selectionSort(list1);
Answer
list1 is 2.5 3.1, 3.1, 6.4
list1 is 3.1, 3.1, 2.5, 6.4
list1 is 6.4, 3.1, 3.1, 2.5
list1 is 3.1, 2.5, 3.1, 6.4
1 points
Question 2
1.
If a key is not in the list, the binarySearch method returns
________.
Answer
-insertion point
insertion point
-insertion point - 1
insertion point - 1
1 points
Question 3
1.
Which of the following statements are correct to invoke the
printMax method in Listing 6.5 in the book? (choose all that
apply)
Answer
printMax(new double[]{1, 2, 3});
printMax(new int[]{1, 2, 3});
printMax(1.0, 2.0, 2.0, 1.0, 4.0);
printMax(1, 2, 2, 1, 4);
1 points
Question 4
1.
Given the following statement
int[ ] list = new int[10];
Answer
The array variable list contains ten values of type int.
The array variable list contains nine values of type int.
The array variable list contains a memory address that refers to an array of 10 int
values.
The array variable list contains a memory address that refers to an array of 9 int
values.
1 points
Question 5
1.
Which of the following declarations are correct? (choose all
that apply)
Answer
int[4] list = new int{1, 2, 3, 4};
int[] list = new int{1, 2, 3, 4};
int[] list = new int[]{1, 2, 3, 4};
int[] list = {1, 2, 3, 4};
1 points
Question 6
1.
When you return an array from a method, the method returns
________.
Answer
a copy of the array
the reference of the array
a copy of the first element
the length of the array
1 points
Question 7
1.
Which of the following statements is valid? (choose all that
apply)
Answer
char[] c = new char[4]{'a', 'b', 'c', 'd'};
char[] c = new char();
double d[] = new double[30];
int[] i = {3, 4, 3, 2};
int i = new int(30);
1 points
Question 8
1.
The array size is specified when the array is declared.
Answer
true
false
1 points
Question 9
1.
You can use the operator == to check whether two variables
refer to the same array.
Answer
true
false
1 points
Question 10
1.
Which of the following statements are correct?
Answer
char[2][] charArray = {{'a', 'b'}, {'c', 'd'}};
char[][] charArray = {{'a', 'b'}, {'c', 'd'}};
char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
char[][] charArray = {'a','b'};
1 points
Question 11
1.
Consider the following statements:
int[][] numbers = {{1}, {1, 2}, {1, 2, 3}};
What is numbers.length and what is numbers[1].length?
Answer
3, 2
3, 3
1, 3
3, 1
1 points
Question 12
1.
Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?
Answer
return new int[]{1, 2, 3};
return new int[][]{{1, 2, 3}, {2, 4, 5}};
return {1, 2, 3};
return int[]{1, 2, 3};
return 1;
1 points
Question 13
1.
Analyze the following code:
public class Test {
public static void main(String[] args) {
final int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for (int i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
}
}
Answer
y is a final array.
The program has a syntax error on the statement x = new int[2], because x is final
and cannot be changed.
The program displays 0 0
The program displays 1 2 3 4
1 points
Question 14
1.
In the following code, what is the printout for list1?
class Test {
public static void main(String[] args) {
int[] list1 = {1, 2, 3};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
for (int i = 0; i < list1.length; i++)
System.out.print(list1[i] + " ");
}
}
Answer
123
012
111
013
1 points
Question 15
1.
Assume double[] scores = {1, 2, 3, 4, 5}, what value does
java.util.Arrays.binarySearch(scores, 3.5) return?
Answer
0
3
-4
-3
4
1 points
Question 16
1.
For the binarySearch method in Section 6.7.2, what is low
and high after the first iteration of the while loop when
invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?
Answer
low is 3 and high is 6
low is 0 and high is 5
low is 4 and high is 6
low is 0 and high is 6
low is 0 and high is 3
1 points
Question 17
1.
Do the following two programs produce the same result?
Program I:
public class Test {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5};
reverse(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
public static void reverse(int[] list) {
int[] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}
}
Program II:
public class Test {
public static void main(String[] args) {
int[] oldList = {1, 2, 3, 4, 5};
reverse(oldList);
for (int i = 0; i < oldList.length; i++)
System.out.print(oldList[i] + " ");
}
public static void reverse(int[] list) {
int[] newList = new int[list.length];
for (int i = 0; i < list.length; i++)
newList[i] = list[list.length - 1 - i];
list = newList;
}
}
Answer
Yes
No
1 points
Question 18
1.
Analyze the following code.
int[] list = new int[5];
list = new int[6];
Answer
The code has syntax errors because you cannot assign a different size array to list.
The code has runtime errors because the variable list cannot be changed once it is
assigned.
The code can compile and run fine. The second line assigns a new array to list.
The code has syntax errors because the variable list cannot be changed once it is
assigned.
1 points
Question 19
1.
What is the representation of the third element in an array
called a?
Answer
a(2)
a(3)
a[3]
a[2]
1 points
Question 20
1.
An array can be used in which of the following ways?
Answer
As a local variable
As a return value of a method
As a parameter of a method
All of the above
Download