This book is now obsolete Please use CSAwesome instead.
8.9. Easy Multiple Choice QuestionsΒΆ
These problems are easier than most of those that you will usually see on the AP CS A exam.
nums.length
- Since the first element in an array is at index 0 the last element is the length minus 1.
nums.length - 1
- Since the first element in an array is at index 0 the last element is the length minus 1.
7-9-1: Which index is the last element in an array called nums
at?
int[] scores = null;
- You can initialize an array reference to null to show that it doesn't refer to any array yet.
int[] scoreArray = {50,90,85};
- You can provide the values for an array when you declare it.
String[] nameArray = new String[10];
- You can declare and array and create the array using the
new
operator in the same statement. String[] nameArray = {5, 3, 2};
- You can not put integers into an array of String objects.
int[] scores = new int[5];
- You can declare and array and create it in the same statement. Use the
new
operator to create the array and specify the size in square brackets.
7-9-2: Which of the following declarations will cause a compile time error?
- 1
- This would be returned from
arr[2]
. - 2
- This returns the value in
arr
at index 3. Remember that the first item in an array is at index 0. - 3
- This would be returned from
arr[1]
. - 6
- This would be returned from
arr[0]
. - 4
- This would be returned from
arr.length
7-9-3: What is returned from arr[3]
if arr={6, 3, 1, 2}
?
- 17.5
- This would be true if the loop stopped at
arr.length - 1
. - 30.0
- This would be true if the loop started at 1 instead of 0.
- 130
- This would be true if it returned
output
rather thanoutput / arr.length
- 32
- This would be true if
output
was declared to be an int rather than a double. - 32.5
- This sums all the values in the array and then returns the sum divided by the number of items in the array. This is the average.
7-9-4: What is returned from mystery
when it is passed {10, 30, 30, 60}
?
public static double mystery(int[] arr)
{
double output = 0;
for (int i = 0; i < arr.length; i++)
{
output = output + arr[i];
}
return output / arr.length;
}
You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-4.
- {-20, -10, 2, 8, 16, 60}
- This would true if it looped through the whole array. Does it?
- {-20, -10, 2, 4, 8, 30}
- This would be true if it looped from the beginning to the middle. Does it?
- {-10, -5, 1, 8, 16, 60}
- It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
- {-10, -5, 1, 4, 8, 30}
- This would be true if array elements didn't change, but they do.
7-9-5: Given the following values of a
and the method doubleLast
what will the values of a
be after you execute: doubleLast()
?
private int[ ] a = {-10, -5, 1, 4, 8, 30};
public void doubleLast()
{
for (int i = a.length / 2; i < a.length; i++)
{
a[i] = a[i] * 2;
}
}
You can step through the code above using the Java Visualizer by clicking on the following link Prob-7-9-5.
- {1, 3, -5, -2}
- This would be true if the contents of arrays could not be changed but they can.
- {3, 9, -15, -6}
- This code multiplies each value in a by the passed amt which is 3 in this case.
- {2, 6, -10, -4}
- This would be correct if we called multAll(2) instead of multAll(3).
- The code will never stop executing due to an infinite loop
- The variable i starts at 0 and increments each time through the loop and stops when it equals the number of items in a.
7-9-6: What are the values in a after multAll(3) executes?
private int[ ] a = {1, 3, -5, -2};
public void multAll(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
i++;
} // end while
} // end method
- {1, 3, -5, -2}
- Does the value of i ever change inside the loop?
- {3, 9, -15, -6}
- Does the value of i ever change inside the loop?
- {2, 6, -10, -4}
- Does the value of i ever change inside the loop?
- The code will never stop executing due to an infinite loop
- The value of i is initialized to 0 and then never changes inside the body of the loop, so this loop will never stop. It is an infinite loop.
7-9-7: What are the values in a after mult(2) executes?
private int[ ] a = {1, 3, -5, -2};
public void mult(int amt)
{
int i = 0;
while (i < a.length)
{
a[i] = a[i] * amt;
} // end while
} // end method