Font size
WorksheetsBasic To String Test
Total questions: 100
Worksheet time: 2hrs 40mins
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)
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.length()?
What is word.charAt(7) ?
What is word.startsWith("A few") ?
What is word.endsWith("man") ?
What is word.substring(3, 7) ?
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)
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
String x = "10";
String y = "20";
String z = x + y;
System.out.println(z);
Guess the output.
1020
10 20
30
"10 20"
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
String word = "A few good men";
What is word.indexOf(“f”)?
2
0
1
3
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) ?
Predict the output :
Red
Invalid color
Invalid color
Compilation fails due to syntax errors
Red
Predict the output :
ArrayIndexOutOfBoundsException
null
0
1
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 s = "whatever".substring(1,4);
What is the value of s?
"hate"
"hat"
"whate"
"what"
StringIndexOutOfBoundsException
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 method would you use to see if two Strings contain the same letters but ignoring capitals and small letters?
equals()
equalsIgnoreCase()
toUpperCase()
compareTo()
Which method was used to convert this String: "IT learners are clever" to "IT LEARNERS ARE CLEVER" ?
toUpperCase()
toLowerCase()
equalsIgnoreCase()
compareTo()
Which method was used to convert this String: "Life is TOO SHORT to be BORED" to: "life is too short to be bored" ?
toLowerCase()
toUpperCase()
equals()
equalsIgnoreCase()
A
B
C
D
E
Which of these class is superclass of String and StringBuffer class?
java.util
java.lang
ArrayList
None of the mentioned
Which of the following statements are incorrect?
String is a class
Strings in java are mutable
Every string is an object of class String
Java defines a peer class of String, called StringBuffer, which allows string to be altered
The String method compareTo() returns
true
false
an int value
1
The class string belongs to ................. package.
java.awt
java.lang
java.applet
java.string
How many objects will be created?
String a = new String("Examveda");
String b = new String("Examveda");
String c = "Examveda";
String d = "Examveda";
4
3
2
none of these
What will be the output of the following Java code?
1 3 5 7 9
0 2 4 6 8
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
What will be the output of the following Java code?
10,20,30,40,50
compile time error
run time error
10,20,30,40
What will be the output of the following Java code?
0 0
0 1
1 2
compiler error
What will be the output of the following Java code?
0 1
0 0
1 2
compiler error
Which of the following is FALSE about arrays on Java?
array is an object
length of an array can be changed
arrays allocated in heap
Predict the output:
Prints nothing
Compile Time Error: variable x is already defined
9
8
Predict the output:
Run command is issued as: Java Sample "Hello World"
"Hello World"
"Hello
Hello
Hello World
Predict the output:
Prints nothing
0
null
OutOfBoundsException
Predict the output:
true false
true true
false true
false false
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);
694
694 1155 2265 2869
The code compiles but outputs something else.
The code does not compile.
Which is a true statement?
If s.contains("abc") is true, then s.equals("abc") is also true.
If s.contains("abc") is true, then s.startsWith("abc") is also true.
If s.startsWith("abc") is true, then s.equals("abc") is also true.
If s.startsWith("abc") is true, then s.contains("abc") is also true.
What is the output of the following?
false 1
false 2
true 1
true 2
Which of these is an incorrect array declaration?
int arr[] = new int[5]
int [] arr = new int[5]
int arr[] = int [5] new
int arr[] = new int[5]
What is the output for the following statement:
System.out.print("1"+"1");
11
2
Error Message
1+1
Which method to use to find length of a string?
length()
size()
long()
count()
What is the data type of this expression?
String
double
boolean
number
What will be written to the screen when the following code runs?
5
10
15
25
What is the value of the following expression?
2
7
2.0
7.0
What is the value of this expression?
1
1.0
3
4
If String str = "Computer Science";, then what is the value of str.indexOf("ute");?
-2
2
3
4
If String str = "Computer Science";, then what is the value of str.indexOf("sci");?
-1
0
5
12
If String str = "Computer Science";, then what is the value of str.substring(4, 6);?
put
om
Sci
ut
If String str = "Computer Science";, then what is the value of str.substring(10);?
Computer
Computer Sc
cience
Weird science
If String str = "Computer Science";, then what is the value of str.length();?
16
17
18
20
If String str1 = "hello"; and String str2 = "world";, then what is the value of str1 + str2?
hello world
helloworld
world
hello
What is the value of this expression?
9
5
-11
-84
What does the following print?
System.out.println( "And yet, it moves!".charAt(0) );
A
n
!
The statement is incorrect.
What does the following print?
String str = "And yet, it moves!"
System.out.println( str.charAt( str.length() ) );
A
n
!
The statment is incorrect.
Examine the following fragment:
String cat = "Hello"
cat += " ";
cat += "World" ;
How many objects are constructed?
0
1
2
3
Examine the following fragment:
String world = "World!";
StringBuffer buf = new StringBuffer( "Hello" );
buf.append(' ');
buf.append( world );
How many StringBuffer objects are constructed?
0
1
2
3
Which phrase best describes a String object after it has been constructed?
Changeable
Write Only
Read Only
Inaccessible
After a StringBuffer has been constructed, how many characters may be added to it?
No more after it has been constructed.
Only as many as the original capacity.
As many as needed; it will grow in size if necessary.
A new StringBuffer must be constructed each time characters are added.
What will be the output of this Snippet?
char n = 1 + 'c';
System.out.println(++n);
E
Run Time Error
e
Compile Time Error
The name of the following should be the file name in Java
Object Name
Variable Nam
Class Name
Array Name
A ___________ file is created after successful compilation of a Java program.
Object file
Array file
Class file
String file
In Java WORA stands for
Write Once Recall Anywhere
Write Once Return Anywhere
Write Once Read Anywhere
Write Once Run Anywhere
Java programs ________________
are only compiled
are only compiled not interpreted
are only interpreted
are both compiled and interpreted
Main() method is not mandatory for a Java program
True
False
