public class Animal
{
/* no constructors or other methods have been declared */
}
public class Fish extends Animal
{
/* no constructors or other methods have been declared */
}
public class Goldfish extends Fish
{
/* no constructors or other methods have been declared */
}
I. Goldfish glub = new Fish();
II. Animal glub = new Fish();
III. Fish glub = new Goldfish();
II is correct, but a Fish is NOT a type of Goldfish. A Fish cannot be instantiated as a Goldfish object, because the Fish class does not inherit from the Goldfish class.
A Goldfish IS-A type of Fish, and a Fish IS-A type of Animal. The Goldfish class inherits from the Fish class, and the Fish class inherits from the Animal class.
When the add method is used with two parameters, the value is added at the specific index, not at the end of the list. In this list, 4 has been added at index 1.
This would be correct if 7 had been placed in the list using add, not set. Remember that the set method replaces the value at the index. It does not move the previous value to the right.
The 9 at index 2 is removed, resulting in [6, 2], then a 4 is added at index 1 resulting in [6, 4, 2]. A 5 is added to the end of the list resulting in [6,4,2,5], and the value at 2 is replaced with a 7 resulting in [6,4,7,5].
Remember that in ArrayLists, indexing starts at 0, not 1. The set method replaces the value at the specified index with a new value, so the original value is deleted.
A sorted array of integers containing 2000 elements is to be searched for key using a binary search method. Assuming key is in the array, what is the maximum number of iterations needed to find key?
2 ^ 8 = 256. There will not be enough passes to guarantee finding the value. Remember that binary search requires log2 (number of elements) passes to guarantee that a value will be found.
2 ^ 10 = 1024. There will not be enough passes to guarantee finding the value. Remember that binary search requires log2 (number of elements) passes to guarantee that a value will be found.
The key will be found in 100 passes, but there is a better answer. Remember that binary search requires log2 (number of elements) passes to find a value.
With binary search, every element of the array does not have to be checked. Remember that although sequential search would require 2000 passes to guarantee the value was found, binary search requires log2 (number of elements) passes to find an object.
Which of the following code segments creates a 7 x 9 array of integers and fills every space in the array with multiples of two (not including the value 0)?
I. int[][] arr = new int [7][9];
II. int[][] arr = new int [7][9];
int count = 1;
for(int i = 0; i < arr.length; i++)
{
for(int j = 0; j < arr[0].length; j++)
{
arr[i][j] = count * 2;
count++;
}
}
III. int[][] arr = new int [7][9];
int count = 1;
int row = 0;
int col = 0;
while (row < arr.length && col < arr[0].length)
{
arr[row][col] = count * 2;
row++;
col++;
count++;
}
II is correct, but III does not fill every space correctly. Only diagonal spaces are filled, so most of the spaces are still filled with 0 at the end of the loop. Notice that every time the while loop cycles, the values of row and col both increase.
III does not fill every space correctly. Only spaces lying on the diagonal are filled because the row and column index change at the same time, and the values are incorrect. Most of the spaces remain filled with 0. Notice that every time the while loop cycles, the values of row and col both increase.
This would occur if the print statement came before the recursive call. Because the compiler works through the recursive call before moving to the other statements, the letters are printed in reverse order.
This would occur if the print statement came before the recursive call and included s.substring(1), not s.substring(0, 1). The statements are printed after the recursive call is made, so the compiler works through every recursive call before it prints out the letters, and the letters are printed in reverse order.
This would occur if the print statement included s.substring(1). Each call of the printString method prints only one letter at a time, because the substring that is printed is s.substring(0,1).
Nothing is printed because an infinite loop occurs
public class Dog
{
private int numLegs = 4;
private String name = "Spot";
public Dog(String theName)
{
/* implementation not shown */
}
public String bark()
{
return "Woof!";
}
public String getName()
{
return name;
}
}
I. public Dog(String theName)
II. bark()
III. getName()
I. for (int i = 0; i <= 8; i++)
{
System.out.print(i);
}
II. int i = 0;
while (i < 8)
{
i +=2;
System.out.print(i);
}
III. for (int i = 0; i <= 8; i +=2)
{
System.out.print(i);
}
Notice the order of the incrementing and the print statement in the while loop. The value i increments before it is printed. The code never prints out 0, so "2468" is printed.
Notice the incrementing in the loops for I and II. In I, the value i increments by 1 and prints out too many values. In II, the first value is not printed.
Eventually, the recursive calls will reach the base case, where y is greater than or equal to x. If y is greater than 1, multiplying by 10 will increase y and y will remain positive.
If y is less than or equal to 0, multiplying by 10 will not make the value greater than x. The base case will never be reached, and the method will continue running until the computer runs out of memory.
public class Cat
{
public String display()
{
System.out.print("Cats! ");
}
}
public class FluffyCat extends Cat
{
public String display()
{
System.out.print("Cool!");
}
}
Although obj is declared to be a Cat at compile time, at run-time it is actually a FluffyCat. The overwritten display method defined in the FluffyCat class will be called.
After the recursive call reaches the base case (where arg = 1), the compiler prints "1". The recursive calls all just return and donβt print anything.
This would be correct if the recursive call specified that arg >= 1 or arg > 0. Because the code ends when arg reaches a value of 1, the code will not print out 0.
This would be correct if the method printed out arg + " " before going to the recursive call. Because the print statement is located at the end of the base case and not the recursive call, not every value is printed.
This would be correct if the method printed arg + " " after the recursive call in the if statement. Because the method does not return any values or strings, and because only the base case has a print statement, only the last value of arg is printed.
This would be correct if at the beginning of the second for loop, y was equal to 0, not to x. The starting value of y changes every time that x increases.
You are trying to write a method sumRow that finds the sum of the values in a specified row of a symmetrical 2-D matrix. Which of the following code segments could replace /* to be determined */ to make the code work correctly?
public int sumRow (int row, int[][] values)
{
int sum = 0;
/* to be determined */
return sum;
}
//I.
for (int[] rowValues : values)
{
for (int x : rowValues)
{
sum += x;
}
}
//II.
for (int i = 0; i < values[0].length;i++)
{
sum += values[row][i];
}
//III.
int col = 0;
while (col < values[0].length)
{
sum += values[row][col];
col++;
}
This always returns 50. Math.random() + 1 calculates a value between 1 and 1.9, and when this value is cast as an int it becomes 1. 1 * 50 always returns 50.
This always returns 50. 1 * 50 returns 50 since multiplication takes precedence befores addition. The value of Math.random() + 50 always falls between 50.0 and 50.9, and this value becomes 50 when it is cast as an int.
This always returns 0, since Math.random() returns a value between 0 and 0.9. When the value of Math.random() is cast an int, its value becomes 0. 0 * 50 returns 0.
This would be correct if the method added the digits in the value. Instead, the method simply finds the number of digits. Check the recursive call again.
Consider an array of integers that contains [12, 8, 4, 6, 13, 29,
7]. If the array is sorted from smallest to largest using an insertion sort method, what will be the order of the array after the third iteration of the sorting method?
This is what would happen with two iterations of selection sort. Remember that selection sort only swaps two elements at a time, while insertion sort places elements in order in the sorted part of the array.
This is what would happen if selection sort was used instead of insertion sort. Remember that selection sort only swaps two elements at a time, while insertion sort places elements in order in the sorted part of the array.
Using insertion sort, we start at the first index and sort the first two values to create a sorted array at the left side of the array. We repeat this step for the second index, creating a sorted array of three elements, and again for the third index, creating a sorted array of four elements.
Consider the classes Car and Minivan, shown below. If obj has been instantiated later in the class as a Minivan, what is printed as a result of obj.drive()?
public class Car
{
public void drive()
{
System.out.print("Vroom vroom! ");
}
}
public class Minivan extends Car
{
public void drive()
{
super.drive();
System.out.print(" Let's go! ");
}
}
The method drive has been overwritten in the Minivan class. Since obj is of type Minivan, the compiler will use the overwritten method. The overwritten method uses super() to call to the method of the parent class, so "Vroom vroom! " is printed. Then, the overwritten method prints out "Letβs go! ".
Although the overwritten method has a call to the method in the parent class, there is another line of code that must be printed. The drive method has been overwritten for the Minivan class.
This would be the case if the overwritten method did not make a call to the class in the parent class. Because the method has a call to the parent class before it does anything else, "Vroom vroom! " is printed.
This code correctly compiles, so there are no errors present. The Minivan class can make a call to a method in the Car class using super, because the Minivan class extends the Car class.
Consider the following method changeArray. An array is created that contains [2, 8, 10, 9, 6] and is passed to changeArray. What are the contents of the array after the changeArray method executes?
This would be correct if data[k - 1] was subtracted from data[k]. Notice that for every instance of the for-loop, data[k] and data[k - 1] are added together and assigned to the index at data[k - 1].
The indexing of this method is correct. The for-loop begins at the last index and ends at the second index, and the method does not access any values other than the ones specified.
Use A and B to represent the expressions -- A == (x > 7), B == !(y < 12)Using DeMorganβs law, !(A && B) is equivalent to !A || !B. The negation of (x > 7) is (x <= 7), and the negation of !(y < 12) is (y < 12).
Use A and B to represent the expressions -- A == (x > 7), B == !(y < 12)!(A && B) is NOT equivalent to (A || B). It should be (!A || !B). Also, (y >= 12) is equivalent to !(y < 12).
Use A and B to represent the expressions -- A == (x > 7), B == !(y < 12)!(A && B) is NOT equivalent to (A && B). !(y < 12) and (y >=12) mean the same thing; changing this does not make the statement the opposite.
Use A and B to represent the expressions -- A == (x > 7), B == !(y < 12)!(A && B) is NOT equivalent to (!A && B). Changing !(y < 12) to (y >= 12) does not negate the statement; these two are equivalent.
Consider the following method evens, which finds the number of even numbers present in an array. Which of the following segments of code would correctly replace /* to be completed */?
public int evens(int [] arr)
{
int count = 0;
for (int x : arr)
{
/* to be completed */
}
return count;
}
// I
if (x % 2 == 0)
count++;
// II
if (x % 2 == 1)
count++;
// III
if (x / 2 == 0)
count++;
// IV
if (x / 2 == 1)
count++;
II counts the odd numbers instead of the even numbers. If x % 2 == 1, the number is odd, not even. IV does not use the remainder operator (%), which checks if numbers are even or odd.
Consider the method findMax, which uses sequential search to find the index of the largest value of an array. In which case would findMax not work properly?
public int findMax(int[] arr)
{
int maxVal = 0;
int index = 0;
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > maxVal)
{
index = i;
maxVal = arr[i];
}
}
return index;
}
This method will not work correctly for all arrays. Look at the starting value for maxVal, and how maxVal is compared to all the values of the array. What happens if every value in the array is less than maxVal?
Although this might present a problem if EVERY value in the array is less than 0, the compiler will move on to the next index without issue if the first value in the array is less than 0.
maxVal is set to zero, so if every number in the array is less than 0, the maxVal will remain 0. A better idea would be to set maxVal to the value of the first element in the array.