Java find output programs (Arrays) | set 1

Find the output of Java programs | Arrays | Set 1: Enhance the knowledge of Java Arrays concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on January 30, 2021

Question 1

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

        for(int i=0; i<5;i++)
        {
            num += arr[i];
        }
        System.out.println(num);
     }
}

Output

Main.java:5: error: ']' expected
        int arr[5]={1,2,3,4,5};
                ^
Main.java:5: error: illegal start of expression
        int arr[5]={1,2,3,4,5};
                 ^
Main.java:5: error: illegal start of expression
        int arr[5]={1,2,3,4,5};
                   ^
Main.java:5: error: not a statement
        int arr[5]={1,2,3,4,5};
                    ^
Main.java:5: error: ';' expected
        int arr[5]={1,2,3,4,5};
                     ^
Main.java:8: error: illegal start of type
        for(int i=0; i<5;i++)
        ^
Main.java:8: error: ')' expected
        for(int i=0; i<5;i++)
                 ^
Main.java:8: error: illegal start of type
        for(int i=0; i<5;i++)
                  ^
Main.java:8: error:  expected
        for(int i=0; i<5;i++)
                   ^
Main.java:8: error: ';' expected
        for(int i=0; i<5;i++)
                    ^
Main.java:8: error:  expected
        for(int i=0; i<5;i++)
                       ^
Main.java:8: error: illegal start of type
        for(int i=0; i<5;i++)
                        ^
Main.java:8: error: '(' expected
        for(int i=0; i<5;i++)
                          ^
Main.java:12: error:  expected
        System.out.println(num);
                          ^
Main.java:12: error:  expected
        System.out.println(num);
                              ^
Main.java:14: error: class, interface, or enum expected
}
^
16 errors

Explanation

The above program will generate syntax errors because of the array declaration.

int arr[5]={1,2,3,4,5};

Here, we declared array arr with size 5, here we must use a new operator to initialize the array.

The correct way is given below:

int arr[];
arr = new int[5];

Question 2

public class Main {
  public static void main(String[] args) {
    int arr[5];
    int num = 0;

    arr = new int[5];
    for (int i = 0; i < 5; i++) {
      num += arr[i];
    }
    System.out.println(num);
  }
}

Output

Main.java:3: error: ']' expected
    int arr[5];
            ^
Main.java:3: error: illegal start of expression
    int arr[5];
             ^
2 errors

Explanation

The above program will generate syntax errors because of the array declaration.

int arr[5];

We cannot define the size of an array like this, the correct way is given below,

int arr[]=new int[5];

Question 3

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

        for(int i=0; i<=5;i++)
        {
            num += arr[i];
        }
        System.out.println(num);
     }
}

Output

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

Explanation

The above program will generate runtime exception because array arr contains 5 elements. Then the index of an array starts from 0 to 4 but we tried to access the element at index 5 that is out of bounds of the array.

Question 4

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

        for(int i=0; i<=4;i++)
        {
            num *= arr[i];
        }
        System.out.println(num);
     }
}

Output

0

Explanation

The above program will print 0 on the console screen. Here, we declared an integer array initialized with 5,3,1,6,2 and an integer variable num initialized with 0.

Within the body of the loop and multiply with num every time but variable num is initialized with 0 that's why the result will be 0.

When I=0;
	Num = num *5;
	Num =0;
When I=1;
	Num = num *3;
	Num =0;
When I=2;
	Num = num *1;
	Num =0;
When I=3;
	Num = num *6;
	Num =0;
When I=4;
	Num = num *2;
	Num =0;

Question 5

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.len();i++)
        {
            if (val > arr[i])
                val = arr[i];
        }

        System.out.println(val);
     }
}

Output

Main.java:10: error: cannot find symbol
        for(int i=1; i<arr.len();i++)
                          ^
  symbol:   method len()
  location: variable arr of type int[]
1 error

Explanation

The above program will generate syntax error because len() method is not available in Java, to get the length of the array we need to use length data members of the array.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.