Menu
Topics Index
...
`

Procedure


If you need explanation Read this topic

If you need Answer Take test on this topic

Procedure
What will be the output of the following program?
class Procedure {
    public static void main(String[] args) {
        String in = "198";
        int n = 5;
        while (n > 0) {
            char[] line = in.toCharArray();
            int i = line.length - 1;
            if (line[i] % 2 != 0) {
                line[i]--;
            }
            System.out.print(line);
            System.out.print(" ");
            n--;
        }
    }
}

A. 198 188 178 168 158
B. 198 197 196 195 194
C. 197 197 197 197 197
D. 198 198 198 198 198
E. Compilation Error or Runtime Error
Topic: Java Character Extraction - toCharArray() Method In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

option D is the ans..i is always 2..and if condition never gets satisfied...

Posted by Soumya Tata    2013-05-29 12:41:38


ans is D ....

Posted by Rasagna Veeramallu    2013-05-29 12:43:44


ans: D...size of char array is 3 so i bcums 2...the if condition never satisfies and the variable remains same, it prints 198 everytime!

Posted by Santosh Munugota    2013-05-29 12:45:04


ans:d because toCharArray() returns length of the string and initializes it to that string ..so the output is 198 all times

Posted by Sai Lalitha Renduchintala    2013-05-29 12:45:11


option d : bcoz array has 1 9 8 in it.. line.length is 3 and i is alwaz 2.. so as 8 is the char value whose ascii value is 56 and 56%2 is always 0 so if is never entered and the elements of array does not get modified and we print the 198 5 tyms as loop repeats for 5 tyms.

Posted by Pramod Jain    2013-05-29 13:00:34


Congratulations Soumya Tata. You are daily dose winner. Please send us your mobile details and we will process the recharge after considering the new likes.

Posted by Merit Campus    2013-05-30 04:37:59


thanku..got rc :)

Posted by Soumya Tata    2013-05-31 05:58:34


© meritcampus 2019

All Rights Reserved.

Open In App