Menu
Topics Index
...
`

For loop output559


If you need explanation Read this topic

If you need Answer Take test on this topic

For loop output559
What will be the output of the following program?
class ForTest
{
    public static void main(String[] args)
    {
        for (char i = 'A'; i <= 'G'; i++)
        {
            if (i % 5 == 0)
            {
                System.out.print((char) (i - 'A' + 'a'));
            }
            else if (i % 2 == 0)
            {
                System.out.print((int) i);
            }
            else
            {
                System.out.print(i);
            }
            System.out.print('~');
        }
    }
}

A. A~98~C~100~E~F~G~
B. 65~b~C~d~E~70~G~
C. 97~B~C~D~E~102~G~
D. a~66~C~68~E~f~G~
E. b~B~C~D~E~g~G~
F. Compilation Error
Topic: for Loop 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.

Ans is D.
As per the program the loop will iterate from A to G and prints the appropriate letter or number... if 'i' value modulo divided by 5 is 0 then it will print the lower case character else if 'i' value modulo divided by 2 is 0 then it will print the appropriate ASCII value else it will print upper case character with a ~(tild) symbol after every number or char.

Posted by Gopinath Manchikanti    2015-02-18 09:31:36


Ans is D

As here ASCII values are used for the character a to g.so their ASCII values are considered for the arithmetic operation and again these ASCII values are converted to char for printing.

--Ascii value of A is 65 which is divisible by 5 so A will get printed.similarly for every character their divisibility is checked by 5 or 2 else that letter is printed.

After every iteration end Tilda symbol is printed.

Posted by Mânïshå Mùlchåndânï    2015-02-18 20:16:50


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-19 04:12:42


© meritcampus 2019

All Rights Reserved.

Open In App