top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What gets printed when the following program is compiled and run?

+1 vote
568 views
class test
 {
    static boolean check;
    public static void main(String args[])
   {
        int i;
        if(check == true)
            i=1;
        else
            i=2;
        if(i=2) i=i+2;
        else i = i + 4;
        System.out.println(i);
     }
}
posted Jan 12, 2016 by Divya Shree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Although the program won't run because of if(i=2) but if your condition become
if(i==2) instead of if(i=2) then output=4

answer Feb 12, 2016 by Shivam Kumar Pandey
0 votes

The program does not compile because of the statement if(i=2).

The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a Boolean.

answer Jan 12, 2016 by Kavana Gowda
Similar Questions
+1 vote
public class example 
{
     int i[] = {0};
         public static void main(String args[])
    {
         int i[] = {1};
          change_i(i);
          System.out.println(i[0]);
      }
      public static void change_i(int i[]) 
       {
         i[0] = 2;
         i[0] *= 2;
      }
}
+2 votes
public class test {
    public static void main(String args[]) { 
    String s1 = "abc";
    String s2 = "abc";
    if(s1 == s2)
        System.out.println(1);
    else
        System.out.println(2);
    if(s1.equals(s2))
        System.out.println(3);
    else
        System.out.println(4);
    }
}
...