No student devices needed. Know more
25 questions
Consider the following code segment.
System.out.println("W");
System.out.println("X");
System.out.print("Y");
System.out.print("Z");
What is printed as a result of executing the code segment?
WXYZ
W
XYZ
WX
YZ
W
X
YZ
W
X
Y
Z
Consider the following code segment.
int x = 10;
int y = 20;
/* missing code */
System.out.print(top / bottom);
Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur?
I. int top = x - y;int bottom = y - x;
II.int top = 2 * x;int bottom = y - top;
III. int top = x + y;int bottom = 2 * top;
I. only
II. only
III. only
I. and II.
II. and III.
Consider the following code segment.
String str = "AP";
str += "CS " + 1 + 2;
System.out.println(str);
What is printed as a result of executing the code segment?
CS AP12
AP CS3
CSAP 12
APCS 12
APCS 3
Consider the following code segment.
String one = "ABC123";
String two = "C";
String three = "3";
System.out.println(one.indexOf(two));
System.out.println(one.indexOf(three));
System.out.println(two.indexOf(one));
What is printed when the code segment is executed?
2
5
-1
2
5
2
2
6
-1
3
6
-1
-1
-1
2
Consider the following method.
public double calculate(double x)
{
return x + 1.5;
}
The following code segment calls the method calculate in the same class.
Double d1 = new Double(7.5);
System.out.println(calculate(d1));
What, if anything, is printed when the code segment is executed?
8.0
8.5
9
9.0
Nothing is printed because the code does not compile. The actual parameter d1 passed to calculate is a Double, but the formal parameter x is a double.
The code segment below is intended to randomly print one of the values 2, 4, 6, or 8 with equal probability.
int val = /* missing code */ ;
val *= 2;
System.out.print(val);
Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
Math.random() * 4 + 1
Math.random() * 8
(int) (Math.random() * 4)
(int) (Math.random() * 4 + 1)
(int) (Math.random() * 8 + 1)
A store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7.
double len1;
double len2;
double len3;
double total = len1 + len2 + len3;
int minLength = (int) (total + 0.5);
System.out.print(minLength);
Which of the following best describes the behavior of the code segment?
The code segment works as intended for all nonnegative values of len1, len2, and len3.
The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.
The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength.
The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by declaring minLength as a double and casting it to an int in the System.out.print statement.
The code segment does not work as intended for any values of len1, len2, and len3. It returns the sum of 0.5 and the three lengths for all values of len1, len2, and len3.
Consider the following code segment, which is intended to display 6.0.
double fact1 = 1 / 2;
double fact2 = 3 * 4;
double product = fact1 * fact2;
System.out.println(product);
Which of the following best describes the error, if any, in the code segment?
There are no errors and the code works as intended.
Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.
The expression fact1 * fact 2 should be cast as double.
The expressions 1 / 2 and 3 * 4 should both be cast as double.
The variables fact1 and fact2 should both be declared as int.
What is the expected output for this program?
6
12
error: incompatible types
error: identifier expected
error: missing return statement
What is the expected output?
-1
0
1
17
18
2010
2010105
2010510
20102010
20101020
The sum of all the integers greater than y but less than or equal to x
The sum of all the integers greater than or equal to y but less than or equal to x
The sum of all the integers greater than y but less than x
The sum of all the integers greater than y but less than or equal to x
The sum of all the integers less than y but greater than or equal to x
0123
00123
0010012
00100123
001001200100123
1 4 10 2 3 4 7 5
1 2 4 6 10 7 3 5
1 2 3 10 6 7 4 5
4 3 1 2 6 7 10 5
6 3 7 4 2 1 10 4
1 1 2 3 4 5 6
2 2 3 4 5 6 7
6 6 5 4 3 2 1
7 7 6 5 4 3 2
7 7 7 7 7 7 7
I only
II only
III only
I and II only
II and III only
A
D
ABD
ABCD
ABCDE
-2 -1 0
-2 -1 0 1 2
0 1 2 0 1 2 0 1 2
-2 0 2
-2 -1 0 0 1 2 2 3 4
Which of the following correctly constructs a Date object in a client class?
Date d = new (2, 13, 1947);
Date d = new Date (2, 13, 1947);
Date d;
d = new (2, 13, 1947);
Date d;
d = Date(2, 13, 1947);
Date d = Date(2, 13, 1947);
A client program creates a Date object as follows:
Date d = new Date(1, 13, 2002);
Which of the following subsequent code segments will cause an error?
String s = d. toString();
int x = d.day();
Date e = d;
Date e = new Date(1, 13, 2002);
int y = d.year;
Explore all questions with a free account