Deck 6: Arrays and Arraylists

Full screen (f)
exit full mode
Question
When the order of the elements is unimportant, what is the most efficient way to remove an element from an array?

A) Delete the element and move each element after that one to a lower index.
B) Replace the element to be deleted with the last element in the array.
C) Replace the element to be deleted with the first element in the array.
D) Replace the element with the next element.
Use Space or
up arrow
down arrow
to flip the card.
Question
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Question
Identify the correct statement for defining an integer array named numarray of ten elements.

A) int[] numarray = new int[9];
B) int[] numarray = new int[10];
C) int[10] numarray;
D) int numarray[10];
Question
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

A) Line 2 declares and initializes an array of three elements with value 5.
B) Line 2 causes a bounds error because 3 is an invalid index number.
C) Line 2 initializes the third element of the array with value 5.
D) Line 2 initializes all the elements of the array with value 5.
Question
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;
}

A) The for loop initializes all the elements of the array.
B) The for loop initializes all the elements except the first element.
C) The for loop initializes all the elements except the last element.
D) The for loop initializes all the elements except the first and last elements.
Question
What is the result of the following code?
For (double element : values)
{
Element = 0;
}

A) The elements of the array values are initialized to zero.
B) The elements of the array element are initialized to zero.
C) The first element of the array values is initialized to zero.
D) The array values is not modified.
Question
Which one of the following statements is a valid initialization of an array named somearray of ten elements?

A) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
B) int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
C) int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
D) int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Question
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)
Question
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]);
}

A) The code snippet does not give any output.
B) The code snippet displays all the marks stored in the array without any redundancy.
C) The code snippet causes a bounds error.
D) The code snippet executes an infinite loop.
Question
When an array myArray is only partially filled, how can the programmer keep track of the current number of elements?

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

A) public static int sum(int[] values)
B) public static int sum()
C) public static int sum(int[] values, int currSize)
D) public static int sum(int[] values, int size, int currSize)
Question
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);

A) The code snippet displays the total marks of all ten subjects.
B) The for loop causes a run-time time error on the first iteration.
C) The code snippet causes a bounds error.
D) The code snippet displays zero.
Question
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]);

A) 1050
B) 2030
C) 3040
D) 4050
Question
In a partially filled array, the number of slots in the array that are not currently used is

A) the length of the array minus the number of elements currently in the array
B) the number of elements currently in the array minus the length of the array
C) the length of the array plus the number of elements currently in the array
D) the number of elements currently in the array
Question
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) I, II
B) I, III
C) II, III
D) I, II, III
Question
The enhanced for loop

A) is convenient for traversing all elements in an array
B) is convenient for traversing elements in a partially filled array
C) is only used for arrays of integers
D) is used to initialize all array elements to a common value
Question
What is the valid range of index values for an array of size 10?

A) 0 to 10
B) 1 to 9
C) 1 to 10
D) 0 to 9
Question
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]);
}

A) 2345
B) 1234
C) 1345
D) 1111
Question
Which code snippet prints out the elements in a partially filled array of integers?

A) for (int i = 0; i < myArray.length(); i++)
{
System.out.print(myArray[i]);
}
B) for (int i = 0; i < myArray.currLength(); i++)
{
System.out.print(myArray[i]);
}
C) for (int i = 0; i < currLength; i++)
{
System.out.print(myArray[i]);
}
D) for (int i = 0; i < myArray.length(); i++)
{
System.out.print(myArray[currLength]);
}
Question
Consider the following line of code:
Int[] somearray = new int[30];
Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?

A) System.out.println(somearray[28]);
B) System.out.println(somearray(28));
C) System.out.println(somearray(27));
D) System.out.println(somearray[27]);
Question
Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution?

A) structure a program using methods
B) adapt a built-in array algorithm
C) decompose the problem into steps
D) assemble a test program and run it
Question
The following statement gets an element from position 4 in an array:
X = a[4];
What is the equivalent operation using an array list?

A) x = a.get(4);
B) x = a.get();
C) x = a.get[4];
D) x = a[4];
Question
When an array reading and storing input runs out of space

A) the program must be recompiled with a bigger size for the array.
B) the array must be "grown" using the growArray method.
C) it automatically resizes to accommodate new elements.
D) the array must be "grown" using the new command and the copyOf method.
Question
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) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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));
}
}
B) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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));
}
}
C) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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[i]);
}
}
D) ArrayList num1;
ArrayList oddnum = new ArrayList();
Int data;
Scanner in = new Scanner(System.in);
For (int i = 0; i < 10; i++)
{
Data = in.nextInt();
Num1.add(data);
If (num1[i] % 2 != 0)
{
Oddnum.add(num1[i]);
}
}
Question
It may be necessary to "grow" an array when reading inputs because

A) the number of inputs may not be known in advance.
B) arrays in Java must be resized after every 100 elements.
C) arrays are based on powers of two.
D) the only alternative is a bounds exception.
Question
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Question
Which code snippet finds the largest value in an array that is only partially full?

A) double largest = values[0];
For (int i = 1; i < values.length; i++)
{
If (values[i] > largest)
{
Largest = values[i];
}
}
B) double largest = values[0];
For (int i = 1; i < values.length; i++)
{
If (values[i] < largest)
{
Largest = values[i];
}
}
C) double largest = values[0];
For (int i = 1; i < currSize; i++)
{
If (values[i] > largest)
{
Largest = values[i];
}
}
D) double largest = values[0];
For (int i = 1; i < currSize; i++)
{
If (values[i] < largest)
{
Largest = values[i];
}
}
Question
What is the value of the count variable after the execution of the given code snippet?
ArrayList num = new ArrayList();
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++;
}
}

A) 1
B) 2
C) 0
D) 3
Question
The binary search is faster than the linear search, providing

A) the size of the array is a power of two.
B) the elements of the array are ordered.
C) the elements of the array are unordered.
D) the element being searched for is actually in the array.
Question
Babbage's machine for automatically producing printed tables was called

A) the slide rule.
B) the difference engine.
C) the mechanical calculator.
D) the cog wheel.
Question
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Question
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

A) I
B) II
C) III
D) II, III
Question
Why is the use of physical objects helpful in algorithm design?

A) It simulates the way the computer actually implements the algorithm
B) It is more abstract than using a pencil and paper
C) Because the constraints on physical things are the same as the constraints on bits and bytes
D) Many people feel it is less intimidating than drawing diagrams
Question
Java 7 introduced enhanced syntax for declaring array lists, which is termed

A) angle brackets.
B) method lists.
C) diamond syntax.
D) symmetric slants.
Question
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Question
If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen?

A) The program will crash.
B) The program will not compile.
C) The program will run but will produce an uncertain result.
D) The compiler will automatically correct the error.
Question
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?

A) it increases by one
B) it decreases by one
C) it stays the same
D) it increases by two
Question
Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it?

A) Sorting
B) Searching
C) Finding the maximum
D) Monte Carlo methods
Question
Which code snippet calculates the sum of all the elements in even positions in an array?

A) int sum = 0;
For (int i = 1; i < values.length; i+=2)
{
Sum++;
}
B) int sum = 0;
For (int i = 0; i < values.length; i++)
{
Sum++;
}
C) int sum = 0;
For (int i = 0; i < values.length; i++)
{
Sum += values[i];
}
D) int sum = 0;
For (int i = 0; i < values.length; i + 2)
{
Sum += values[i];
}
Question
Which code snippet calculates the sum of all the even elements in an array values?

A) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] % 2) == 0)
{
Sum += values[i];
}
}
B) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] % 2) == 0)
{
Sum++;
}
}
C) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] / 2) == 0)
{
Sum += values[i];
}
}
D) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] / 2) == 0)
{
Sum++;
}
}
Question
Consider the following code snippet:
Int[][] arr =
{
{ 13, 23, 33 },
{ 14, 24, 34 }
};
Identify the appropriate statement to display the value 24 from the given array?

A) System.out.println(arr[1][2]);
B) System.out.println(arr[2][2]);
C) System.out.println(arr[1][1]);
D) System.out.println(arr[2][1]);
Question
Consider the following code snippet:
Int cnt = 0;
Int[][] numarray = new int[2][3];
For (int i = 0; i < 3; i++)
{
For (int j = 0; j < 2; j++)
{
Numarray[j][i] = cnt;
Cnt++;
}
}
What is the value of numarray[1][2] after the code snippet is executed?

A) 2
B) 5
C) 3
D) 4
Question
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?

A) "abc"
B) "def"
C) "ghi"
D) "jkl"
Question
Consider the following method:
Public static int mystery(int length, int n)
{
Int[] result = new int[length];
For (int i = 0; i < result.length; i++)
{
Result[i] = (int) (n * Math.random());
}
Return result;
}
Which statement is correct about the code?

A) The method works perfectly
B) The method returns a random number
C) The method return type should be changed to int[]
D) The method has a bounds error when the array size is too large
Question
What is the output of the following code snippet?
Public static void main(String[] args)
{
String[] arr = { "aaa", "bbb", "ccc" };
Mystery(arr);
System.out.println(arr[0] + " " + arr.length);
}
Public static void mystery(String[] arr)
{
Arr = new String[5];
Arr[0] = "ddd";
}

A) ddd 5
B) ddd 3
C) aaa 5
D) aaa 3
Question
Why is the following method header invalid?
Public static int[5] meth(int[] arr, int[] num)

A) Methods that return an array cannot specify its size
B) Methods cannot have an array as a return type
C) Methods cannot have an array as a parameter variable
D) Methods cannot have two array parameter variables
Question
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]?
Public static void main(String[] args)
{
Double data[] = {32, 54, 67.5, 29, 35};
______________
System.out.println(str);
}

A) String str = Arrays.format(data);
B) String str = data.toString();
C) String str = Arrays.toString(data);
D) String str = str + ", " + data[i];
Question
Select the statement that reveals the logic error in the following method.
Public static double minimum(double[] data)
{
Double smallest = 0.0;
For (int i = 0; i < data.length; i++)
{
If (data[i] < smallest)
{
Smallest = data[i];
}
}
Return smallest;
}

A) double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 });
B) double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 });
C) double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 });
D) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });
Question
Which one of the following statements is true about passing arrays to a method?

A) By default, arrays are passed by value to a method
B) Arrays, when updated in a called method, are not reflected to the calling method
C) By default, arrays are passed by reference to a method
D) Arrays are passed only if size is specified as another argument
Question
Which one of the following is the correct header for a method named arrMeth that is called like this:
ArrMeth(intArray); // intArray is an integer array of size 3

A) public static void arrMeth(int[] ar, int n)
B) public static void arrMeth(r[], n)
C) public static void arrMeth(int n , int[] ar)
D) public static void arrMeth(int[] ar)
Question
How many elements can be stored in an array of dimension 2 by 3?

A) 2
B) 3
C) 5
D) 6
Question
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?

A) int[][] num = new int[20][2];
B) int[][] num = new int[2][20];
C) int[][] num = new int[20,2];
D) int[][] num = new int[2,20];
Question
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?

A) 00
B) 31
C) 30
D) 03
Question
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, data2.length);
B) data2 = Arrays.copyOf(data, data.length);
C) data2 = Arrays.copyOf(data, data.size());
D) data2 = Arrays.copyOf(data);
Question
Consider the following code snippet:
String[] data = { "123", "ghi", "jkl", "def", "%&*" };
Which statement sorts the data array in ascending order?

A) data = Arrays.sort();
B) Arrays.sort(data);
C) data = Arrays.sort(data.length);
D) data = Arrays.sort(data);
Question
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);
B) data = Arrays.copyOf(data, 2);
C) data = Arrays.copyOf(data, 2 * data);
D) data = Arrays.copyOf(data, 2 * data.size());
Question
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");
}

A) There is nothing wrong.
B) There is compile-time error.
C) There is a bounds error.
D) There is a logic error.
Question
Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns?

A) int[][] arr =
{
{ 1, 1, 1 },
{ 2, 2, 2 },
};
B) int[][] arr =
{
{ 1, 1 },
{ 2, 2 },
{ 3, 3 }
};
C) int[][] arr =
{
{ 1, 1 }
{ 2, 2 }
{ 3, 3 }
};
D) int[][] arr =
{
{ 1, 1, 1 }
{ 2, 2, 2 }
{ 3, 3, 3 }
};
Question
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?

A) System.out.println(arr[1][2]);
B) System.out.println(arr[2][3]);
C) System.out.println(arr[2][1]);
D) System.out.println(arr[3][2]);
Question
Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row.
Int[][] counts =
{
{ 0, 0, 1 },
{ 0, 1, 1, 2 },
{ 0, 0, 1, 4, 5 },
{ 0, 2 }
};

A) int cols = counts[2].size();
B) int cols = counts.length[2];
C) int cols = counts.length;
D) int cols = counts[2].length;
Question
Consider the following code snippet:
ArrayList arrList = new ArrayList();
For (int i = 0; i < arrList.size(); i++)
{
ArrList.add(i + 3);
}
What value is stored in the element of the array list at index 0?

A) 0
B) 3
C) 6
D) None
Question
Consider the following code snippet:
ArrayList somedata = new ArrayList();
Somedata.add(10.5);
What is the size of the array list somedata after the given code snippet is executed?

A) 0
B) 1
C) 10
D) 2
Question
Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use?

A) An array declared as int[] marks;
B) A array list declared as ArrayList marks = new ArrayList();
C) An array declared as int marks[10000];
D) An array declared as int marks[10];
Question
What is the value of the cnt variable after the execution of the code snippet below?
ArrayList somenum = new ArrayList();
Somenum.add(1);
Somenum.add(2);
Somenum.add(1);
Int cnt = 0;
For (int index = 0; index < somenum.size(); index++)
{
If (somenum.get(index) % 2 == 0)
{
Cnt++;
}
}

A) 1
B) 2
C) 3
D) 0
Question
What is the result of the following definition of an array list?
ArrayList points;

A) The statement causes a compile time error as the size of the array list is not defined.
B) The statement creates an array list of size 0.
C) The statement creates an array list with unlimited size.
D) The statement defines an array list reference but leaves its value as null.
Question
Consider the following code snippet:
Public static void check(ArrayList chknum1)
{
Int cnt = 0;
For(int i = 0; i < chknum1.size(); i++)
{
If(chknum1.get(i) == 0)
{
Cnt++;
}
}
System.out.println("The total 0 values in the list are: " + cnt);
}
Which one of the following is true about the check method in the given code snippet?

A) The check method counts all the elements with value 0 in an array list passed as a parameter to the method.
B) The check method removes all the elements with value 0 from an array list passed as a parameter to the method.
C) The check method counts all the elements with value 0 in an array list passed as a parameter to a method and also returns the count.
D) The check method adds 0 to the elements of an array list as a parameter to a method and also returns the array list.
Question
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?

A) public static void func1(int[] listData, int varData)
B) public static void func1(ArrayList listData, varData)
C) public static void func1(ArrayList ldata, int vdata)
D) public static void func1(int vdata, ArrayList ldata)
Question
What is the value of myArray[1][2] after this code snippet is executed?
Int count = 0;
Int[][] myArray = new int[4][5];
For (int i = 0; i < 5; i++)
{
For (int j = 0; j < 4; j++)
{
MyArray[j][i] = count;
Count++;
}
}

A) 8
B) 9
C) 19
D) 11
Question
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?

A) 5
B) 7
C) 9
D) 10
Question
Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10?

A) int maximum = 0;
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
B) int maximum = arrList.get(0);
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
C) int maximum = arrList.get(1);
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
D) int maximum = arrList.get(0);
For (int counter = 1; counter > arrList.size(); counter++)
{
If (arrList.get(counter) < maximum)
{
Maximum = arrList.get(counter);
}
}
Question
What is the output of the following code snippet?
ArrayList num;
Num)add(4);
System.out.println(num.size());

A) 1
B) 0
C) 4
D) Error because num is not initialized
Question
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?

A) public static void passAList(ArrayList myList(10), int[] intArr)
B) public static void passAList(ArrayList myList, int[20] intArr)
C) public static void passAList(ArrayList myList, int[] intArr)
D) public static void passAList(ArryaList myList, int intArr)
Question
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.
Question
Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter?

A) public static void passAList(ArrayList num[5])
B) public static void passAList(ArrayList num, 5)
C) public static void passAList(ArrayList num)
D) public static void passAList(num)
Question
Which one of the following statements about declaring an array list as a method parameter is true?

A) An array list declared as a method parameter should be accompanied with its size.
B) An array list declared as a method parameter is a constant value by default.
C) An array list value cannot be modified in a method when the array list is declared as a parameter.
D) An array list value can be modified inside the method.
Question
Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10?

A) public static void func(int[][] arr)
B) public static void func(int[10][] arr)
C) public static void func(int[][10] arr)
D) public static void func(int[10][10] arr)
Question
Consider the following code snippet:
Int val = arr[0][2];
Which value of arr is stored in the val variable?

A) The value in the first row and the second column
B) The value in the first row and the first column
C) The value in the first row and the third column
D) The value in the third row and the second column
Question
Which one 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?

A) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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));
}
}
B) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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));
}
}
C) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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[i] = num1[i];
}
}
D) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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[i] = num1[i];
}
}
Question
What is the output of the following code snippet?
Public static int check(ArrayList listData)
{
Int sum = 0;
For (int i = 0; i < listData.size(); i++)
{
Sum = sum + listData.get(i);
}
Return sum;
}
Public static void main(String[] args)
{
ArrayList vdata = new ArrayList();
Int rsum;
For (int cnt = 0; cnt < 3; cnt++)
{
Vdata.add(cnt + 1);
}
Rsum = check(vdata);
System.out.println(rsum);
}

A) 4
B) 2
C) 3
D) 6
Question
Consider the following code snippet:
ArrayList num1 = new ArrayList();
Int data;
Scanner in = new Scanner(System.in);
For (int i = 0; i < 5; i++)
{
Data = in.nextInt();
Num1.add(data);
If (data == 0 && num1.size() > 3)
{
Num1.remove(num1.size() - 1);
}
}
System.out.println("size is : " + num1.size());
What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

A) size is : 1
B) size is : 2
C) size is : 4
D) size is : 0
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/100
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 6: Arrays and Arraylists
1
When the order of the elements is unimportant, what is the most efficient way to remove an element from an array?

A) Delete the element and move each element after that one to a lower index.
B) Replace the element to be deleted with the last element in the array.
C) Replace the element to be deleted with the first element in the array.
D) Replace the element with the next element.
B
2
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

A) I, II
B) I, III
C) II, III
D) I, II, III
A
3
Identify the correct statement for defining an integer array named numarray of ten elements.

A) int[] numarray = new int[9];
B) int[] numarray = new int[10];
C) int[10] numarray;
D) int numarray[10];
B
4
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

A) Line 2 declares and initializes an array of three elements with value 5.
B) Line 2 causes a bounds error because 3 is an invalid index number.
C) Line 2 initializes the third element of the array with value 5.
D) Line 2 initializes all the elements of the array with value 5.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
5
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;
}

A) The for loop initializes all the elements of the array.
B) The for loop initializes all the elements except the first element.
C) The for loop initializes all the elements except the last element.
D) The for loop initializes all the elements except the first and last elements.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
6
What is the result of the following code?
For (double element : values)
{
Element = 0;
}

A) The elements of the array values are initialized to zero.
B) The elements of the array element are initialized to zero.
C) The first element of the array values is initialized to zero.
D) The array values is not modified.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
7
Which one of the following statements is a valid initialization of an array named somearray of ten elements?

A) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
B) int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
C) int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
D) int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
8
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)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
9
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]);
}

A) The code snippet does not give any output.
B) The code snippet displays all the marks stored in the array without any redundancy.
C) The code snippet causes a bounds error.
D) The code snippet executes an infinite loop.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
10
When an array myArray is only partially filled, how can the programmer keep track of the current number of elements?

A) access myArray.length()
B) maintain a companion variable that stores the current number of elements
C) access myArray.currentElements()
D) access myArray.length() - 1
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
11
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?

A) public static int sum(int[] values)
B) public static int sum()
C) public static int sum(int[] values, int currSize)
D) public static int sum(int[] values, int size, int currSize)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
12
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);

A) The code snippet displays the total marks of all ten subjects.
B) The for loop causes a run-time time error on the first iteration.
C) The code snippet causes a bounds error.
D) The code snippet displays zero.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
13
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]);

A) 1050
B) 2030
C) 3040
D) 4050
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
14
In a partially filled array, the number of slots in the array that are not currently used is

A) the length of the array minus the number of elements currently in the array
B) the number of elements currently in the array minus the length of the array
C) the length of the array plus the number of elements currently in the array
D) the number of elements currently in the array
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
15
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) I, II
B) I, III
C) II, III
D) I, II, III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
16
The enhanced for loop

A) is convenient for traversing all elements in an array
B) is convenient for traversing elements in a partially filled array
C) is only used for arrays of integers
D) is used to initialize all array elements to a common value
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
17
What is the valid range of index values for an array of size 10?

A) 0 to 10
B) 1 to 9
C) 1 to 10
D) 0 to 9
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
18
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]);
}

A) 2345
B) 1234
C) 1345
D) 1111
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
19
Which code snippet prints out the elements in a partially filled array of integers?

A) for (int i = 0; i < myArray.length(); i++)
{
System.out.print(myArray[i]);
}
B) for (int i = 0; i < myArray.currLength(); i++)
{
System.out.print(myArray[i]);
}
C) for (int i = 0; i < currLength; i++)
{
System.out.print(myArray[i]);
}
D) for (int i = 0; i < myArray.length(); i++)
{
System.out.print(myArray[currLength]);
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
20
Consider the following line of code:
Int[] somearray = new int[30];
Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?

A) System.out.println(somearray[28]);
B) System.out.println(somearray(28));
C) System.out.println(somearray(27));
D) System.out.println(somearray[27]);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
21
Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution?

A) structure a program using methods
B) adapt a built-in array algorithm
C) decompose the problem into steps
D) assemble a test program and run it
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
22
The following statement gets an element from position 4 in an array:
X = a[4];
What is the equivalent operation using an array list?

A) x = a.get(4);
B) x = a.get();
C) x = a.get[4];
D) x = a[4];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
23
When an array reading and storing input runs out of space

A) the program must be recompiled with a bigger size for the array.
B) the array must be "grown" using the growArray method.
C) it automatically resizes to accommodate new elements.
D) the array must be "grown" using the new command and the copyOf method.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
24
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) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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));
}
}
B) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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));
}
}
C) ArrayList num1 = new ArrayList();
ArrayList oddnum = new ArrayList();
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[i]);
}
}
D) ArrayList num1;
ArrayList oddnum = new ArrayList();
Int data;
Scanner in = new Scanner(System.in);
For (int i = 0; i < 10; i++)
{
Data = in.nextInt();
Num1.add(data);
If (num1[i] % 2 != 0)
{
Oddnum.add(num1[i]);
}
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
25
It may be necessary to "grow" an array when reading inputs because

A) the number of inputs may not be known in advance.
B) arrays in Java must be resized after every 100 elements.
C) arrays are based on powers of two.
D) the only alternative is a bounds exception.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
26
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
27
Which code snippet finds the largest value in an array that is only partially full?

A) double largest = values[0];
For (int i = 1; i < values.length; i++)
{
If (values[i] > largest)
{
Largest = values[i];
}
}
B) double largest = values[0];
For (int i = 1; i < values.length; i++)
{
If (values[i] < largest)
{
Largest = values[i];
}
}
C) double largest = values[0];
For (int i = 1; i < currSize; i++)
{
If (values[i] > largest)
{
Largest = values[i];
}
}
D) double largest = values[0];
For (int i = 1; i < currSize; i++)
{
If (values[i] < largest)
{
Largest = values[i];
}
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
28
What is the value of the count variable after the execution of the given code snippet?
ArrayList num = new ArrayList();
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++;
}
}

A) 1
B) 2
C) 0
D) 3
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
29
The binary search is faster than the linear search, providing

A) the size of the array is a power of two.
B) the elements of the array are ordered.
C) the elements of the array are unordered.
D) the element being searched for is actually in the array.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
30
Babbage's machine for automatically producing printed tables was called

A) the slide rule.
B) the difference engine.
C) the mechanical calculator.
D) the cog wheel.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
31
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
32
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

A) I
B) II
C) III
D) II, III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
33
Why is the use of physical objects helpful in algorithm design?

A) It simulates the way the computer actually implements the algorithm
B) It is more abstract than using a pencil and paper
C) Because the constraints on physical things are the same as the constraints on bits and bytes
D) Many people feel it is less intimidating than drawing diagrams
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
34
Java 7 introduced enhanced syntax for declaring array lists, which is termed

A) angle brackets.
B) method lists.
C) diamond syntax.
D) symmetric slants.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
35
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

A) I, II
B) I, III
C) II, III
D) I, II, III
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
36
If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen?

A) The program will crash.
B) The program will not compile.
C) The program will run but will produce an uncertain result.
D) The compiler will automatically correct the error.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
37
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?

A) it increases by one
B) it decreases by one
C) it stays the same
D) it increases by two
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
38
Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it?

A) Sorting
B) Searching
C) Finding the maximum
D) Monte Carlo methods
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
39
Which code snippet calculates the sum of all the elements in even positions in an array?

A) int sum = 0;
For (int i = 1; i < values.length; i+=2)
{
Sum++;
}
B) int sum = 0;
For (int i = 0; i < values.length; i++)
{
Sum++;
}
C) int sum = 0;
For (int i = 0; i < values.length; i++)
{
Sum += values[i];
}
D) int sum = 0;
For (int i = 0; i < values.length; i + 2)
{
Sum += values[i];
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
40
Which code snippet calculates the sum of all the even elements in an array values?

A) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] % 2) == 0)
{
Sum += values[i];
}
}
B) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] % 2) == 0)
{
Sum++;
}
}
C) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] / 2) == 0)
{
Sum += values[i];
}
}
D) int sum = 0;
For (int i = 0; i < values.length; i++)
{
If ((values[i] / 2) == 0)
{
Sum++;
}
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
41
Consider the following code snippet:
Int[][] arr =
{
{ 13, 23, 33 },
{ 14, 24, 34 }
};
Identify the appropriate statement to display the value 24 from the given array?

A) System.out.println(arr[1][2]);
B) System.out.println(arr[2][2]);
C) System.out.println(arr[1][1]);
D) System.out.println(arr[2][1]);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
42
Consider the following code snippet:
Int cnt = 0;
Int[][] numarray = new int[2][3];
For (int i = 0; i < 3; i++)
{
For (int j = 0; j < 2; j++)
{
Numarray[j][i] = cnt;
Cnt++;
}
}
What is the value of numarray[1][2] after the code snippet is executed?

A) 2
B) 5
C) 3
D) 4
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
43
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?

A) "abc"
B) "def"
C) "ghi"
D) "jkl"
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
44
Consider the following method:
Public static int mystery(int length, int n)
{
Int[] result = new int[length];
For (int i = 0; i < result.length; i++)
{
Result[i] = (int) (n * Math.random());
}
Return result;
}
Which statement is correct about the code?

A) The method works perfectly
B) The method returns a random number
C) The method return type should be changed to int[]
D) The method has a bounds error when the array size is too large
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
45
What is the output of the following code snippet?
Public static void main(String[] args)
{
String[] arr = { "aaa", "bbb", "ccc" };
Mystery(arr);
System.out.println(arr[0] + " " + arr.length);
}
Public static void mystery(String[] arr)
{
Arr = new String[5];
Arr[0] = "ddd";
}

A) ddd 5
B) ddd 3
C) aaa 5
D) aaa 3
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
46
Why is the following method header invalid?
Public static int[5] meth(int[] arr, int[] num)

A) Methods that return an array cannot specify its size
B) Methods cannot have an array as a return type
C) Methods cannot have an array as a parameter variable
D) Methods cannot have two array parameter variables
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
47
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]?
Public static void main(String[] args)
{
Double data[] = {32, 54, 67.5, 29, 35};
______________
System.out.println(str);
}

A) String str = Arrays.format(data);
B) String str = data.toString();
C) String str = Arrays.toString(data);
D) String str = str + ", " + data[i];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
48
Select the statement that reveals the logic error in the following method.
Public static double minimum(double[] data)
{
Double smallest = 0.0;
For (int i = 0; i < data.length; i++)
{
If (data[i] < smallest)
{
Smallest = data[i];
}
}
Return smallest;
}

A) double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 });
B) double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 });
C) double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 });
D) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
49
Which one of the following statements is true about passing arrays to a method?

A) By default, arrays are passed by value to a method
B) Arrays, when updated in a called method, are not reflected to the calling method
C) By default, arrays are passed by reference to a method
D) Arrays are passed only if size is specified as another argument
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
50
Which one of the following is the correct header for a method named arrMeth that is called like this:
ArrMeth(intArray); // intArray is an integer array of size 3

A) public static void arrMeth(int[] ar, int n)
B) public static void arrMeth(r[], n)
C) public static void arrMeth(int n , int[] ar)
D) public static void arrMeth(int[] ar)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
51
How many elements can be stored in an array of dimension 2 by 3?

A) 2
B) 3
C) 5
D) 6
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
52
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?

A) int[][] num = new int[20][2];
B) int[][] num = new int[2][20];
C) int[][] num = new int[20,2];
D) int[][] num = new int[2,20];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
53
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?

A) 00
B) 31
C) 30
D) 03
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
54
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, data2.length);
B) data2 = Arrays.copyOf(data, data.length);
C) data2 = Arrays.copyOf(data, data.size());
D) data2 = Arrays.copyOf(data);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
55
Consider the following code snippet:
String[] data = { "123", "ghi", "jkl", "def", "%&*" };
Which statement sorts the data array in ascending order?

A) data = Arrays.sort();
B) Arrays.sort(data);
C) data = Arrays.sort(data.length);
D) data = Arrays.sort(data);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
56
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);
B) data = Arrays.copyOf(data, 2);
C) data = Arrays.copyOf(data, 2 * data);
D) data = Arrays.copyOf(data, 2 * data.size());
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
57
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");
}

A) There is nothing wrong.
B) There is compile-time error.
C) There is a bounds error.
D) There is a logic error.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
58
Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns?

A) int[][] arr =
{
{ 1, 1, 1 },
{ 2, 2, 2 },
};
B) int[][] arr =
{
{ 1, 1 },
{ 2, 2 },
{ 3, 3 }
};
C) int[][] arr =
{
{ 1, 1 }
{ 2, 2 }
{ 3, 3 }
};
D) int[][] arr =
{
{ 1, 1, 1 }
{ 2, 2, 2 }
{ 3, 3, 3 }
};
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
59
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?

A) System.out.println(arr[1][2]);
B) System.out.println(arr[2][3]);
C) System.out.println(arr[2][1]);
D) System.out.println(arr[3][2]);
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
60
Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row.
Int[][] counts =
{
{ 0, 0, 1 },
{ 0, 1, 1, 2 },
{ 0, 0, 1, 4, 5 },
{ 0, 2 }
};

A) int cols = counts[2].size();
B) int cols = counts.length[2];
C) int cols = counts.length;
D) int cols = counts[2].length;
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
61
Consider the following code snippet:
ArrayList arrList = new ArrayList();
For (int i = 0; i < arrList.size(); i++)
{
ArrList.add(i + 3);
}
What value is stored in the element of the array list at index 0?

A) 0
B) 3
C) 6
D) None
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
62
Consider the following code snippet:
ArrayList somedata = new ArrayList();
Somedata.add(10.5);
What is the size of the array list somedata after the given code snippet is executed?

A) 0
B) 1
C) 10
D) 2
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
63
Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use?

A) An array declared as int[] marks;
B) A array list declared as ArrayList marks = new ArrayList();
C) An array declared as int marks[10000];
D) An array declared as int marks[10];
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
64
What is the value of the cnt variable after the execution of the code snippet below?
ArrayList somenum = new ArrayList();
Somenum.add(1);
Somenum.add(2);
Somenum.add(1);
Int cnt = 0;
For (int index = 0; index < somenum.size(); index++)
{
If (somenum.get(index) % 2 == 0)
{
Cnt++;
}
}

A) 1
B) 2
C) 3
D) 0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
65
What is the result of the following definition of an array list?
ArrayList points;

A) The statement causes a compile time error as the size of the array list is not defined.
B) The statement creates an array list of size 0.
C) The statement creates an array list with unlimited size.
D) The statement defines an array list reference but leaves its value as null.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
66
Consider the following code snippet:
Public static void check(ArrayList chknum1)
{
Int cnt = 0;
For(int i = 0; i < chknum1.size(); i++)
{
If(chknum1.get(i) == 0)
{
Cnt++;
}
}
System.out.println("The total 0 values in the list are: " + cnt);
}
Which one of the following is true about the check method in the given code snippet?

A) The check method counts all the elements with value 0 in an array list passed as a parameter to the method.
B) The check method removes all the elements with value 0 from an array list passed as a parameter to the method.
C) The check method counts all the elements with value 0 in an array list passed as a parameter to a method and also returns the count.
D) The check method adds 0 to the elements of an array list as a parameter to a method and also returns the array list.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
67
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?

A) public static void func1(int[] listData, int varData)
B) public static void func1(ArrayList listData, varData)
C) public static void func1(ArrayList ldata, int vdata)
D) public static void func1(int vdata, ArrayList ldata)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
68
What is the value of myArray[1][2] after this code snippet is executed?
Int count = 0;
Int[][] myArray = new int[4][5];
For (int i = 0; i < 5; i++)
{
For (int j = 0; j < 4; j++)
{
MyArray[j][i] = count;
Count++;
}
}

A) 8
B) 9
C) 19
D) 11
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
69
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?

A) 5
B) 7
C) 9
D) 10
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
70
Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10?

A) int maximum = 0;
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
B) int maximum = arrList.get(0);
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
C) int maximum = arrList.get(1);
For (int counter = 1; counter < arrList.size(); counter++)
{
If (arrList.get(counter) > maximum)
{
Maximum = arrList.get(counter);
}
}
D) int maximum = arrList.get(0);
For (int counter = 1; counter > arrList.size(); counter++)
{
If (arrList.get(counter) < maximum)
{
Maximum = arrList.get(counter);
}
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
71
What is the output of the following code snippet?
ArrayList num;
Num)add(4);
System.out.println(num.size());

A) 1
B) 0
C) 4
D) Error because num is not initialized
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
72
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?

A) public static void passAList(ArrayList myList(10), int[] intArr)
B) public static void passAList(ArrayList myList, int[20] intArr)
C) public static void passAList(ArrayList myList, int[] intArr)
D) public static void passAList(ArryaList myList, int intArr)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
73
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.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
74
Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter?

A) public static void passAList(ArrayList num[5])
B) public static void passAList(ArrayList num, 5)
C) public static void passAList(ArrayList num)
D) public static void passAList(num)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
75
Which one of the following statements about declaring an array list as a method parameter is true?

A) An array list declared as a method parameter should be accompanied with its size.
B) An array list declared as a method parameter is a constant value by default.
C) An array list value cannot be modified in a method when the array list is declared as a parameter.
D) An array list value can be modified inside the method.
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
76
Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10?

A) public static void func(int[][] arr)
B) public static void func(int[10][] arr)
C) public static void func(int[][10] arr)
D) public static void func(int[10][10] arr)
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
77
Consider the following code snippet:
Int val = arr[0][2];
Which value of arr is stored in the val variable?

A) The value in the first row and the second column
B) The value in the first row and the first column
C) The value in the first row and the third column
D) The value in the third row and the second column
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
78
Which one 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?

A) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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));
}
}
B) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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));
}
}
C) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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[i] = num1[i];
}
}
D) ArrayList num1 = new ArrayList();
ArrayList evennum = new ArrayList();
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[i] = num1[i];
}
}
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
79
What is the output of the following code snippet?
Public static int check(ArrayList listData)
{
Int sum = 0;
For (int i = 0; i < listData.size(); i++)
{
Sum = sum + listData.get(i);
}
Return sum;
}
Public static void main(String[] args)
{
ArrayList vdata = new ArrayList();
Int rsum;
For (int cnt = 0; cnt < 3; cnt++)
{
Vdata.add(cnt + 1);
}
Rsum = check(vdata);
System.out.println(rsum);
}

A) 4
B) 2
C) 3
D) 6
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
80
Consider the following code snippet:
ArrayList num1 = new ArrayList();
Int data;
Scanner in = new Scanner(System.in);
For (int i = 0; i < 5; i++)
{
Data = in.nextInt();
Num1.add(data);
If (data == 0 && num1.size() > 3)
{
Num1.remove(num1.size() - 1);
}
}
System.out.println("size is : " + num1.size());
What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

A) size is : 1
B) size is : 2
C) size is : 4
D) size is : 0
Unlock Deck
Unlock for access to all 100 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 100 flashcards in this deck.