CPSC 1110 Quiz1

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The enhanced for loop is

convenient for traversing all elements in an array.

Consider the following 2-dimensional array. Which expression gives the number of elements in the third row? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } };

counts[2].length

The integer array numbers will be filled with values from the Scanner object in. If there are more input values than there are spaces in the array, only enough values to fill the array should be read. The integer variable currentSize should be set to the number of values read. Partial code to do this is given below: int[] numbers = new int[100]; Scanner in = new Scanner (System.in); int currentSize = 0; while (/* Put condition here */) { int value = in.nextInt(); numbers[currentSize] = value; currentSize++; } What condition will complete this code?

currentSize < numbers.length && in.hasNextInt()

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)

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.

This code snippet is an implementation of what algorithm? int searchedValue = 42; int pos = 0; boolean found = false; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } }

sequential search

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

The array list contains at least one element.

Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution?

decompose the problem into steps

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

When an array reading and storing input runs out of space

the array could be "grown" using the new command and the copyOf method.

Consider the code snippet below: int searchedValue = 42; int pos = 0; boolean found = false; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } } When the loop finishes what does the value of the boolean variable found indicate?

whether or not the value 42 was found in the array

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

What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size());

Error because num is not initialized

Which statements about array algorithms are true? I. The array algorithms are building blocks for many programs that process arrays. II. Java contains ready-made array algorithms for every problem situation. III. It is inefficient to make multiple passes through an array if you can do everything in one pass.

I, III only

Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; }

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

A collection of test cases that can be used on future versions of a program is called:

a test suite.

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); }

array list bound error

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

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

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

Consider the following line of code for calling a method named func1: func1(listData, varData); Which one of the following method headers is valid for func1, where listData is an integer array list and varData is an integer variable?

public void func1(ArrayList<Integer> ldata, int vdata)

Which one of the following is a correct declaration for a method named passAList that has as arguments an array list myList of size 10 and an integer array intArr of size 20, and that modifies the contents of myList and intArr?

public void passAList(ArrayList<Integer> myList, int[] intArr)

The binary search is more efficient than the linear search, providing

the elements of the array are ordered.

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

the number of inputs may not be known in advance.

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

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?

"ghi"

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 following code snippet? int[] values = { 1, 2, 3, 4}; values[2] = 24; values[values[0]] = 86; for (int i = 0; i < values.length; i++) { System.out.print (values[i] + " "); }

1 86 24 4

What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length);

2

What is the output of the following code snippet? int[] values = { 10, 20, 30, 7}; int[] numbers = values; numbers[2] = 24; values[3] = numbers[0] + 6; System.out.println (numbers[2] + " " + numbers[3] + " " + values[2] + " " + values[3]);

24 16 24 16

Assume the following variable has been declared and given a value as shown: int[][] data = { {9, 17, -4, 21 }, {15, 24, 0, 9}, {6, 2, -56, 8}, }; Which is the value of data.length?

3

Assume the following variable has been declared and given a value as shown: int[] numbers = {9, 17, -4, 21 }; Which is the value of numbers.length?

4

Assume the following variable has been declared and given a value as shown: int[][] data = { {9, 17, -4, 21 }, {15, 24, 0, 9}, {6, 2, -56, 8}, }; Which is the value of data[0].length?

4

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

4 9 15 22

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

6

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

9

What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); }

Ann, Tony

Which one of the following statements is true about using arrays with methods?

Arrays can be method arguments, and return values, just like any other values.

Consider the following code snippet: public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); names.add("Janet"); ArrayList<String> names2 = reverse(names); } public static ArrayList<String> reverse(ArrayList<String> names) { ArrayList<String> result = new ArrayList<String>(); for (int i = names.size() - 1; i >= 0; i--) { result.add(names.get(i)); } return <String>result; } Which statement is true after the main method is executed?

Compilation error due to the return statement in reverse 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 only

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, III only

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values) { for (int i = 1; i < values.length; i++) { values[i - 1] = values[i]; } } What does the method do?

It moves each element of the array values to a lower index position and overwrites the first element.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = temp; } What does the method do?

It swaps the integer values stored in positions p1 and p2 of the array values.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = values[p1]; } What does the method do?

It swaps the integer values stored in positions p1 and p2 of the array values.

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.

Consider the following code snippet. Which statement should be used to fill in the empty line so that the output will be [32, 54, 67.5, 29, 35]? public static void main(String[] args) { double data[] = {32, 54, 67.5, 29, 35}; ______________ System.out.println(str); }

String str = Arrays.toString(data);

Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } }

The code has a run-time error

What is displayed after executing the given code snippet? int[] mymarks = new int[10]; int total = 0; Scanner in = new Scanner(System.in); for (int cnt = 1; cnt <= 10; cnt++) { System.out.print("Enter the marks: "); mymarks[cnt] = in.nextInt(); total = total + mymarks[cnt]; } System.out.println(total);

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.

If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen?

The program will not compile.

Assume the method doSomething has been defined as follows: public static int [] doSomething (int[] values) { int [] result = new int[values.length - 1]; for (int i = 0; i < result.length; i++) { result[i] = values[i] + values[i + 1]; } return result; } What is printed by the statements below? int [] nums = {3, 18, 29, -2} ; System.out.print(Arrays.toString(doSomething(nums)));

[21, 47, 27]


संबंधित स्टडी सेट्स

Professional Nursing: Documentation

View Set

Purchasing and Materials Management Final

View Set

Processing of Data - DLMBDSA01 Unit 4

View Set

14 conscious and unconscious thought

View Set

Energy in the 21st Century Test #1

View Set

Mike Meyers' CompTIA Network+ - Chapter 9: Network Naming

View Set

Chapter 2 Exam - Life Provisions

View Set

Chapter 8: Net Present Value and Other Investment Criteria

View Set