1/39
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Assume that the BankAccount is a predefined class and that the declaration BankAccount[] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for
1000 reference variables, each of which point to a single BankAccount entry
Which of the following is a legal way to declare and instantiate an array of 10 Strings?
String[] s = new String[10];
The “off-by-one” error associated with arrays arises because
the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
We compare sorting algorithms by examining
the number of instructions executed by the sorting algorithm
Both the Insertion Sort and the Selection Sort algorithms have efficiencies on the order of ____ where n is the number of values in the array being sorted
n²
Consider the array declaration and instantiation: int[] arr = new int[5]; Which of the following is true about arr?
It stores 5 elements with legal indices between 0 and 4
If an int array is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header?
(int[] a)
The following code accomplishes which of the tasks written below? Assume list is an int array that stores positive int values only.
foo = 0;
for (j=0; j<list.length; j++)
if (list[j] > foo) foo = list[j];
it stores the largest value in list (the maximum) in foo
Assume an int array, candy, stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
Which of the following code could be used to compute the total number of bars sold by the children?
for (int j=0; j<12; j++) sum += candy[j];
In Java, arrays are
objects
What does the following code do? Assume list is an array of int values, temp is some previously initialized in value, and c is an int initialized to 0.
for (j=0; j<list.length; j++)
if (list[j]<temp)c++;
It counts the number of elements in list that are less than temp
An int array stores the following values.
Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?
2, 4, 12, 9, 6, 8, 18
If int[] x = new int[15]; and the statement x[-1] = 0; is executed, then which of the following Exceptions is thrown?
ArrayIndexOutOfBoundsException
If x is a char, and values is an int array, then values[x]
casts x as an int based on x’s Unicode value for instance, if x is ‘a’ then it uses 97 and if x is ‘z’ then it uses 122)
To initialize a String array names to store the three Strings “Huey”, “Duey”, and “Louie”, you would do
String[] names = {“Huey”, “Duey”, “Louie”};
To declare a two-dimensional int array called twoD, which of the following would you use?
int[][] twoD;
Which of the following statements loops through every element in the ArrayList alist?
for (Object item: alist)
Which of the following correctly declares an ArrayList of Car objects?
ArrayList cars;
Which of the following expressions gives the last element in the ArrayList alist?
alist.get(alist.size()-1)
Which of the following statements adds item to the end of the ArrayList alist?
alist.add(item);
Assume values is an int array that is currently filled to capacity, with the following values:
What is returned by values[3]?
2
Assume values is an int array that is currently filled to capacity, with the following values:
What is the value of values.length?
7
Assume values is an int array that is currently filled to capacity, with the following values:
Which of the following loops would adequately add 1 to each element stored in values?
for (j=0; j<=values.length;j++) values[j]++;
Assume values is an int array that is currently filled to capacity, with the following values:
The statement System.out.println(values[7]); will
cause an ArrayOutOfBoundsException to be thrown
Which of the following is true with respect to A1, A2 and A3?
A3 is a subclass of A2 and A2 is a subclass of A1
Which of the following lists of instance data are accessible in class A2?
x, z, a, b
The instruction super(); does which of the following?
calls the constructor as defined in the current class’ parent class
Inheritance through an extended (derived) class supports which of the following concepts?
code reuse
All classes in Java are directly or indirectly subclasses of the ____ class.
Object
Which of the following is not a method of the Object class?
compareTo
Which of the following is true regarding Java classes?
All classes must have 1 parent but may have any number of children (derived or extended) classes
A variable declared to be of one class can later reference an extended class of that class. This variable is known as
polymorphic
In order to determine the type that a polymorphic variable refers to, the decision is made
by the Java run-time environment at run time
The relationship between a child class and a parent class is referred to as a(n) ____ relationship.
is-a
Consider the following recursive factorial method:
public int factorial(int x)
{
if (x>1) return x*factorial(x-1);
else return 1;
}
What is returned if factorial(3) is called?
6
Consider the following recursive factorial method:
public int factorial(int x)
{
if (x>1) return x*factorial(x-1);
else return 1;
}
What is returned if factorial(0) is called?
1
Consider the following recursive factorial method:
Consider the following recursive factorial method:
public int factorial(int x)
{
if (x>1) return x*factorial(x-1);
else return 1;
}
How many times is the factorial method invoked if originally called with factorial(5)? Include the original method call in your counting.
5
Consider the following recursive factorial method:
Consider the following recursive factorial method:
public int factorial(int x)
{
if (x>1) return x*factorial(x-1);
else return 1;
}
What condition defines the base case for this method?
(x > 1)
What is wrong with the following recursive sum method? The method is supposed to sum up the values between 1 and x (for instance, sum(5) should be 5+4+3+2+1=15)
public int sum(int x)
{
if(x==0) return 0;
else return sum(x-1)+x;
}
the base condition should be (x<=0) instead of (x==0)
Why is the following method one of which has infinite recursion?
public int infiniteRecursion(int n);
{
if (n>0) return infiniteRecursion(n)+1;
else return 0;
}
Because the recursive call does not move the parameter closer to the base case