Computer Science 2 Midterm

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

What value is returned as the result of the call mystery(5)?

243

for(int i = 0; i < 5; i++) { for(int j=0; j < 5; j++) System.out.println("*");} How Many stars

25

int[][] arr = {{ 6, 2, 5, 7 }, { 7, 6, 1, 2}}; for(int j =0; j < arr.length; j++) { for(int k = 0; k < arr[0].length; k++) { if(arr[j][k] > j + k) { System.out.println("!"); } How many times will "!" be printed when the code segment is executed?

6

int[] nums = {3, 6, 1, 0, 1, 4, 2}; What value will be returned as a result of the call mystery(nums)?

7

!(!(a != b ) && (b > 7))

(a != b) || (b <= 7)

Assume that myList is an ArrayList that has been correctly constructed and populated with objects. Which of the following expressions produces a valid random index for myList?

(int)( Math.random() * myList.size() )

int[] arr = {7, 2, 5, 3, 0, 10}; for (int k = 0; k < arr.length - 1; k++) { if (arr[k] > arr[k + 1]) System.out.print(k + " " + arr[k] + " "); }

0 7 2 5 3 3

new integer 37, 3, 0 What is printed as a result of executing the code segment?

1,2,0

recur class print obj factor 5

120

What value is returned as a result of the call recur(27)?

16

int[] nums = {0, 4, 4, 5, 6, 7}; int result = bSearch(nums, 0, nums.length - 1, 4); What is the value of result after the code segment has been executed?

2

int[] nums = {10, 20, 30, 40, 50}; int result = bSearch(nums, 0, nums.length - 1, 40); How many times will the bSearch method be called as a result of executing the code segment, including the initial call?

2

How many times will the merge method be called as a result of executing the code segment?

3

How many times will the bSearch method be called as a result of executing the statement, including the initial call?

4

What value will be returned by the call binarySearch(inputList, 0, 8, 70)?

6

Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?

After the change, the method will never check the first element in list b

Which of the following code segments will assign the correct String to grade for a given integer score?

I and III only (the one that has the else at the bottom and the one that has the &&)

Assume that sum1D works correctly. Which of the following can replace / * missing code * / so that the sum2D method works correctly? (returns the sum of elements in the 1d arr

I, II, III

For which of the following test cases will the call seqSearchRec(5) always result in an error? data contains only one element. data does not contain the value 5. data contains the value 5 multiple times.

II only

Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ?

It has no impact on the behavior of the method.

What is recursion in Java?

Recursion is a way of defining a method to call itself repeatedly.

spices, garlic, onion, pepper /missing code/

String[][] things

Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been properly declared and initialized. Which of the following can be used to print the elements in the four corner elements of arr ?

System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);

Which of the following can be used to print the elements in the four corner elements of arr?

System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);

public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } } Which of the following describes what the method arrayMethod() does to the array nums?

The contents of the array nums are reversed.

Assume that int val has been declared and initialized with a value that satisfies the precondition of the method. Which of the following best describes the value returned by the call what(val)?

The number of digits in the decimal representation of val is returned.

What is printed as a result of the call whatsItDo("WATCH")?

WATC WAT WA W

What will happen if a recursive method doesn't include a base case?

What will happen if a recursive method doesn't include a base case?

What of the following will cause an infinite loop?

Whenever a includes a value that is less than or equal to zero.

Which of the following code segments will produce the same output as the code segment above?

Which of the following code segments will produce the same output as the code segment above?

List<Integer> list1 = new ArrayList<Integer>(); list1.add(new Integer(10)); list1.add(new Integer(27)); list1.add(new Integer(5)); list1.set(2, new Integer(0)); list1.add(2, new Integer(1)); list1.add(new Integer(33)); System.out.println(list1);

[10, 27, 1, 0, 33]

old list new list 100 200 300 400

[200, 400]

System.out.println(mystery(6)); k * k + 3

[4, 7, 12, 19, 28, 39]

ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84);conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating);

[9.84, 8.93, 7.63]

List<Integer> list1 = new ArrayList<Integer>(); list1.add(new Integer(10)); list1.add(new Integer(27)); list1.add(new Integer(5)); list1.set(2, new Integer(0)); list1.add(2, new Integer(1)); list1.add(new Integer(33)); System.out.println(list1.isEmpty());

false

dog cat snake lizard fish remove 3 print

[dog, fish, cat]

(x && y) || !(x && y)

always true

boolean[][] arr = {{ false, true, false }, { false, false, true }};

boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

double[][] table = new double[5][6]; Which of the following method calls returns a value of true ?

checkIndexes(table, 4, 5)

What all gets printed when the following method is run? void test i= +i

i=0j=2 i=0 j=1 i=1 j=2 i=1 j=0

private int[] data; public int seqSearchRec(int target) { return seqSearchRecHelper(target, data.length - 1); } private int seqSearchRecHelper(int target, int last) { // Line 1 if (data[last] == target) return last; else return seqSearchRecHelper(target, last - 1); } Which of the following should be used to replace // Line 1 in seqSearchRecHelper() so that seqSearchRec() will work as intended?

if (last < 0) return -1;

Which of the following choices is the correct/preferred syntax for declaring an array of ten primitive integers? Choose the best answer.

int[] a = new int[10];

public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) result[j] = a1[j]; for (int k = 0; k < a2.length; k++) result[ /* index */ ] = a2[k]; return result; } Which of the following expressions can be used to replace /* index */ so that append will work as intended?

k + a1.length

Which of the following could replace /* missing code */ so that the code segment works as intended? letters a,b,c,

letters[1][0] + letters[2][2] + letters[2][0]

Which of the following is true of method mystery? point a and point b and point c

n will always be greater than 2 at // Point B.

Which of the following represents the value returned as a result of the call compute(n, k)?

n^k

What is printed out if we call stringMagic("Java Is Fun");?

nuF sI avaJ

String[] myPets ;myPets = new String[6] ;myPets[0] = "Sharkey"; myPets[1] = "MyGuy"; myPets[2] = "Rio"; int r = myPets.length;

r has the value of 6

Which one of the following statements would be an accurate replacement for this code?

return somethingIsTrue();

recur class print obj factor 12

runtime error

This method will return true if and only if:

s contains two or more of the same character in a row

int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1]; Which of the following represents the contents of arr as a result of executing the code segment?

{1, 2, 3, 5, 6, 7, 7}

Which of the following represents the arrays merged the last time the merge method is executed as a result of the code segment above?

{30, 50, 80} and {20, 60, 70} are merged to form {20, 30, 50, 60, 70, 80}

For example, if things contains {{ "salad", "soup"}, {"water", "coffee" }}, then count(things) should return 2. Which of the following can be used as the value of things to return 3?

{{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}

What are the contents of mat after the code segment has been executed? new int 3,4

{{2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1}}


Kaugnay na mga set ng pag-aaral

Chapter 2: Evaluating Nutrition Information

View Set

Cisco Ethernet Concepts / Modules 4-7

View Set

Exam 2--Ch 47: Management of Patients With Intestinal and Rectal Disorders

View Set

Narrative Poem: The Wife of Bath's Tale

View Set

Marketing Management Chapter Eleven

View Set

Chapter 3: Investment Companies and Life Insurance Products

View Set

Managerial Accounting - Chapter 2

View Set