Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Programming Quiz - unit 1b

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

Where should the import java.util.Scanner; statement be placed in a Java program?

a)

Inside the main method

b)

At the beginning of the program before the class declaration

c)

At the end of the program

d)

Anywhere in the program

2.

What will the following code output? String text = "Hello, Java!"; int index = text.indexOf("Java"); System.out.println(index);

a)

7

b)

6

c)

5

d)

8

3.

What is the value of y after the following statements? int y = 4; y *= 2;

a)

2

b)

4

c)

6

d)

8

4.

What does \t represent in a string?

a)

A tab space

b)

A new line

c)

A backspace

d)

A space

5.

What will System.out.println("Java".charAt(2)); output?

a)

J

b)

a

c)

v

d)

Throws an error

6.

What will the following code output? String text = "Hello, World!"; char ch = text.substring(7, 12).charAt(2); System.out.println(ch);

a)

W

b)

r

c)

l

d)

o

7.

What method is used to read a double value using a Scanner object?

a)

nextDouble()

b)

getDouble()

c)

readDouble()

d)

inputDouble()

8.

What will the following code output? String a = "Hello"; String b = "World"; System.out.println(a + " " + b);

a)

Hello World

b)

HelloWorld

c)

Hello

d)

Throws an error

9.

Which of the following declares a character variable correctly?

a)

char ch = 'A';

b)

char ch = "A";

c)

char ch = A;

d)

char ch <- 'A';

10.

What will System.out.println("Java".length()); output?

a)

3

b)

4

c)

5

d)

Throws an error

11.

What is the value of x after the following code? int x = 9; x /= 3;

a)

1

b)

3

c)

6

d)

9

12.

What does the following code output? String str = "abcdef"; String result = str.substring(str.indexOf('c'), str.indexOf('e')); System.out.println(result);

a)

cd

b)

ce

c)

cde

d)

Throws an Error

13.

What does str.lastIndexOf('a') return for the string str = "banana"?

a)

1

b)

3

c)

5

d)

Throws an error

14.

Which of the following correctly declares a string variable in Java?

a)

String name = "Alice";

b)

string name = "Alice";

c)

String name == "Alice";

d)

String name: "Alice";

15.

What will "hello".equals("Hello") return?

a)

true

b)

false

c)

Throws an error

d)

null

16.

What will the following code output? String str = "Programming"; String sub = str.substring(0, 5); System.out.println(sub);

a)

Progr

b)

rammi

c)

gramm

d)

Programming

17.

What will the following code output? String str = "hello"; System.out.println(str.toUpperCase());

a)

HELLO

b)

hello

c)

Hello

d)

Throws an error

18.

If you create a Scanner object as follows,

Scanner sc = new Scanner(System.in);
How can you read an integer input?

a)

int num = sc.nextInt();

b)

int num = sc.readInt();

c)

int num = sc.getInt();

d)

int num = sc.inputInt();

19.

What will the value of x be after the following statements? int x = 5; x = x + 3;

a)

3

b)

5

c)

8

d)

15

20.

What do the following lines of code do?

Scanner sc = new Scanner(System.in); String name = sc.nextLine();

a)

Read a single character

b)

Read an integer

c)

Read a whole line of input

d)

Throws an error