1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Consider an integer array nums, which has been properly declared and initialized with one or more integer values.
Which of the following code segments counts the number of odd values found in nums and stores the count in oddCount?
I.
int oddCount = 0;
int index = -1;
while (index <= nums.length - 2) {
index++;
if (nums[index] % 2 == 1) {
oddCount++;
}
}
II.
int oddCount = 0;
for (int index = 1; index < nums.length; index++) {
if (nums[index] % 2 == 1) {
oddCount++;
}
}
III.
int oddCount = 0;
for (int value : nums) {
if (nums[index] % 2 == 1) {
oddCount++;
}
}
A. I only
B. II only
C. I and II only
D. I and III only
E. I, II, and III
A. I only
Consider the following method.
public static void doubleEverything(int[] numbers) { for (int index = 0; index < numbers.length; index++) { numbers[index] = numbers[index] * 2; } }
Which of the following code segments, if any, can be used to replace the body of the method so that numbers will contain the same values?
I.
for (int num : numbers) {
num = num * 2;
}
II.
for (int num : numbers) {
num[index] = num[index] * 2;
}
III.
for (int num : numbers) {
numbers[index] = numbers[index] * 2;
}
A. I only
B. I and II only
C. II and III only
D. I, II, and III
E. None of the code segments will return equivalent results.
E. None of the code segments will return equivalent results.
Consider the following code segment.
int[] arr = {5, 3, 9, 7, 1};
for (int value : arr) {
value = value + 15;
System.out.print(value + " ");
}
for (int value : arr) {
System.out.print(value + " ");
}
What is printed as a result of executing the code segment?
A. 0 1 2 3 4 0 1 2 3 4
B. 5 3 9 7 1 5 3 9 7 1
C. 15 16 17 18 19 0 1 2 3 4
D. 20 18 24 22 16 5 3 9 7 1
E. 15 16 17 18 19 15 16 17 18 19
D. 20 18 24 22 16 5 3 9 7 1
Consider the following code segment.
for (int count = 1; count < 11; count += 3) {
System.out.print(count);
}
Which of the following code segments will produce the same output as the code segment above?
Choice A
int count = 1;
while (count < 11) {
count += 3;
System.out.print(count);
}
Choice B
int count = 1;
while (count < 11) {
System.out.print(count);
count += 3;
}
Choice C
int count = 0;
while (count < 11) {
System.out.print(count);
count += 3;
}
Choice D
int count = 0;
while (count < 11) {
count += 3;
System.out.print(count);
}
Choice E
int count = 0;
while (count <= 11) {
count += 3;
System.out.print(count);
}
A. Choice A
B. Choice B
C. Choice C
D. Choice D
E. Choice E
B. Choice B
What is the output of the following code snippet?
double[] numArray = new double[10];
numArray[0] = 5.5;
System.out.print(numArray[3]);
A. 0
B. 0.0
C. 10
D. 5.5
E.ArrayIndexOutOfBoundsException
B. 0.0
Consider the following code segment:
int[] values = {1, 7, 3, 4, 9, 2, 1};
int sum = values[3] + values[4];
values[0] = sum;
sum += values[0];
What is stored in sum after this code segment is run?
A. {3, 4, 0}
B. {4, 9, 1}
C. 13
D. 14
E. 26
E. 26
Consider the following three classes.
public class Shape {
public void printShape() {
System.out.print("Shape ");
}
}
public class Square extends Shape {
public void printShape() {
System.out.print("Square ");
}
}
public class Triangle extends Shape {
public void printShape() {
System.out.print("Triangle ");
}
}
What is printed as a result of executing the following code segment?
Shape[] shapes = { new Square(), new Triangle(), new Shape(), new Square() };
for (int index = 0; index < shapes.length; index++) {
shapes[index].printShape();
}
A. Shape Shape Shape Shape
B. Square Triangle Shape Square
C. Square Triangle Triangle Square
D. Square Triangle Square Square
E. Shape Shape Square Shape
B. Square Triangle Shape Square
What is output by the following code?
int total = 0;
for (int outer = 1; outer <= 4; outer++) {
for (int inner = 1; inner <= outer; inner++) {
total += outer;
}
}
System.out.println(total);
A. 4
B. 10
C. 16
D. 20
E. 30
E. 30
Consider the following method, which is intended to return a String array that contains the elements of the parameter names arranged in reverse order.
public String[] reverse(String[] names) {
String[] reversedNames = new String[names.length];
for (int index = 0; index < names.length; index++) {
/ missing statement /
}
return reversedNames;
}
For example, if names contains
{"Ken", "Isaiah", "Jose", "Esosa"}
then a new array containing
{"Esosa", "Jose", "Isaiah", "Ken"}
should be returned and the parameter names should be left unchanged.
Which code segment should replace / missing statement /?
A. reversedNames[index] = names[-index];
B. reversedNames[index] = names[index - names.length];
C. reversedNames[index] = names[index - names.length - 1];
D. reversedNames[index] = names[names.length - index - 1];
E. reversedNames[index] = names[names.length - index];
D. reversedNames[index] = names[names.length - index - 1];
In the following code segment, assume that score has been properly declared and initialized. The code segment is intended to print the number of times score appears in the 1D array grades.
double[] grades = {95.5, 88.0, 94.2, 95.5, 83.5, 80.0, 88.0, 95.5, 98.3}; int count = 0; for (int index = 0; index <= grades.length; index++) { if (grades[index] == score) { count++; } }
The code segment does not work as intended. Which of the following changes should be made so the code segment works correctly?
A. The Boolean expression in the for loop header should be changed to index < grades.length.
B. The Boolean expression in the for loop header should be changed to index < grades.length - 1.
C. The expression in the if statement should be changed to count += count.
D. The condition in the if statement should be changed to grades[index] = score.
E. The condition in the if statement should be changed to grades == score.
A. The Boolean expression in the for loop header should be changed to index < grades.length.
How many times does the body of the for loop below execute?
for (int count = 0; count < 14; count += 3) {
System.out.println(count + " ");
}
A. 4
B. 5
C. 6
D. 14
E. 15
B. 5
The method countEvens below is intended to return the number of times an even number appears in the 1D array numbers. The method may not work as intended.
public int countEvens(int[] numbers) {
int count = 0;
for (int index = 0; index < numbers.length; index++) { // Line 4
if (numbers[index] % 2 == 0) {
count++;
}
}
return count;
}
Which of the following changes, if any, can be made to Line 4 so that the method will work as intended?
A. Changing int index = 0; to int index = 1;
B. Changing index < numbers.length; to index <= numbers.length;
C. Changing index < numbers.length; to index < numbers.length-1;
D. Changing index++ to index += 3
E. No change is necessary, the method works correctly as is.
E. No change is necessary, the method works correctly as is.
Consider the following addFive method.
public class Calculator {
public void addFive(int[] numbers) {
for (int index = 0; index < numbers.length; index += 2) {
numbers[index] += 5;
}
}
}
What values will be stored in values after the following code segment is executed?
int[] values = {-15, 0, 3, 7, 14};
Calculator calc = new Calculator();
calc.addFive(values);
A. {-15, 0, 3, 7, 14}
B. {-10, 5, 8, 12, 19}
C. {-10, 5, 8, 7, 14}
D. {-10, 0, 8, 7, 19}
E. {-15, 5, 3, 12, 14}
D. {-10, 0, 8, 7, 19}
What is printed to the console based on the code below?
String[] animals = {"cat", "puppy", "hippo", "rabbit", "kangaroo"};
for (int index = animals.length - 1; index >= 0; index--) System.out.print(animals[index] + " ");
}
A. catpuppyhipporabbitkangaroo
B. cat puppy hippo rabbit kangaroo
C. cat
puppy
hippo
rabbit
kangaroo
D. kangaroo
rabbit
hippo
puppy
cat
E. kangaroo rabbit hippo puppy cat
E. kangaroo rabbit hippo puppy cat
Consider the following code segment.
int[] numbers = {0, 6, 0, 4, 0, 0, 2};
int count = 0;
for (int index = 0; index < numbers.length; index++) {
if (numbers[index] != 0) {
numbers[count] = numbers[index];
count++;
}
}
int[] updated = new int[count];
for (int index = 0; index < count; index++) {
updated[index] = numbers[index];
}
What will the 1D array updated contain after executing this code segment?
A. 6, 4, 2
B. 0, 0, 0, 0, 6, 4, 2
C. 6, 4, 2, 4, 0, 0, 2
D. 0, 6, 0, 4, 0, 0, 2
E. 6, 4, 2, 0, 0, 0, 0
A. 6, 4, 2