Click the Start button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the Next button to go to the next question. Click on the Previous button to go to the previous question. Use the number buttons to jump to a particular question. Click the Pause button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the Finish button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.
Integer int1 = new Integer(3);
Integer int2 = new Integer(3);
Integer int3 = int2;
I. (int3.equals(int2))
II. (int1.equals(int2))
III. (int3 == int2)
IV. (int1 == int2)
V. (int2 == int3)
I and II only
How about III and V? Since int3 was set to int2 they do refer to the same object.
I, II, III, and V
The variables int1 and int2 refer to two different objects (even though they have the same value) so IV will be false.
All will return true
Look at IV. Are int1 and int2 referring to the same object?
III, IV, and V only
I and II are also true since they have the same value. IV is not since they donβt refere to the same object.
If the search value is not in the array, a sequential search will have to check every item in the array before failing, a binary search will be faster.
The search value is the last element in the array
In this case a sequential search will have to check every element before finding the correct one, whereas a binary search will not.
The value is in the middle of the array.
This would be true for a binary search, not for a sequential search.
The search value is the first element in the array.
Only when the search value is the first item in the array, and thus the first value encountered in sequential search, will sequential be faster than binary.
There is a method called checkString that determines whether a string is the same forwards and backwards. The following data sets can be used for testing the method. Which is a best set of test cases?
Data set 1: "aba", "abba", "aBa", "z"
Data set 2: "bcb", "bcd", "c"
Data set 1 since it has more test cases.
More test cases isnβt necessarily better. The tests should test the range of possible outcomes.
Data set 2 since it only contains strings with lower case characters.
It is actually better to test with strings that contain both upper and lower case characters.
Data set 1 since it has test cases with both upper and lower case characters.
There is a better answer. While it is good to test with strings that contain both upper and lower case characters there is another reason why data set 2 is better.
Data set 2 since it contains strings which should return true and should return false.
You want to test all possible results and data set 1 only contains strings that should return true.
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(5));
list1.add(new Integer(4));
list1.add(new Integer(3));
list1.set(2, new Integer(2));
list1.add(2, new Integer(1));
list1.add(new Integer(0));
System.out.println(list1);
[0, 1, 2, 4, 5]
This code does not sort the items in the list.
[5, 4, 1, 0]
There is only one set. The add moves all current values at the index and above to the right one before adding at that index.
[5, 4, 2, 1, 0]
The add moves over all items at that index before adding the value at the index.
[5, 4, 1, 2, 0]
The add method with one parameters will add that value to end of the list. The method set changes the value at that index in the list. The method add with an index will move anything at the index or above one to the right and then set the value of that index.
public class TimeRecord
{
private int hours;
private int minutes; // 0<=minutes<60
public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}
// postcondition: returns the number of hours
public int getHours()
{ /* implementation not shown */ }
// postcondition: returns the number
// of minutes; 0 <= minutes < 60
public int getMinutes()
{ /* implementation not shown */ }
// precondition: h >= 0; m >= 0
// postcondition: adds h hours and
// m minutes to this TimeRecord
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/* missing code */
}
// ... other methods not shown
}
// Consider the following declaration that appears in a client program:
TimeRecord[] timeCards = new TimeRecord[100];
// Assume that timeCards has been initialized with TimeRecord
// objects. Consider the following code segment that is intended to compute
// the total of all the times stored in timeCards.
TimeRecord total = new TimeRecord(0,0);
for (int k = 0; k < timeCards.length; k++)
{
/* missing expression */
}
Which of the following can be used to replace /* missing expression */ so that the code segment will work as intended?
I.
total.advance(timeCards[k].getHours(), timeCards[k].getMinutes());
II.
timeCards[k].advance();
III.
total += timeCards[k].advance();
IV.
total.advance(timeCards[k].hours, timeCards[k].minutes);
V.
timeCards[k].advance(timeCards[k].getHours(), timeCards[k].getMinutes());
I
This will add each current time card hours and minutes to the total.
II
This wonβt total the hours and minutes and there is no advance method that takes no arguments.
IV
The fields hours and minutes are private and canβt be accessed directly in another class.
V
This will add the hour and minutes from the current time record to itself.
A sequential search loops through the elements of an array starting with the first and ending with the last and returns from the loop as soon as it finds the passed value. It has to check every value in the array when the value it is looking for is not in the array. This would take 10 executions of the loop.
public int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
4
This would be true if it was return(a[1]*= 2);.
7
This would be true if it was a[0]--; Or it would be true if array indices started at 1, but they start with 0.
2
The statement a[1]--; is the same as a[1] = a[1] - 1; so this will change the 3 to a 2. The return (a[1] * 2) does not change the value at a[1].
3
This canβt be true because a[1]--; means the same as a[1] = a[1] - 1; So the 3 will become a 2. Parameters are all pass by value in Java which means that a copy of the value is passed to a method. But, since an array is an object a copy of the value is a copy of the reference to the object. So changes to objects in methods are permanent.
int a = 10, b = 3, t;
for (int i=1; i<=6; i++)
{
t = a;
a = i + b;
b = t - i;
}
a = 6 and b = 7
This would be true if the loop stopped when i was equal to 6.
a = 6 and b = 13
Actually i = 6 and t = 6 and a = 13 after the loop finishes.
a = 13 and b = 0
The variable i loops from 1 to 6 i = 1, t = 10, a = 4, b = 9 i = 2, t = 4, a = 11, b =2 i = 3, t = 11, a = 5, b = 8 i = 4, t = 5, a = 12, b = 1 i = 5, t = 12, a = 6, b = 7 i = 6, t = 6, a = 13, b = 0
a = 6 and b = 0
Actually i = 6 and t = 6 and b = 0 after the loop finishes.
This would only be correct if we had s1 = s2; after s2.toLowerCase(); was executed. Strings are immutable and so any change to a string returns a new string.
HI THERE
This would be correct if we had s1 = s3; after s3.toUpperCase(); was executed. Strings are immutable and so any change to a string returns a new string.
Hi There
Strings are immutable meaning that any changes to a string creates and returns a new string, so the string referred to by s1 does not change
null
This would be true if we had s1 = s4; after s4 = null; was executed. Strings are immutable and so any changes to a string returns a new string.
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
8
The variable col is 2, so it adds 2 + 2 + 3 + 1 which is 8.
9
This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
12
This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4 + 2 which is 12.
10
This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.
int [][] mat = new int [3][4];
for (int row = 0; row < mat.length; row++)
{
for (int col = 0; col < mat[0].length; col++)
{
if (row < col)
mat[row][col] = 3;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 1;
}
}
This will fill mat with 3 if the row index is less than the column index, 2 if the row index is equal to the column index, and a 1 if the row index is greater than the column index.
Assume that temp is an int variable initialized to be greater than zero and that a is an array of type int. Also, consider the following code segment. What of the following will cause an infinite loop?
for ( int k = 0; k < a.length; k++ )
{
while ( a[ k ] < temp )
{
a[ k ] *= 2;
}
}
Whenever a has a value larger then temp.
Values larger then temp will not cause an infinite loop.
When all values in a are larger than temp.
Values larger then temp will not cause an infinite loop.
Whenever a includes a value equal to temp.
Values equal to temp will not cause an infinite loop.
Whenever a includes a value that is less than or equal to zero.
When a contains a value that is less than or equal to zero, then multiplying that value by 2 will never make the result larger than the temp value (which was set to some value > 0), so an infinite loop will occur.
public static void conditionTest(int num1, int num2)
{
if ((num1 > 0) && (num2 > 0))
{
if (num1 > num2)
System.out.println("A");
else
System.out.println("B");
}
else if ((num2 < 0) || (num1 < 0))
{
System.out.println("C");
}
else if (num2 < 0)
{
System.out.println("D");
}
else
{
System.out.println("E");
}
}
B
This would be true if num1 and num2 were both greater than 0 and num1 was less than or equal to num2. However, num2 is less than 0.
C
The first test is false since num2 is less than 0 and for a complex conditional joined with And (&&) to be true both expressions must be true. Next, else if ((num2<0) || (num1<0)) is executed and this will be true since num2 is less than 0 and for a complex conditional joined with Or (||) only one of the expressions must be true for it to execute.
D
This will not happen since if num2 is less than 0 the previous conditional would be true ((num2<0) || (num1<0))).
E
This will not happen since if num2 is less than 0 the previous conditional would be true ((num2<0) || (num1<0))).