Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

String Methods

Total questions: 10

Worksheet time: 30mins

Name
Class
Date
1.

Choose the line that assigns "love" to str2.

a)

str2 = str.substring(2,6);

b)

str2 = str.substring(2,5);

c)

str2 = str.substring(2);

d)

str2 = str.substring(4);

2.

Choose the line that assigns "boba." to str2.

a)

str2 = str.substring(7);

b)

str2 = str.substring(7,11);

c)

str2 = str.substring(8,12);

d)

str2 = str.substring(8);

3.

Choose the line that assigns "I" to str2.

a)

str2 = str.substring(0,1);

b)

str2 = str.substring(1,2);

c)

str2 = str.substring(0);

d)

str2 = str.substring(1);

4.

What is the output from running this code block?

a)

equals method

b)

== operator

c)

(Nothing)

d)

equals method

== operator

5.

What is the result of running this code block?

a)

"." is printed.

b)

An exception is thrown.

c)

"America." is printed.

d)

"a" is printed.

6.

What is the result of running this code block?

a)

"." is printed.

b)

An exception is thrown.

c)

"America." is printed.

d)

"a" is printed.

7.

What method-call below could be used to determine whether str contains the String "States"? Choose all that apply.

a)

str.contains("States")

b)

str.indexOf("States")

c)

str.equals("States")

d)

str.compareTo("States")

8.

What line of code assigns the first position of the String "time!" to the variable index?

a)

index = str.indexOf("time!");

b)

index = str.indexOf(5);

c)

index = str.contains("time!");

d)

index = str.contains(5);

9.

What could possibly be the output from running this code block?

a)

-4

b)

0

c)

15

d)

true

10.

Which method calls could be reliably used to decide whether the two strings are equal?

a)

str.compareTo(str2)

b)

str.equals(str2)

c)

str == str2

d)

str.contains(str2)