Java Strings Aptitude Questions and Answers.
This section contains Aptitude Questions and Answers on Java Strings Class - Reading Strings, Printing Strings, Converting and Manipulating String. This section also covers methods of the String class.
List of Java Strings Aptitude Questions
1)
Is following statement is correct?
System.out.println("My name is ".concat("Mike"));
- Yes
- No
Correct Answer: 1
Yes
The statement is correct; output will be My name is Mike.
2)
What will be the output by following code snippet?
System.out.printf("%d,%f",10,20.35);
- Error
- 10,20.35
- 10,20.350000
- 10,20.00
Correct Answer: 3
10,20.350000
System.out.printf() can be used to print mixed data (same as c programming) in java.
3)
What is the return type of String.compareTo() method.
- boolean
- byte
- char
- int
Correct Answer: 4
int
Method String.compareTo() compares two string (sometime it can be used to compare string with other object too) and returns difference of first dissimilar characters.
4)
Which method is used to compare two strings ignoring the case?
- compareToIgnoreCase()
- compareToI()
- compareToWithoutCase()
- None of these
Correct Answer: 1
compareToIgnoreCase()
Method compares two strings without checking the case and returns the difference of first dissimilar characters.
5)
What will be the output by following code snippet?
String text1="I am Mike";
String text2="I am Nike";
System.out.println(text1.contentEquals(text2));
- true
- false
- -1
- 1
Correct Answer: 2
false
Method contentEquals() returns true or false.