WorksheetsJava Quiz - Frankenberger 2022
Total questions: 25
Worksheet time: 13mins
How do you compare two String objects (str1 and str2) to see if they are equal?
if (str1 == str2)
if (str1.equals(str2))
if (str1 = str2)
Which of the following would print the integer 13?
System.out.println(arr[0])
System.out.println(arr[1])
System.out.println(arr[2])
System.out.println(arr[3])
Are the following lines a compiler error, or a runtime error?
char c = "a";
System.out.println(c);
Compiler Error
Runtime Error
What is the output of the following code segment?
(a)
What is the range of the following random number?
double random = Math.random();
(inclusive) 0 - 100 (inclusive)
(inclusive) 0-1 (inclusive)
(inclusive) 0-1 (exclusive)
The code causes a compiler error because Math.random() must be stored as an int
95
45
14
59
Which of the following returns the length of an array with the name arr?
arr.length()
arr.size()
length(arr)
arr.length
Which of the following Java keywords allows you to exit a loop?
final
static
break
abstract
Which of the following operators is used to concatenate two or more strings?
&&
||
*
+
Which operator allows you to check for more than one clause at a time, but only one has to be correct?
&&
||
==
!=
What is returned from the following method call?
System.out.println(mystery(10));
10
10.0
The code causes a compile error, because the method returns a double, but the parameter is an int.
String x = "Intro to Java";
int i = x.indexOf("j");
What is the value of i?
0
-1
9
The code does not compile
What is printed after the execution of the following code?
A
B
C
D
F
What is printed after this code execution?
A
B
C
D
E
What is printed after the code finishes executing?
Compiler Error
Nothing is printed
14121086420
02468101214
What is the final value of i?
14
-1
16
15
What is printed from the following code execution?
16
16.9
0.9
17
5!
4!
2.3!
5.1!
Result: 1.43.7
Result: 5.1
Result: 5
True or False?
This is the correct way to write the main method:
public static void main(String[] args)
True
False
String[] arr = new String[3];
arr[0] = "Hello";
arr[1] = "World";
arr[2] = "!";
What is the value of the following variable?
String x = arr[0] + arr[1] + arr[2];
Hello World!
HelloWorld!
Hello World
HelloWorld
What value is printed at the end of the following code execution?
Hello!
Nothing is printed
The while statement causes a syntax error
The code will continuously print Hello!
(Infinite Loop)
What is printed when the following method is called?
Hello
Hello
there
The code does not compile, because the method is returning nothing.
The code does not compile because there is an unreachable statement, causing a compile error.
The following method is supposed to take two int parameters and return x^y. For example, if the method call is square(5,3) it should return 5^3 or 125.
However, the program is not working as intended.
Why?
The math class must be imported before any methods can be used from it.
Math.pow() expects the base to come first, and then the exponent to come after.
Ex: Math.pow(x,y)
Math.pow() returns an integer, and the method returns a double, causing a compile error.
