Unit 5 Test Quizlet
Question: 14 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
3
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
Error. The data is out of bounds.
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.
Java
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[][];
double[][] connectFour = new double[6][7];
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;
something[2][3] = 8.8;
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.
1.3
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. The way the other 2 are typed out do not work as arrays.
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
There will be a compiler error
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);
int[][] popcorn = new int[8][10];
What would be a correct way to instantiate this 2D array? 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 = { "1, 0, 10, 0", "3, 8, 38, 0" }; int[][] table = new int[1, 0, 10, 0][3, 8, 38, 0];
int[][] table = { {1, 0, 10, 0}, {3, 8, 38, 0} };
What is the proper way to get the number of items in an ArrayList called list? list.length list.size list.length() list.size()
list.size()
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}
{1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}
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
17
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.
2.3
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
5
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
6
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
None of the above, none of them have a length of 5
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.
You cannot use int as the type in an ArrayList.
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"]
["X", "O", "O", "X", "O", "O", "X", "O", "O"]
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;
double[] temp = {3.1, 4.1, 5.9, 2.6, 8.4}; something[1] = temp;
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();
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();
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);
int chocolateFlavorCount = 0; for(int i = 0; i < flavorArray.length; i++) { if(flavorArray[i].contains("chocolate")) { chocolateFlavorCount++; } } System.out.println(chocolateFlavorCount);
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));
int[] anotherArray = {5, 10, 15, 20, 25}; System.out.println(anotherArray[(anotherArray.length - 2)] - 15);. Length is 5.