Ap Comp-Sci A - Unit 6

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

["X", "O", "O", "X", "O", "O", "X", "O", "O"]

/

What is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; int i = 0; while (i < scores.length - 1) { System.out.println(scores[i] * 2); i ++; } Java 160184182136 160184182136176 184182136 Infinite loop

1

What is the output of the following code snippet? int x = 25; int[] numArray = new int[x]; x = 100; System.out.println("x is " + x); System.out.println("The size of numArray is " + numArray.length); x is 100 The size of numArray is 25 x is 25 The size of numArray is 100 length is undefined ArrayOutOfBoundsException Answered

1

What is the output of the following code snippet? int[] scores = {80, 92, 91, 68, 88}; for(int score : scores) { System.out.println(score); } 8092916888 80 92 91 68 88 ArrayIndexOutOfBounds exception will be thrown score

1

What will be the output of the following code snippet? int[] scores = {80, 92, 91, 68, 88}; int myIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] < scores[myIndex]) { myIndex = i; } } System.out.println(scores[myIndex]); 68 92 88 3

1

When would you use a for-each loop instead of a for loop? If you want to access every element of an array and want to refer to elements through a variable name instead of an array index. If you want to modify elements of the array. If you want to iterate over every other element in an array. If you want to iterate over only a few elements of an array or collection.

1

Which index is the last element in an array called highTemps? highTemps.length - 1 highTemps.length highTemps.length() highTemps.length() - 1

1

Which of the following code snippet gets the THIRD element in the scores array and assigns it to an int called myScore , then sets the FOURTH element in the scores array (previously declared) to 72? int myScore = scores[2]; scores[3] = 72; int myScore = scores[3]; scores[4] = 72; myScore = scores[3]; scores[4] = 72; scores[4] = 72; int myScore = scores[3];

1

Which of the following loops will NOT print every element in an integer array? for (int i = 1; i < array.length; i++) { System.out.println(array[i-1]); } for (int i = 1; i <= array.length; i++) { System.out.println(array[i-1]); } for (int number : array) { System.out.println(number); } int i = 1; while (i <= array.length) { System.out.println(array[i-1]); i++; } int i = 0; while (i < array.length) { System.out.println(array[i]); i++; }

1

Which of the following statements is valid? Assume that variable arr is an array of n integers and that the following is true: arr[0] != arr[i] for all values from 1 through n - 1 The element arr[0] does not occur anywhere else in the array arr has no duplicates arr has duplicates arr is sorted arr is not sorted

1

How do you initialize an array called arr of 5 ints with default values? int[] arr = new int[6]; int[] arr = new int[5]; int arr = new int[5]; int arr = new int;

2

True or False: You can change a primitive value stored in an array by updating the enhanced for-loop variable. True False

2

What does the following code do when executed? int[] highTemp = {88, 92, 94, 90, 88, 83}; int target = 90; int count = 0; for (int temp : highTemp) { if (highTemp >= target) { count ++; } } System.out.println(count); It will count the number of times the value in the highTemp array exceeds the target value. It will produce an error because the conditional statement is not set up correctly. It will produce an ArrayIndexOutOfBoundsException It will count the number of times that the value in the array is greater than the previous value in the array.

2

The following code is designed to return true if a target value is present in an array of integers. public boolean search(int[] arr, int target){ for (int number : arr) { if (number != target) { return false; } } return true; } Which of the following statements is true? The code will not work correctly because the loop will not access each of the numbers correctly. The code will not work correctly because it may return true too soon. The code will not work correctly because the method may return false too soon. The code will work as intended.

3

The following codes are intended to add 5 to each item in the array.I. int[] numbers = {1, 2, 3, 4}; for (int i = 0; i < numbers.length; i++) { numbers[i] += 5; } II. int[] numbers = {1, 2, 3, 4}; for (int number : numbers) { number += 5; } Which statement is true? Both I and II will correctly add 5 to the numbers array. Neither I nor II will correctly add 5 to the numbers array. I will correctly add 5 to the numbers array. II will not correctly add 5 to the numbers array. II will correctly add 5 to the numbers array. I will not correctly add 5 to the numbers array.

3

What does the following code snippet check for? int[] numbers = {1, 2, 3, 3, 4, 5}; boolean mysteryBoolean = false; for (int i = 0; i < numbers.length - 1; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] == numbers[j]) { mysteryBoolean = true; } } } Reorders the array from smallest to largest value Reorders the array from largest to smallest value Finds duplicate values in an array Accesses all consecutive pairs of

3

What is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; for(int i = 0; i < scores.length; i++) { System.out.println(scores[i] - 1); } Java 8092916888 8193926989 7991906787 91906787

3

What will the following code snippet output? int[ ] values = {17, 34, 56, 2, 19, 100}; for (int value : values) { if (value % 2 == 0) System.out.println(value + " is even"); } 17 is even19 is even 17 is odd19 is odd 34 is even56 is even2 is even100 is even 34 is odd56 is odd2 is odd100 is odd

3

What does the following code snippet do? int[] scores = {80, 92, 91, 68, 88}; int sum = 0; for (int i = 0; i < scores.length; i++){ sum += scores[i]; } System.out.println((double) sum / scores.length); Prints the maximum value of the array Prints the median of the array Prints the sum of the array Prints the average of the array Answered

4

What is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; for (int i = 0; i < scores.length; i += 2) { System.out.println(scores[i] = scores[i] * 2); } Java 160182 160184182136176 809188 160182176

4

What is the output after this code snippet runs? int[] scores = {80, 92, 91, 68, 88}; for(int i = 0; i < scores.length; i--) { System.out.println(scores[i]); } Java 8092916888 8193926989 91906787 ArrayIndexOutOfBoundsException

4

Which of the following arrays has a length of 5? double[] arr = new double[6]; double[] arr = {0, 1, 2, 3, 4, 5}; double[] arr = new double[]{1, 2.3, 4, 5}; All of these None of these

4

Which of the following initializes an array of 4 doubles called temperatures with the following initial values: 80.2, 65.3, 12.17, 20.21? int[] temperatures = {80.2, 65.3, 12.17, 20.21}; double[] temperatures; double[7] temperatures = new double[]; double[] temperatures = {80.2, 65.3, 12.17, 20.21}; Answered

4

Which replacement(s) for /* some value */ will always result in correct execution of findMin?I. Integer.MAX_VALUE Java II. Integer.MIN_VALUE Java III. arr[0] Java I only II only III only I and III only II and III only

4

public class SayHello { public void run() { String stringArray[] = {"h", "e", "l", "l", "o", "w"}; for(int i=0; i <= stringArray.length; i++) { System.out.print(stringArray[i]); } } } hel hello hellow Error

4

Given the following code, what will be printed as a result? int[] scores = {80, 92, 91, 68, 88}; int index = 0; int mystery = 91; while (index < scores.length) { if (scores[index] == mystery) { break; } index ++; } System.out.println(index); Java 0 1 2 3

option 3

What will print out when the following code executes? int[] nums = {1, 2, 3, 4, 5, 6}; int count = 0; while (nums[count] % 2 != 0) { nums[count+1] ++; count ++; } System.out.println(count); 0 1 2 3 4

option 3 or 2


Ensembles d'études connexes

BIO 181 Exam 4 Homework Questions

View Set

Chapter 9: Performance Evaluation in Decentralized Organizations

View Set

Com 202 midterm/ final after 143

View Set

TEAS - English - Knowledge of Language

View Set

Life - Policy Provisions, Options And Riders - Terms to know

View Set

English 316.50A/50B Quiz Questions

View Set

Basic economic concepts Unit test

View Set