Array and ArrayList - Chapter 7

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

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++): } }

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

2345

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

30

What is the output of the following code snippet? int [] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.println(myarray[3]);

3040

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

6

What is the output of the code snippet below? int [][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 3, 2, 1, 3 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val);

6

What will be printed by the statements below? int [] values = { 4, 5, 6, 7 }; values[0] = values[3]; values[3] = values[0]; for (int i = 0; i < values.length; i++) { System.out.print(values[i] + " "); }

7 5 6 7

Your program needs to store an integer sequence of unknown length. Which of the following is most suitable to use?

A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();

When the order of the elements is unimportant, what is the most efficient way to remove an element from an array?

Replace the element to be deleted with the last element in the array.

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

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); names.add("Harry");

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

It may be necessary to "grow" an array when reading inputs because

The number of inputs may not be known in advance.

Consider the following code snippet: String [] data = { "abc", "def", "ghi", "jki" }; String [] data2; Which statement copies the data array to the data2 array?

data2 = Arrays.copyOf(data, data.length);

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.println(str); }

for (String str: arr)

Identify the correct statement for defining an integer array named numarray of ten elements.

int [] numarray = new int[10];

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 = sum + values[i]; }

The enhanced for loop is

is convenient for traversing all elements in an array.

When an array myArray is only partially filled, how can the programmer keep track of the current number of elements?

maintain a companion variable that stores the current number of elements

Assume the variable numbers has been declared to be an array that has at least one element. Which of the following represents the last element in numbers?

numbers[numbers.length - 1]

The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list?

x = a.get(4);


Set pelajaran terkait

Business Math Cumulative Exam 1a

View Set

1828- ANDREW JACKSON AND JOHN CALHOUN

View Set

Cryptography Applications - public key infrastructure

View Set

chap 6 Nursing Care of Mother and Infant During Labor and Birth

View Set

Human Body (Laymans term/ Anatomical term)

View Set

Anatomy and Physiology: Case Studies

View Set