Consider the method minVal, shown below. minVal compares every value in the given array to min to find the smallest value, which is then returned. At the beginning of the code, min is set to 1. Which of the following is the best value to set min so that the method will compile and work as intended?
public int minVal (int[][] arr)
{
int min = 1;
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[0].length; j++)
{
if (arr[i][j] < min)
min = arr[i][j];
}
}
return min;
}
Notice where min is set in the code. At the time that min is set, i and j have not been delcared and cannot be used. This choice will create a compile-time error.
Using the first value in the array guarantees that the correct minimum value will be found and returned, regardless of the range of numbers in the array.
Setting min equal to 0 might find the minimum value in some cases. However, if every number in the array is positive, then min will remain 0 and it will not find the minimum value in the array.
If min is set to -1, the method would only work correctly if there was a value in the array that was equal to or smaller than -1. If all of the values in the array are greater than -1, then the correct minimum value will not be found.
This value would only work correctly if there was a value in the array that was less than 1. If the array is filled with positive numbers, 1 will remain the minimum and the correct minimum may not be found.
Use A and B to represent the expressions -- A becomes !(x >= 7), B becomes (x > 2). ! (A && B) does NOT equal A && !B. !(x >= 7) is the same as (x < 7).
Use A and B to represent the expressions -- A becomes !(x >= 7), B becomes (x > 2). ! (A && B) does NOT equal !A && !B. Also, the negation of (x > 2) is (x <= 2), not (x < 2).
Use A and B to represent the expressions -- A becomes !(x >= 7), B becomes (x > 2). ! (A && B) is equal to !A || !B, according to DeMorganβs law. The negation of !(x >= 7) is (x >= 7), and the negation of (x > 2) is (x <= 2).
Use A and B to represent the expressions -- A becomes !(x >= 7), B becomes (x > 2). ! (A && B) does NOT equal A || !B. The negation of (x > 2) is (x <= 2), not (x < 2), and !(x >= 7) is the same as (x < 7).
// I.
int total = 1;
for (int i = 0; i < 10; i++)
{
total = total * num;
}
// II.
int count = 0;
int total = 1;
while (count < 10)
{
count++;
total = total * num;
}
// III.
int total = 1;
for (int i : 10)
{
total = total * num;
}
Consider the Animal and Cat classes, shown below. In another class, the line Animal fluffy = new Cat ("orange", "Fluffy",
11) appears. Which of the following declarations will compile without error?
public class Animal
{
private String color;
private String name;
public Animal (String theColor, String theName)
{
name = theName;
color = theColor;
}
public String makeNoise() { return ""; }
public int getWeight() { return 0; }
}
public class Cat extends Animal
{
private int weight;
public Cat (String theColor, String theName, int theWeight)
{
super (theColor, theName);
weight = theWeight;
}
public String makeNoise()
{
return "Meow!";
}
public int getWeight()
{
return weight;
}
}
I. fluffy.color;
II. fluffy.getWeight();
III. fluffy.makeNoise();
Color is a private instance variable located in the Animal class. Private instance variables cannot be directly accessed using dot notation in external classes.
Color is a private instance variable located in the Animal class. Private instance variables cannot be directly accessed using dot notation in external classes.
This method calculates n! (n factorial) by subtracting 1 from n until n equals 1. Then, it works through the calls, multiplying each value of n by the previous values. 5 * 4 * 3 * 2 * 1 equals 120.
Notice the recursive call. This would be correct if the code added n to the value returned by the recursive call. Instead, the returned value is multiplied by n.
Consider the binSearch method shown below, which uses a binary search algorithm to locate an integer key in an array. Assume intArr is an array of integers containing [5, 7, 9, 11, 21,
29, 36, 45]. How many iterations of the while loop occur in binSearch(5, intArr)?
This would be the correct answer if sequential search is used. Remember that the loop will continue until a value is returned or the value is not found, regardless of the position of key.
After the first instance of the while loop, high = 7 and mid = 3. Because intArr[3] is greater than 5, high becomes 2, mid becomes 1, and the loop passes again. intArr[2] is also greater than 5, so high becomes 0, mid becomes 0, and the loop passes again. intArr[0] equals 5, so the key was found in three iterations of the while-loop.
This number is too high for a binary search algorithm. There are 8 elements in the array, and binary search uses, at a maximum, log2 (number of elements) iterations. log2 (8) is less than 4.
This number is too high for a binary search algorithm. There are 8 elements in the array, and binary search uses, at a maximum, log2 (number of elements) iterations. log2 (8) is less than 5.
This would be correct if the recursive call contained a return to n + "! " in addition to the call to numList. Notice the recursive call in this problem. Only the value of numList(n - 1) is returned, with nothing else added.
This would be correct if the recursive call contained a call to numList AND a return of n + "! ". Notice the recursive call in this problem. Only the value of numList(n - 1) is returned, with nothing else added.
The method makes recursive calls until 0 is reached, then "0! " is returned. None of the recursive calls modify the returned response, so only "0! " is returned.
An infinite loop will not occur in this method, because of the precondition. After a certain number of calls, n will reach the base case and the method will end.
8 is added at index 2, then index 4 is set to equal 1. The value at index 3 is removed, and 9 is added to the end of the array. Finally, the value at index 5 is set to equal 5.
When the add method has two parameters, the first parameter specifies the index and the second is the value to add at that index which moves any existing values to the right. The two parameters are not added to the end of the array.
The set method differs from the add method in that it replaces the original value at the specified index. The set method does NOT shift the numbers to the right of the specified index.
The add method adds the specified value at the specified index and shifts every index to the right of the current index. It does NOT delete the value at the original index.
The method columnSum is shown below. columnSum returns the sum of all of the values in a specified column col of a 2-D array. Which of the following should replace /* to be completed
*/ so that the method will work as intended?
These would be the contents of intArray if intArray was sorted through merge sort. Remember that insertion sort does not break an array into smaller arrays to sort, and values are inserted into a pre-sorted array.
These would be the contents of intArray after three passes of selection sort. Remember that while selection sort swaps the lowest value in the array with the specified index, insertion sort places the value at the specified index in a pre-sorted array.
These would be the contents of intArray after two passes of selection sort. Remember that while selection sort swaps the lowest value in the array with the specified index, insertion sort places the value at the specified index in a pre-sorted array.
After the first pass, the contents of intArray are in the same order, since the presorted array contains only one value. After the second pass, the contents are [8, 12, 34, 6, 10, 14, 2, 4] and the presorted array contains three elements. After the third pass, 6 is inserted in the presorted array, and the contents are now [6, 8, 12, 34, 10, 14, 2, 4].
The Vehicle, Bike, and Car classes are shown. The objects a and b have been declared in a different class. Which of the following lines will compile without error?
public class Vehicle
{
private int wheels;
private String color;
public Vehicle (String theColor, int theWheels)
{
wheels = theWheels;
color = theColor;
}
public int numOfWheels()
{
return wheels;
}
public String getColor()
{
return color;
}
}
public class Bike extends Vehicle
{
public Bike (String theColor, int theWheels)
{
super (theColor, theWheels);
}
/* no other constructors or methods implemented */
}
public class Car extends Vehicle
{
public Car (String theColor, int theWheels()
{
super (theColor, theWheels);
}
/* no other constructors or methods implemented */
}
Vehicle a = new Bike ("green", 2);
Vehicle b = new Car ("red", 4);
I. b.wheels;
II. a.getColor();
III. b.numOfWheels();
IV. a.color;
color is a private instance variable located in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
wheels is a private instance variable located in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
wheels and color are both private instance variables in the Vehicle class. Private instance variables can not be directly accessed using dot notation in external classes.
This would be correct if s.substring(0, 1) was returned BEFORE the recursive call. Because the recursive call is placed before s.substring(1), the compiler loops through the entire word and returns the last character of the word before any other character.
Notice the substrings in this method. s.substring(1), not s.substring(0, 1) is used in the recursive call. s.substring(1) starts at the first index, taking off the first letter of a string and returning the rest of the characters.
Notice the substrings in this method. s.substring(0, 1), not s.substring(1) is returned. s.substring(0, 1) only returns one character, so only one character at a time is returned to the method.
This would be correct if the last line returned s.substring(1) and wordMixer(s.substring(0, 1)). Because the first substring is used to make a call to the string with only the first character removed, the code will loop through all of the letters before it returns a character.
The method divisible is shown below. In order for divisible to compile and run as intended, the method must return true if x is evenly divisible by y with no remainder, returning false otherwise. Which of the following could replace /* to be completed
*/ to make the code work as intended?
/* Precondition: x and y are both integers greater than 0 */
public boolean divisible (int x, int y)
{
if ( /* to be completed */)
return true;
return false;
}
The remainder operator (%) returns the remainder left by integer division. If x % y == 1, x is not evenly divisible by y, as there is a remainder of 1 left over.
The remainder operator (%) returns the remainder left by integer division. If x % y == 2, x is not evenly divisible by y, because there is a remainder of 2 left after the division.
The division operator does not check if one number is divisible by another. In integer division, remainders are calculated by the remainder operator (%).
A database containing 2,000 sorted integers must be searched using a binary search algorithm. What is the maximum number of iterations of the binary search method that must occur in order to find a specified value or guarantee that it is not in the database?
2 ^ 9 is 512, which is not enough elements to cover every element in the database. Remember that binary search requires log2 (number of elements) iterations to perform.
2 ^ 11 is 2048. 11 iterations is more than enough to find the value or guarantee that it is not in the database. Binary search takes log2 (number of elements) iterations to perform.
The value will be found in 20 iterations, but a smaller number of iterations could be used. Remember that binary search requires log2 (number of elements) iterations to perform correctly.
public class Person
{
private String name;
private int age;
public Person(String theName, int theAge)
{
name = theName;
age = theAge;
}
public String sayName()
{
return name;
}
public int getAge()
{
return age;
}
}
public class Student extends Person
{
private int grade;
public Student(String theName, int theAge, int theGrade)
{
super(theName, theAge);
grade = theGrade;
}
public String sayName()
{
return "My name is " + super.sayName();
}
public String sayName(String nickname)
{
return "My name is " + name + " but I like to be called " + nickname;
}
public int getGrade()
{
return grade;
}
public void changeGrade()
{
grade++;
}
}
Having a constructor in the Student class that has a different parameter list than the constructor in the Person class.
This is not an example of method overloading. In this constructor method, the parent constructor is called, but the method is not overloaded. Method overloading occurs when a class has two or more methods with the same name and a different parameter list (like a different number of parameters).
Having a sayName() method in Person and in Student.
This is an example of method overridding, not method overloading. Method overridding occurs when a method is redefined in a subclass, and the method has the same parameter list. Method overloading occurs when there are two or more methods with the same name and different parameter lists in the same class.
Having sayName() and sayName(String nickname) in the Student class.
In the Student class, there are two different sayName methods. The second sayName method has the same name and same return type, but the parameter lists differ. This is an example of method overloading.
Having the changeGrade() method in the Student class.
Method overloading occurs when a class has two or more methods with the same name and different parameters. There is a method in the Student class with the same name and two different parameter lists.
You are trying to write the countDown method. The countDown method takes a parameter num and decrements it by 1, printing every time until num equals 0. Which of the following loops will make the countDown method compile and work as intended?
// I.
for (int i = num; i > 0; i--)
{
System.out.print (i + " ");
}
// II.
while (num > 0)
{
System.out.print (num + " ");
num --;
}
/// III.
for (int i : num)
{
System.out.print(i + " ");
i --;
}
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.