wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Basic To String Test

Total questions: 100

Worksheet time: 2hrs 40mins

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.

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

14.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
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.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]
20.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

21.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

22.

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

a)

double

b)

String

c)

int

d)

number

23.

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

24.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

25.

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

26.

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)

27.

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

28.

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

29.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

30.

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"

31.

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)

32.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

33.

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

34.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

35.
String word = "A few good men";
What is word.startsWith("A few") ?
a)
true
b)
false
36.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
37.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

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

Predict the output :

a)

Red

Invalid color

b)

Invalid color

c)

Compilation fails due to syntax errors

d)

Red

40.
Which of the following statements are true about arrays in Java? Select all that apply.
a)
Stores homogeneous data.
b)
Size cannot be changed during runtime.
c)
Can store only user defined objects.
d)
Array index starts at 1.
41.

Predict the output :

a)

ArrayIndexOutOfBoundsException

b)

null

c)

0

d)

1

42.

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.

43.

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

44.

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


What is the value of s?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

e)

StringIndexOutOfBoundsException

45.

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.

46.

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

47.

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.

48.

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

49.

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

50.

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.

51.

Which method would you use to see if two Strings contain the same letters but ignoring capitals and small letters?

a)

equals()

b)

equalsIgnoreCase()

c)

toUpperCase()

d)

compareTo()

52.

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

53.

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

54.
a)

A

b)

B

c)

C

d)

D

e)

E

55.

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

a)

java.util

b)

java.lang

c)

ArrayList

d)

None of the mentioned

56.

Which of the following statements are incorrect?

a)

String is a class

b)

Strings in java are mutable

c)

Every string is an object of class String

d)

Java defines a peer class of String, called StringBuffer, which allows string to be altered

57.

The String method compareTo() returns

a)

true

b)

false

c)

an int value

d)

1

58.

The class string belongs to ................. package.

a)

java.awt

b)

java.lang

c)

java.applet

d)

java.string

59.

How many objects will be created?

String a = new String("Examveda");

String b = new String("Examveda");

String c = "Examveda";

String d = "Examveda";

a)

4

b)

3

c)

2

d)

none of these

60.

What will be the output of the following Java code?

a)

1 3 5 7 9

b)

0 2 4 6 8

c)

0 1 2 3 4 5 6 7 8 9

d)

1 2 3 4 5 6 7 8 9 10

61.

What will be the output of the following Java code?

a)

10,20,30,40,50

b)

compile time error

c)

run time error

d)

10,20,30,40

62.

What will be the output of the following Java code?

a)

0 0

b)

0 1

c)

1 2

d)

compiler error

63.

What will be the output of the following Java code?

a)

0 1

b)

0 0

c)

1 2

d)

compiler error

64.

Which of the following is FALSE about arrays on Java?

a)

array is an object

b)

length of an array can be changed

c)

arrays allocated in heap

65.

Predict the output:

a)

Prints nothing

b)

Compile Time Error: variable x is already defined

c)

9

d)

8

66.

Predict the output:

Run command is issued as: Java Sample "Hello World"

a)

"Hello World"

b)

"Hello

c)

Hello

d)

Hello World

67.

Predict the output:

a)

Prints nothing

b)

0

c)

null

d)

OutOfBoundsException

68.

Predict the output:

a)

true false

b)

true true

c)

false true

d)

false false

69.

What is the output of the following?


String teams = new String("694");

teams.concat(" 1155");

teams.concat(" 2265");

teams.concat(" 2869");

System.out.println(teams);

a)

694

b)

694 1155 2265 2869

c)

The code compiles but outputs something else.

d)

The code does not compile.

70.

Which is a true statement?

a)

If s.contains("abc") is true, then s.equals("abc") is also true.

b)

If s.contains("abc") is true, then s.startsWith("abc") is also true.

c)

If s.startsWith("abc") is true, then s.equals("abc") is also true.

d)

If s.startsWith("abc") is true, then s.contains("abc") is also true.

71.

What is the output of the following?

a)

false 1

b)

false 2

c)

true 1

d)

true 2

72.

Which of these is an incorrect array declaration?

a)

int arr[] = new int[5]

b)

int [] arr = new int[5]

c)

int arr[] = int [5] new

d)

int arr[] = new int[5]

73.

What is the output for the following statement:

System.out.print("1"+"1");

a)

11

b)

2

c)

Error Message

d)

1+1

74.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

75.

What is the data type of this expression?

a)

String

b)

double

c)

boolean

d)

number

76.

What will be written to the screen when the following code runs?

a)

5

b)

10

c)

15

d)

25

77.

What is the value of the following expression?

a)

2

b)

7

c)

2.0

d)

7.0

78.

What is the value of this expression?

a)

1

b)

1.0

c)

3

d)

4

79.

If String str = "Computer Science";, then what is the value of str.indexOf("ute");?

a)

-2

b)

2

c)

3

d)

4

80.

If String str = "Computer Science";, then what is the value of str.indexOf("sci");?

a)

-1

b)

0

c)

5

d)

12

81.

If String str = "Computer Science";, then what is the value of str.substring(4, 6);?

a)

put

b)

om

c)

Sci

d)

ut

82.

If String str = "Computer Science";, then what is the value of str.substring(10);?

a)

Computer

b)

Computer Sc

c)

cience

d)

Weird science

83.

If String str = "Computer Science";, then what is the value of str.length();?

a)

16

b)

17

c)

18

d)

20

84.

If String str1 = "hello"; and String str2 = "world";, then what is the value of str1 + str2?

a)

hello world

b)

helloworld

c)

world

d)

hello

85.

What is the value of this expression?

a)

9

b)

5

c)

-11

d)

-84

86.

What does the following print?

System.out.println( "And yet, it moves!".charAt(0) );

a)

A

b)

n

c)

!

d)

The statement is incorrect.

87.

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.

88.

Examine the following fragment:

String cat = "Hello"


cat += " ";

cat += "World" ;

How many objects are constructed?

a)

0

b)

1

c)

2

d)

3

89.

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

90.

Which phrase best describes a String object after it has been constructed?

a)

Changeable

b)

Write Only

c)

Read Only

d)

Inaccessible

91.

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.

92.
If stringName is "Java is cool". What is the result of stringName.substring(0, 6)?
a)
Java is
b)
Java i
c)
Java
d)
s cool
93.
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
94.

What will be the output of this Snippet?


char n = 1 + 'c';

System.out.println(++n);

a)

E

b)

Run Time Error

c)

e

d)

Compile Time Error

95.

The name of the following should be the file name in Java

a)

Object Name

b)

Variable Nam

c)

Class Name

d)

Array Name

96.

A ___________ file is created after successful compilation of a Java program.

a)

Object file

b)

Array file

c)

Class file

d)

String file

97.
When we want to print in Java, we use the code ......
a)
system.out.print();
b)
System.out.println();
c)
System.Out.Printf();
d)
System.out.show();
98.

In Java WORA stands for

a)

Write Once Recall Anywhere

b)

Write Once Return Anywhere

c)

Write Once Read Anywhere

d)

Write Once Run Anywhere

99.

Java programs ________________

a)

are only compiled

b)

are only compiled not interpreted

c)

are only interpreted

d)

are both compiled and interpreted

100.

Main() method is not mandatory for a Java program

a)

True

b)

False