Basic Java Quiz (AP Computer Science in Java (Mocha))
Which code segment will print "Hello Karel" to the screen in Java? A: System.out.printLine("Hello Karel"); B: print "Hello Karel"; C: System.out.println("Hello Karel"); D: System.println("Hello Karel");
C
What is a String type in Java? A: An extremely large integer value B: A sequence of integers C: A single character D: A sequence of characters
D
What does this Java expression evaluate to? EX: true && !false A: True B: False
A
What is a boolean in Java? A: A true or false value B: A very precise number C: A single character D: A sequence of characters
A
What will this code segment output? EX: System.out.println("Hello"); System.out.println("World"); A: Hello World B: HelloWorld C: Hello World D: Hello World
A
Which for loop will properly print "hello" 10 times? A: for(int i = 0; i < 10; i++) { System.out.println("hello"); } B: for(int i = 10) { System.out.println("hello"); } C: for(int i = 0; i++; i < 10) { System.out.println("hello"); } D: for(int i = 10; i < 0; i++) { System.out.println("hello"); }
A
Which of the below is a logical operator? A: ! B: ## C: ** D: %
A
Which of the following logical statements is equivalent to EX: !(A || B) Where A and B are boolean values A: !A && !B B: !A || !B C: !A || B D: A && B
A
Which of the below is NOT a Java arithmetic operator? A: + B: - C: # D: /
C
Which of the following is a proper way to declare and initialize a variable in Java? A: myInteger = 100; B: char = 'a'; C: int myNumber = 10; D: "Variable"
C Correct! The variable is both declared and initialized.
What is casting in Java? A: What Karel does when she breaks her leg. B: Casting is turning a value of one type into another type. C: A way to get user input in Java. D: A way to print to the screen in Java.
B
What is the proper way to compare String values in Java? A: The == operator B: The .equals() String method C: The = operator D: The | operator
B
Which of the following is correct loop and a half structure? A: while(condition) { // code } B: while(true) { // code if(condition) { break; } // code } C: for(int i = 0; i <= 1.5; i++) { // code } D: for(int i = 0; i < 2; i++) { // code break; // code }
B
Why do we use if statements in Java? A: To break out of some block of code B: To do something only if a condition is true C: To do something while a condition is true D: To repeat something for a fixed number of times
B
Which of the following is NOT a comparison operator? A: < B: ? C: == D: >=
B Correct. This is not a comparison operator in Java
What is the result of this expression? EX: 150 % 100 A: 0 B: 100 C: 50 D: 3
C
What will this code segment output if the user inputs the number 10? EX: int yourInteger = readInt("Please input a number"); System.out.println(yourInteger); A: yourInteger B: "yourInteger" C: 10 D: "10"
C
In the following line of code: EX: boolean shortCircuit = true && (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated? A: Yes B: No
A Correct! The && logical operator will only short circuit if the first boolean expression is false, because false && (anything) is equal to false. Here the first boolean is true, so Java needs to evaluate the second boolean expression in order to determine if the entire statement is true or false. So the 5 / 0 == 0 operation will be evaluated, causing the program to crash (dividing by 0 is illegal).
What does this Java expression evaluate to? EX: 80 >= 80 A: "true" B: true C: false D: This expression will error
B
What will this while loop do? EX: while(true) { System.out.println("Hello"); } A: Print Hello one time B: Print Hello in an infinite loop C: Print Hello until program receives user input D: Nothing
B
Which statement can we use inside of a loop to end it? A: System.out.println; B: break; C: SENTINEL; D: else
B
In the following line of code: EX: boolean shortCircuit = true || (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated? A: Yes B: No
B Since true || (anything) is equal to true, there is no need to evaluate the second part of the boolean expression, Java already knows that the end result will be true. So the 5 / 0 == 0 operation is never evaluated.
What will this java expression evaluate to? EX: (int) 9.9 A: 0 B: 10 C: 9 D: 5
C
Which if statement is written properly? A: if (boolean expression): // code to execute B: if boolean expression { // code to execute } C: if(boolean expression) { // code to execute } D: if boolean expression [ // code to execute ]
C
Which of the following logical statements is equivalent to EX: !(A && B) Where A and B are boolean values A: !A && B B: !A && !B C: !A || !B D: !A || B
C
Which of these input methods was not introduced in the video? A: readInt B: readDouble C: readChar D: readBoolean
C
Why do we use while loops in Java? A: To break out of some block of code B: To do something if a condition is true C: To repeat some code while a condition is true D: To repeat something for a fixed number of times
C
Which of the below is an example of a boolean? A: 10.1 B: "true" C: 'F' D: true
D
Why do we use for loops in Java? A: To break out of some block of code B: To do something if a condition is true C: To do something while a condition is true D: To repeat something for a fixed number of times
D
Which of the below is not a primitive type in Java? A: int B: char C: boolean D: double E: apple
E An `apple` is not a primitive type in Java. You could, however, create your own User Defined Type called `Apple`.