Menu
Topics Index
...
`

Loops Are Simple


If you need explanation Read this topic

If you need Answer Take test on this topic

Loops Are Simple
What will be the output of the following program?
public class LoopsAreSimple {

    public static void main(String[] args) {
        char c = 'A';
        int i = 0;
        for (; i < 4; i++, m(i, c)) {
            c += i;
        }
        m(i, c);
    }

    private static void m(int i, char c) {
        for (int j = 0; j < i; j++) {
            System.out.print(c);
        }
    }
}

A. ABBDDDGGGGKKKK
B. ABBDDDGGGGGGGG
C. BDDGGGKKKKKKKK
D. Some other output or Compilation Error or Runtime Error
Topic: Java Methods - Parameter Passing And Scope

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 A first it prints A then add 1 to A and becomes B prints 2 times then B+2=D prints 3 times and then D+3=G prints 4 times and then G+4=K prints 4 times

Posted by Yeshu Yeshwanthi    2013-10-04 09:59:51


ans must be B bcoz c is incremented i tyms n printed i tyms.. bt at the end the function m is called again so G is printed 8 tyms

Posted by Pramod Jain    2013-10-04 10:22:43


b is the ans here we are adding the updated c value to I so initially we print for I=0 and then I=1,2,3. after 4 iteration we invoke a last call of m method which also results the same result as prev iteration since I in not incremented thereby giving G once again

Posted by Karteek Paruchuri    2013-10-04 10:26:56


Congratulations Karteek Paruchuri. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-10-05 06:10:40


© meritcampus 2019

All Rights Reserved.

Open In App