Computer Science: Application Multiple Choice
1. What data structure might you use to store a list of Shoppers in a grocery line, where the number of people in line changes?
|
array |
|
ArrayList |
|
2D array |
|
HashMap |
2. How do you create an array of 5 ints?
|
int[] arr = new int[6]; |
|
int[] arr = new int[5]; |
|
int arr = new int[5]; |
|
int arr = new int; |
3.
int[] arr = {1, 2, 3, 4, 5}; int[] copy = arr; copy[4] = 2;
After this code, what is the value of arr[4]?
|
5 |
|
4 |
|
2 |
|
This code will error. |
4. Consider the following method that processes an array to find the smallest value in the array. The array has a nonzero length and is initialized with int values.
// arr is the array to be processed
public static int findMin (int[] arr)
{
int min = /* some value */;
int index = 0;
while (index < arr.length)
{
if (arr[index] < min)
min = arr[index];
index++;
}
return min;
}
Which replacement(s) for /* some value */ will always result in correct execution of findMin? I.
Integer.MAX_VALUE
II.
Integer.MIN_VALUE
III.
arr[0]
|
I only |
|
II only |
|
III only |
|
I and III only |
|
II and III only |
5. How do you create an ArrayList of Strings?
|
String[] list = new String[]; |
|
ArrayList<String> list = new ArrayList<String>(); |
|
ArrayList<String> list = new ArrayList<String>; |
|
List<String> list = new List<String>(); |
6. Which of these is a key difference between arrays and ArrayLists?
|
Arrays cannot store primitive types but ArrayLists can. |
|
Arrays are objects but ArrayLists are not objects. |
|
Arrays are fixed size but ArrayLists can change in size. |
|
Arrays have extra helper methods like get but ArrayLists do not. |
7. Consider a Dog class that has a default constructor. Suppose a list ArrayList<Dog> is initialized. Which of the following will not cause an IndexOutOfBoundsException to be thrown?
|
list.add(list.size(), new Dog()); |
|
for (int i = 0; i <= list.size(); i++) list.set(i, new Dog()); |
|
Dog dog = list.get(list.size()); |
|
list.add(-1, new Dog()); |
|
Dog dog = list.remove(list.size()); |
8. Which of the following are valid statements in Java? I:
ArrayList<String> list = new List<String>();
II:
List<String> list = new ArrayList<String>();
III:
List<String> list = new List<String>();
|
I only |
|
II only |
|
III only |
|
II and III |
|
I, II, and III |
9.
int[][] grid = new int[5][3]
What is the value of grid[0].length?
|
0 |
|
5 |
|
3 |
|
15 |
10. A method is to be written to search a 2D array for a value that is larger than a given item and return its index. The problem specification does not indicate what should be returned if there are several values that are larger in the 2D array. Which of the following would be the best course of action?
|
The method should be written assuming that there is only one value in the 2D array that is larger than the given item. |
|
The method should be written to output a message whenever more than one larger value is found. |
|
The method should be written to return the index of every occurrence of a larger value. |
|
After the first suitable index is returned, the method should be written to delete all later larger items. |
|
The specification should be modified to specify what should be done if there is more than one index with larger values. |
11. What method do you use to add a key-value pair into a HashMap?
|
add |
|
enter |
|
place |
|
put |
12. What is another name for the Base 2 number system?
|
Binary number system |
|
Octal number system |
|
Decimal number system |
|
Hexadecimal number system |
13. How many digits are there in the Octal (Base 8) number system?
|
2 |
|
7 |
|
8 |
|
10 |
14. What is the decimal value of the binary number 1101?
|
16 |
|
13 |
|
26 |
|
2 |
15. How many possible values can be created with only 3 bits?
|
3 |
|
6 |
|
8 |
|
12 |
16. Since ints are represented by 32 bits, they can only represent a certain range of values. This range goes from Integer.MIN_VALUE to Integer.MAX_VALUE. What happens when we perform an operation tries to store a value outside of this range?
int x = Integer.MIN_VALUE - 1;
What is the value of x?
|
0 |
|
Integer.MAX_VALUE |
|
Integer.MIN_VALUE |
|
This line of code will cause an error |
17. The color of a pixel can be represented using the RGB (Red, Green, Blue) color model, which stores values for red, green, and blue (each ranging from 0 to 255). How many bits (binary digits) would be needed to represent a color in the RGB model?
|
24 |
|
8 |
|
40 |
|
16 |
|
32 |
18. Suppose the characters 0, 1, …, 8, 9, A, B, C, D, E, F are used to represent a hexadecimal (base-16) number, where A = 10, B = 1,…F = 15. What is the largest base-10 integer that can be represented with a two-digit hexadecimal number, such as 17 or 9F?
|
The values of two-digit hexadecimal numbers are unbounded |
|
256 |
|
255 |
|
32 |
|
99 |
19. Consider the following class for questions 1 and 2.
public class ArrayTester
{
private int[] testArray = {7, 8, 9, 10};
public void decrement (int num)
{ num--;}
public void testMethod1()
{
for (int i = 0; i < testArray.length; i++)
{
decrement (testArray[i]);
System.out.print(testArray[i] + “ “);
}
}
public void testMethod2()
{
for (int element : testArray)
{
decrement(element);
System.out.println(decrement + “ “);
}
}
}
What output will be produced by calling testMethod1 for an ArrayTester object?
|
There will be no output. It will cause an ArrayIndexOutOfBoundsException. |
|
7 8 9 10 |
|
8 9 10 11 |
|
9 10 11 12 |
|
0 0 0 |
20. Consider the ArrayTester class from question 1. What output will be produced by calling testMethod2 for an ArrayTester object?
|
There will be no output. It will cause an ArrayIndexOutOfBoundsException. |
|
7 8 9 10 |
|
8 9 10 11 |
|
9 10 11 12 |
|
0 0 0 |
21. Which of the following arrays has a length of 5?
|
double[] arr = new double[6]; |
|
double[] arr = {0, 1, 2, 3, 4, 5}; |
|
double[] arr = new double[]{1, 2.3, 4, 5}; |
|
All of the above |
|
None of the above |
22. Which of the following are valid arrays?
I. int[] coolArray = {1, 2, 3};
II. int[] threeThings = {1.0, 2.0, 3.0};
III. int[] = {"1", "2", "3"};
|
I only |
|
II only |
|
III only |
|
I and II |
|
I and III |
23. What is the output of the following program?
public class SayHello
{
public void run()
{
String stringArray[] = {"h", "e", "l", "l", "o", "w"};
for(int i=0; i <= stringArray.length; i++)
{
System.out.print(stringArray[i]);
}
}
}
|
hel |
|
hello |
|
hellow |
|
Error |
|
None of the above |
24. When run, which of the following yields the integer 5?
|
int[] newArray = {5, 5, 5, 5, 5, 5}; System.out.println(newArray.length); |
|
String pets[] = new String[5]; System.out.println(pets.length()); |
|
int[] anotherArray = {5, 10, 15, 20, 25}; System.out.println(anotherArray[(anotherArray.length - 2)] - 15); |
|
String[] favoriteNumbers = {"5", "7", "12", "13", "5"}; System.out.println(favoriteNumbers[favoriteNumbers[0]]); |
|
int[] greatArray = {5, 4, 3, 2, 1, 0}; System.out.println(greatArray.get(0)); |
25. What values will the array contain after the following the code is run?
int[] someArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
for (int i = someArray.length - 2; i >= 0; i-=2)
{
someArray[i] = someArray[i] - 5;
}
|
{6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4} |
|
{1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11} |
|
{1, 3, 5, 7, 9, 7, 5, 3, 1, -1, -3} |
|
{-4, 1, -2, 3, -4, 5, -6, 7, -8, 9} |
26. You are working at the frozen yogurt shop in the mall. Your boss asks you to count how many flavors have the word chocolate in their name. You begin going through the list of flavors, but quickly realize that their are over 9000 flavors to check! Luckily, your boss stored all of the different flavors in a Java Array named flavorArray on the company computer. Write a Java program to count the number of flavors that have chocolate in their name.
|
int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i++) { flavorArray[i].contains("chocolate"); } System.out.println(chocolateFlavorCount); |
|
int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i--) { if(flavorArray[i].contains("chocolate")) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount); |
|
int chocolateFlavorCount = 0; while(flavorArray.length) { if(flavorArray.contains("chocolate")) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount); |
|
int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i++) { if(flavorArray[i].contains("chocolate")) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount); |
27. What will this program output?
int mysteryNumber = 0;
String[] mysteryArray = {"Finn", "Jake", "Bubblegum"};
for(int i = 0; i < mysteryArray.length; i++)
{
mysteryNumber += mysteryArray[i].length();
}
|
0 |
|
17 |
|
1 |
|
16 |
28. What will the Array gameBoard contain after this code runs?
int WIDTH = 3;
int HEIGHT = 3;
String[] gameBoard = new String[WIDTH * HEIGHT];
for(int m = 0; m < HEIGHT; m++)
{
for(int n = 0; n < WIDTH; n++) {
int someNumber = m * WIDTH + n;
if(someNumber % 3 == 0)
{
gameBoard[someNumber] = "X";
}
else
{
gameBoard[someNumber] = "O";
}
}
}
|
["X", "O", "X", "O", "X", "O", "X", "O", "X"] |
|
["X", "O", "O", "X", "O", "O", "X", "O", "O"] |
|
["O", "X", "X", "O", "X", "X", "O", "X", "X"] |
|
["O", "X", "O", "X", "O", "X", "O", "X", "O"] |
29. What would be a correct way to instantiate this 2D array?
|
int[][] table = { 1, 0, 10, 0, 3, 8, 38, 0 }; |
|
int[][] table = { {1, 0, 10, 0}, {3, 8, 38, 0} }; |
|
int[][] table = { {1}, {0}, {10}, {0}, {3}, {8}, {38}, {0} }; |
|
int[][] table = { "1, 0, 10, 0", "3, 8, 38, 0" }; |
|
int[][] table = new int[1, 0, 10, 0][3, 8, 38, 0]; |
30. Which is the correct way to construct and assign a 2D array, with 8 rows and 10 columns, to the variable popcorn?
|
int[8][10] popcorn; |
|
int[8][10] popcorn = new int[8][10]; |
|
int[][] popcorn = new int[8][10]; |
|
int[8][10] popcorn = new int[][]; |
|
int[][] popcorn = new int[][](8, 10); |
31. We want to create a 2D double array with 6 rows and 7 columns and assign it to connectFour. Which of these is correct?
|
double[][] connectFour = new double[6][7] |
|
double[][] connectFour = new connectFour[6][7] |
|
double[][] connectFour = double[6][7] |
|
new double[6][7] connectFour |
|
double[6][7] connectFour = new double[][] |
32. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[1][2]?
|
6.8 |
|
6.1 |
|
2.3 |
|
1.3 |
|
The array does not have an element at that location. |
33. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[2][1]?
|
6.8 |
|
6.1 |
|
2.3 |
|
1.3 |
|
The array does not have an element at that location. |
34. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something.length?
|
5 |
|
4 |
|
3 |
|
12 |
|
8 |
35. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
What is the value of something[2].length?
|
5 |
|
4 |
|
3 |
|
12 |
|
8 |
36. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
How do you replace the value 13.21 with 8.8?
|
something[2][3] = 8.8; |
|
something[2][4] = 8.8; |
|
something[13.21] = 8.8; |
|
something[3][4] = 8.8; |
|
something[3][3] = 8.8; |
37. Given the following:
double[][] something =
{ {2.5, 6.8, 8.3, 2.3, 0.0},
{6.1, 10.2, 1.3, -2.5, -9.9},
{1.1, 2.3, 5.8, 13.21, 34.55} };
We want to replace the row {6.1, 10.2, 1.3, -2.5, -9.9} with a complete new array that looks like {3.1, 4.1, 5.9, 2.6, 8.4}.
|
double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[2] = temp; |
|
something[1] = {3.1, 4.1, 5.9, 2.6, 8.4}; |
|
something[2] = new {3.1, 4.1, 5.9, 2.6, 8.4}; |
|
double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[1] = temp; |
|
something[1][0] = 3.1; something[1][1] = 4.1; something[1][2] = 5.9; something[1][3] = 2.6; something[1][4] = 8.4; |
38. Given the following:
String[][] poem =
{ {"I", "am", "the", "cookie", "monster."},
{"Would", "you", "like", "a", "cookie?"},
{"COOOOKIE", "OM", "NOM", "NOM", "NOM"} }
Which of the following code fragments would produce the following output?
I am the cookie monster.
Would you like a cookie?
COOOOKIE OM NOM NOM NOM.
|
for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem.length; word++) { System.out.print(poem[line][word] + " "); } System.out.println() } |
|
for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[word].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println() } |
|
for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[line][word] + " "); } System.out.println() } |
|
for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; word++) { System.out.print(poem[word][line] + " "); } System.out.println() } |
|
for (int line = 0; line < poem.length; line++) { for (int word = 0; word < poem[line].length; line++) { System.out.println(poem[line][word] + " "); } System.out.println() } |
39. What is the proper way to get the number of items in an ArrayList called list?
|
list.length |
|
list.size |
|
list.length() |
|
list.size() |
40. What will the following code print?
ArrayList<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
System.out.println(list[0]);
|
"Hello" |
|
"World" |
|
"list[0]" |
|
There will be a compiler error |
|
None of the above |
41. What is wrong with the following code?
ArrayList<int> list = new ArrayList<int>();
list.add(1);
System.out.println(list.get(0));
|
It will throw an IndexOutOfBoundsException. |
|
You cannot use int as the type in an ArrayList. |
|
list is a reserved word, so you can’t call the variable that. |
|
You need to define the new ArrayList on a new line of code. |
|
Nothing. The above code is correct. |
42. What will the following code print?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
int sum = 0;
for (int i = 0; i < list.size(); i+= 2)
{
sum += list.get(i);
}
System.out.println(sum);
|
0 |
|
5 |
|
6 |
|
9 |
|
15 |
43. What will the following code print?
ArrayList<String> list = new ArrayList<String>();
list.add("I");
list.add("love");
list.add("coding");
list.add("in");
list.add("Java");
list.remove(3);
System.out.println(list.get(3));
|
3 |
|
coding |
|
in |
|
Java |
|
It will throw an IndexOutOfBoundsException. |
44. Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?
|
128 |
|
256 |
|
4 |
|
8 |
|
16 |
45. What is the precondition for binary search to work on an array?
|
The array must be sorted. |
|
The array must contain only integers. |
|
The array must be of even size. |
|
The array must be of odd size. |
|
The element being searched for must be in the array. |
46. How many comparisons will the selection sort algorithm make on an array of 8 elements?
|
3 |
|
8 |
|
24 |
|
28 |
|
64 |
47. The following array is to be sorted biggest to smallest using insertion sort.
[10, 40, 50, 30, 20, 60]
What will the array look like after the third pass of the for loop?
|
[60, 50, 40, 30, 20, 10] |
|
[50, 40, 10, 30, 20, 60] |
|
[50, 40, 30, 10, 20, 60] |
|
[60, 50, 40, 30, 10, 20] |
|
[10, 30, 40, 50, 20, 60] |
48. When will method myMethod cause a stack overflow (i.e., cause computer memory to be exhausted)?
public static int myMethod (int y, int z)
{
if (y > z)
return y * z;
else
return myMethod(y - 2, z);
}
|
The method will never cause a stack overflow |
|
For all values y and z |
|
Only when y = 2 |
|
Only when y ≤ z |
|
Only when y > z |