NEW
Font size
WorksheetsProgramming Methods Worksheet
Total questions: 60
Worksheet time: 30mins
What is the result of "Hello".length()?
4
5
6
0
Which of the following creates a string in Java?
String str = "Hello";
String str = new String("Hello");
Both A and B
None of the above
What will be the output of System.out.println("abc" + "def");?
A) abcdef
B) abc def
C) ab cdef
D) Compilation Error
Which method checks if a string starts with a specific prefix?
startsWith()
endsWith()
includes()
indexOf()
What will System.out.println("Java".charAt(0)); print?
J
a
Java
0
Which of the following is a method to convert a string to lowercase?
toLowerCase()
lowerCase()
makeLower()
downcase()
How do you compare two strings in Java?
==
equals()
compareTo()
What does str.trim() do?
Removes all characters
Removes leading and trailing whitespace
Converts the string to lowercase
Splits the string into an array
Which of the following will return a substring from a string?
str.sub()
str.substring()
str.slice()
str.split()
What will be the output of "hello".concat(" world")?
hello world
hello
world
Compilation Error
Which method is used to replace a character in a string?
replace()
change()
modify()
update()
What will be the result of "Java".compareTo("Java")?
1
-1
0
A compilation error
Which of the following will check if a string contains a specific sequence of characters?
find()
includes()
contains()
has()
What will be the output of "abc".substring(1, 3)?
ab
bc
Which method converts a string into an array of characters?
toArray()
split()
toCharArray()
charArray()
Which method would you use to get the index of a character in a string?
findIndex()
indexOf()
getIndex()
charAtIndex()
What is the result of "hello".equalsIgnoreCase("Hello")?
true
false
0
Compilation error
How can you check if a string is empty?
str.isEmpty()
str.length() == 0
Both A and B
str.equals("")
What does the StringBuilder class do?
It creates immutable strings.
It creates mutable strings.
It converts strings to numbers.
It is not a class in Java.
Which method would you use to split a string into an array based on a delimiter?
split()
divide()
slice()
substring()
21. The output of "abc".repeat(3) will be:
abcabcabc
abcabc
abc3
abc abc abc
Which of the following will convert a string to a number?
parseInt()
valueOf()
toNumber()
Both A and B
What does the StringBuilder method append() do?
Adds a new string to the beginning
Replaces a string
Adds a new string to the end
Deletes a string
Which of the following methods can be used to compare two strings lexicographically?
compareTo()
equals()
==
What is the output of "Hello World".indexOf('W')?
A) 0
B) 5
C) 6
D) -1
Which method is used to replace all occurrences of a character in a string?
replaceAll()
replaceChar()
changeAll()
substitute()
How do you convert a string to a character array?
str.toArray()
str.charArray()
str.toCharArray()
str.getCharArray()
What is the result of "abcdef".substring(2)?
abc
cd
cdef
bcdef
What is the output of "Hello".replace('e', 'a')?
Hallo
Hello
Halla
Hella
Which method would you use to convert a string to uppercase?
toUpperCase()
makeUpper()
uppercase()
capitalize()
What will be the result of "a" + "b" + "c"?
abc
a b c
ab c
Compilation Error
What will be the output of "abc".lastIndexOf('a')?
0
1
2
-1
Which of the following creates a new string object?
String s = "Hello";
String s = new String("Hello");
Both A and B
None of the above
Which method will return a new string that is a substring of this string?
split()
substring()
slice()
getString()
What will be the output of System.out.println("Java".toUpperCase());?
java
Java
JAVA
Compilation Error
Which method removes all white spaces from a string?
trim()
strip()
removeSpaces()
deleteWhitespace()
What will be the output of "apple".compareTo("Apple")?
Positive number
Negative number
0
Compilation error
What is the purpose of String.format()?
To format strings
To concatenate strings
To convert strings to numbers
To compare strings
Which of the following methods checks if two strings are equal, ignoring case differences?
equalsIgnoreCase()
equals()
compareTo()
compareToIgnoreCase()
What does the StringBuilder method insert() do?
Inserts a string at the specified index
Deletes a string
Replaces a substring
Appends a string to the end
What is the output of "abc".concat("def")?
abcdef
abc def
ab cdef
Compilation Error
Which of the following methods can split a string into multiple strings?
substring()
split()
charAt()
indexOf()
What does the method String.valueOf() do?
Converts a string to an integer
Converts an object to a string representation
How would you check if a string contains only digits?
isDigit()
matches("[0-9]+")
isNumeric()
isDigitsOnly()
What will be the output of "Hello".indexOf('l')?
A) 1
B) 2
C) 3
D) -1
Which of the following will give you the last character of a string?
str.charAt(str.length() - 1)
str.lastChar()
str.charAtLast()
str.length() - 1
What does str.split(",") do?
Joins strings
Splits a string by commas
Replaces commas with spaces
None of the above
Which method can be used to check if a string is not empty?
!str.isEmpty()
str.length() > 0
Both A and B
str.notEmpty()
What is the output of System.out.println("Java".substring(0, 2));?
Ja
av
Java
Jav
Which of the following creates a new string object in the heap?
A) String str = "Hello";
B) String str = new String("Hello");
C) Both A and B
D) None of the above
What does the String.isBlank() method do?
Checks if the string is empty
Checks if the string contains only whitespace
Both A and B
None of the above
What is the output of " ".isBlank()?
true
false
null
Compilation Error
Which method can be used to remove leading and trailing whitespace from a string?
trim()
strip()
What is the primary purpose of StringBuffer in Java?
To create immutable strings
To create mutable strings
To store string literals
To concatenate strings
Which method in String can be used to get a stream of lines from a string?
lines()
getLines()
splitLines()
streamLines()
What will be the output of "Hello\nWorld".lines().count()?
A) 1
B) 2
C) 3
D) Compilation Error
What does it mean for a string to be immutable in Java?
The string can be changed after it is created
The string cannot be changed after it is created
The string can only be modified using StringBuffer
None of the above
Which of the following statements about immutable strings is true?
Strings are mutable and can be modified.
Modifying a string creates a new object.
Immutable strings are more memory-efficient.
Both B and C
Which of the following methods can be used on an immutable string?
append()
insert()
substring()
set()
Fill in the blank: The substring() method can be used to create a new string from an ________ string without changing the original.
immutable
mutable
temporary
dynamic
