Home »
Java »
Java find output programs
Java find output programs (Arrays) | set 3
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.
Submitted by Nidhi, on January 30, 2021
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]);
}
}
}
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.
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);
}
}
}
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 };
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);
}
}
}
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.
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);
}
}
}
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.
Question 5
public class Main
{
public static void main(String []args)
{
String[] arrStr = { 'INDIA', 'USA', 'CHINA' };
for(String s : arrStr)
{
System.out.println(s);
}
}
}
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.