CSC ch 6 arrays

¡Supera tus tareas y exámenes ahora con Quizwiz!

When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs: casting trimming auto-boxing auto-fixing

auto-boxing

Which one of the following code snippets accepts the integer input in an array named num1 and stores the odd integers of num1 in another array named oddnum? a b c d

b

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

b

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

b

Which code snippet finds the largest value in an array that is only partially full? a b c d

c

Which code snippet calculates the sum of all the elements in even positions in an array? a b c d

d

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement grows the data array to twice its size? data = Arrays.copyOf(data, 2 * data.length); data = Arrays.copyOf(data, 2); data = Arrays.copyOf(data, 2 * data); data = Arrays.copyOf(data, 2 * data.size());

data = Arrays.copyOf(data, 2 * data.length);

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" };String [] data2; In Java 6 and later, which statement copies the data array to the data2 array? data2 = Arrays.copyOf(data, data2.length); data2 = Arrays.copyOf(data, data.length); data2 = Arrays.copyOf(data, data.size()); data2 = Arrays.copyOf(data);

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

Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution? structure a program using methods adapt a built-in array algorithm decompose the problem into steps assemble a test program and run it

decompose the problem into steps

Java 7 introduced enhanced syntax for declaring array lists, which is termed angle brackets. method lists. diamond syntax. symmetric slants.

diamond syntax.

Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. for (String str : arr) for (str : String arr) for (str[] : arr) for (arr[] : str)

for (String str : arr)

Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int cols = counts[2].size(); int cols = counts.length[2]; int cols = counts.length; int cols = counts[2].length;

int cols = counts[2].length;

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

Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer? int[][] num = new int[20][2]; int[][] num = new int[2][20]; int[][] num = new int[20,2]; int[][] num = new int[2,20];

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

The enhanced for loop is convenient for traversing all elements in an array is convenient for traversing elements in a partially filled array is only used for arrays of integers is used to initialize all array elements to a common value

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? access myArray.length() maintain a companion variable that stores the current number of elements access myArray.currentElements() access myArray.length() - 1

maintain a companion variable that stores the current number of elements

Which one of the following is the correct header for a method named arrMeth that is called like this: public static void arrMeth(int[] ar, int n) public static void arrMeth(r[], n) public static void arrMeth(int n , int[] ar) public static void arrMeth(int[] ar)

public static void arrMeth(int[] ar)

Babbage's machine for automatically producing printed tables was called the slide rule. the difference engine. the mechanical calculator. the cog wheel.

the difference engine

In a partially filled array, the number of slots in the array that are not currently used is the length of the array minus the number of elements currently in the array the number of elements currently in the array minus the length of the array the length of the array plus the number of elements currently in the array the number of elements currently in the array

the length of the array minus the number of elements currently in the array

It may be necessary to "grow" an array when reading inputs because the number of inputs may not be known in advance. arrays in Java must be resized after every 100 elements. arrays are based on powers of two. the only alternative is a bounds exception.

the number of inputs may not be known in advance.

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); x = a.get(); x = a.get[4]; x = a[4];

x = a.get(4);

Consider the following code snippet in Java 6 or later: String[] data = { "abc", "def", "ghi", "jkl" };String[] data2 = Arrays.copyOf(data, data.length - 1); What does the last element of data2 contain? "abc" "def" "ghi" "jkl"

"ghi"

What is the valid range of index values for an array of size 10? 0 to 10 1 to 9 1 to 10 0 to 9

0 to 9

What is the value of the count variable after the execution of the given code snippet? 1 2 0 3

1

Consider the following code snippet. What does the array contain at the end of the program? 20 random numbers Undefined data due to compilation error 20 zeros because array num is not changed by method Array index bound error

20 zeros because array num is not changed by method

What is the output of the given code snippet? 2345 1234 1345 1111

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? 00 31 30 03

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]); 1050 2030 3040 4050

3040

Consider the following code snippet: What is the value of numarray[1][2] after the code snippet is executed? 2 5 3 4

5

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

6

What is the output of the following statements? Cal, Bob, Ann Ann, Bob Ann, Cal, Tony Array list bound error

Array list bound error

Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order? data = Arrays.sort(); Arrays.sort(data); data = Arrays.sort(data.length); data = Arrays.sort(data);

Arrays.sort(data);

Which one of the following statements is true about passing arrays to a method? By default, arrays are passed by value to a method Arrays, when updated in a called method, are not reflected to the calling method By default, arrays are passed by reference to a method Arrays are passed only if size is specified as another argument

By default, arrays are passed by reference to a method

Which statements are true about the buffer overrun attack launched over the Internet in 1988? I. The buffer overrun exploited a program that was written in C running on the Unix operating system II. The Java programming language generates a run-time exception when buffer overrun occurs III. In recent years computer attacks have lessened I, II I, III II, III I, II, III

I, II

Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change II. Array lists are more efficient than arrays III. Array lists are easier to use than arrays I, II I, III II, III I, II, III

I,III

When a Java program terminates and reports an exception, the error message contains which pieces of useful information? I. The compile and revision control history of the source code changes that caused the error II. The name of the exception that occurred III. The stack trace I, II I, III II, III I, II, III

II, III

Which statements about the enhanced for loop are true? I. It is suitable for all array algorithms II. It does not allow the contents of the array to be modified III. It does not require the use of an index variable I, II I, III II, III I, II, III

II, III

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 Group of answer choices I II III II, III

III

Which one of the following statements is correct for the given code snippet? Line 2 declares and initializes an array of three elements with value 5. Line 2 causes a bounds error because 3 is an invalid index number. Line 2 initializes the third element of the array with value 5. Line 2 initializes all the elements of the array with value 5.

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

Why is the use of physical objects helpful in algorithm design? It simulates the way the computer actually implements the algorithm It is more abstract than using a pencil and paper Because the constraints on physical things are the same as the constraints on bits and bytes Many people feel it is less intimidating than drawing diagrams

Many people feel it is less intimidating than drawing diagrams

Why is the following method header invalid? public static int[5] meth(int[] arr, int[] num) Methods that return an array cannot specify its size Methods cannot have an array as a return type Methods cannot have an array as a parameter variable Methods cannot have two array parameter variables

Methods that return an array cannot specify its size

When the order of the elements is unimportant, what is the most efficient way to remove an element from an array? Delete the element and move each element after that one to a lower index. Replace the element to be deleted with the last element in the array. Replace the element to be deleted with the first element in the array. Replace the element with the next element.

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

Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it? Sorting Searching Finding the maximum Monte Carlo methods

Searching

Consider the following code snippet. Which statement should be used to fill in the empty line so that provides the output will be [32, 54, 67.5, 29, 35]? String str = Arrays.format(data); String str = data.toString(); String str = Arrays.toString(data); String str = str + ", " + data[i];

String str = Arrays.toString(data);

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? System.out.println(arr[1][2]); System.out.println(arr[2][3]); System.out.println(arr[2][1]); System.out.println(arr[3][2]);

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

What is the result of the following code? The elements of the array values are initialized to zero. The elements of the array element are initialized to zero. The first element of the array values is initialized to zero. The array values is not modified.

The array values is not modified.

What is displayed after executing the given code snippet? The code snippet displays the total marks of all ten subjects. The for loop causes a run-time time error on the first iteration. The code snippet causes a bounds error. The code snippet displays zero.

The code snippet causes a bounds error.

What is the result of executing this code snippet? int marks[] = { 90, 45, 67 }; The code snippet does not give any output. The code snippet displays all the marks stored in the array without any redundancy. The code snippet causes a bounds error. The code snippet executes an infinite loop.

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 2; the final size of friends is 3 The final size of names is 3; the final size of friends is 2 The final size of names is 3; the final size of friends is 3 Compilation error

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

Which one of the following statements is correct about the given code snippet? The for loop initializes all the elements of the array. The for loop initializes all the elements except the first element. The for loop initializes all the elements except the last element. The for loop initializes all the elements except the first and last elements.

The for loop initializes all the elements except the first element.

Is there any thing wrong with the following code snippet? There is nothing wrong. There is compile-time error. There is a bounds error. There is a logic error.

There is a logic error.

Which code snippet calculates the sum of all the even elements in an array values? a b c d

a

What is the output of the following code snippet? String [] arr ={"aaa", "bbb", "ccc"}; ddd 5 ddd 3 aaa 5 aaa 3

aaa 3


Conjuntos de estudio relacionados

Learning Swedish - M1: 1.1 Hello Goodbye

View Set

MGMT 352 Chapter 10- Global Strategy

View Set

9-1 Simplifying Radical Expressions

View Set

Jason Udemy CompTIA Security+ (SY0-601) Practice Exam #3

View Set

anesthesia II exotic small animal hw classmarker

View Set

Chapter 15: Care of Intraoperative Patients

View Set

environmental science exam 1 study guide

View Set