Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
B. 14
C. 16
D. 28
E. None of these
16
B. 7
" + y + "
C. 6
21
10
" + z);
D. 6
24
E. None of these
C. 4
B. 3.5
D. compile error
E. None of these
D. compile error
E. None of these
D. compile error
E. None of these
B. 0.75
C. 1
B. 3.75
C. 3
Topic Test
Topic 1 - Java Review
1
Topic Test
Topic 1 - Java Review
6. What is the output of the following code segment?
int x = 12;
int y = x;
x++;
x += 5;
System.out.println(x + " " + y);
A. 18
12
B. 18
18
C. 12
18
D. 18
13
E. None of these
B. 2
-1
C. 1
D. 1
-2
E. None of these
D. 4
2
1
3
E. None of these
0
0
B. 3
3
0
2
C. 4
4
1
1
B. II only
C. III only
E. None of these
Topic Test
Topic 1 - Java Review
2
Topic Test
Topic 1 - Java Review
For questions 10 13 consider the following methods:
public int tempest(int x, int y)
{
return (x + y) / 2;
}
public int tempest(int x)
{
return x * x + 1;
}
public int shrew( int x )
{
int y = tempest(x - 1, x * 3);
int result = 0;
if( (y % 2) == 0 )
result = y / 2;
else
result = y;
return result;
}
B. x
C. 10
D. 4
E. None of these
D. compile error
E. None of these
B. 3
C. 3.0
B. 10
C. 4
D. compile error
E. None of these
D. 10
E. None of these
B. 5
C. 9
Topic Test
Topic 1 - Java Review
3
Topic Test
Topic 1 - Java Review
14. Consider the following code segment:
int x;
int y;
// code to initialize x and y
if( x > y )
System.out.println("x");
else if( y > x )
System.out.println("y");
else
System.out.println("x and y");
If the above segment is executed one time how many times could the println method be called?
A. 0
B. 1
C. 2
D. unknown
E. None of these
D. abcd
E. None of these
B. a
C. ab
Which of the following values of x will cause the message "out of range" to be printed?
I.
0
II. 100
III. 150
A. I only
B. II only
C. III only
D. I and II only
E. None of these
Topic Test
Topic 1 - Java Review
4
Topic Test
Topic 1 - Java Review
17. What is the output of the following code segment?
int total = 0;
final int LIMIT = 10;
for(int i = 0; i < LIMIT; i++)
{
total++;
}
System.out.println( total );
A. unknown
B. 0
C. 9
D. 10
E. None of these
D. 99
E. None of these
D. 50
E. None of these
E. None of these
B. 100
C. 90
B. 15
C. 120
B. 3
C. 4
Topic Test
Topic 1 - Java Review
5
Topic Test
Topic 1 - Java Review
21. In the code segment from question 20 assume the first line is changed to:
int x = 3;
What is the output of the following code segment assuming nothing else is changed?
A. . the loop never terminates
B. 8
C. 6
D. 7
E. None of these
22. In the code segment from question 20 which of the following are logically equivalent to the
while loop's boolean expression, x != 1 ?
I. x < 1 && x > 1
II. x < 1 || x > 1
III. !( x == 1 )
A. I only
B. II only
C. III only
E. None of these
D. 0
E. None of these
B. 9
C. 12
B. 2
C. 0
D. An ArrayIndexOutOfBoundsException occurs
E. None of these
B. never
E. None of these
Topic Test
Topic 1 - Java Review
6
Topic Test
Topic 1 - Java Review
26. Consider the following code segment:
int[] list;
int temp;
// code to initialize list
for(int i = 0; i < list.length; i++)
{
temp = list[i];
list[i] = list[ list.length i];
list[ list.length i ] = temp;
}
1
3
2
7
3
5
4
7
What are the contents of list after the code segment executes?
A.
11
11
11
B.
C.
D.
E. None of these
B. Horns
C. Long+Horns
D. s3
E. None of these
D. compile error
E. None of these
B. A&M6
C. A&Mxy
Topic Test
Topic 1 - Java Review
7
Topic Test
Topic 1 - Java Review
29. What is the output of the following code segment?
String s1 = "A&M";
int x = 5;
int y = 1;
String s3 = x + y + s1;
System.out.println( s3 );
A. 51A&M
B. 6A&M
C. xyA&M
D. compile error
E. None of these
D. runtime error
E. None of these
B. Fant
C. Fan
Topic Test
Topic 1 - Java Review
8
Topic Test
Topic 1 - Java Review
Topic Test
Topic 1 - Java Review
9
Topic Test
Topic 1 - Java Review
Topic Test
Topic 1 - Java Review
10
Topic Test
Topic 1 - Java Review
2. Consider an array of ints that represents measurements of a person's blood sugar level over a
period of time. Assume readings are taken every 15 minutes. The readings are stored in the array
in the order they are taken. Write a method that returns the length of the longest consecutive
series of readings that are greater than or equal to some threshold value.
0
1
2
3 4
5
6
7
8
9 10
11
150 125 150 155 145 140 135 125 160 165 155 125
For example, assume the threshold is sent an argument equal to 150. Any readings greater than or
equal to the value of the threshold are considered high. Your method must find the length of the
longest consecutive series of readings that are greater than or equal to the threshold. In the above
example there are three series of readings greater than or equal to the threshold, [0], [2-3], [8-10].
The longest of these is three readings so the method must return 3. If no readings are greater than
or equal to the threshold then the method must return 0.
Complete the following method:
public int longestStreak(int[] readings, int threshold)
// pre: readings != null
// post: as described in question
Topic Test
Topic 1 - Java Review
11
Topic Test
Topic 1 - Java Review
Topic Test
Topic 1 - Java Review
12