Knowee
Questions
Features
Study Tools

public class Test{     public static int x = 20;     public static void fun(int arr[], int x){          while(x > arr.length){               x = x / 2;          }          for(int i=0;i<arr.length;i++){               arr[i] = x;          }     }     public static void main(String[] args) {          int[] a = {2,3,5,7,11};          int x = 100;          fun(a,x);          for(int i=0;i<a.length;i++){               System.out.print(a[i]+ " ");          }          System.out.println(x);     }}

Question

public class Test{     public static int x = 20;     public static void fun(int arr[], int x){          while(x > arr.length){               x = x / 2;          }          for(int i=0;i<arr.length;i++){               arr[i] = x;          }     }     public static void main(String[] args) {          int[] a = {2,3,5,7,11};          int x = 100;          fun(a,x);          for(int i=0;i<a.length;i++){               System.out.print(a[i]+ " ");          }          System.out.println(x);     }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This Java program defines a class named Test with a static integer variable x and a static method fun(). The fun() method takes an integer array and an integer as parameters. It then performs a loop where it divides the passed integer x by 2 until it is less than or equal to the length of the array. After that, it assigns the value of x to each element in the array.

In the main() method, an integer array a is defined with the elements {2,3,5,7,11}, and an integer x is defined with the value 100. The fun() method is then called with a and x as arguments. After the fun() method is executed, the program prints out each element in the array a followed by the value of x.

However, it's important to note that the x printed at the end of the main() method is the local x (value 100), not the x that was passed to and modified in the fun() method. This is because Java is pass-by-value, meaning that changing the value of x inside the fun() method does not affect the value of x in the main() method.

This problem has been solved

Similar Questions

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] + " "); }}

public class Main { public static int ElementsAndIndices(int[] arr) { if (arr == null || arr.length == 0) { return -1; } int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == i) { count++; } } return count; } public static void main(String[] args) { int[] arr = {10, 1, 12, 3, 5, 8, 9, 7, 12, 23}; System.out.println(ElementsAndIndices(arr)); // Output: 3 int[] arr2 = {-3, 0, 1, 3, 5, 7}; System.out.println(ElementsAndIndices(arr2)); // Output: 1 } } :6: error: cannot find symbol int result = ElementsAndIndices(arr); ^ symbol: method ElementsAndIndices(int[]) location: class SolutionClass 1 error it is showing this error

Determine output:public class Test{ public static void main(String args[]){ int i, j; for(i=1, j=0;i<10;i++) j += i; System.out.println(i); }}1011920

public static void main(String[] args){  for (int i = 3; i <= 7; i++) {int num;if(i%2 == 0){num = 0;  for (int j = 1; j <= 4; j++)  {  System.out.print(num);  num = (num == 0)? 3 : 2;  }} else {num = 1;  for (int j = 1; j <= 3; j++){System.out.print(num);num = (num == 0)? 2 : 5;}  }  System.out.println();}}

public class Question { public static void main(String args[]) { int f = 0, g = 1; for(int i = 0; i <= 5; i++) { System.out.println(f); f = f + g; g = f - g; } }}

1/3

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.