Q:

Java find output programs (Arrays) | set 2

belongs to collection: Java find output programs

0

Find the output of Java programs | Arrays | Set 2: Enhance the knowledge of Java Arrays concepts by solving and finding the output of some Java programs.

Question 1:

public class Main
{
     public static void main(String []args)
     {
        int arr[]={5,3,1,6,2};
        
        int val=0;

        val = arr[0];
        for(int i=1; i<=arr.length;i++)
        {
            if (val > arr[i])
                val = arr[i];
        }

        System.out.println(val);
     }
}

Question 2:

public class Main
{
     public static void main(String []args)
     {
        int arr[]={5,3,1,6,2};
        
        int val=0;

        val = arr[0];
        for(int i=1; i<arr.length;i++)
        {
            if (val < arr[i])
                val = arr[i];
        }

        System.out.println(val);
     }
}

Question 3:

public class Main
{
     public static void main(String []args)
     {
        int arr[]={5,3};
        
        int val=0;

        arr = new int[5];
        
        val = arr[0];
        for(int i=1; i<arr.length;i++)
        {
            if (val < arr[i])
                val = arr[i];
        }

        System.out.println(val);
     }
}

Question 4:

public class Main
{
     public static void main(String []args)
     {
        int myArray[][];
        int P,Q;
        
        myArray = new int[2][2];

        for (int P = 0; P < 2; P++)
        {
            for (Q = 0; Q < 2; Q++)
                myArray[P][Q] = P + Q;
        }

        for (P = 0; P < 2; P++)
        {
            for (Q = 0; Q < 2; Q++)
                System.out.println(myArray[P][Q] + " ");
        }

     }
}

Question 5:

public class Main
{
     public static void main(String []args)
     {
        int arr[][] = new int[][] 
                    {
                        new int[] {21, 22, 23, 24},
                        new int[] {25, 26},
                        new int[] {27, 28,29},
                        new int[] {30, 31,32,33,34}
                    };

        for (int I = 0; I < arr.length; I++)
        {
            for (int J = 0; J < arr[I].length; J++)
                System.out.print(arr[I][J] + " ");
            System.out.println();
        }
     }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at Main.main(Main.java:12)

Explanation:

The above program will generate runtime exception because arr.length will return 5, and the highest index of the array is 4 but we are trying to access the element at index 5, which is out of bounds of the array.

Answer Question 2:

Output:

6

Explanation:

In the above program, we created an integer array that contains 5 elements, and an integer variable val with initial value 0.

val = arr[0];

Here, we assigned the first value of array that is 5 to the variable val. Here, we find the largest element from the array. here we compare variable val with each item of the array and assign a greater value into the variable val and finally val contains the largest value, that will be printed on the console screen.

Answer Question 3:

Output:

0

Explanation:

In the above program, we created an integer array initialized with 5, 3. After that, we reallocated the array using a new operator that's why all 5 elements initialized with 0 then the final value of variable val will be 0.

Answer Question 4:

Output:

Main.java:10: error: variable P is already defined in method main(String[])
        for (int P = 0; P < 2; P++)
                 ^
1 error

Explanation:

The above program will generate syntax error because we declare a local variable P and then we re-define variable P in the for loop that's why error gets generated in the program.

Answer Question 5:

Output:

21 22 23 24 
25 26 
27 28 29 
30 31 32 33 34 

Explanation:

In the above program, we created a two-dimensional jagged array. As we know that a two-dimensional jagged array contains the different number of elements in the different rows.

for (int I = 0; I < arr.length; I++)
{
    for (int J = 0; J < arr[I].length; J++)
        System.out.print(arr[I][J] + " ");
    System.out.println();
}

Here, the nested loop is used to print elements of a jagged array. Here, we used length data member with a row index, which is given below. Because every row may have a different number of elements.

arr[i].length

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Arrays) | set 3... >>
<< Java find output programs (Arrays) | set 1...