NEW
Font size
WorksheetsJava Code Snippets Quiz 1
Total questions: 22
Worksheet time: 11mins
What will the following code snippet print?
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
1, 2, 3, 4, 5
1 2 3 4 5
5 4 3 2 1
1 3 2 4 5
What is the result of the following code snippet?
String text = "Hello, world!";
System.out.println(text.length());
13
12
14
11
What is the output of the following code snippet?
int x = 10;
if (x > 5) {
System.out.println("Hello");
} else if (x > 7) {
System.out.println("World");
} else {
System.out.println("Java");
}
Java
Hello
Hello World Java
Hello Java World
What will be the result of the following code?
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : numbers) {
sum += num;
}
13
12
14
15
What will be the output of the following code?
String text = "Hello, World!";
System.out.println(text.substring(7, 12));
"World"
" World!"
"World!"
", World!"
What is the result of the following code?
String name = "John Doe";
System.out.println(name.charAt(4));
'o'
'e'
' '
'D'
What is the output of the following code snippet?
int x = 5;
System.out.println(x++);
4
5
6
7
What will be the output of the following code snippet?
String str = "Hello, World!";
System.out.println(str.indexOf("World"));
5
7
6
8
What does the following code snippet do?
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]);
Prints the length of the array
Prints the sum of all array elements
Prints the value at index 2
What is the output of the following code snippet?
int x = 10;
int y = x % 3;
System.out.println(y);
3.333
1
10
3
What does the following code snippet do?
String name = "John Doe";
String[] parts = name.split(" ");
Splits the string into individual characters
Reverses the order of characters in the string
Removes all whitespace from the string
Splits the string into an array of substrings
What is the output of the following code snippet?
System.out.println(Math.round(4.7));
4
5
4.0
5.0
What will be the output of the following code?
int x = 10;
if (x > 5 && x < 15) {
System.out.println("Hello");
} else {
System.out.println("World");
}
Hello
World
Hello World
World Hello
What will be the output of the following code?
int x = 5;
System.out.println(x > 3 && x < 10);
false
true
What is the result of the following code?
String name = "John Doe";
System.out.println(name.substring(5));
Doe
Doe
n Doe
oe
What will be the output of the following code snippet?
int x = 5;
int y = ++x;
System.out.println(y);
5
6
4
7
What is the output of the following code snippet?
System.out.println(Math.abs(-7));
-7
7
7.0
-7.0
What is the output of the following code snippet?
int x = 2;
System.out.println(Math.pow(x, 3));
2
4
8
6
What will be the output of the following code snippet?
String str = "Hello, World!";
System.out.println(str.toUpperCase());
Hello, World!
HELLO, WORLD!
HeLlO, wOrLd!
HELLO, world!
What is the value of the variable "x" after the following code is executed?
int x = 5;
x += 3;
5
3
14
8
What does the following code snippet do?
int x = 5;
int y = (x > 3) ? 10 : 15;
Sets y to 5
Sets y to 10
Sets x to 5
Sets x to 10
What is the output of the following code snippet?
System.out.println(Math.sqrt(16));
4
4.0
3
16.0
