Topics Index

Compare for loop examples

If you need explanation Read this topic

If you need Answer Take test on this topic

Compare for loop examples
Compare the following Programs?
public class LoopExample1
    public static void main(String[] args)
        for (char i = 0, j = 1; i < 5 && j < 6; i++)
            System.out.print((int) i + " " + (int) j + ", ");

public class LoopExample2
    public static void main(String[] args)
        for (int i = 0, j = 1; i < 5 && j < 6; i++)
            System.out.print(i + " " + j + ", ");

A. Both LoopExample1 and LoopExample2 produce same output.  
B. LoopExample1 and LoopExample2 produce different output.
C. LoopExample2 compiles and runs with out any error, 
but LoopExample1 gives compilation error.
D. LoopExample1 compiles and runs with out any error,
 but LoopExample2 gives compilation error.
E. Both LoopExample1 and LoopExample2 fail to execute. 
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.

a.... In Java,char is a numeric type. When you initiAlise 0 to a char, you get to the unicode code point. .........A char is in fact mapped to an int, where 0 is ascii represent null........ now here again in print stmt it says (int)i i.e we have to print the ascii value of char I and j .....wich are in integer form hence the output is same as that of thw output of secomd loop

Posted by Asma Mujtaba Khan    2015-01-01 08:57:13

Ans is A after execution I see that 'for' loop is also accepting 'char' both out put is same
While compiling printing Int will accept the integers
So both both loops are same

Posted by Fuzail Ahmed    2015-01-01 09:00:10

ans a.1st program char variable is used on for loop, thn char value explicitly changed to int value,here range of givn value is in both char and int boundary, 2 nd program int variable used sop prints value depends on iteration

Posted by Maheshwari Natarajan    2015-01-01 09:02:08

ans is A as in the first program class loopexample1 " i"and " j" are initialised as char in the for loop.In  print statement it is converted into int  by type conversion and results in the output as  0 1, 1 2, 2 3, 3 4, 4 5,                                                                   in the second program loopexample2  initially they are initialized as int ie i,j and output is printed as  0 1, 1 2, 2 3, 3 4, 4 5,     therefore they produce same output

Posted by Ramya Reddy Addulla    2015-01-01 09:04:56

ANS IS A (Both the output produces same output)

0 1,1 2,2 3,3 4,4 5

In loopExaxpmle1 char counter is assigned to to integer value which is treated as ascii value...while printing them "TYPECASTING" is used to convert char to int.No compilation error wil get produced.
same is applied for loopExample2 with the difference that instead of char counter integer counter is used.

Sequence of execution:
Iteration 1
i=0 j=1
Iteration 2
i=1 j=2
Iteraton 3
i=2 j=3
Iteration 4
i=3 j=4
Iteration 5
i=4 j=5
now when i=5 and 5<5 returns false so loop will terminate.

Posted by Mânïshå Mùlchåndânï    2015-01-01 09:16:31

This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Ramya Reddy Addulla, Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-03 04:10:12

© meritcampus 2019

All Rights Reserved.

Open In App