What is the output of the following Java code:Code:class hacker{ public static void main(String[] arg) { int[] a = { 128, 132, 98, 116, 100 }; int x = func(a); System.out.println(x); } public static int func(int[] no) { int b = no[0]; for (int i: no) { if (i > b) b = i; } return b; }}
Question
What is the output of the following Java code:Code:class hacker{ public static void main(String[] arg) { int[] a = { 128, 132, 98, 116, 100 }; int x = func(a); System.out.println(x); } public static int func(int[] no) { int b = no[0]; for (int i: no) { if (i > b) b = i; } return b; }}
Solution
The output of the given Java code is 132.
Here's the step-by-step explanation:
-
The main method is the entry point of the Java program. It creates an array 'a' with the elements 128, 132, 98, 116, 100.
-
It then calls the function 'func' with the array 'a' as the argument.
-
Inside the function 'func', it initializes a variable 'b' with the first element of the array 'no' (which is the array 'a' passed from the main method).
-
Then it iterates over each element 'i' in the array 'no'. If 'i' is greater than 'b', it assigns the value of 'i' to 'b'. This means that 'b' will always hold the greatest value found so far in the array.
-
After the loop, it returns the value of 'b', which is the greatest value in the array.
-
Back in the main method, it prints the returned value, which is 132, the greatest value in the array 'a'.
Similar Questions
What is the output of the following Java code?Code:class HackerEarth{ public static void main(String[] args) { int val = 1; while((val<=5 ? val++: --val)<5) { System.out.print("MAC"); } if(val>5); System.out.print("PC"); }}
What is output of the following code:public class Test{ public static void main(String[] args){ int[] x = {120, 200, 016 }; for(int i = 0; i < x.length; i++) System.out.print(x[i] + " "); }
What's the output of the following code?public static void main(String[] args) { int[] vals = {4, 3, 2, 1}; for (int v : vals) { System.out.println(indexOf(vals, v)); }}public static int indexOf(int[] all, int element) { for (int i = 0; i < all.length; i++) { if (all[i] == element) return i; } return -1;}
What is the output of the following code :class MyClass {public static void main(String[] args) {int i=1,j=1;for (;i<3;i++){for(;j<4;j++);j++;System.out.print(i + j+" ");}}
What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.