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.
Assume that list has been initialized with the following Integer objects: [9, 3, 17, 2, 16, 4, 1]. Which of the following shows the values in list after a call of mystery(4)?
private List list<Integer>;
public void mystery(int n)
{
for (int i= 0; i < n; i++)
{
Object obj = list.remove(0);
list.add((Integer)obj);
}
}
[9, 3, 17, 2, 16, 4, 1]
This would be true if it moved all of the values from the front to the back. But does it?
[1, 4, 16, 2, 17, 3, 9]
This would be true if the code reversed the list. But does it? Remember that remove(0) removes the first item in the list and returns it. The add method adds the item to the end of the list.
[9, 3, 17, 16, 4, 1, 2]
This would be true if only the value 2 was moved to the end of the list. Is that what this code does?
[16, 4, 1, 9, 3, 17, 2]
This code moves the first item to the end of the list 4 times. So it moves the 9, 3, 17, and 2.
[2, 16, 4, 1, 9, 3, 17]
This would be true if the call was mystery(3) instead of mystery(4). Then it would move the first 3 values in the list to the end of the list.
public static int recur(int n)
{
if (n <= 1)
return 1;
else
return (recur(n-1) + recur(n-2));
}
8
The call recur(5) will return recur(4) + recur(3). The call recur(4) returns 5. The call recur(3) returns 3. So recur(5) returns 5 + 3 = 8.
1
This method will only return 1 when n is less than or equal to 1. In this case n is 5.
2
This would be true if the call was recur(2). This would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2.
5
This would be true if the call was recur(4). This would return recur(3) + recur(2). The call recur(3) returns 3. The call recur(2) returns 2. So recur(4) returns 3 + 2 = 5.
3
This would be true if the call was recur(3). This would return recur(2) + recur(1). The call to recur(1) would return 1. The call to recur(2) would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2. Thus recur(3) would return 2 + 1 = 3.
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.set(1,"c");
list.add(2, "d");
list.set(2, "e");
list.add("g");
System.out.println(list);
[a, c, e, d, g]
This would be true if it was list.add(2, "e") instead of list.set(2, "e").
[c, e, d, b, g]
This would be true if the first item in a list was at index 1, but it is at index 0.
[a, c, e, g]
This code adds "a" to the end of the list: ["a"] and then "b" to the end of the list: ["a", "b"]. Then it changes the value at index 1 to "c": ["a", "c"]. Then it adds "d" at position 2 which first moves to the right any existing values ["a", "c", "d"]. Then it sets the value at index 2 to "e": ["a", "c", "e"]. Then it adds "g" to the end: ["a", "c", "e", "g"].
[a, b, e, d, g]
For this to be true the 3rd line would have to be list.add("c"). Is it?
public void m(int[][]p)
{
int height = p.length;
for (int row = 0; row < height / 2; row++)
{
for (int col = 0; col <p[0].length; col++)
{
p[row][col] = p[height - row - 1][col];
}
}
}
Mirrors the values from the top half to the bottom half of the 2D array
This would be true if it was p[height - row - 1][col] = p[row][col];
Mirrors the values from the left halt to the right half of the 2D array
This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
Mirrors the values from the bottom half to the top half of the 2D array
This loops through the top half rows (height / 2) and mirrors the values from the bottom half p[row][col] = p[height - row - 1][col]; So p[0][0] = p[height - 1][0] and p[0][1] = p[height - 1][1].
Mirrors the values from the right half to the left half of the 2D array
This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
All values remain the same.
How can this be true since p[row][col] = p[height - row - 1][col]?
//precondition: x >=0
public static void mystery (int x)
{
System.out.print(x % 10);
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
12344321
This method prints the right most digit (x % 10 returns the right most digit) and then if x / 10 is not equal to 0 (x < 10) it returns mystery of the current number after chopping off the right most digit. So mystery(4321) prints 1 and then calls mystery(432) which prints 2 and then calls mystery(43) which prints 3 and then calls mystery (4) which prints 4. Since 4 / 10 is equal to 0 it wonβt do a recursive call. It prints 4 again and mystery(4) returns. Execution will return to mystery(43) after the recursive call to mystery(4) and the 3 will print and then mystery (43) will return. Execution will return to mystery(432) after the recursive call to mystery(43) and the 2 will print and then mystery (432) will return. Execution will return to mystery(4321) after the recursive call to mystery(432) and the 1 will print and then mystery (4321) will return.
1234
This would be true if there wasnβt a second System.out.print(x % 10) after if.
4321
This would be true if the first call to System.out.print(x % 10); wasnβt in the method.
43211234
This would be true if it was mystery(1234).
32144123
How does the 3 get printed first? Remember that x % 10 returns the right most digit in x.
for (int j = 1; j <=5; j++)
{
for (int k = 1; k < 3; k++)
System.out.print(j * k + " ");
}
1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 5 1 5 2
This would be true if line 3 was: System.out.print(j + " " + k + " ");
1 2 2 4 3 6 4 8
This would be true if line 1 was: for (int j = 1; j < 5; j++).
1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2
This would be true if line 1 was: for (int j = 1; j < 5; j++) and if line 3 was: System.out.print(j + " " + k + " ");
5 10 15 4 8 12 3 6 9 2 4 6 1 2 3
This would be true if line 1 was: for (int j = 5; j >=1; j--) and line 2 was: for (int k = 1; k <= 3; k++).
1 2 2 4 3 6 4 8 5 10
This prints j * k and for each value of j from 1 to 5, k changes from 1 to 2. So when j is 1 it will print 1 2. When j is 2 it will print 2 4. When j is 3 it will print 3 6. When j is 4 it will print 4 8. When j is 5 it will print 5 10.
public void sample(int num1, int num2)
{
int result = 99;
if (num1==num2)
{
result = 0;
}
else if (num1>num2)
{
result = 1;
}
else {
result = -1;
}
System.out.println(result);
}
public void method1(int num1, int num2)
{
int result=99;
if (num1 == num2)
{
result = 0;
}
else {
if(num1 > num2)
{
result = 1;
}
else
{
result = -1;
}
}
System.out.println(result);
}
public void method2(int num1, int num2)
{
int result = 99;
if (num1 == num2)
{
result = 0;
}
if (num1 >= num2)
{
result = 1;
}
else
{
result = -1;
}
System.out.println(result);
}
public void method3(int num1, int num2)
{
int result = 99 ;
if (num1 == num2)
{
result = 0;
}
if (num1 > num2)
{
result = 1;
}
if (num1 < num2)
{
result = -1;
}
System.out.println(result);
}
method1 and method3
The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
method1 only
Another will work as well.
method2 only
This one wonβt work. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
method2 and method3
While method3 will work, method2 wonβt. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
String s1 = new String("hi");
String s2 = new String("hi");
String s3 = s2;
Which of the following would return true:
I. s1.equals(s2)
II. s1 == s2
III. s2.equals(s3);
IV. s2 == s3;
I and III
These are both true, but one more is also true.
All are true
Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.
I, III, and IV
I is true since they have the same characters in the same order. III and IV are both true since they refer to the same object.
II and IV
II is not true. Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.