Both of the expressions inside the parentheses were altered. If we wanted to distribute the negation symbol "!" then we would leave the expressions inside the parentheses alone.
Negating less than or equals (<=) results in greater than (>). In addition, less than (<) in the second argument should have been changed to greater than or equals (>=).
The argument is 4 so will have 4 recursive calls and then return 3 when we get to mystery(1). Each call will multiply our result by 3, so you can think of this as 3 raised to the 4th power (or 3 * 3 * 3 * 3 = 81).
I. An array that is in reverse order (from largest to smallest).
II. An array that is in sorted order already (from smallest to largest).
III. An array that is in random order (not already sorted).
If an array is already sorted from smallest to largest then we do not need to move anything in the array and we would only need to go through each element at most once, so this is fastest run time for insertion sort.
An array in reverse order is actually the worst run time for insertion sort because we would need to move everything to make it in order from smallest to largest.
int max = 5;
//Loop I
for (int i = 0; i < max; i++)
{
System.out.print(i);
}
//Loop II
int j = 0;
while (j < max)
{
System.out.print(j);
j++;
}
//Loop III
int k = 0;
for (int i = max; i > 0; i--)
{
System.out.print(i);
}
Both of these loops will have the correct output. They iterate (and print each value) starting from 0 until the max value which we defined earlier in our code.
We add 1 to value before actually printing it, so the first value printed will be 16. The last time through the loop the value will be 29 (less than 30) but then the code will add one so it will print 30.
The code adds one to value before the value is printed so 16 will be the first value printed. The last time through the loop the value will be 29 (less than 30) but then the code will add one so it will print 30.
The outer loop will execute 5 times, each time the outer loop executes the middle loop will execute 5 times, and each time the middle loop executes the inner loop will execute 5 times. So the answer is 5 * 5 * 5 = 125.
The number of times a loop executes is (largest value in loop - smallest value in loop + 1) each loop executes (5 - 1 + 1 = 5) times. When you have nested loops you multiply the number of times each loop executes. So the result is 5 for the outer loop * 5 for the middle loop * 5 for the innermost loop.
public class Animal
{
/* Some code */
}
public class Cat extends Animal
{
/* Some code */
}
I. Cat inherits the constructors of Animal
II. Cat cannot add new methods and private instance variables that Animal does not have.
III. Cat can override existing public methods of Animal
If arr[i] < someValue for all i from 0 to k, HELLO will be printed on each iteration of the for loop. The number of times a loop executes is the biggest value in the loop - the smallest value in the loop + 1 (k - 0 + 1 is k + 1).
Since there is no terminating condition surrounding our recursive method call (because the call lies outside of the if statement), it will keep doing recursive calls until we eventually get a run time error.
Since there is no statement that terminates the recursive call to stringRecursion (the length of the string s will increase until it is greater than 16, but the recursive call will keep happening because the recursive call is outside the if statement) the computer will keep doing recurisve calls until it runs out of memory and a run time error will happen.
Only when the length of the input string is greater than or equal to 16.
Since the recursive call is outside the condition and the conditional doesnβt include a return then this will result in infinite recursion and eventually a run time error.
The length of the string will not matter in this case because the recursive call to stringRecursion will always happen, since the recursive call lies outside the body of the conditional. The string length will only determine if the string s is printed out to the console or not.
We will get run time errors regardless of the length of the string s. This is due to the fact that the recursive call lies outside the body of the conditional. If the length of the string s is less than 16 then we will get something printed out to the console until the length of s becomes greater than 16, and then we will continue in a infinite recursion.
public class A
{
public void method1() { };
}
public class B extends A
{
// Instance variables and other methods not shown
public void method1()
{
/* implementation not shown */
}
}
public class C extends B
{
//Instance variables and other methods not shown
public void method2(C o)
{
/* implementation not shown */
}
public static void main(String[] args)
{
C objectC = new C();
B objectB = new B();
// Missing code
}
}
I objectC.method1();
II objectB.method2(objectC);
III objectC.method2(objectB);
Method II will produce a compile time error because class B (the superclass) does not inherit the methods of class C due to the fact that class C is its subclass. But, it is not the only call that will result in a compile time error.
Method II will produce a compile time error because class B (the superclass) does not inherit the methods of class C due to the fact that class C is its subclass. Method III will produce an error because of the parameter it takes in. objectB is not a class C type object which is what the method definition for method III required.
I ArrayList<String> stringList = new List<String>();
II ArrayList<int> intList = new ArrayList<int>();
III ArrayList<String> stringList = new ArrayList<String>();
Suppose that the following method takes in a two dimensional array called matrix. After the method call printMatrix(matrix) what will the output be? Possible options are listed below the method definition.
/* assume that matrix has the following values */
7654
3210
4567
0123
public static void printMatrix(int[][] matrix)
{
for (int i = 0; i < matrix.length; i++)
{
for (int t = 0; t < i; t++)
{
System.out.print(matrix[i][t]);
}
System.out.println();
}
}
Possible output:
I.
7654
3210
4567
0123
II.
7
32
456
0123
III.
3
45
012
IV.
7
3
4
0
Since the inside for loop starts with t = 0 and continues while t < i (and i begins at 0) it will not be print out every single element of the 4x4 matrix.
This anwser is not correct because our inside for loop will start with t = 0 and loop while t < i and, as such, the entire first row of our matrix will be ignored, since both t and i = 0 and t is not less than i.
When i = 0, the inner for loop does not get executed and the entire first row of the matrix is ignored. When i = 1 t goes from 0 to 0 and the element matrix[1][0] will be printed out. Similarly, when i = 2 we will print out elements matrix[2][0] and matrix[2][1]. Finally, when i = 3, we will print out matrix[3][0], matrix[3][1] and matrix[3][2].
This would be the correct anwser if we kept incrementing i by one (the outer for loop) but the inner for variable t would always be 0. We would get the first element of each row.
We will not get an index out of bounds exception since we made sure to increment i only until the max length of the array and the other variable we use to index, t, will only increase while it is still less than i.
If randomList is an ArrayList of Integer objects and is initially set to {0, 1, 2, 3}, what will randomList look like after the following code is executed?
After we add 5 and 7 to the end of the array we remove the element at index 2 (which was 2). Then we use the index we had previously obtained (also 2) to add a new element 4. This pushes the element already at that index (and the ones after it) one space to the right. Fianlly, we set the element at index 1 to be 8. This sets the value at index 1 to 8.
The substring method takes two arguments, a start index (which is inclusive) and an end index (which is exclusive). The first substring is from index 1 (counter + 1) to index 2 (counter + 2). However the second index is not included so its just index 1 which is e. We then simply keep getting every indidual element from the string one by one until the end of the string.
Even though the end of the substring is specified as index counter + 2, which will be past the end of the string the last time through the loop, substring doesnβt include the value at the end index, so the code will execute.
The first substring element has a start value of index 1 and so f will not be printed out. Also because each substring is a single character, no character will be repeated in the substring.
public class B
{
public int myValue;
public B()
{
myValue = 0;
}
public B(int x)
{
myValue = x;
}
}
public class C extends B
{
public C()
{
super(0);
}
}
Even though class C has a super class with a constructor that takes in a single int argument, class C does not have a constructor that takes an int value.
Consider the following method. Assume that String s = "rain"; and int b = 4; have been executed. What are the values of s and b after test(s,b) is executed?
Strings are immutable so changing str doesnβt affect the string that s refers to. The value of b also will not change since Java passes a copy of the value.
Since strings are immutable any change returns a new string and doesnβt affect what s refers to. Also the value of primitive types are copied and nothing done in test affects the orignal primitive value.
I. Insertion sort requires more storage space than mergesort.
II. Insertion sort is only more efficient than mergesort in the case that we have a very small and nearly sorted array.
III. Insertion sort is almost always less efficient than mergesort.
Insertion sort is more efficient for a small array because merge sort has extra overhead from the recursive function calls that cause it to take longer.
Merge sort uses the "divide and conquer" approach to sort an array. This will end up being more efficient than insertion sort in the case where we have a long unordered array.
Statement III is true but statement I is false since mergesort often utilizes a temporary array and will actually require more storage space than insertion sort.
Merge sort uses the "divide and conquer" approach to sort an array. This will end up being more efficient than insertion sort in the case where we have long unordered array. However if we have a very small almost sorted array, then insertion sort will outperform merge sort.
The method alter shifts the values in the columns starting at column c + 1 and shifting back to entry to the left of c + 1. This matrix is what would result if c was three and we were shifitng the number there to the two spots before it.
Method alter shifts the values in the columns, starting at column c + 1, one column to the left. It also overwrites column c. Here are the replacements made for the method call alter(1): matrix[0][1] = matrix[0][2], matrix[0][2] = matrix[0][3], matrix[1][1] = matrix[1][2], matrix[1][2] = matrix[1][3], matrix[2][1] = matrix[2][2], matrix[2][2] = matrix[2][3]