HW Week 10

Ace your homework & exams now with Quizwiz!

7) 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);} A. for (string str : arr) B. for (str : String arr) C. for (str[] : arr) D. for (arr[] : str)

A. for (string str : arr)

10) 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]

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

values[current] > max

16) Arrays:

- Groups together variables of the same data type. - Reference to every variable is done with a single name - Each member is accessed with an index

1) 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]); } 1345 2345 1234 1111

2345

11) 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

6) What should you check for when calculating the largest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the minimum value in the first element. d) The array list contains the maximum value in the first element.

A

17) Method:

A sequence of instructions that performs a task. A basic programming unit in Java. Its basic elements are: a complete section of code a start point and an end point own set of variables modifier(s) a return statement list of arguments a return type a name

12) Which 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)); } }

15) 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.

13) 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]

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

4) Which of the following code snippets will generate a random number between 0 (inclusive) and 79 (inclusive)? (Assume Random generator = new Random();.) int val= generator.nextInt(79); int val= generator.nextInt(79) + 1; int val = generator. nextInt(80); int val = generator.nextInt(80) - 1;

int val = generator.nextInt(80);

5) Which one of the following statements is a valid initialization of ~an array named somearray of ten elements~? int [10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

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

14) Suppose you would like to initialize the two dimensional array mat so that it represents the matrix below: 12345 67891 23456 How would you declare mat? int[][]mat= new {{1,2,3,4,5},{6,7,8,9,1},{2,3,4,5,6}}; int[][]mat= {{1,2,3,4,5}{6,7,8,9,1}{2,3,4,5,6}}; none of the options are correct int[][] mat = {{1,2,3,4,5},{6,7,8,9,1},{2,3,4,5,6}};

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

3) Assume the variable numbers has been declared to be an array that has at least one element. Which is the following represents the last element in numbers? numbers.length numbers[numbers.length] numbers[numbers.length - 1] numbers[-1]

numbers[numbers.length - 1]

18) Return:

return type: the data type of the value returned from the method (void: no value will be returned) returning values: The value is returned to the calling method in the return statement.

9) 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 (/condition/) max = val; }

val > max


Related study sets

Introduction and the Importance of Listening in Our Lives Week 4

View Set

Insurance - Practice TEST Review

View Set

Principles of Microeconomics (Ch. 9,11,12,13)

View Set

Human Diseases Final Study Guide

View Set

Business Law Chapter 2 Multiple Choice

View Set

SURNAME, FIRST NAME INITIAL.MIDDLE NAME INITIAL (DATE). TITLE: Subtitle (ed). PublisherEnglish

View Set