Font size
WorksheetsJava Basics Recap
Total questions: 25
Worksheet time: 37mins
Which method to use to find length of a string?
length()
size()
long()
count()
Is String a primitive data type?
Yes
No
Both
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()
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 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 will get stored in variable word after the evaluation of the below given code snippet:
String txt,word;
txt="bookmark";
word=txt.substring(txt.indexOf('k')+1);
(a)
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
What is the return type of equalsIgnorecase() method?
int
String
char
boolean
String word = "A few good men";
What is word.indexOf(“f”)?
2
0
1
3
What is the output of the following:
for(int x=1 ; x<5 ; x++){
System.out.print(x);
}
1234
01234
012345
12345
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(9,12));
pass
fail
Compilation Error
I will pass the exams.
Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++
24
23
20
25
What will the output of the following code segment be?
int a = 1, b = 2;
System.out.println(++a+”,”+b++);
1,2
2,2
2,3
3,3
What would be output by the following program segment?
String n1 = "Samuel";
String n2 = "Samantha";
System.out.print(n1.compareTo(n2);
false
true
a positive number, 20
a negative number, -20
String target = "millisecond";
for (int i=0; i<target.length(); i++)
{
if (target.charAt(i) == 'l')
break;
else
System.out.print(target.charAt(i));
}
Suppose we have defined
int m = 18;
int n = 4;
What is the value of a in the expression?
int a = m / n + m % n;
6.5
6
6.0
100
none of the above
What is the most appropriate way to check if two Strings are equal?
string1 == string2
string1.equals(string2)
string1.same(string2)
string1.equalsIgnorecase(string2)
What is the output of the following:
for(int x=1 ; x<5 ; x++){
System.out.print(x);
}
1234
01234
012345
12345
System.out.println((int)Math.random()* 7) + 1);
The following code segment will print (a)
System.out.println(6 % 9);
double x = (int) (1.0 / 2) is (a)
