
Unit 6 Progress Check: MCQ
Quiz
•
Computers
•
9th Grade
•
Practice Problem
•
Hard
Sueann Seccafico
Used 2+ times
FREE Resource
Enhance your content in a minute
14 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Consider the following code segment.
int[] arr = {10, 20, 30, 40, 50};
for(int x = 1; x < arr.length - 1; x++) {
arr[x + 1] = arr[x] + arr[x + 1];
}
Which of the following code segments would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5}?
{10, 20, 30, 70, 120}
{10, 20, 50, 90, 50}
{10, 20, 50, 90, 140}
{10, 30, 60, 100, 50}
{10, 30, 60, 100, 150}
Answer explanation
Correct. The code line arr[0] = 10; sets the first element to 10, while the code line arr[1] =10; sets the second element to 10.
2.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Consider the following method.
public int[] transform(int[] a) { a[0]++; a[2]++; return a; }
The following code segment appears in a method in the same class as transform.
/* missing code */ arr = transform(arr);
After executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
I. int[] arr = {0, 0, 0, 0};
II. int[] arr = new int[0];
III. int[] arr = new int[4];
I only
II only
III only
I and II
I and III
Answer explanation
Correct. The transform method increases the first and third elements of the array by 1. In order to produce the intended result, the array passed to the method should contain {0, 0, 0, 0}. Code segment I works as intended. This statement uses an initializer list to create an array with four elements, each equal to 0. Code segment II does not work as intended. The statement creates a new array of length 0 (an array with no elements). Code segment III works as intended. The statement creates a new array of length 4, whose elements are each assigned the default value 0.
3.
MULTIPLE SELECT QUESTION
1 min • 1 pt
Consider the following method, which is intended to return the number of strings of length greater than or equal to 3 in an array of String objects.
public static int checkString(String[] arr) {
int count = 0;
for (int k = 0; k < arr.length; k++) {
if (arr[k].length() >= 3) {
count++; }
}
return count;
}
Which of the following code segments compile without error?
I. checkString(new String[]);
II. checkString(new String[0]);
III. String[] str = {"cat", "dog"}; checkString(str);
I only
II only
III only
I and II
I and III
Answer explanation
The size of an array must be established when the array is created, either explicitly by specifying the size within the brackets or implicitly by using an initializer list. Statement I does not compile because the array size is missing. Statement II compiles because it correctly creates an array of size 0. Statement III compiles because it creates an array of size 2, using an initializer list.
4.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Consider the following code segment.
int[] arr = {10, 20, 30, 40, 50};
for(int x = 1; x < arr.length - 1; x++) {
arr[x + 1] = arr[x] + arr[x + 1]; }
Which of the following represents the contents of arr after the code segment has been executed?
{10, 20, 30, 70, 120}
{10, 20, 50, 90, 50}
{10, 20, 50, 90, 140}
{10, 30, 60, 100, 50}
{10, 30, 60, 100, 150}
Answer explanation
The code starts with the second element of the array (i.e., the element at index 1) and adds the current element to the next element of the array. The code will set the third element to 20 + 30, which is 50; it will then set the fourth element to 50 + 40, which is 90; finally, it will set the last element to 90 + 50, which is 140.
5.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Consider the following code segment.
int[] arr = {4, 3, 2, 1, 0}; int total = 0; for (int k = 0; k <= total; k++) {
if (arr[k] % 2 == 0) {
total += arr[k]; } else { total -= arr[k]; }
}
System.out.print(total);
What, if anything, is printed as a result of executing the code segment?
2
1
0
-4
Nothing is printed because the code segment causes a runtime error.
Answer explanation
During the first iteration of the for loop, the Boolean expression arr[0] % 2 == 0 or 4 % 2 == 0 evaluates to true, so total is incremented by 4, becoming 4. During the second iteration of the for loop, the Boolean expression arr[1] % 2 == 0 or 3 % 2 == 0 evaluates to false, so total is decremented by 3, becoming 1. At this point, the Boolean expression k <= total or 2 <= 1 evaluates to false, and the for loop terminates. The current value of total, 1, is printed.
6.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
The array fruits is declared below.
String [] fruits = {"apples", "bananas", "cherries", "dates"};
Which of the following code segments will cause an ArrayIndexOutOfBoundsException?
I. for (int i = 0; i <= fruits.length; i++) {
System.out.println(fruits[i]); }
II. for (int i = 0; i <= fruits.length - 1; i++) {
System.out.println(fruits[i]); }
III. for (int i = 1; i <= fruits.length; i++) {
System.out.println(fruits[i - 1]); }
I only
II only
I and III only
II and III only
I, II, and III
Answer explanation
Code segment I iterates i from 0 to 4. The last array element is at index 3, so an ArrayIndexOutOfBoundsException occurs when attempting to access the element at index 4. Code segment II iterates i from 0 to 3. It accesses each element of the array without error. Code segment III iterates i from 1 to 4. Since it accesses array elements at index i - 1, it accesses the array elements at indexes 0 to 3 without error.
7.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
The Fibonacci numbers are a sequence of integers. The first two numbers are 1 and 1. Each subsequent number is equal to the sum of the previous two integers. For example, the first seven Fibonacci numbers are 1, 1, 2, 3, 5, 8, and 13.
The following code segment is intended to fill the fibs array with the first ten Fibonacci numbers. The code segment does not work as intended.
int[] fibs = new int[10];
fibs[0] = 1;
fibs[1] = 1;
for (int j = 1; j < fibs.length; j++) {
fibs[j] = fibs[j - 2] + fibs[j - 1]; }
Which of the following best identifies why the code segment does not work as intended?
In the for loop header, the initial value of j should be 0.
In the for loop header, the initial value of j should be 2.
The for loop condition should be j < fibs.length - 1.
The for loop condition should be j < fibs.length + 1.
The for loop should increment j by 2 instead of by 1.
Answer explanation
Correct. If j starts at 1, the attempt to access fibs[j - 2] during the first loop iteration will throw an ArrayIndexOutOfBoundsException. Starting the loop at j = 2 avoids this error.
Access all questions and much more by creating a free account
Create resources
Host any resource
Get auto-graded reports

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
17 questions
Basic Docs
Quiz
•
8th - 9th Grade
10 questions
Logic Gates
Quiz
•
9th - 12th Grade
15 questions
CIW
Quiz
•
9th - 12th Grade
10 questions
Word basics
Quiz
•
8th - 12th Grade
10 questions
Online Databases vs. Search Engines
Quiz
•
6th - 10th Grade
15 questions
Systems Architecture
Quiz
•
KG - 9th Grade
10 questions
Microsoft Publisher
Quiz
•
7th - 10th Grade
10 questions
Virus Informáticos
Quiz
•
1st - 10th Grade
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
fractions
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
15 questions
Equivalent Fractions
Quiz
•
4th Grade
20 questions
Figurative Language Review
Quiz
•
6th Grade
