Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java String

Total questions: 110

Worksheet time: 2hrs 32mins

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.

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.

22.

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

23.

String s = "whatever".substring(1,4);


What is the value of s?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

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.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

34.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

35.

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

a)

double

b)

String

c)

int

d)

number

36.

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

37.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

38.

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

39.

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)

40.

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"

41.

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

42.

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

43.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

44.

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)

45.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

46.

int[] myArray = {11, 22, 33, 44, 55};

which code is correct to print length of above int-array?

a)

System.out.println(int[].myArray.length());

b)

System.out.println(myArray.length);

c)

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

d)

System.out.println(int[].myArray.size());

47.

String[] friends = {"John", "Mike", "Samantha", "Monica"};

which code is correct to print "Samantha" using array functionality?

a)

System.out.println(friends[2]);

b)

System.out.println(friends[3]);

c)

System.out.println(friends["Samantha"]);

d)

System.out.println(friends{2});

48.

Select correct code to create an array of "double" datatype of length 5.

a)

double arr = double[5];

b)

double{} arr = new double{5};

c)

double[5] arr = new double[];

d)

double[] arr = new double[5];

49.

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

50.
a)

A

b)

B

c)

C

d)

D

e)

E

51.
a)

A

b)

B

c)

C

d)

D

e)

E

52.
a)

A

b)

B

c)

C

d)

D

e)

E

53.
a)

A

b)

B

c)

C

d)

D

e)

E

54.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

55.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

56.

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

a)

double

b)

String

c)

int

d)

number

57.

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

58.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

59.

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)

60.

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"

61.

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

62.

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

63.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

64.

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)

65.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

66.

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

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

What is the value of pos after the following code executes?


String s1 = "abccba";

int pos = s1.indexOf("b");

a)

2

b)

1

c)

4

d)

-1

69.

What is the value of pos after the following code executes?


String s1 = "abccba";

int pos = s1.indexOf("e");

a)

-1

b)

0

c)

1

d)

error

70.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
71.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
72.
String word = "A few good men";
What is word.indexOf(“f”)?
a)
2
b)
0
c)
1
d)
-1
73.
String word = "A few good men";
What is word.indexOf(“f”)?
a)
2
b)
0
c)
1
d)
-1
74.
String word = "A few good men";
What is word.indexOf(“bad”)?
a)
0
b)
-1
c)
3
75.
String word = "A few good men";
What is word.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]
76.

Which of the following is NOT a primitive data type?

a)

double

b)

float

c)

boolean

d)

String

77.

What would be output by the following program segment?


String n1 = "Samuel";

String n2 = "Samantha";


System.out.print(n1.compareTo(n2);

a)

false

b)

true

c)

a positive number, 20

d)

a negative number, -20

78.

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

79.

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName.concat(lastName));


Guess the output.

a)

John.concat(Doe)

b)

John Doe

c)

JohnDoe

d)

"John Doe"

80.

String x = "10";

String y = "20";

String z = x + y;


Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

81.

Which of these class is superclass of String and StringBuffer class?

a)

java.util

b)

java.lang

c)

ArrayList

d)

None of the mentioned

82.

Which of these is an incorrect statement?

a)

String objects are immutable, they cannot be changed

b)

String object can point to some other reference of String variable

c)

StringBuffer class is used to store string in a buffer for later use

d)

None of the mentioned

83.

What is the output of this program?

class String_demo{

public static void main(String args[])

{

char chars[] = {'a', 'b', 'c'};

String s = new String(chars);

System.out.println(s);

}

}

a)

a

b)

b

c)

c

d)

abc

84.

What is the output of following program?

class string_class{

public static void main(String args[]){

String obj = "hello";

String obj1 = "world";

String obj2 = obj;

obj2 = " world";

System.out.println(obj + " " + obj2);

}

}

a)

hello hello

b)

world world

c)

hello world

d)

world hello

85.

The String method compareTo() returns

a)

true

b)

false

c)

an int value

d)

1

86.

What is the output of the following code?

String a = "19";

String b = "75";

System.out.println(a+" "+b);

a)

19 75

b)

94

c)

Compilation Error

d)

1975

87.

What method of class String is used to remove white spaces at the beginning and end of String objects?

a)

remove()

b)

trim()

c)

concat()

d)

Substring()

88.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

89.

What is the output of the following?

String a = "I will pass the exams.";

String b = " I will pass the exams. ";

if (a.equals(b))

System.out.println("fail");

else

System.out.println(b.substring(9,12));

a)

pass

b)

fail

c)

Compilation Error

d)

I will pass the exams.

90.

Which of these class is superclass of String and StringBuffer class?

a)

java.util

b)

java.lang

c)

ArrayList

d)

None of the mentioned

91.

String name = "Mark Hayes";

String team = "Yankees";

String enemy = "Red Sox";

String funny = "Yankees will win the World Series!";

String yearsWon = "2009 2000 1999 1998 1996";


System.out.println( team.substring(2, 7) );

(a)  

92.
Which of the following statements are valid, given the mystery function definition below?
static int mystery(String p1)
a)
All of these are valid
b)
mystery("data");
c)
int result = mystery("data");
d)
if (mystery("data") == 3)
93.
What will be the output of the following for() loop:
String target = "millisecond";
for (int i=0; i<target.length(); i++)
{
if (target.charAt(i) == 'l')
break;
else
System.out.print(target.charAt(i));
}
 
a)
m
b)
mi
c)
millisecond
d)
miisecond
94.

What does the following print?

String str = "And yet, it moves!"

System.out.println( str.charAt( str.length() ) );

a)

A

b)

n

c)

!

d)

The statment is incorrect.

95.

Examine the following fragment:

String world = "World!";

StringBuffer buf = new StringBuffer( "Hello" );


buf.append(' ');

buf.append( world );

How many StringBuffer objects are constructed?

a)

0

b)

1

c)

2

d)

3

96.

After a StringBuffer has been constructed, how many characters may be added to it?

a)

No more after it has been constructed.

b)

Only as many as the original capacity.

c)

As many as needed; it will grow in size if necessary.

d)

A new StringBuffer must be constructed each time characters are added.

97.

You wish to compare the contents of two StringBuffer objects. How should this be done?

a)

Use the == operator.

b)

Use the equals() method of StringBuffer.

c)

Construct a String object from each StringBuffer and use their equals() method.

d)

Do a character-by-character comparison using charAt() and ==.

98.
Why don't we have to save the result of the append() method of Stringbuffer in a variable?
a)
It changes the Stringbuffer instance on which the method is called on.
b)
There is only one variable of type Stringbuffer, so Java knows where to save the result
c)
Java Magic
99.
If a String personName = "Carlos", what's the return of personName.indexOf( 'z' )?
a)
5
b)
Carl
c)
-1
d)
Does not return a int
100.

Examine the following fragment:

String cat = "Hello"


cat += " ";

cat += "World" ;

How many objects are constructed?

a)

0

b)

1

c)

2

d)

3

101.

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

a)

equals()

b)

equalsIgnoreCase()

c)

toUpperCase()

d)

toLowerCase()

102.

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

103.

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

104.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

105.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

106.

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

a)

double

b)

String

c)

int

d)

number

107.

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

108.

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

109.

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)

110.

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"