Question: Question 8 Consider the following code: int[] arr = new int[ < some positive integer > ]; int index; // assume arr is filled with
Question 8
Consider the following code: int[] arr = new int[ < some positive integer > ]; int index; // assume arr is filled with values here // assume the user enters a value for index here if((index >= 0) && (index < arr.length)) { System.out.println(arr[index]); } When could this code throw an exception?
A)When index is less than 0.
B)When index is equal to 0.
C)When index is greater than the arr.length.
D)When index is equal to the arr.length.
E)This code cannot throw an exception because the if statement prevents it.
Question 9
Assume table has been declared and initialized as a two-dimensional integer array with 9 rows and 5 columns. Which segment of code will correctly find the sum of the elements in the fifth column?
A)int sum = 0; for(int i = 0; i < table.length; i++) sum += table[4][i];
B)int sum = 0; for(int i = 0; i < table.length; i++)
C)int sum = 0; for(int i = 0; i < table[0].length; i++) sum += table[i][4];
D)int sum = 0; for(int outer = 0; outer < table[0].length; outer++) for(int inner = 0; inner < table.length; inner++) sum += table[outer][4];
E)int sum = 0; for(int outer = 0; outer < table.length; outer++) for(int inner = 0; inner < table[0].length; inner++) sum += table[outer][4];
Question 10
What is the maximum number of locations a binary search algorithm will have to examine when looking for a particular value in an array of 130 elements?
A)1
B)2
C)6
D)8
E)130
Question 11
Which keyword is required if you do NOT want a method to be accessible outside its own class?
A)abstract
B)int
C)final
D)private
E)static
Question 12
Which of the following sorting algorithms is described by this text? "Split the array in two parts. Take each part, and split it into two parts. Repeat this process until each part has only two items. Check the order of the two items and swap if necessary. Then, take the sorted part and combine it with an adjacent part, sorting as you combine. Repeat until all parts have been combined."
A)Insertion sort
B)Merge sort
C)Heap sort
D)Quick sort
E)Selection sort
Question 13
Consider the following code: String[] fruits = {"doggie", "badger", "cat", "gopher"}; int i = 3; String str = "d"; for(String item : fruits) { i = item.indexOf("d") + 2; str += item.substring(i); } System.out.println(str); Assume that the loop accesses array elements in order. What is output by this code?
A)dgiegercatopher
B)dggieeratopher
C)dogbadcat
D)oggieerher
E)A StringIndexOutOfBoundsException occurs.
Question 14
When the following code is run, which of the following exceptions could occur if the precondition is met? // precondition: a is NOT null public String doSomething(ArrayList a, int b) { int i; String s; for(i = 0; i < a.size(); i++) { s = (String)(a.get(i)); if(s.length() == b) { return s; } } return null; }
A)ArithmeticException
B)ArrayIndexOutOfBoundsException
C)ClassCastException
D)IndexOutOfBoundsException
E)NullPointerException
Question 15
Which statement could be used to generate a random number between 1 and 6, inclusive, and assign it to the variable dice?
A)int dice = Math.random(6);
B)int dice = Math.random(6) + 1;
C) int dice = Math.random() * 6;
D)int dice = (Math.random() * 6) + 0.5;
E)int dice = (int)(Math.random() * 6) + 1;
Question 16
Which statements are true for both an abstract class and an interface?
- Both can have concrete methods
- Both imply an inheritance relationship
- Neither can be instantiated
A)I only
B)II only
C)III only
D)I and II only
E)II and III only
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
