Q:

Java find output programs (Arrays) | set 3

belongs to collection: Java find output programs

0

Find the output of Java programs | Arrays | Set 3: 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 NUM = 0,I=0,J=0;
        int arr[] = {7,8,9,11,17} ;
    
        
        for (I = 0; I < arr.length; I++)
        {
            NUM = 0;
            for (J = 2; J < (arr[I] / 2); J++)
            {
                if (arr[I] % J == 0)
                {
                    NUM = 1;
                    break;
                }
            }

            if (NUM == 0)
                System.out.println(arr[I]);
        }

     }
}

Question 2:

public class Main
{
     public static void main(String []args)
     {
        float[] floatArr = { 20.2, 21.4, 32.4 };

        for (float F : floatArr)
        {
            Console.WriteLine(F % 3);
        }
     }
}

Question 3:

public class Main
{
     public static void main(String []args)
     {
        float[] floatArr = { 20.2F, 21.4F, 32.4F };

        for (float F : floatArr)
        {
            System.out.println(F % 3);
        }
     }
}

Question 4:

public class Main
{
     public static void main(String []args)
     {
        float[] floatArr1 = { 20.2F, 21.4F, 32.4F };
        float[] floatArr2 = floatArr1;
        
        for (float F : floatArr2)
        {
            System.out.println(F % 3);
        }
     }
}

Question 5:

public class Main
{
     public static void main(String []args)
     {
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };

        for(String s : arrStr)
        {
            System.out.println(s);
        }
     }
}

All Answers

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

Answer Question 1:

Output:

7
11
17

Explanation:

In the above program, we created an integer array arr with 5 values. We also created three local variables NUM, I, and J initialized with 0. Here we find prime numbers from the array and print them on the console screen.

Note: Prime numbers are those numbers that can only divide by 1 or itself. But 2 is the lowest prime number.

In the given array 7, 11, and 17 are the prime numbers.

Answer Question 2:

Output:

Main.java:5: error: incompatible types: possible lossy conversion from double to float
        float[] floatArr = { 20.2, 21.4, 32.4 };
                             ^
Main.java:5: error: incompatible types: possible lossy conversion from double to float
        float[] floatArr = { 20.2, 21.4, 32.4 };
                                   ^
Main.java:5: error: incompatible types: possible lossy conversion from double to float
        float[] floatArr = { 20.2, 21.4, 32.4 };
                                         ^
Main.java:9: error: cannot find symbol
            Console.WriteLine(F % 3);
            ^
  symbol:   variable Console
  location: class Main
4 errors

Explanation:

The above program will generate syntax error because we initialized float array with double values, here we need to use values with suffix "F".

The correct way is given below:

float[] floatArr = { 20.2F, 21.4F, 32.4F };

Answer Question 3:

Output:

2.2000008
0.39999962
2.4000015

Explanation:

In the above program, we created an array of float values. Then we used for each loop. The for each loop is used to access values from array one by one.

Here, we find the remainder of each value and print on the console screen.

Answer Question 4:

Output:

2.2000008
0.39999962
2.4000015

Explanation:

In the above program, we created an array floatArr1 of float values and then assign the reference of array floatArr1 to array floatArr1. Then we used for each loop with array floatArr2 and access each element from array one by one.

Here, we find the remainder of each value and print on the console screen.

Answer Question 5:

Output:

Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                            ^
Main.java:5: error: ';' expected
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                              ^
Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                  ^
Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                     ^
Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                         ^
Main.java:5: error: not a statement
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                       ^
Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                            ^
Main.java:5: error: unclosed character literal
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                                  ^
Main.java:5: error: not a statement
        String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
                                              ^
Main.java:7: error: illegal start of type
        for(String s : arrStr)
        ^
Main.java:7: error: ')' expected
        for(String s : arrStr)
                    ^
Main.java:7: error:  expected
        for(String s : arrStr)
                             ^
Main.java:12: error: class, interface, or enum expected
}
^
13 errors

Explanation:

The above program will generate syntax error because we used single quotes with string instead of double-quotes.

Note: Single quotes are used for a single character and double quotes are used for the string in Java.

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 (this Keyword) | set 1... >>
<< Java find output programs (Arrays) | set 2...