Chp 7 Reading Quiz

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How would you access the middle box on a tic-tac-toe board stored in a two-dimensional array named board? a board[1,1] b board[2][2] c board(1, 1) d board[1][1]

board[1][1]

Rewrite the following traditional for loop header as an enhanced for loop, assuming that ArrayList<String> names has been initialized. for (int i = 0; i < names.size(); i++) { // code that processes the names array } a for (names : String s) b for (s : names) c for (String s : names) d for (String names: s)

for (String s : names)

Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; a int cols = counts.length[2]; b int cols = counts[2].length(); c int cols = counts[2].size(); d int cols = counts[2].length;

int cols = counts[2].length;

Which statement declares a variable that references an array of integers? a int[6] intData = new int[]; b int intData[] = new int[6]; c int[] intData = int[6]; d int() intData = new int(6);

int intData[] = new int[6];

Which code snippet calculates the sum of all the elements in even positions in an array? a int sum = 0; for (int i = 0; i < values.length(); i = i + 2) { sum += values[i]; } b int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum += values[i]; } c int sum = 0; for (int i = 0; i < values.length-1; i+=2) { sum += values[i]; } d int sum = 0; for (int i = 0; i < values.length; i+=2) { sum++; }

int sum = 0; for (int i = 0; i < values.length; i = i + 2) { sum += values[i]; }

Which code snippet calculates the sum of all the even numbers stored in an array values? a int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) != 0) { sum += values[i]; } } b int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } } c int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) = 0) { sum += values[i]; } } d int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } }

int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } }

Which statement replaces the third element in an array list named players? a players[2] = "Sally"; b players.set("Sally", 2); c players.set(2, "Sally"); d players.set(3, "Sally");

players.set(2, "Sally");

Which expression yields the number of elements in an array list named sData? a sData.size b sData.length() c sData.size() d sData.length

sData.size()

What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } } Answer:

1

The code below yields a two-dimensional array with elements. (How many?) final int ROWS = 4; final int COLUMNS = 3; String[][] board = new String[ROWS][COLUMNS];

12

What is the output of the following code fragment, assuming that the array dataValues has been initialized to contain the sequence: 2, 4, 6, 8, 10 ? for (int d: dataValues) { d = d * 2; } System.out.println(dataValues[0]); Answer:

2

What is the output of the given code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); } Answer:

2345

Consider the following code snippet: What is the value of numarray[1][2] after the code snippet is executed? int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } Answer:

5

Consider the following code. What will be output if the user enters 'E' ? char letters[] = {'F', 'A', 'V', 'E', 'S', 'U', 'N', 'E', 'B', 'U', 'W', 'G'}; char what = //user enters a letter int location = -1; for (int i = 0; i < letters.length; i++) { if ( what == letters[i]) location = i; } System.out.println(location); Answer:

7

What is the value of myArray[1][2] after this code snippet is executed? int count = 0; int[][] myArray = new int[4][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { myArray[j][i] = count; count++; } } Answer:

9

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); } a Cal, Bob, Tony b Ann, Tony c Ann, Bob d Array list bound error e Cal, Bob, Ann

Ann, Tony

Which code fragment constructs an arrray list named players that is initialized to contain the strings "Player 1" and "Player 2"? a ArrayList<String> players = new ArrayList<String>(); players(0) = "Player 1"; players(1) = "Player 2"; b ArrayList<String> players = new ArrayList<String>(); players.set(0, "Player 1"); players.set(1, "Player 2"); c ArrayList<String> players = {"Player 1", "Player 2"}; d ArrayList<String> players = new ArrayList<String>(); players.add("Player 1"); players.add("Player 2");

ArrayList<String> players = new ArrayList<String>(); players.add("Player 1"); players.add("Player 2");

What is the purpose of the algorithm below? double s=0; for (int d: data) { s = s + Math.pow(d,2); } System.out.println("s = " + s); a Calculate the sum of the squares of the elements of the array. b Double each element of the array and calculate their sum. c Calculate the sum of the elements of the array. d Nothing; the array can't be modified

Calculate the sum of the squares of the elements of the array.

A method cannot return an array. True False

False

The statement below is a valid array list declaration: ArrayList<double> values = new ArrayList<double>(); True False

False

When storing a large data set of numbers it is more efficient to use an array list than an array. True False

False

Which statement(s) are true about enhanced for loops? I. An enhanced for loop is convenient for traversing all elements of an array list. II. An enhanced for loop can only traverse array lists. III. An enhanced for loop is convenient for traversing a partially filled array. IV. An enhanced for loop cannot be used to modify the elements of an array. a I only b All statements are true c I and IV d I and II

I and IV

Which statement(s) about the size of a Java array, array list, and string are true? I. The syntax for determining the size of an array, an array list, and a string in Java is consistent among the three II. The string uses s.size(), while the array list uses a.length() III. The array uses a.length, which is not a method call a I only b II only c III only d None are true

III only

Which statement accesses the first element of an array list of strings named sData? a String s = sData[0]; b String s = sData.get(0); c String s = sData.get(1); d String s = sData.get[0];

String s = sData.get(0);

Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array? a System.out.println(arr[2][3]); b System.out.println(arr[2][1]); c System.out.println(arr[3][2]); d System.out.println(arr[1][2]);

System.out.println(arr[1][2]);

What is the result of executing this code snippet? int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); } a The code snippet causes a bounds error. b The code snippet displays all the integers stored in the array. c The code snippet executes an infinite loop. d The code snippet does not compile.

The code snippet causes a bounds error.

Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry"); a The final size of names is 3; the final size of friends is 3 b The final size of names is 3; the final size of friends is 2 c The final size of names is 2; the final size of friends is 3 d Compilation error

The final size of names is 2; the final size of friends is 3

Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry"); a The final size of names is 3; the final size of friends is 2 b The final size of names is 3; the final size of friends is 3 c The final size of names is 2; the final size of friends is 3 d Compilation error

The final size of names is 3; the final size of friends is 3

What does this loop compute? double b = accounts.get(0).getBalance(); for (int i = 1; i < accounts.size(); i++) { BankAccount a = accounts.get(i); if (a.getBalance() < b) b = a.getBalance(); } return b; a The bank account with the largest balance. b The value of the smallest balance. c The bank account with the smallest balance. d The value of the largest balance.

The value of the smallest balance.

What is the most significant disadvantage of using arrays? a Index values range from 0 to length + 1 b You cannot copy values from one array to another c Their length is fixed d You cannot determine the number of elements in the array

Their length is fixed

An array list passed into a method as a parameter can be modified inside the method. True False

True

The statement below is a valid array list declaration: ArrayList<Integer> values = new ArrayList<Integer>(); True False

True

When you are searching an array list for a specific item(s) to remove, the recommended approach is: a Use an enhanced for loop and traverse from the end to the beginning of the array list. b Use a traditional for loop and traverse from the end to the beginning of the array list. c Use a traditional for loop and traverse from the beginning to the end of the array list. d Use an enhanced for loop and traverse from the beginning to the end of the array list.

Use a traditional for loop and traverse from the end to the beginning of the array list.

What is the output of the following code snippet? public static void main(String[] args) { String[] arr = { "aaa", "bbb", "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery(String[] arr) { arr = new String[5]; arr[0] = "ddd"; } a aaa 5 b ddd 3 c aaa 3 d ddd 5

aaa 3


संबंधित स्टडी सेट्स

Disease Mechanisms II Exam 2 Material Part 1

View Set

ARE PcM 5.0: Practice Management

View Set

MGT 3830 Chapter 3 study material 65

View Set

ch2, Chapter 1: Project Mangement

View Set

CH 4 - Accrual Accounting Concepts

View Set