top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What happens when the following program is compiled and run.

+1 vote
842 views
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;
      }
}
posted Jan 12, 2016 by Kavana Gowda

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

3 Answers

0 votes

The program prints 4.

answer Jan 12, 2016 by Reshmi S
0 votes

compilation info
Main.java:8: error: class example is public, should be declared in a file named example.java
public class example
^
1 error

answer Jan 12, 2016 by Shivaranjini
0 votes
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is 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;
      }
}

Output:

4

answer Jan 12, 2016 by Shivaranjini
Similar Questions
+1 vote
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);
     }
}
+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);
    }
}
...