WorksheetsGame Design Semester 1 Test Review
Total questions: 75
Worksheet time: 1hrs 27mins
Consider the following code segment:
Scanner sc = new Scanner(System.in);
int input;
input = sc.nextInt();
if(input > 0) { System.out.println("Fizz");
}
else { System.out.println("Buzz");
}
Which user input will result in "Buzz" being displayed on the console?
Consider the following Java code segment:
Scanner sc = new Scanner(System.in);
double input;
input = sc.nextDouble();
System.out.println("You said: " + input);
Which of the following user inputs would cause the program to crash?
Consider the following Main.java file from a repl.it project.
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Scanner kb = Scanner(System.in);
int input;
input = kb.nextInt();
if(input % 3 == 1) {
System.out.println("Fizz");
}
else {
System.out.println("Buzz");
}
}
}
When the project is compiled, it generates the following error message:
Main.java:4: error: cannot find symbol Scanner kb = Scanner(System.in); ^ symbol: method Scanner(InputStream) location: class Main
What must you do to resolve this issue?
A student wrote the following code segment which uses a Scanner named sc to read from System.in.
5
6 int num;
7 String word;
8
9 System.out.print("Enter an integer: ");
10 num = sc.nextInt( );
11
12 System.out.print("Enter a word: ");
13 word = sc.nextLine( );
14
15 if(num > word.length( ))) {
16 System.out.println(num);
17 }
18 else {
19 System.out.println(word);
20 }
21
The program should read in a number and a word from the keyboard and then compare the number provided to the length of the word. If the number entered is greater than the number of characters in the word, the program should display the number. Otherwise, the program should display the word. The program is not behaving as expected. Which of the following would resolve the error?
Consider the following code segment:
String str = “90739”;
char ch = str.charAt(1);
System.out.println(ch);
What is displayed on the console when the code segment runs?
In the "Return the Ball" program provided, which variable represents the current speed and direction of the ball?
import processing.core.PApplet;
public class Sketch extends PApplet {
float diameter;
float xPosition;
float step;
public void settings() {
size(600, 375);
}
public void setup() {
diameter = 25;
xPosition = diameter / 2;
step = 2;
}
public void draw() {
background(0);
fill(255);
ellipse(xPosition, height / 2, diameter, diameter);
if( xPosition + diameter / 2 >= width ) {
step = step * -1;
}
xPosition = xPosition + step;
}
}
What would the output be for this code segment?
int i = 0;
while( i < 10 ) {
System․out․print(i + " ");
i = i + 2;
}
int a = 100;
while( a > 5 ) {
System.out.print(a + " ");
/* Missing Code */
}
The code segment above has some code missing. When complete, the segment should display the following output on the console: 100 25 6 Which of the following choices below should replace /* Missing Code */ so the completed program produces the correct output?
/* Missing Code */
while( a < 10 ) {
System.out.print(a + " ");
a = a + 3;
}
The code segment above has some code missing. When complete, the segment should display the following output on the console: 2 5 8 Which of the following choices below should replace /* Missing Code */ so the completed program produces the correct output
What is the output for the following code segment:
int i = 1;
while( i < 10 ) {
i = i * 2;
System․out․print(i + " ");
}
How many ellipses will display from the following code segment:
int x = 50;
size (300, 300);
while (x <= 250) {
ellipse (x, 90, 40, 60);
x = x + 100;
}
Predict the output of the following Java program.
static int alpha;
static int bravo;
static int charlie;
public static void main(String[] args) {
pandora();
netflix();
youtube();
youtube();
hulu();
}
public static void youtube() {
alpha = bravo - charlie;
bravo = charlie; charlie = 9;
}
public static void netflix() {
alpha = bravo;
bravo = charlie;
charlie = alpha - bravo;
}
public static void hulu() {
System․out․println(alpha);
System․out․println(bravo);
System․out․println(charlie);
}
public static void pandora() {
alpha = 8;
bravo = 6;
charlie = 7;
}
