Final COP 2210

Ace your homework & exams now with Quizwiz!

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

The array list contains at least one element.

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 current = 1; current < values.length; current++) { if (/* Put condition here */) max = values[current]; }

values[current] > max

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]); }

2345

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

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());

6true 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)); } }

Consider the following line of code: int[] some array = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of some array?

Consider the following line of code: int[] some array = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of some array?

Which one of the following statements is correct for the given code snippet? int[] number = new int[3]; // Line 1 number[3] = 5; // Line 2

Line 2 causes a bounds error because 3 is an invalid index number.

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]

Which code snippet calculates the sum of all the elements in even positions in an array?

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

Suppose you would like to initialize the two dimensional array mat so that it represents the matrix below: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 How would you declare mat?

int[][] mat = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 1}, {2, 3, 4, 5, 6}};

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


Related study sets

Medical Abbreviations as tol - Bx

View Set

Proving Angles Congruent Practice

View Set

AES - Getting Started with Google Applications

View Set

Java Software Structures:: Chapter 11: Binary Search Trees

View Set

Microbial Motility and Chemotaxis

View Set

CE Edge Prep - Property Valuation and Financial Analysis

View Set

Chapter 17: The Enlightenment & the American Revolution

View Set