Data Structures

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the output of the following code snippet? int[] scores = {80, 92, 91, 68, 88}; for(int score : scores) { System.out.println(score); } A) 80 92 91 68 88 B) 80 92 91 68 88 C) ArrayIndexOutOfBounds exception will be thrown score

A) 80 92 91 68 88 Because as the For Each loop iterates through the code it prints each each value on its own line.

Given the following while loop, which would be the equivalent for loop? int i = 0; while (i < 20) { System.out.print(i); i += 2; } A) for (int i = 0; i < 20; i += 2) { System.out.print(i); } B) for (int i = 0; i < 20; i ++) { System.out.print(i); } C) for (int i = 0; i <= 20; i += 2) { System.out.print(i); } D) for (i = 0; i < 20; i +2 ) { System.out.print(i); }

A) for (int i = 0; i < 20; i += 2) { System.out.print(i); } Remember, the last section of the traditional for loop does not have the semicolon (";").

A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array. Which of the following would be the correct way to print out all the indices that are more than 5 feet above sea level? public static void above5(double[][] array) A) { for(int row = array.length-1; row > 0; row--) { for(int column = 0; column < array[row].length; column++) { if(array[row][column] > 5.0) { System.out.println(row +"," + column); } } } } B) public static void above5(double[][] array) { for(int row = 0; row <= array.length; row++) { for(int column = 0; column < array[row].length; column++) { if(array[row][column] > 5.0) { System.out.println(row +"," + column); } } } } C) public static void above5(double[][] array) { for(int row = 0; row < array.length; row++) { for(int column = 0; column < array[row].length; column++) { if(array[row][column] > 5.0) { System.out.println(array[row][column]); } } } } D) public static void above5(double[][] array) { for(int row = 0; row < array.length; row++) { for(int column = 0; column < array[row].length; column++) { if(array[column] > 5.0) { System.out.println(row +"," + column); } } } }

A) { for(int row = array.length-1; row > 0; row--) { for(int column = 0; column < array[row].length; column++) { if(array[row][column] > 5.0) { System.out.println(row +"," + column); } } } }

What is another name for the Base 2 number system? A) Binary number system B) Octal number system C) Decimal number system D) Hexadecimal number system

A) Binary number system

When would you use a for-each loop instead of a for loop? A) If you want to access every element of an array and want to refer to elements through a variable name instead of an array index. B) If you want to modify elements of the array. C) If you want to iterate over every other element in an array. D) If you want to iterate over only a few elements of an array or collection.

A) If you want to access every element of an array and want to refer to elements through a variable name instead of an array index.

Protocols for data sharing on the internet are? A) rules and conventions for communication between network devices. B) ways to avoid email phishing attempts. C) Ideas for establishing a positive digital footprint. D) Are now considered obsolete with technologies like Web 2.0 and HTML5.

A) rules and conventions for communication between network devices.

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? A) list.add(list.size(), new Dog()); B) for (int i = 0; i <= list.size(); i++) list.set(i, new Dog()); C) Dog dog = list.get(list.size()); D) list.add(-1, new Dog()); E) Dog dog = list.remove(list.size());

A)list.add(list.size(), new Dog()); The index range for ArrayList is 0 ≤ index ≤ size() - 1. So for methods get, set, and remove, the last in-bounds index is size() - 1. The exception is the add method, which adds an element to the end of the list and takes an index parameter list.size().

Given the following code snippet int[ ] values = {150, 34, 320, 2, 11, 100}; for (int i=0; i < values.length; i++) { if (values[i] % 10 == 0) { System.out.println(values[i] + " is divisible by 10"); } } which for-each loop would produce the same output? A) for (value : values) { if (value % 10 == 0) System.out.println(value + " is divisible by 10"); } B) for (int value : values) { if (value % 10 == 0) System.out.println(value + " is divisible by 10"); } C) for (int values : value) { if (value % 10 == 0) System.out.println(value + " is divisible by 10"); } D) for (int value : values) { if (value / 10 == 0) System.out.println(value + " is divisible by 10"); }

B) for (int value : values) { if (value % 10 == 0) System.out.println(value + " is divisible by 10"); } In a For Each loop the term ("value") always comes before the array ("values").

What is the decimal value of the binary number 1101? A) 16 B) 13 C) 26 D) 2

B) 13 1101 = 1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 1*(8) + 1*(4) + 0*(2) + 1*(1) = 8 + 4 + 1 = 13

What data structure might you use to store a list of Shoppers in a grocery line, where the number of people in line changes? A) array B) ArrayList C) 2D Array D) HashMap

B) ArrayList because you can add and subtract terms from an ArrayList, which you cannot do with an array.

How do you create an ArrayList of Strings? A) String[] list = new String[]; B) ArrayList<String> list = new ArrayList<String>(); C) ArrayList<String> list = new ArrayList<String>; D) List<String> list = new List<String>();

B) ArrayList<String> list = new ArrayList<String>(); Always repeat the first section ("ArrayList<String>") and then do not forget the brackets at the end.

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>(); A) I only B) II only C) III only D) II and III E)I, II, and III

B) II only List is an interface, it cannot be instantiated. We cannot make a new List. ArrayList is a class that implements the list interface, so we are able to make a new ArrayList and store it in a List variable.

How do you create an array of 5 ints? A) int[] arr = new int[6]; B) int[] arr = new int[5]; C) int arr = new int[5]; D) int arr = new int;

B) int[] arr = new int[5]; Because the square brackets ("[ ]") signify an array and the count for array ints does not include 0.

Which of the following are some good ways to protect your own personal data? I. Use privacy settings to limit exposure II. Review posts you are tagged in and take action if needed III. Permanently delete old social media accounts you don't use IV. Google yourself on a regular basis V. Use strong passwords Vi. Stay on sites with http:// A)I, II, and III B)I-V all C)IV, V, and VI D)I- VI all

B)I-V all

What will the following code snippet output? int[ ] values = {17, 34, 56, 2, 19, 100}; for (int value : values) { if (value % 2 == 0) System.out.println(value + " is even"); } A) 17 is even 19 is even B) 17 is odd 19 is odd C) 34 is even 56 is even 2 is even 100 is even D) 34 is odd 56 is odd 2 is odd 100 is odd

C) 34 is even 56 is even 2 is even 100 is even

A 2D double array terrainMap is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array. Which of the following would be a correct way to write the traversal for this 2D array? A) for(double[]{} row: terrainMap) { for(double[] num: row) { System.out.print(num + " "); } System.out.println(); } B) for(double[] row: terrainMap) { for(double[] num: row) { System.out.print(num + " "); } System.out.println(); } C) for(double[] row: terrainMap) { for(double num: row) { System.out.print(num + " "); } System.out.println(); } D) for(double[] row: terrainMap) { for(double num: terrainMap) { System.out.print(num + " "); } System.out.println(); }

C) for(double[] row: terrainMap) { for(double num: row) { System.out.print(num + " "); } System.out.println(); }

Which of these methods will properly traverse two ArrayLists and print any index that have the same value in both ArrayLists? A) public void printSharedValues(ArrayList<Integer> array1, ArrayList<E> array2) { int index = 0; while(index < array1.size()) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } } B) public void printSharedValues(ArrayList<Integer> array1, ArrayList<E> array2) { int index = 0; while(index < array1.size()) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index--; } } C) public void printSharedValues(ArrayList<Integer> array1, ArrayList<E> array2) { int index = 0; int size; if(array1.size() > array2.size()) { size = array2.size(); } else { size = array1.size(); } while(index < size) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } } D) public void printSharedValues(ArrayList<Integer> array1, ArrayList<E> array2) { int size; if(array1.size() > array2.size()) { size = array2.size(); } else { size = array1.size(); } while(index < size) { int index = 0; if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } }

C) public void printSharedValues(ArrayList<Integer> array1, ArrayList<E> array2) { int index = 0; int size; if(array1.size() > array2.size()) { size = array2.size(); } else { size = array1.size(); } while(index < size) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } } If one of the ArrayLists is longer than the other, then we need to ensure that we are only evaluating up to the shorter ArrayList. If we iterate through the longer ArrayList, it will cause an IndexOutofBounds exception when trying to access indices in the shorter array.

int[] arr = {1, 2, 3, 4, 5}; int[] copy = arr; copy[4] = 2; After this code, what is the value of arr[4]? A) 5 B) 4 C) 2 D) Error

C) 2 Because the code reassigns the value of the 4th term to 2.

int[][] grid = new int[5][3] What is the value of grid[0].length? A) 0 B) 5 C) 3 D) 15

C) 3

How many digits are there in the Octal (Base 8) number system? A) 2 B) 7 C) 8 D) 10

C) 8

Which of these is a key difference between arrays and ArrayLists? A) Arrays cannot store primitive types but ArrayLists can. B) Arrays are objects but ArrayLists are not objects. C) Arrays are fixed size but ArrayLists can change in size. D) Arrays have extra helper methods like get but ArrayLists do not.

C) Arrays are fixed size but ArrayLists can change in size. Using the ".add" and ".remove" methods you can add or subtract terms from an ArrayList.

A 2D double array is declared and initialized to track the terrain of a city park. Each value in the 2D array represents the height of a particular latitude and longitude above sea level. Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array. The creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - which traversal method would make the most sense in order to do so? A) Row-Major Order B) Enhanced for loop C) Column-Major Order D) This isn't possible given the 2D array.

C) Column-Major Order

A field of computer science that's related to keeping data private and secure called A) The CIA B) Computer Ethics C) Cybersecurity D) Crowdsourcing

C) Cybersecurity

Will this method correctly traverse an ArrayList and remove all multiples of 3? public void remove3s(ArrayList<Integer> array) { int counter = 0; while(counter < array.size()) { if(array.get(counter) %3 == 0) { array.remove(counter); counter++ } else { counter++; } } } A) Yes, this method is written correctly, and will remove all multiples of 3 in an ArrayList. B) No, this method is not written correctly, as the counter in the else statement will skip the next value, as the values will shift down in the ArrayList. C) No, this method will not work because the methods used to access the ArrayList are incorrect. D) No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList.

D) No, this method is not written correctly, as the counter in the if statement will skip the next value, as the values will shift down in the ArrayList. The else statement should have the counter, but the if statement should not. If the values shift down when a value is removed, then the counter should not increase, and the value that is shifted down to counter should be evaluated.

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] A) I only B) II only C) III only D) I and III only E) II and III only

D) I and III only Integer.MIN_VALUE does not work because the test in the loop will always result in false. There's no array element that can be less than the smallest integer.

Some good and practical ways to avoid a phishing attempt are to I. Not click through any links in an email that seem suspicious II. Not download any attachments in an email that seem suspicious III. Not use email services at all IV. Consider the source of each email V. Consider if the email contents sound too good to be true, like you have won a prize or something A) I and II B) IV and V C) I - V all D) I, II, IV and V

D) I, II, IV and V

Which of the following only has beneficial impacts on our society? I. Collaboration II. Communication III. Sharing of information IV. Anonymity A) I and II B) III C) IV D) None of these

D) None of these

What method do you use to add a key-value pair into a HashMap? A) add B) enter C) place D) put

D) put

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? A) The method should be written assuming that there is only one value in the 2D array that is larger than the given item. B) The method should be written to output a message whenever more than one larger value is found. C) The method should be written to return the index of every occurrence of a larger value. D) After the first suitable index is returned, the method should be written to delete all later larger items. E) The specification should be modified to specify what should be done if there is more than one index with larger values.

E) The specification should be modified to specify what should be done if there is more than one index with larger values.

True or False: You can modify the elements of an array when you traverse it with a for-each loop.

False


Set pelajaran terkait

African kingdoms, the Atlantic slave trade, & the origins of black america

View Set

U.S. History Chapters 25, 26, 27, & 28 Terms and Review Questions

View Set

Modern Principles of Economics (1)

View Set