wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Code Snippets Quiz 1

Total questions: 22

Worksheet time: 11mins

Name
Class
Date
1.

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] + " ");

}

a)

1, 2, 3, 4, 5

b)

1 2 3 4 5

c)

5 4 3 2 1

d)

1 3 2 4 5

2.

What is the result of the following code snippet?

String text = "Hello, world!";

System.out.println(text.length());

a)

13

b)

12

c)

14

d)

11

3.

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");

}

a)

Java

b)

Hello

c)

Hello World Java

d)

Hello Java World

4.

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;

}

a)

13

b)

12

c)

14

d)

15

5.

What will be the output of the following code?

String text = "Hello, World!";

System.out.println(text.substring(7, 12));

a)

"World"

b)

" World!"

c)

"World!"

d)

", World!"

6.

What is the result of the following code?

String name = "John Doe";

System.out.println(name.charAt(4));

a)

'o'

b)

'e'

c)

' '

d)

'D'

7.

What is the output of the following code snippet?

int x = 5;

System.out.println(x++);

a)

4

b)

5

c)

6

d)

7

8.

What will be the output of the following code snippet?

String str = "Hello, World!";

System.out.println(str.indexOf("World"));

a)

5

b)

7

c)

6

d)

8

9.

What does the following code snippet do?

int[] numbers = {1, 2, 3, 4, 5};

System.out.println(numbers[2]);

a)

Prints the length of the array

b)

Prints the sum of all array elements

c)

Prints the value at index 2

10.

What is the output of the following code snippet?

int x = 10;

int y = x % 3;

System.out.println(y);

a)

3.333

b)

1

c)

10

d)

3

11.

What does the following code snippet do?

String name = "John Doe";

String[] parts = name.split(" ");

a)

Splits the string into individual characters

b)

Reverses the order of characters in the string

c)

Removes all whitespace from the string

d)

Splits the string into an array of substrings

12.

What is the output of the following code snippet?

System.out.println(Math.round(4.7));

a)

4

b)

5

c)

4.0

d)

5.0

13.

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");

}

a)

Hello

b)

World

c)

Hello World

d)

World Hello

14.

What will be the output of the following code?

int x = 5;

System.out.println(x > 3 && x < 10);

a)

false

b)

true

15.

What is the result of the following code?

String name = "John Doe";

System.out.println(name.substring(5));

a)

Doe

b)

Doe

c)

n Doe

d)

oe

16.

What will be the output of the following code snippet?

int x = 5;

int y = ++x;

System.out.println(y);

a)

5

b)

6

c)

4

d)

7

17.

What is the output of the following code snippet?

System.out.println(Math.abs(-7));

a)

-7

b)

7

c)

7.0

d)

-7.0

18.

What is the output of the following code snippet?

int x = 2;

System.out.println(Math.pow(x, 3));

a)

2

b)

4

c)

8

d)

6

19.

What will be the output of the following code snippet?

String str = "Hello, World!";

System.out.println(str.toUpperCase());

a)

Hello, World!

b)

HELLO, WORLD!

c)

HeLlO, wOrLd!

d)

HELLO, world!

20.

What is the value of the variable "x" after the following code is executed?

int x = 5;

x += 3;

a)

5

b)

3

c)

14

d)

8

21.

What does the following code snippet do?

int x = 5;

int y = (x > 3) ? 10 : 15;

a)

Sets y to 5

b)

Sets y to 10

c)

Sets x to 5

d)

Sets x to 10

22.

What is the output of the following code snippet?

System.out.println(Math.sqrt(16));

a)

4

b)

4.0

c)

3

d)

16.0