wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Test

Total questions: 35

Worksheet time: 37mins

Name
Class
Date
1.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

2.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

3.

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

a)

double

b)

String

c)

int

d)

number

4.

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

5.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

6.

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

7.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

8.

String x = "10";

String y = "20";

String z = x + y;

System.out.println(z);

Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

9.

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

10.

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

11.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

12.

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)

13.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

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.startsWith("A few") ?
a)
true
b)
false
18.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
19.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

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

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

22.

int len = "jump it!".length();


What is the value of len?

a)

8

b)

7

c)

9

d)

6

e)

This code does not compile.

23.

int x = "quarantine".indexOf("a");


What is the value of x?

a)

4

b)

3

c)

2

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

24.

String s1 = "hello";

String s2 = new String("hello");

if (s1 == s2) System.out.print("Hey");

else System.out.print("Yo");


What is the output?

a)

Hey

b)

Yo

c)

There is no output.

d)

This code will not compile.

25.

int p = "Go Paw Patrol!".indexOf("p");


What is the value of p?

a)

0

b)

4

c)

3

d)

-1

e)

This code will not compile.

26.

String s = "cool cat".substring(6);


What is the value of s?

a)

"cat"

b)

"at"

c)

"cool c"

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

27.

int x = "Mary".compareTo("Molly");

if (x>=0) System.out.print("Yes");

else System.out.print("No");


What is the output?

a)

Yes

b)

No

c)

There is no output.

d)

This code will not compile.

28.

String s1 = "goat";

String s2 = s1.substring(0,s1.length());

boolean z = s1.equals(s2);


What is the value of z?

a)

true

b)

false

c)

This code will not compile.

d)

StringIndexOutOfBoundsException

29.

String s = "good job".substring(3, 9);


What is the value of s?

a)

"d job"

b)

"od job"

c)

"d jo"

d)

"od jo"

e)

StringIndexOutOfBoundsException

30.

int x = "bob".compareTo("Bob");

if (x>=0) System.out.print("Good");

else System.out.print("Bad");


What is the output?

a)

Good

b)

Bad

c)

There is no output.

d)

This code will not compile.

31.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

str.length() -2

b)

str.length() -4

c)

str.length() - 3

d)

str.length() - 1

e)

str.length()

32.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

pos>0

b)

pos>=0

c)

pos<0

d)

pos<=0

e)

pos==0

33.

Select all correct statement(s) about "String" data type of java-

a)

String is a class

b)

String is an array of characters

c)

String values are enclosed between " "

d)

We can use "+" operator with strings

34.

Select any two correct statements-

a)

length returns total number of characters of a string

b)

length() is a method

c)

length is a property

d)

length() counts total number of elements of an array

35.

Strings are _______

a)

Immutable

b)

Mutable