Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

String

Total questions: 10

Worksheet time: 10mins

Name
Class
Date
1.

What is the output of the code segment below?

String s = "dogfood";

System.out.prinln( s.substring(0, 3));

a)

food

b)

dog

c)

ood

d)

dfo

2.

What is the output of the code segment below?

String s = "dogfood";

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

a)

3

b)

7

c)

6

d)

5

e)

4

3.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.substring(2, 5) );

a)

food

b)

dog

c)

dogs

d)

ood

e)

gfo

4.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.substring(4) );

a)

food

b)

dog

c)

dogs

d)

ood

e)

dr who

5.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.substring(3).length() );

a)

3

b)

2

c)

-1

d)

4

e)

5

6.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.indexOf( "dog" ) );

a)

3

b)

2

c)

-1

d)

4

e)

0

7.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.indexOf( "oo" ) );

a)

3

b)

2

c)

-1

d)

4

e)

0

8.

What is the output of the code segment below?

String s = "dogfood";

System.out.println( s.indexOf( "cat" ) );

a)

3

b)

2

c)

-1

d)

4

e)

0

9.

What is the output of the code segment below?

String s = "abac-adae-bfbg-bhbi";

int index = s.indexOf( "ae" );

System.out.println( index );

a)

8

b)

-1

c)

6

d)

7

e)

there is no output due to syntax error.

10.

What is the output of the code segment below?

String s = "abac-adae-bfbg-bhbi";

int index = s.indexOf( "-b" );

System.out.println( s.substring( index ) );

a)

abac-adae-bfbg-bhbi

b)

-bfbg-bhbi

c)

bfbg-bhbi

d)

bhbi

e)

there is no output.