Topics Index

Output of for loop

If you need explanation Read this topic

If you need Answer Take test on this topic

Output of for loop
What will be the output of the following program?
public class ForTest

    public static void main(String[] args)
        int m = 7;
        int n = m + 'a' - 'X';
        int i = m;
        for (; i <= n;) {
            m += i++;

        System.out.println("i = " + i + " m = " + n + " n = " + m);

A. i = 17 m = 16 n = 16
B. i = 17 m = 122 n = 16
C. i = 17 m = 16 n = 122
D. Compilation Error
E. Goes into infinite loop
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.


Posted by Goutham Singarapu    2015-01-12 03:48:24

Ans is we have the 3 local variables m n we assigned m with the 7 and n with 'a'-'X' means it will take its ascii values (A-Z(65-90))(a-z(97-122)) than n become 16 and i is assigned with the 7 next using the for loop we are adding the value of i to m here we are using the post increment so 1st it will give value and later it will increment..after coming out of the loop i=17 and n=16 and we are printing the value of m =n and n=m

Posted by Uday Kumar    2015-01-12 03:57:56

Ans is Option C.... after execution starts variable m value became 7 and n value became 16 because character 'a' value is 97 and character 'X' value is 88, and i value became 7 after compiler enter into loop we know i value is 7 and n value is 16 so loop will iterate 10 times and statement inside the loop will execute 10 times then i will become i=17, and m will become m=122, finally it will come out of the loop and it will execute sop here we assigned the  value i=i i.e i=17 and m=n i.e m=16 and n=m i.e n=122 so final ans is i=17 m=16 n=122

Posted by Bhagi Bhagyasri    2015-01-12 04:57:55

i'll go with option C .. .. here Execution Starts from main() method. .. variables will be smoothly initialized as in 2nd line "chars are converted into their corresponding ASCII values (integers)" .. So Now in the loop i is incremented (post increment ) which makes the loop condition fail at certain instance .. so we will get an particular value for m,n,i .. They are printed in last statement.. as i =17 , m=16, n=122 ..

Posted by ?????????? ?????    2015-01-12 05:45:00

ans is C as m,n,i are initialized m=7 n=16 as  it takes ascii values of 'a' and 'X' and i is assigned same value as m compiler enters into the forloop given condition i<=n first of all m=m+i++; here first m=7+7=14 and next it executes 10 times untill condition fails ie i  becomes greater than n then i=17 m=122 and n=16 then it comes out of loop and in print statement it prints i=17 m=16 as "m="+n is given and "n="+m is given so it prints n=122so final ans is i=17 m=16 n=122

Posted by Ramya Reddy Addulla    2015-01-12 06:00:42


Posted by Gaurav Bansal    2015-01-13 03:38:30


Posted by Shubham Bansal    2015-01-13 03:48:55

In my point of view none of them is correct

Posted by Jitendra Rupeja    2015-01-17 16:20:58

i=7 m=-16 n=7

Posted by Jitendra Rupeja    2015-01-17 16:23:22

This dose is now closed and the winners are Bhagi Bhagyasri,  for 'First Correct Comment', Ramya Reddy Addulla,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Bhagi Bhagyasri. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-19 04:46:07

© meritcampus 2019

All Rights Reserved.

Open In App