
AP Computer Science A - Code Segment Question
Authored by Eagle Chan
Computers
12th Grade
Used 1+ times

AI Actions
Add similar questions
Adjust reading levels
Convert to real-world scenario
Translate activity
More...
Content View
Student View
25 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following code segment. for (int j = 0; j < 4; j++) { for (/* missing code */) { System.out.print(j + " "); } System.out.println(); } This code segment is intended to produce the following output: 0 1 1 2 2 2 3 3 3 3 Which of the following can be used to replace /* missing code */ so that this code segment works as intended?
int k = 0; k < j; k++
int k = 0; k <= j; k++
int k = j; k < 4; k++
int k = j; k <= 4; k++
2.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
What is the correct answer?
The outer loop counts from j = 0 up to j = 3, incrementing by 1. The inner loop counts up from k = 0 to k = j.
The outer loop counts from j = 1 up to j = 4, incrementing by 1. The inner loop counts up from k = 1 to k = j.
The outer loop counts from j = 0 up to j = 3, incrementing by 2. The inner loop counts up from k = 0 to k = j.
The outer loop counts from j = 1 up to j = 4, incrementing by 2. The inner loop counts up from k = 1 to k = j.
3.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following code segment. int val = 48; int div = 6; while ((val % 2 == 0) && div > 0) { if (val % div == 0) { System.out.print(val + " "); } val /= 2; div--; } What is printed when the code segment is executed?
48 12 6
48 12 6 3
48 12 6 3 1
48 24 12 6
4.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following code segment. for (int j = 4; j > 0; j--) { for (/* missing code */) { System.out.print(k + " "); } System.out.println(); } This code segment is intended to produce the following output. 0 1 2 3 0 1 2 0 1 0 Which of the following can be used to replace /* missing code */ so that this code segment works as intended?
int k = 0; k < j; k++
int k = 0; k <= j; k++
int k = j; k > 0; k--
int k = j; k >= 0; k--
5.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following code segment, which is intended to print the sum of all the odd integers from 1 to 101, inclusive. int r = 1; int sum = 0; while (r <= 101) { /* missing code */ r++; } System.out.println(sum); Which of the following can be used to replace /* missing code */ so that the code segment works as intended?
sum += r;
sum += r * r;
sum += r / 2;
sum += r - 1;
6.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following code snippets correctly adds the value of r to sum only when r is odd?
if (r % 2 != 1) { sum += r; }
if (r % 2 != 1) { sum++; }
if (r % 2 == 1) { sum += r; }
if (r % 2 == 1) { sum++; }
7.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following code segment. The code is intended to read nonnegative numbers and compute their product until a negative number is read; however, it does not work as intended. (Assume that the readInt method correctly reads the next number from the input stream.) int k = 0; int prod = 1; while (k >= 0) { System.out.print("enter a number: "); k = readInt(); // readInt reads the next number from input prod = prod * k; } System.out.println("product: " + prod); Which of the following best describes the error in the program?
The variable prod is incorrectly initialized.
The while condition always evaluates to true.
The negative number entered to signal no more input is included in the product.
If the user enters a zero, the computation of the product will be terminated prematurely.
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?