wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java String-2

Total questions: 55

Worksheet time: 55mins

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)

char

d)

Array

14.

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

15.

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

16.

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

17.

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

18.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

19.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

20.

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

a)

double

b)

String

c)

int

d)

number

21.

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

22.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

23.

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

24.

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)

25.

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"

26.

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

27.

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

28.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

29.

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)

30.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

31.

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

32.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
33.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
34.
String word = "A few good men";
What is word.startsWith("A few") ?
a)
true
b)
false
35.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
36.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

37.
a)

A

b)

B

c)

C

d)

D

e)

E

38.
a)

A

b)

B

c)

C

d)

D

e)

E

39.
a)

A

b)

B

c)

C

d)

D

e)

E

40.
a)

A

b)

B

c)

C

d)

D

e)

E

41.

Pick all primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

42.

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

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

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

45.

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

46.

What is the output of the following?

int x = 9;

if (x>'0')

System.out.println("Hello");

else

System.out.println("World");

a)

Hello

b)

World

c)

Syntax Error

d)

Compilation Error

47.

What is the output of the following:

for(int x=65 ; x<70 ; x++){

System.out.print(String.valueOf(x));

}

a)

6566676869

b)

ABCDE

c)

Error

d)

A

B

C

D

E

48.

What is the output of the following:

String myWord = "FUN";

myWord.toLowerCase();

System.out.println(myWord);

a)

fun

b)

Fun

c)

FUN

d)

"fun"

49.

What is the output of the following:

int y = 5;

do{

System.out.println("This is not the output!");

y++;

}while(y>5);

a)

6

b)

Compilation error

c)

This is not the output!

(printed 5 times)

d)

This is not the output!

50.

What is the output of the following?

boolean yes=true, no=false;

if (!yes||no)

System.out.println("maybe");

else

System.out.println("yes!");

a)

no

b)

maybe

c)

Compilation Error

d)

yes!

51.

int x = 25;

if(5*5)

System.out.println("The answer is 25!");

else

Sytem.out.println("The answer is zero");

a)

true

b)

The answer is 25!

c)

The answer is zero

d)

Compilation Error

52.

What is the output of the following?

String a = "Are you ready kids?";

String b = "Aye! Aye! Captain!";

System.out.println(a.concat(b));

a)

Are you ready kids? Aye! Aye! Captain!

b)

Compilation Error

c)

Are you ready kids?Aye! Aye! Captain!

d)

Aye! Aye! Captain!

53.

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(8,12));

a)

pass

b)

fail

c)

Compilation Error

d)

I will pass the exams.

54.

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

55.

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