QUIZ 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

1

Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet?

30

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

5

How many elements can be stored in an array of dimension 2 by 3?

6

What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty());

6 true 1 false

Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum?

ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

What should you check for when calculating the smallest value in an array list?

The array list contains at least one element.

Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet?

The code snippet compares the values of two array lists and stores the count of total matches found.

What will be printed by the statements below? ArrayList<String> names = new ArrayList<String>(); names.add("Annie"); names.add("Bob"); names.add("Charles"); for (int i = 0; i < 3; i++) { String extra = names.get(i); names.add (extra); } System.out.print (names);

[Annie, Bob, Charles, Annie, Bob, Charles]

What will be printed by the statements below? ArrayList<String> names = new ArrayList<String>(); names.add("Annie"); names.add("Bob"); names.add("Charles"); names.set(2, "Doug"); names.add(0, "Evelyn"); System.out.print (names);

[Evelyn, Annie, Bob, Doug]

Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); }

for (String str : arr)

Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList?

int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } }

Which one of the following is the correct definition for initializing data in a two-dimensional array with three rows and two columns?

int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

Which one of the following statements is the correct declaration for a two-dimensional array of 20 rows and 2 columns of type int?

int[][] num = new int[20][2];

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int val: values) { if (/* Put condition here */) max = val; }

val > max


Ensembles d'études connexes

ECON 315 Macroeconomic theory HW #2 part 1

View Set

Insurance- Missed practice exam questions

View Set

Chapter 2 Origins of Government Test

View Set

CYBERSECURITY CHAPTER 1 QUIZ STUDY GUIDE

View Set

NCLEX PN Infancy to Adolescence Review

View Set