- 2

About Java MCQ

What is the output of this code? String[]a=(“76.88”).split(“.”); System.out.Println(a.length); But how the output is 0?

14th Oct 2022, 3:09 AM
Mustakim Rahman
Mustakim Rahman - avatar
4 RĂ©ponses
+ 2
Mustakim Rahman dot (.) is a special character of Java Regex so you cannot split string on dot that's why length is 0 If there is \\. then output is 2
14th Oct 2022, 2:55 PM
AÍąJ
AÍąJ - avatar
+ 3
You have syntax errors in your snippet. Quotation marks are not standard, and println must be lowercase. Anyway, split argument is actually a regular expression. In regex world, the dot means "any character". So if you split the string across any character, then there is nothing left! If you want to split at the dot, you need to escape it in the regex, and in Java you need to use double backslashes. String[] a = "76.88".split("\\."); System.out.println(a.length); // output: 2
14th Oct 2022, 3:57 AM
Tibor Santa
Tibor Santa - avatar
0
I also think 2 but in mcq challenge it shows 0 as correct answere
14th Oct 2022, 4:45 AM
Mustakim Rahman
Mustakim Rahman - avatar
0
As I said, the code in your description does not work, because it has syntax errors.
14th Oct 2022, 12:59 PM
Tibor Santa
Tibor Santa - avatar