Topic Test

Topic 1 - Java Review

Multiple Choice. Pick the best response to each question.

1. What is the output of the following code segment?
int x;
x = 1 + 3 * 2;
x = x * 2;
System.out.println(x);
A. 7 / B. 14 / C. 16 / D. 28 / E. None of these
2. What is the output of the following code segment?
int x = 7;
int y = 3;
y++;
x--;
int z = x * y;
System.out.println(x + " " + y + " " + z);
A. 8 2 16 / B. 7 3 21 / C. 6 4 10 / D. 6 4 24 / E. None of these
3. What is the output of the following code segment?
int x = 14;
int y = 4;
int z = x / y;
System.out.println(z);
A. 3 / B. 3.5 / C. 4 / D. compile error / E. None of these
4. What is the output of the following code segment?
int x = 3;
double a = 4.0;
double b = x / a;
System.out.println(b);
A. 0 / B. 0.75 / C. 1 / D. compile error / E. None of these
5. What is the output of the following code segment?
double a = 30.0;
double b = 8.0;
int x = (int)( a / b );
System.out.println(x);
A. 4 / B. 3.75 / C. 3 / D. compile error / E. None of these
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
For questions 7 and 8 consider method ado.
public void ado(int x, int y)
{x = y * 2;
y = x – 3;
System.out.println( x + " " + y );
}
7. What is the output of the following code segment?
ado(2, 1);
A. 2 1 / B. 2 -1 / C. 1 2 / D. 1 -2 / E. None of these
8. What is the output of the following code segment?
int a = 3;
int b = 2;
ado(a, b);
System.out.println( a + " " + b );
A. 3 0
3 0 / B. 3 0
3 2 / C. 4 1
4 1 / D. 4 1
2 3 / E. None of these
9. Consider the following code segment:
int x;
int y;
//code to initialize x and y
if( x + y > 0 )
//body of if statement
Which of the following are equivalent to the boolean expression x + y > 0 ?
I. x > 0 & y > 0
II. x > 0 || y > 0
III. x > y
A. I only / B. II only / C. III only / D. I and III only / E. None of these
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;
}
10. What is the output of the following code segment?
System.out.println( tempest(5, 3) );
A. 5 3 / B. x y / C. 10 / D. 4 / E. None of these
11. What is the output of the following code segment?
System.out.println( tempest(3) );
A. Nothing / B. 3 / C. 3.0 / D. compile error / E. None of these
12. What is the output of the following code segment?
System.out.println( tempest( tempest(2), tempest(2,4) );
A. Nothing / B. 10 / C. 4 / D. compile error / E. None of these
13. What is the output of the following code segment?
System.out.println( shrew(5) );
A. 4 / B. 5 / C. 9 / D. 10 / E. None of these
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
15. What is the output of the following code segment?
int x = 70;
if( x > 0 )
System.out.print("a");
if( x > 10 )
System.out.print("b");
if( x > 100 )
System.out.print("c");
if( x > 0 & x < 100 )
System.out.print("d");
A. abd / B. a / C. ab / D. abcd / E. None of these
16. Consider the following code segment:
int x;
// code to initialize x
if( x < 0 || x > 100)
System.out.print("out of range");
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
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
18. What is the output of the following code segment?
int total = 0;
final int LIMIT = 10;
for(int i = 0; i < LIMIT; i++)
{for(int j = 0; j < LIMIT; j++)
{total++;
}
}
System.out.println( total );
A. 10 / B. 100 / C. 90 / D. 99 / E. None of these
19. What is the output of the following code segment?
int total = 0;
final int LIMIT = 5;
for(int i = 0; i < LIMIT; i++)
{for(int j = LIMIT; j > i; j--)
{total++;
}
}
System.out.println( total );
A. 55 / B. 15 / C. 120 / D. 50 / E. None of these
20. What is the output of the following code segment?
int x = 8;
int count = 0;
while( x != 1 )
{if( (x % 2) == 0)
x = x / 2;
else
x = x * 3 + 1;
count++;
}
System.out.println( count );
A. 0 / B. 3 / C. 4 / D. the loop never terminates / E. None of these
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 / D. II and III only / E. None of these
23. What is the output of the following code segment?
int[] list = new int[5];
for(int i = 0; i < list.length; i++)
{list[i] = i * 3;
}
System.out.println( list[3] );
A. 3 / B. 9 / C. 12 / D. 0 / E. None of these
24. What is the output of the following code segment?
int[] list = {5, 2, 12, 0, 11, 2};
System.out.println( list[ list[1] + list[3] ] );
A. 12 / B. 2 / C. 0 / D. An ArrayIndexOutOfBoundsException occurs / E. None of these
25. Consider the following code segment:
int[] list;
// code to initialize list
for(int i = 0; i < 20; i++)
{list[i] = 5;
}
When will the above code result in an ArrayIndexOutOfBoundsException?
A. always / B. never / C. when list has 20 or more elements / D. when list has fewer than 20 elements / E. None of these
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;
}
Assume list is initialed to hold the following values:
0 1 2 3 4
11 / 3 / 7 / 5 / 7
What are the contents of list after the code segment executes?
A.
7 / 5 / 7 / 3 / 11
B.
7 / 11 / 3 / 7 / 5
C.
11 / 3 / 7 / 5 / 7
D.
5 / 5 / 5 / 5 / 5
E. None of these
27. What is the output of the following code segment?
String s1 = "Long";
String s2 = "Horns";
String s3 = s1 + s2;
System.out.println( s3 );
A. Long / B. Horns / C. Long+Horns / D. s3 / E. None of these
28. What is the output of the following code segment?
String s1 = "A&M";
int x = 5;
int y = 1;
String s3 = s1 + x + y;
System.out.println( s3 );
A. A&M51 / B. A&M6 / C. A&Mxy / D. compile error / E. None of these
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
30. What is the output of the following code segment?
String s1 = "Fantastic";
String s2 = s1.subString(0,3);
System.out.println( s2 );
A. Fantastic / B. Fant / C. Fan / D. runtime error / E. None of these

Free Response Questions:

1. Write a method that has a single integer parameter greater than 0. The method returns -1 if the parameter is "deficient", 0 if it is "perfect", and 1 if it is "excessive."

A perfect number is one that is equal to the sum of its factors excluding itself.

The factors of 6 are 1,2,3,6. Excluding itself the sum of 6's factors are 1 + 2 = 3 = 6.

6 is a perfect number.

A number is deficient if the sum of its factors excluding itself are less than the number. 8 is deficient because its factors are 1,2,4,8. Excluding itself the sum of 8's factors are 1 + 2 + 4 = 7.

8 is deficient.

A number is excessive if the sum of its factors excluding itself are greater than the number. 12 is excessive because its factors are 1, 2, 3, 4, 6, 12.

Excluding itself the sum of 12's factors are 1 + 2 + 3 + 4 + 6 = 16.

12 is excessive.

public int numberType(int num)

// pre: num > 0

// post: return -1 if nums is deficient, 0 if it is perfect, or 1 // if it excessive

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

1