10.12 Part A Segment Two Exam - Multiple-Choice

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is output by the following code? ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b =a; a.add(4); b.add(5); a.add(6); b.add(7); System.out.println(a.get(3));

7

What is the maximum number of locations a binary search algorithm will have to examine when looking for a particular value in a sorted array of 500 elements? 7 1 2 9 500

9

What is the output of the following code? ArrayList<Integer> grades = new ArrayList<Integer>(); grades.add(88); grades.add(92); grades.add(95); grades.add(1, 80); grades.add(83); System.out.println(grades.get(1 + 5 % 2)); 95 92 An IndexOutOfBoundsException occurs 88 An ArrayIndexOutOfBoundsException occurs

92

Assume a two-dimensional array is declared as follows: int arr[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; Which of these statements are true? arr.length is 3 arr[0].length is 3 arr[2][3] is 12 I only I and III only III only I and II only I, II, and III

I and III only

Which keyword is required if you want to define a class constant? private abstract public int final

final

Consider the following code: String[] fruits = {"apple", "pear", "mango", "peach"}; int i = 3; String str = "p"; for(String item : fruits){ i = item.indexOf("p") + 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 StringIndexOutOfBoundsException occurs. learangoach ppplepearpeach peearmangoeach plearangoach

plearangoach

What is stored in the array arr after the following code executes? (2 points)int[ ] arr = {11, 33, 50, 70};int i = 0;while(i < arr.length){if((arr[i] % 2) == 1){arr[i] = arr[i] % 3;}else{arr[i] = arr[i] / 3;}i++;} {2, 10, 3, 16} {0, 10, 16, 23} {2, 0, 16, 23} An ArrayIndexOutOfBoundsException occurs. {2, 16, 23, 0}

{2, 0, 16, 23}

Consider the following code block. String str = "Good Evening. How are you?"; String newString = str.substring(0, str.indexOf(".")); Which of the following BEST describes the result of the code above? "12" is assigned to newString There is a syntax error in the code "Good Evening" is assigned to newString "How are you?" is assigned to newString "Good Evening." is assigned to newString

"Good Evening" is assigned to newString

Which of the following expressions is equivalent to !(a && b)? (!a) && (!b) (a && b) (!a) || (!b) (a != b) (a || b)

(!a) || (!b)

Consider the following code segment. What would print when the code executes? int num1 = 12; int num2 = 2; while (num2 < num1){ System.out.print(num1 % num2 + " "); num2++; } 0 0 0 2 0 5 4 3 2 1 0 0 0 2 0 5 0 3 2 1 0 0 0 2 0 5 4 3 2 The loop never executes 0 0 2 0 5 4 3 2 1

0 0 0 2 0 5 4 3 2 1

Consider the code block below. for (int x = 0; x < 3; x++ ) { for (int y = 1; y < x; y++ ) { System.out.print(x + " "); }} What is the result when the code executes? 3 2 2 Nothing is printed 2 3 2

2

How many dimensions are in the array? int[][] array1 = new int[15][10]; 1 3 2 0

2

Consider the code segment below. int x = 3; int y = 5; int z = 12; if (x > 0 && y <= 10){ System.out.println(y − x * z % x + y); } What would print as a result? 10 11 5 1 0

10

The following method is intended to remove all values from the ArrayList < Integer > aList that have the same value as val; however, this method does NOT work correctly. public void removeValue(ArrayList < Integer > aList, int val){ int i; for(i = 0; i < aList.size(); i++){if(aList.get(i) == val){ aList.remove(i);}}} If aList initially contains 2 3 4 3 3 4 4 5 4 3 2 1 and val is equal to 3, then aList should contain 2 4 4 4 5 4 2 1 after removeValue is invoked. What does aList actually contain after removeValue is invoked? (2 points) 2 4 3 3 4 4 5 4 3 2 1 2 4 4 4 5 4 2 1 2 4 4 3 4 4 5 4 2 1 2 4 3 4 4 5 4 3 2 1 2 4 3 4 4 5 4 2 1

2 4 3 4 4 5 4 2 1

Consider the method below. What would the result of test(0) be public static int test(int n){ if (n == 0)return 3; else return 2 * test(n - 1);} 22 3 6 24 1

3

What is the value of vals[1][2]? double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}};

3.5

Consider the following method: public int mystery(int n){ if(n > 6){ return 1 + mystery(n - 1); } return n % 3; } What is the value of mystery(10)? 10 8 9 4 6

4

What is output by the following code? ArrayList < Integer > a = new ArrayList < Integer >(); ArrayList b = a; a.add(4); b.add(5); a.add(5); b.add(7); System.out.println(b.get(0)); An IndexOutOfBoundsException occurs. 7 5 6 4

4

What is the maximum number of locations a sequential search algorithm will have to examine when looking for a particular value in an array of 50 elements? 50 25 6 1 12

50

What is the value of vals[1][3]? double[][] vals = {{1.1, 1.3, 1.5}, {3.1, 3.3, 3.5}, {5.1, 5.3, 5.5}, {7.1, 7.3, 7.5}}; 1.5 An ArrayIndexOutOfBoundsException error 3.5 1.1

An ArrayIndexOutOfBoundsException error

Suppose the following array and integer are declared: int[] numbers = {4, 5, 6, 7}; int index = 1 + 6 % 4 * 2; What is the value of numbers[index]? 7 An ArrayIndexOutOfBoundsException occurs. 6 5 4

An ArrayIndexOutOfBoundsException occurs.

Assume arr2 is declared as a two-dimensional array of integers. Which of the following segments of code successfully calculates the sum of all elements arr2? int sum = 0; for(int j = 0; j < arr2.length; j++){ for(int k = 0; k < arr2[j].length; k++){ sum += arr2[j][k]; }} int sum = 0; for(int j = 0; j < arr2.length; j++){ for(int k = 0; k < arr2[j].length; k++){ sum += arr2[k][j]; }} int sum = 0; for(int[] m : arr2){ for(int n : m){ sum += n; }} I and III only II only I only I and II only II and III only

I and III only

Assume that you have an array of integers named arr. Which of these code segments print the same results? int i = 0;while (i < arr.length){System.out.println(arr[i]);i++;} int i;for (i = 0; i <= arr.length; i++){System.out.println(arr[i]);} for (int i : arr){System.out.println(i);} II and III only All three print different results All three print the same results I and III only I and II only

I and III only

Consider the expression: !((x > y) && (y <= 0)). It is equivalent to which of the following expressions? (2 points) !(x > y) || !(y <= 0) !(x > y) && !(y <= 0) (x <= y) || (y > 0) I only II only II and III only I and III only III only

I and III only

Suppose you write a subclass of Insect named Ant. You add a new method named doSomething to the Ant class. You write a client class that instantiates an Ant object and invokes the doSomething method. Which of the following Ant declarations will NOT permit this? Insect a = new Ant(); Ant a = new Ant(); Ant a = new Insect(); II only I and II only III I and III only I only

I and III only

Consider the Animal, Bird, and Cardinal classes shown below. Which of the following object declarations will compile without error? public class Animal{/* constructors and methods not shown */} public class Bird extends Animal{/* constructors and methods not shown */} public class Cardinal extends Bird{/* constructors and methods not shown */} Cardinal red = new Cardinal(); Animal tweety = new Bird(); Bird chirpy = new Cardinal(); II and III only I, II and III III only I only II only

I, II and III

int x = __; int y = __; if (y / x > 0)System.out.print("first "); System.out.print("second"); For which values of x and y below will this program execute "first second"? x = 5; y = 10; x = 6; y = 7; x = 1; y = 3; I only I, II and III I and II only II only II and III only

I, II and III

Assume that you have an array of integers named arr. Which of these code segments print the same results? int i = 0;while(i < arr.length){System.out.println(arr[i]);i++;} int i;for(i = 0; i < arr.length; i++){System.out.println(arr[i]);} for(int i : arr){System.out.println(i);} I, II, and III I and III only II and III only I and II only II only

I, II, and III

Consider an integer array, vals, which has been declared with one or more integer values. Which of the code segments updates vals so that each element contains the current value less 2? for (int v: vals){v = v −2;} for (int m = 0; m < vals.length; m++ ){vals[m] = vals[m] −2;} int m = 0;while (m < vals.length){vals[m] = vals[m] − 2;} I, II, and III II and III III only II only I and III

II only

String[][] letters = {{"A", "B", "C", "D"}, {"E", "F", "G", "H"}, {"I", "J", "K", "L"}}; for(int x = 1; x‹letters[0].length; x++){ for(int y = 2; y‹letters.length; y++){ System.out.print(letters[y][x] + " "); }} What is the result when the code executes? J K L J G K H L B F J C G K D H L F J G K H L E I F J G K H L

J K L

Which of the following sorting algorithms is written recursively? Binary Merge Selection Insertion Sequential

Merge

Consider the code block below. String str = " hi "; for (int x = 0; x < 3; x++ ) { for (int y = 1; y < 1; y++ ) { System.out.print(x + str);}} What is the result when the code executes? 2 hi Nothing is printed 2hi 3hi 2 hi 3 hi 2hi 2hi

Nothing is printed

Consider the following declarations: /*** Write a description of class SomeList here.** @author (your name)* @version (a version number or a date)*/ public class SomeListCorrected{ private int maxItems = 100; // some value - I chose 100 private int[] items = {1, 4, 9, 16, 25, 36, 49, 64, 81}; private int numItems = items.length; // constructor(s) and other methods not shown//precondition: 0 <= numItems < maxItems public int find (int num){boolean found = false; int loc = 0; while (!found && loc < numItems){ if (items[loc] == num){ found = true; return loc; }else loc++; }return −1; }} Which of the following is a correct postcondition for the find method? Returns −1 if num is not in this list; index of num otherwise Returns index of num if num is in this list; nothing otherwise Returns the number of items currently in this list Returns true if num is found in this list; false otherwise Returns true if num is found in this list; void otherwise

Returns −1 if num is not in this list; index of num otherwise

Which of the following sorting algorithms is described by this text? "Find the smallest item in the array and swap it with the item currently in index 0. Then, find the smallest remaining item, and swap it with the value at index 1. Continue this process until all items are placed into the array in the correct order." Quick sort Merge sort Selection sort Insertion sort Heap sort

Selection sort

The code segment below should print "10 8 6 4 2 0". 1.) int x = 10; 2.) while (x >= 0){System.out.print(x + " "); 3.) x-=2;} Does it work as intended? If NOT, what code segment below would correct it? Line 2: while (x > 1) Line 2: while (x < 1) Line 3: x += 2; Line 1: int x = 12; The code runs as expected

The code runs as expected

The code below is intended to replace all of the items in the array by half of their value. int [ ] arr = {10, 20, 30, 40}; for(int value: arr){ value/=2; } for(int value: arr){ System.out.print(value + " "); } Which BEST explains why the code does NOT work as intended? The value/=2 equation is not formatted correctly The array is declared incorrectly The print statement has a syntax error in it The value of the temporary variable cannot be changed in a for:each loop The syntax of the for:each loop is incorrect

The value of the temporary variable cannot be changed in a for:each loop

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? When index is greater than the arr.length. When index is less than 0. This code cannot throw an exception because the if statement prevents it. When index is equal to 0. When index is equal to the arr.length.

This code cannot throw an exception because the if statement prevents it.

Consider the following code: public class MyClass{ private int secret; public int getSecret(){return secret; }} Is the instance variable secret really private, meaning that it cannot be changed by client code? Yes, because it is private No, because getSecret returns a reference to secret, which allows direct access to the instance variablecloseIncorrect No, because getSecret returns the value of secret, which can be changed by the client Yes, because it is private and non-static No, because MyClass is a public class, and so its instance variables are all accessible by outside classes

Yes, because it is private

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? int sum = 0;for(int i = 0; i < table.length; i++)sum += table[4][i]; int sum = 0;for(int outer = 0; outer < table.length; outer++)for(int inner = 0; inner < table[0].length; inner++)sum += table[outer][4]; int sum = 0;for(int i = 0; i < table[0].length; i++)sum += table[i][4]; int sum = 0;for(int outer = 0; outer < table[0].length; outer++)for(int inner = 0; inner < table.length; inner++)sum += table[outer][4]; int sum = 0;for(int i = 0; i < table.length; i++)sum += table[i][4];

int sum = 0;for(int i = 0; i < table.length; i++) sum += table[i][4];

Polymorphism occurs at? none of the above run time early binding time debug time compile time

run time


Ensembles d'études connexes

Chapter 18: The New South and The New West

View Set

Silvestri Practice Questions - Chapter 66 (Addictions)

View Set

2. Disruptive innovation and ambidexterity (Connect, Perform)

View Set

Taxonomy, Fish, Amphibians, Reptiles

View Set

Counting Atoms, Elements, and Molecules

View Set

Bellwork End of 3rd Quarter Final

View Set

Practice Exam - Salesperson Course 1

View Set

SmartBook Ch. 16.1 - 16.2 Receptor and General Senses

View Set

Wrist and Hand, Vasculature, Connective Tissue

View Set