Font size
WorksheetsJava String-2
Total questions: 55
Worksheet time: 55mins
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
char
Array
int[] myArray = {11, 22, 33, 44, 55};
which code is correct to print length of above int-array?
System.out.println(int[].myArray.length());
System.out.println(myArray.length);
System.out.println(myArray.length());
System.out.println(int[].myArray.size());
String[] friends = {"John", "Mike", "Samantha", "Monica"};
which code is correct to print "Samantha" using array functionality?
System.out.println(friends[2]);
System.out.println(friends[3]);
System.out.println(friends["Samantha"]);
System.out.println(friends{2});
Select correct code to create an array of "double" datatype of length 5.
double arr = double[5];
double{} arr = new double{5};
double[5] arr = new double[];
double[] arr = new double[5];
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
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
A
B
C
D
E
A
B
C
D
E
A
B
C
D
E
A
B
C
D
E
Pick all 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.substring(3, 7) ?
What is the value of pos after the following code executes?
String s1 = "abccba";
int pos = s1.indexOf("b");
2
1
4
-1
What is the value of pos after the following code executes?
String s1 = "abccba";
int pos = s1.indexOf("e");
-1
0
1
error
What is the output of the following?
int x = 9;
if (x>'0')
System.out.println("Hello");
else
System.out.println("World");
Hello
World
Syntax Error
Compilation Error
What is the output of the following:
for(int x=65 ; x<70 ; x++){
System.out.print(String.valueOf(x));
}
6566676869
ABCDE
Error
A
B
C
D
E
What is the output of the following:
String myWord = "FUN";
myWord.toLowerCase();
System.out.println(myWord);
fun
Fun
FUN
"fun"
What is the output of the following:
int y = 5;
do{
System.out.println("This is not the output!");
y++;
}while(y>5);
6
Compilation error
This is not the output!
(printed 5 times)
This is not the output!
What is the output of the following?
boolean yes=true, no=false;
if (!yes||no)
System.out.println("maybe");
else
System.out.println("yes!");
no
maybe
Compilation Error
yes!
int x = 25;
if(5*5)
System.out.println("The answer is 25!");
else
Sytem.out.println("The answer is zero");
true
The answer is 25!
The answer is zero
Compilation Error
What is the output of the following?
String a = "Are you ready kids?";
String b = "Aye! Aye! Captain!";
System.out.println(a.concat(b));
Are you ready kids? Aye! Aye! Captain!
Compilation Error
Are you ready kids?Aye! Aye! Captain!
Aye! Aye! Captain!
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));
pass
fail
Compilation Error
I will pass the exams.
What is the output of the following code?
String a = "19";
String b = "75";
System.out.println(a+" "+b);
19 75
94
Compilation Error
1975
What method of class String is used to remove white spaces at the beginning and end of String objects?
remove()
trim()
concat()
Substring()
