public class Bird
{
private String color;
public Bird(String theColor)
{
/* implementation not shown */
}
public void makeNoise()
{
/* implementation not shown */
}
public void eat()
{
/* implementation not shown */
}
public string showFeathers()
{
return color;
}
}
public class Swan extends Bird
{
/* no constructors or other methods have been declared */
}
I. this.color = "blue";
II. eat();
III. Swan s = new Swan("blue");
The color is a private instance variable in Bird. Children classes do not have direct access to private variables. They must use the public getter and setter methods to access the private variables.
Consider the following code. Assume that list is an ArrayList of integers that contains [7, 3, 2]. What will the contents of list be after the following code is executed?
Remember that in ArrayLists, indexing starts at 0, not at 1. If the add method has two parameters, then the value is added at a specific index, not at the end of the list.
Remember that there are two add methods for ArrayLists. If the add method has two parameters, then a value is added at a specific index, not at the end of the list.
4 is added to the end of the ArrayList, then 8 is added at index one between 7 and 3. The 3 in index two is removed, then the 2 in the second index is replaced with 1. Finally, 3 is added to the end of the ArrayList, which contains [7, 8, 1, 4, 3].
The method rowSums returns an array of integers. Each element of the array holds the sum of the corresponding row of a 2-D matrix. Which line correctly fills in \* to be determined *\ in rowSums?
public int[] rowSums(int[][] arr)
{
int[] ans = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[0].length; j++)
{
/* to be determined */
}
}
return ans;
}
In Java, assignments work from right to left. This answer assigns the value of ans[i] in the 1-D array to the value of the 2-D array. Instead, we want to add the values of the row i in the 2-D array and assign this sum to ans[i] in the 1-D array.
In order to return the right array, the value at ans[i] must contain the sums of every element in row i of the 2-D array. The second for-loop adds the value of every element in row i of the 2-D array and assigns these values to ans[i].
This line reassigns the value of arr[i][j] to ans[i], but it does not sum all the values in the row. This line would return an array with the value in the last column of each row.
Remember that assignment works from right to left in Java. This line adds the value of ans[i] in the 1-D array to the value of arr[i][j] in the 2-D array. The 2-D array should not be modified by this method.
Consider the following method binSearch, which uses binary search to locate an element key in an array of integers arr. If list is an array of integers containing {4, 7, 9, 11, 20, 24,
30, 41}, how many iterations of the while loop occur in binSearch(30, list)?
30 would not have been located in 1 iteration of the while loop. After one iteration, low would equal 0, mid would equal 3, and high would equal 7. Because list[3] is equal to 11, not 30, nothing is returned, low becomes 4, and the while-loop continues.
30 would not have been located in 2 iterations of the while loop. After two iterations, mid would equal 5. Because list[5] is equal to 24, not 30, low would increase, and the while-loop would run again. Try one more iteration of the while loop.
30 would be found in 3 iterations. After the third iteration of the while loop, mid would equal 6. list[6] equals 30, so 6 is returned and the while-loop is exited.
4 iterations is too many iterations. Only 3 iterations are needed to find 30 in the array. After 4 iterations for an array with 7 elements, either the key is not present in the array or the key is at the first or last index of the array.
Only 3 iterations of the while loop are needed to find 30 in the array. After 5 iterations for an array with seven elements, it must be that the key was not found.
The exclamation point is returned only once, when the method reaches its base case. Because the compiler works through the recursive calls to the end of the word before it returns any strings, the letters are printed in reverse order.
The compiler works through all of the recursive calls before it returns any strings. The exclamation point is returned first, followed by the letters of the original string in reverse order.
This string would be correct if the substring was returned before the recursive call. Because the recursive call occurs before the substring is returned, the compiler reaches the end of the string before it returns the letters, so the letters are reversed.
The exclamation point is printed before the letters of the word. The method makes recursive calls until the length of the string equals 0 and the base case has been reached. Then, an exclamation point is returned to the recursive calls, and the letters are returned after the exclamation point in reverse order.
This method makes multiple calls, removing the first letter from the string until the length of the string in the call equals 0. Then, it returns an exclamation point, followed by the letters of the string in reverse order.
I. for (int i = 0; i < 11; i++)
{
System.out.print(i * 10 + " ");
}
II. int i = 0;
while (i <= 10)
{
System.out.print(i * 10 + " ");
i++;
}
III. for (int i = 0; i <= 100; i += 10)
{
System.out.print(i + " ");
}
public class Person
{
private String name;
private int age;
public Person(String theName, int theAge)
{
name = theName;
age = theAge;
}
}
public class Student extends Person
{
private int grade;
public Student(String theName, int theAge, int theGrade)
{
/* to be completed */
}
}
I. name = theName;
age = theAge;
grade = theGrade;
II. super(theName, theAge);
grade = theGrade;
III. super(theName, theAge);
name = theName;
age = theAge;
grade = theGrade;
This answer correctly calls on the constructor in the Person class using super. Then, it correctly instantiates the instance variable grade, located in the Student class.
name and age are private instance variables in the Person class. Children classes do not have direct access to private variables in the parent class. Although the Person constructor has correctly been implemented using the super keyword, name and age cannot be accessed by the Student class.
II is correct, but name and age instance variables found in the Person class. Instance variables are not inherited and cannot be modified by sub classes.
name and age are private instance variables in the Person class. Although the constructor from the Person class may be implemented using super, the instance variables in the parent class are not directly accessible by the child class.
A list of integers containing [12, 8, 7, 30, 62, 45, 10, 3] is sorted from largest to smallest using a selection sort method. After three passes, what does the list look like?
Since 62 is the largest value in the array, it swaps position with the value in index 0 of the array, 12. 45 is the next largest value, and it swaps with 8. 30 is the next largest value, and it swaps with 7. So, after three passes the list contains [62, 45, 30, 7, 12, 8, 10, 3].
public class Animal
{
private String name;
public Animal(String theName)
{
name = theName;
}
public Animal()
{
name = "Animal";
}
public String makeNoise()
{
return "";
}
;
public String getName()
{
return name;
}
}
public class Pig extends Animal
{
public Pig(String theName)
{
super(theName);
}
public String makeNoise()
{
return "Oink!";
}
public String getName()
{
return "My name is " + super.getName() + "!";
}
public static void main(String[] args)
{
Animal piglet = new Pig("Piglet");
System.out.print(piglet.getName());
}
}
At run-time, piglet is a Pig object. The compiler uses the overwritten getName method located in the Pig class, which prints out "My name is " before calling on the getName method in the Animal class.
This would be correct if the getName method had not been overwritten in the Pig class. Because piglet is a Pig object at run-time, the compiler uses the getName method from the Pig class.
Check the constructor method in the Pig class. The Pig class constructor uses the Animal class constructor that has one String parameter, not the default Animal constructor.
The constructor in the Pig class uses the Animal class constructor that takes in a string parameter, not the default constructor. The getName method has been overwritten in the Pig class, so "My name is " is printed before the name of the object.
Consider the following method oddArray, which changes every even number value in the array to 0. By the end of the method, only odd numbers will be present in the array. Which line correctly completes /* to be
determined */ to make the code work as intended?
public void oddArray (int[] arr)
{
for (int i = 0; i < arr.length; i++)
{
//if the number at arr[i] is even, it becomes 0
if( /* to be determined */ )
arr[i] = 0;
}
}
Consider the method emptyList, shown below. The method returns true if an ArrayList of integers is filled with zeros and false otherwise. Which of the following should replace /* to be completed
*/ so that the method will work as intended?
public boolean emptyList (ArrayList <Integer> list)
{
/* to be completed */
}
// I.
for (int i = 0; i < list.size(); i++)
{
if (list.get(i) != 0)
return false;
}
return true;
// II.
for (int i = 0; i < list.size(); i++)
{
if (list[i] != 0)
return false;
}
return true;
// III.
for (int i = 0; i < list.size(); i++)
{
if (list.get(i) != 0)
return true;
}
return false;
This answer checks every index in the list, correctly selects the values in the list and compares them to zero. If a value is not equal to zero, the method returns false, and the array is NOT empty. Otherwise, the method returns true.
This always returns 25. Math.random() produces a number between 0 and 1, so when it is added to 25 and cast as an integer, the number always becomes 25.
This always returns 1. Math.random() produces a value between 0 and 1, so casting Math.random() to an int results in 0. 0 * 25 remains 0, and 0 + 1 equals 1.
A list of 120 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to confirm that a name is not in the list?
This is not enough passes to guarantee that a name is not present. 2 ^ 5, is 32, which is not enough elements. Remember that binary search takes log2 (number of elements) passes at most to find an item.
2 ^ 7 is 128, which is greater than 120. 120 passes will guarantee that the name is not present in the list. Binary search takes log2 (number of elements) at most to find an item.
Yes, you would know by 10 passes, but there is a better answer. Remember that binary search takes log2 (number of elements) passes at most to find an item.
Yes, you would know by 12 passes, but not all 12 passes are required. Remember that binary search takes log2 (number of elements) passes at most to find an item.
This would be correct if the recursive call was located in an else statement. If the string length is 15 or greater, "s" will not be printed, but the recursive call will still occur.
There is no base case present in this method that stops the recursive calls. This method will continue until the compiler runs out of memory. You could fix this code by placing the recursive call in an else statement or creating a base case to end the call.
public class Fruit
{
private String name;
private boolean seeds;
public Fruit(String theName)
{
name = theName;
seeds = true;
}
public void setSeeds()
{
seeds = !seeds;
}
}
public class Grape extends Fruit
{
private String color;
public Grape(String theName, String theColor)
{
super(theName);
color = theColor;
}
}
public class SeedlessGrape extends Grape
{
public SeedlessGrape(String theName, String theColor)
{
super(theName, theColor);
setSeeds();
}
}
I. Fruit a = new SeedlessGrape("grape", "red");
II. Grape b = new Grape("grape");
III. SeedlessGrape c = new Grape("grape", "green");
The Grape class constructor has two parameters. Although a Grape IS-A fruit, the Grape constructor must have two string parameters to compile without error.
A Grape is NOT a SeedlessGrape. The inheritance relationship is incorrect, and III does not compile. Object a is a Fruit at compile-time and a SeedlessGrape at run-time. A SeedlessGrape IS-A Fruit, so the code compiles.
The method printNames is located below. It prints out all the names in a 2-D matrix. Which of the following correctly replaces /*
to be determined */ to make the method work as intended?
x refers to a String object, not an index in the array. x can be printed directly, because the second for-loop individually selects Strings in each row of the array.
This method uses two for-each loops. The variable x refers to a single String located in the array, so only x needs to be printed. This method will loop through the entire 2-D array, printing out all the names in the matrix.
Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B). Also, (x < 10) is not correct negation for (x > 10); the correct negation is (x <= 10).
Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B). The AND should be changed to an OR.
Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is equivalent to (!A || !B), according to DeMorganβs principle. The negation of (x > 10) is (x <= 10), and the negation of (x <= 5) is (x > 5).
Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (A || B). Both A and B should also be negated.
This method calculates 3 ^ num. It goes through the recursive calls until num reaches 1, then 3 is multiplied by itself (num) times. The method has been called four times, and 3 ^ 4 is 81.
This would be correct if the recursive method called 3 + mystery (num - 1), and num was equal to 5. Check the base case and the parameter and try again.