Ap computer science Arrays

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the valid range of index values for an array of size 10?

0 to 9

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

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

3040

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

III

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.

Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?

System.out.println(somearray[27]);

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.

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

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

a. II, III

Why is the use of physical objects helpful in algorithm design?

a. Many people feel it is less intimidating than drawing diagrams

What is the result of the following code? for (double element : values) { element = 0; }

a. The array values is not modified.

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?

a. data = Arrays.copyOf(data, 2 * 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?

a. decompose the problem into steps

Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this?

a. remove an element

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?

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

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?

b. ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } }

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

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

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?

b. "ghi"

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

b. I, II

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

b. I, III

Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it?

b. Searching

The enhanced for loop

b. is convenient for traversing all elements in an array

Babbage's machine for automatically producing printed tables was called

b. the difference engine

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

b. x = a.get(4);

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

c. double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] > largest) { largest = values[i]; } }

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

c. II, III

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

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

Is there any thing wrong with the following code snippet? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); }

c. There is a logic error.

Java 7 introduced enhanced syntax for declaring array lists, which is termed

c. diamond syntax

Consider using a deck of cards as a way to visualize a shuffle algorithm. When two cards shuffle their position, what has to happen to the size of the array holding them?

c. it stays the same

The binary search is faster than the linear search, providing

c. the elements of the array are ordered.

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

d. I, III

When an array reading and storing input runs out of space

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

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

d. the number of inputs may not be known in advance.

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)

Which code snippet prints out the elements in a partially filled array of integers?

for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); }

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

int[ ] numarray = new int[10];

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

Suppose you wish to write a method that returns the sum of the elements in the partially filled array myArray. Which is a reasonable method header?

public static int sum(int[ ] values, int currSize)

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

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


Kaugnay na mga set ng pag-aaral

stat exam 3 (confidence intervals)

View Set

Psychology Chapter 6 Summative Quiz

View Set

Comprehensive Mental Health and Psychiatric Nursing NCLEX Practice Quiz #1: 75 Questions

View Set

Interactive Summary Module 5 Social Media

View Set

Hess&Hunt and Inman: Domain III-Topic B

View Set

Week 4 Check Your Understanding Assignment

View Set