What is output to the screen by the following code?
AP CA A Unit 4 Review

Quiz
•
Computers
•
11th - 12th Grade
•
Hard
Sammi McConnell
Used 60+ times
FREE Resource
20 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
2 1 0 3 2 1 0 3 2 1
1 0 3 2 1 0 3 2 1 0
1 0 3 2 1 0 3 2 1
1 2 3 0 1 2 3 0 1 2
Answer explanation
This has the correct number of iterations and the correct pattern
2.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Assuming that s is a correctly initialized String (that does not end in a space) which of the following best describes what the algorithm below does?
Prints the number of times a space appears in str.
Prints the character after each space in str.
Prints the character before each space in str
Prints each character in str followed by a space.
Answer explanation
The indexOf function returns the index of the first appearance of a character. For each iteration the character in str after this is printed, then str is replaced by everything after that space. This continues until indexOf (“ “) returns -1 when there are no spaces left
3.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
What is output by the following code?
saopmoee
saompoeel
aspoomee
Nothing is printed because there is an out of bounds error
Answer explanation
The code iterates forwards through str1 (“awesome”) starting at s and backwards through str2 (“leopard”) starting at a. It starts printing a character from str1 then str2 going back and forth until st1 runs out of letters in which case the loop ends.
4.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Which of the following statements will return a random odd number between 5 and 15 inclusive?
2 * (int) (6 * Math.random()) + 5
2 * (int) (6 * Math.random() + 5)
(int) (2 * 5 * Math.random() + 5)
2 * (int) (5 * Math.random()) + 5
Answer explanation
This is correct. The Math.random() method returns a random double between 0, inclusive and 1 exclusive. Therefore 6 * Math.random() will return a random double which is greater than or equal to 0 and smaller than 6. When this is cast to an int, the nearest integer value below will be returned. Therefore (int) (6 * Math.random()) will be an int between 0 and 5 inclusive. Multiplying this by 2 ensures every other number is printed: 2 * (int) (6 * Math.random()) returns a random number between 0, 2, 4, 6, 8 or 10 inclusive, and then shifting by 5 creates the values 5, 7, 9, 11, 13, 15
5.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
Set the following for loop header so that it prints the numbers, 10 5 0 -5 -10.
10, > -10, -= 5
10, >= -10, -= 5
-10, <= 10, ++
10, > 10, -= 5
Answer explanation
i starts as 10, decreases by 5 each time, and no longer runs once the value of i is less than -10, so 10 5 0 -5 and -10 are printed
6.
MULTIPLE CHOICE QUESTION
1 min • 1 pt
What does short circuit evaluation mean in the following code?
if a >= b is false it evaluates c != d
if a >= b is false it doesn't evaluate c != d
if a >= b is true it evaluates c != d
c != d is evaluated first, and if true it evaluates a >= b
Answer explanation
This is correct and is how short circuit evaluations works in an && expression (Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR ) you can stop as soon as you find the first condition which satisfies or negates the expression.)
-Explanation of why option if a >= b is true it evaluates c != d is wrong: Even though this is true, it does not represent short circuit evaluation and how it works.
7.
MULTIPLE SELECT QUESTION
1 min • 1 pt
For which of the following replacements for /* missing condition */ will the segment result in an infinite loop? (May choose more than one)
I. a > 0
II. a == 12
III. a % 3 != 5
Answer explanation
Both of these conditions will result in an infinite loop
I - Since a starts at 5, which is great than 0, and because a is increased each time through the loop, it will always be greater than 0, this will result in an infinite loop, but it is not the only condition to cause an infinite loop
II - This will actually result in a loop that never runs
III - Since a value of a % 3 can only equal 0, 1 or 2, regardless of what the value of a is increased to, it will never equal 5, so the loop will always run, but is is not the only condition to cause an infinite loop
Create a free account and access millions of resources
Similar Resources on Quizizz
20 questions
Python

Quiz
•
12th Grade
20 questions
Python variables and operators quiz

Quiz
•
9th - 12th Grade
22 questions
Fundamentals of Programming (GCSE Computer Science AQA)

Quiz
•
9th - 11th Grade
15 questions
Python - Loops, Patterns & List manipulations

Quiz
•
11th Grade
22 questions
Loops Quiz

Quiz
•
11th Grade
22 questions
Strings in python -1

Quiz
•
11th Grade
16 questions
Arithmetic Types Declare Initialize Relation Logical Ops

Quiz
•
9th Grade - University
19 questions
Programming Techniques Survey

Quiz
•
8th - 12th Grade
Popular Resources on Quizizz
15 questions
Multiplication Facts

Quiz
•
4th Grade
20 questions
Math Review - Grade 6

Quiz
•
6th Grade
20 questions
math review

Quiz
•
4th Grade
5 questions
capitalization in sentences

Quiz
•
5th - 8th Grade
10 questions
Juneteenth History and Significance

Interactive video
•
5th - 8th Grade
15 questions
Adding and Subtracting Fractions

Quiz
•
5th Grade
10 questions
R2H Day One Internship Expectation Review Guidelines

Quiz
•
Professional Development
12 questions
Dividing Fractions

Quiz
•
6th Grade