wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JAVA string-CS1

Total questions: 20

Worksheet time: 11mins

Name
Class
Date
1.

String s ="I like it";

int len = "s.length();

What is the value of len?

a)

8

b)

7

c)

9

d)

6

2.

String s="my school";

int x = s.indexOf("c");

What is the value of x?

a)

1

b)

2

c)

4

d)

3

3.

String s1="whatever";

String s2 = s1.substring(1,4);

What is the value of s2?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

4.

String s = "cute dog";

System.out.print(s.substring(6));

What is the output?

a)

"dog"

b)

"do"

c)

"og"

5.

String s = "nice job".substring(3, 5);

What is the value of s?

a)

e

b)

e job

c)

"ce jo"

6.

Which method would you use to check if two Strings are exactly the same?

a)

equals()

b)

equalsIgnoreCase()

c)

toUpperCase()

d)

toLowerCase()

7.

Which method was used to convert this String: "IT learners are clever" to "IT LEARNERS ARE CLEVER" ?

a)

toUpperCase()

b)

toLowerCase()

c)

equalsIgnoreCase()

d)

compareTo()

8.

Which method was used to convert this String: "Life is TOO SHORT to be BORED" to: "life is too short to be bored" ?

a)

toLowerCase()

b)

toUpperCase()

c)

equals()

d)

equalsIgnoreCase()

9.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

10.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

11.

String txt = "Hello World";

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


Choose the correct output.

a)

"HELLO WORLD"

b)

HELLO WORLD

c)

Hello world

d)

Hello World

12.

What will be the output of below code?


String statement = "Outfit of the day";

System.out.println(statement.Substring(3,6));

a)

it of

b)

tfit

c)

fit

d)

FIT

13.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

14.

What is the correct relation between length and last-index of array?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

15.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
16.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
17.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

18.
String word = "A few good men";
What is word.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]
19.

is this code Good or Bad Java?

String greeting ="hi";

if (greeting == "hello")

{

}

a)

Good

b)

Bad

20.

String s ="Johannesburg"

System.out.print(s.substring(2,5) )

will return:

a)

ohan

b)

han

c)

hann

d)

oha