WorksheetsAPCSA Unit 4 test
Total questions: 35
Worksheet time: 18mins
Write a method that loops until the user inputs the correct secret password or until the user fails to enter the correct password 10 times. The secret password the program should look for is the String "secret"
Assume that a Scanner object called input has been correctly initialized.
What would the method call myMethod("Karel The Dog", 'e') return?
1
2
3
4
What kind of error would the method call myMethod("Frog") cause?
Compile Time Error: String index out of range
Runtime Error: String index out of range
Compile Time Error: Unexpected return value
Runtime Error: Syntax Error in method definition
What will the call to the method funWithNumbers(314159) print to the screen?
314159
951413
314
951
What does the call to the method someMethod(3,1) output?
9
12
11
10
Write a method that will ask for user input until user inputs the String “no”. Allow the user to input any capitalization of the String “no” (“no”, “No”, “NO”, “nO”) Also, have the method return the number of loops.
Assume that a Scanner object input has already been created.
What will the call to method patternGrid(3,4,'#') print?
####
####
####
###
###
###
####
####
####
####
This code will error.
What would the call to method divideByTen(340) output?
340
40
30
3
34
Consider the method:
What is returned as a result of the call mystery("Lunchbox", 4)?
xobh
xoxxob
xoxbox
Lunc
LLuLun
Consider the code segment:
What will the value of count be after executing the code segment?
2
1
10
4
3
Consider the code segment:
What will the final result be when run in the console?
Consider the code segment:
The code segment is intended to print every other letter in the word, starting with index 0, to produce the result C f t r a. Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?
int i = 0; i < word.length(); i+=2
int i = -1; i < word.length(); i+=2
int i = 0; i < word.length(); i++
int i = -1; i < word.length(); i+=1
int i = word.length() - 1; i < -1; i-=2
Consider the code segment:
Which of the following for loops would produce the same value if System.out.println(sum) were printed? Choose all that apply
I
II
III
IV
Consider the method:
What would the value of sum be after the method call countPs("Peter Piper picked a pack of pickled peppers")?
7
9
2
0
4
How many times will the word "Heyo!" be printed in the code segment?
12
16
20
15
9
Consider the code segment:
Which of the following best explains how changing i > 0 to i >= 0 will change the result?
An additional value will be printed, as the for loop will iterate one additional time.
There will be no change in the program because the for loop will iterate the same number of times.
One fewer value will be printed, as the for loop will iterate one fewer time.
This program will result in an infinite loop, as the condition will never be false.
Consider the method:
What value word would be printed as a result of the call mix("Hippopotamus", "p")?
Hippopotamus
Hi0o0tamus
Hi00o0otamus
Hiootamus
Hi0po0otamus
Consider the incomplete code segment, which is intended to increase the value of each digit in a String by one. For example, if num is 12345, then the resulting String would be 23456.
Which of the following should replace /* Missing Loop Header */ so that the code segment works as intended?
counter < num.length() - 1
counter > num.length() - 1
counter < num.length()
counter > num.length()
counter < num.indexOf(counter)
Consider the code segment:
Which of the following best explains the result of changing j < 4 to j > 4 on line 1?
The numbers will be printed in reverse order compared to the original because the outer loop will occur in reverse order.
The program will produce the same result, as the number of iterations in the outer loop hasn’t changed.
An infinite loop will occur because the termination of the loop will never be reached.
No output will be produced, as the boolean condition will never be met in the outer for loop.
Consider the code segment:
What, if anything, is printed as a result of executing this statement?
150
310
10204080
10204080160
There will not be any result printed, as ints cannot be converted to type String.
Which of the following are not primitive types?
int
String
Double
double
boolean
What is the proper syntax to declare and initialize a variable called temperature to have the value 70.4?
int temperature = 70.4;
double temperature = 70.4;
String temperature = 70.4;
double temperature = 70.4
String temperature = "70.4";
What is the result of this expression?
4 + 8 * 3 / 4 + 5 % 2
11
6
7
1
0
Which expression returns the 1’s place of an integer x?
x % 10
x / 10
x % 100
x + 1
x % 2
Which of the following would equal 2?
Choose three that apply.
int x = 0;
x++;
x+= x;
int y = 4;
y++;
y/=2
int z = 4;
z += 2;
z /= 2;
int a = 6;
a /= 3;
a += 0.5;
int b = 64;
b /= 32;
b -= 0.5;
What are parameters?
The value that a method returns.
The formal values that a method prints to the screen.
The type that is given to a variable.
The formal names given to the data that gets passed into a method.
All non-void methods must have which of the following?
a return value.
a parameter.
an argument.
a print statement.
What is true of a void method?
It returns any value.
It takes no parameters.
It returns no value.
It can take any parameter.
What does the method call mystery("APCSA"); return for the following method?
public String mystery(String x) {
return x * 3;
}
APCSAAPCSAAPCSA
APCSA APCSA APCSA
APCSA3
This method is improperly written.
APCSA 3
What is wrong with this method definition?
public int printPayAmount(int amount) {
System.out.println(amount);
}
This method should be private
Nothing is returned from this method
This is a String type method
The method is never called
Why do we use methods in Java?
I. To make code easier to understand
II. To define global variables
III. To avoid repeated code
IV. To simplify code
V. To avoid needing to define them as public or private
I-IV
III, IV, and V
I, II, III, and IV
I, III, and IV
I, II,III, and V
After the execution of the following lines of code, what will be output onto the console?
String letters = "ABCde"; String name = "Karel the Dog"; String letter = "D"; System.out.println(name.indexOf(letter)); System.out.println(letters.indexOf(name.substring(3,4))); System.out.println(letters.indexOf(letter));
10
4
-1
-1
7
10
0
4
-1
10
7
3
10
3
-1
What will this method call output given yesOrNo(true)?
public String yesOrNo(boolean myBoolean) {
if(myBoolean == true) { return "Yes"; }
else { return "No"; }
}
"N0"
"Yes"
0
This program will erorr
true
Which expression is true?
true && !true
!false || !true
true && false
false || false || !true
What will this program print if the value of grade is 80?
A
B
C
Nothing
The program will not run
