ap computer science lesson 4 quizzes

Ace your homework & exams now with Quizwiz!

9

Evaluate the following code segment. What is the value of count after execution? (3 points) int count = 1;for(int outer = 0; outer < 4; outer++)for(int inner = 1; inner < 3; inner++)count++;

0 <= x <= 9

Errors can be syntax errors or logic errors (the code works, but not as intended). What is the range of possible values for the variable x? (2 points) int x = (int)(Math.random() * 10);

0, 1, 2, 3, 4, 5

Assume rand has been declared as an object of the Random class. Consider the following statement: int randomNum = rand.nextInt(5 + 1); Which set or sets of integers represent possible values for randomNum? (3 points)

options 2 and 3 only

Errors can be syntax errors or logic errors (the code works, but not as intended). Given the following code segment, which of the expressions could take the place of < condition > to cause 10 iterations of the while loop? (2 points) int flipCount = 0;while( ‹condition› ){// code not shownflipCount++;} flipCount <= 10 flipCount <= 9 flipCount != 10

The value of answer is Y

Errors can be syntax errors or logic errors (the code works, but not as intended). What conclusion can be made about the state of the program when the while loop terminates? Assume answer is a declared and initialized String. (4 points) while( !answer.equals( "Y")){// code not shown}

0 3 6 9 12 15 18

Errors can be syntax errors or logic errors (the code works, but not as intended). What output will be produced by the following code segment? (2 points) int num = 0;while(num <= 15){System.out.print(num + " ");num += 3;}System.out.println(num);

option 2 and 3 only

Errors can be syntax errors or logic errors (the code works, but not as intended). Which of the following is equivalent to while(userGuess != secretNumber)? (4 points) while( userGuess < secretNumber && userGuess > secretNumber) while( userGuess < secretNumber || userGuess > secretNumber) while( !(userGuess == secretNumber) )

option 2 only

Errors can be syntax errors or logic errors (the code works, but not as intended). The loop must iterate 3 times to generate a lottery number. Which of the following loops contains an error? (4 points) for(int n = 1; n < lotNumLength; n++) for(int n = 1; n <= lotNumLength; n++) for(int n = 0; n < lotNumLength; n++)

option 2 only

Telephone numbers are in the form of 1234567890. The first 3 digits, "123," are considered the area code. Consider the code below: String phoneNum1 = //contains any valid phone number, implementation not shown Which of the following statements correctly obtains the area code of the number? (4 points) String areaCode1 = phoneNum1.substring(0, 2); String areaCode1 = phoneNum1.substring(0, 3); String areaCode1 = phoneNum1.substring(3);

A triangle resting on its base of 5 stars

Using the following code segment, which best describes the output? (3 points) for(int outer = 0; outer < 5; outer += 2){for(int inner = 0; inner < outer + 1; inner++){System.out.print("*");}System.out.println();}

if(phoneNum1.compareTo(phoneNum2) > 0) { //phoneNum1 comes after phoneNum2 }

Consider the code below: String phoneNum1 = //contains any valid phone number, implementation not shownString phoneNum2 = //contains any valid phone number, implementation not shown Which of the following blocks of code determines that phoneNum1 comes after phoneNum2 numerically? (4 points) Example:String phoneNum1 = "407-555-2207";String phoneNum2 = "407-555-1000";

9

Errors can be syntax errors or logic errors (the code works, but not as intended). How many times will the following while loop repeat? (2 points) int flipCount = 1;while(flipCount < 10){// code not shownflipCount++;}

The looper iterates one too few times

Errors can be syntax errors or logic errors (the code works, but not as intended). The intent of the following code segment is to simulate flipping a coin five times. What happens when the code is run? (2 points) int flipCount = 1;while(flipCount < 5){//code not shownflipCount++;}


Related study sets

AEB 3133 Exam 2 (Modules 3 & 4) Spring 23

View Set

Chapter 18 - Global Climate Change

View Set

Khan Academy Biology Questions Chapter 1

View Set

Targeted Medical-Surgical 2016: Cardiovascular

View Set

The Word Within the Word LIst 1-15

View Set

Foundations Ch 39 Fluid Imbalance PrepU

View Set