Font size
WorksheetsTest
Total questions: 35
Worksheet time: 37mins
Is String a primitive data type?
Yes
No
Both
Which method to use to find length of a string?
length()
size()
long()
count()
Which data type gets returned from length() method in String class?
double
String
int
number
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
What is the correct way to find the length of "txt" string?
int len = length(txt);
float len = txt.length();
int len = txt.length();
double len = length(txt);
Which is correct method to convert String into uppercase
changeUpperCase()
convertUpperCase()
toUpper()
toUpperCase()
String txt = "Hello World";
System.out.println(txt.toUpperCase());
Choose the correct output.
"HELLO WORLD"
HELLO WORLD
Hello world
Hello World
Choose correct purpose of indexOf() in String class.
returns the index (the position) of the last occurrence of a specified text in a string
returns the character of the first occurrence of a specified character in a string
returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)
returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)
String x = "10";
String y = "20";
String z = x + y;
System.out.println(z);
Guess the output.
1020
10 20
30
"10 20"
What will be the output of below code?
String statement = "Outfit of the day";
System.out.println(statement.Substring(3,6));
it of
tfit
fit
FIT
Choose most appropriate purpose of trim() method in String?
to remove white spaces
to get a substring of the string
to cut String at desired index
to remove extra white spaces from start and end of String
What is the return type of equalsIgnorecase() method?
int
String
char
boolean
What is the most appropriate way to check if two Strings are identical?
string1 == string2
string1.equals(string2)
string1.same(string2)
string1.equalsIgnorecase(string2)
(Multiple answer question)
Pick all NON-primitive datatypes from below :
String
int
primitive
Array
What is the correct relation between length and last-index of array?
last-index is always 1 more than the length
last-index is 1 less than the length (but not always)
last-index is always 1 less than the length
last-index is always equal to the length
What is word.length()?
What is word.charAt(7) ?
What is word.startsWith("A few") ?
What is word.endsWith("man") ?
String word = "A few good men";
What is word.indexOf(“f”)?
2
0
1
3
What is word.substring(3, 7) ?
Choose most appropriate purpose of trim() method in String?
to remove white spaces
to get a substring of the string
to cut String at desired index
to remove extra white spaces from start and end of String
int len = "jump it!".length();
What is the value of len?
8
7
9
6
This code does not compile.
int x = "quarantine".indexOf("a");
What is the value of x?
4
3
2
This code does not compile.
StringIndexOutOfBoundsException
String s1 = "hello";
String s2 = new String("hello");
if (s1 == s2) System.out.print("Hey");
else System.out.print("Yo");
What is the output?
Hey
Yo
There is no output.
This code will not compile.
int p = "Go Paw Patrol!".indexOf("p");
What is the value of p?
0
4
3
-1
This code will not compile.
String s = "cool cat".substring(6);
What is the value of s?
"cat"
"at"
"cool c"
This code does not compile.
StringIndexOutOfBoundsException
int x = "Mary".compareTo("Molly");
if (x>=0) System.out.print("Yes");
else System.out.print("No");
What is the output?
Yes
No
There is no output.
This code will not compile.
String s1 = "goat";
String s2 = s1.substring(0,s1.length());
boolean z = s1.equals(s2);
What is the value of z?
true
false
This code will not compile.
StringIndexOutOfBoundsException
String s = "good job".substring(3, 9);
What is the value of s?
"d job"
"od job"
"d jo"
"od jo"
StringIndexOutOfBoundsException
int x = "bob".compareTo("Bob");
if (x>=0) System.out.print("Good");
else System.out.print("Bad");
What is the output?
Good
Bad
There is no output.
This code will not compile.
Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?
str.length() -2
str.length() -4
str.length() - 3
str.length() - 1
str.length()
Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?
pos>0
pos>=0
pos<0
pos<=0
pos==0
Select all correct statement(s) about "String" data type of java-
String is a class
String is an array of characters
String values are enclosed between " "
We can use "+" operator with strings
Select any two correct statements-
length returns total number of characters of a string
length() is a method
length is a property
length() counts total number of elements of an array
Strings are _______
Immutable
Mutable
