Question

package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...

package array;

public class Test {

static int[] data = (0,1,2,3,4,5,6,7,8);

public static void main (String[] a) {

for ( int i = 0;i<data.length; i++) {

if(i %3 == 0) {

System.out.print("A");

System.out.print(data[i]);

System.out.print(" ");

}

}

I need an explanation of what this code is doing ? is there anything wrong with it

}}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// correct after removing syntax error

public class Test {

static int[] data = {0,1,2,3,4,5,6,7,8};

public static void main (String[] args)

{

for ( int i = 0;i<data.length; i++) {

if(i %3 == 0) {

System.out.print("A");

System.out.print(data[i]);

System.out.print(" ");

}

}

}}

output:- A0 A3 A6

EXPLANATION:- SINCE ARRAY IS STATIC SO IT CAN ASSCESS ANY METHOD SO FOR WILL GET EXECUTE AND WHEN I%3 ==0 FOR I =0,3,6 IT WILL PRINT A AND THEN DATA AT data[0] which will print A0 similarly for i =3 and 6

Add a comment
Know the answer?
Add Answer to:
package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • What is wrong the following code? Explain. public class Test { public static void main(String[] args)...

    What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • What is printed by running the following code? public static void main(String[] args) { int[] nums...

    What is printed by running the following code? public static void main(String[] args) { int[] nums = {2, 3, 4}; int n = 5; changeMe1(n, nums); System.out.print( n ); System.out.print(nums[0]); changeMe2(n, nums); System.out.print( n ); System.out.print(nums[0]); } public static void changeMe1(int number, int[] list) { number++; list[0]++; } public static void changeMe2(int number, int[] list) { number = 9; list = new int[1]; list[0] = 99; }

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • Analyze the following code: public class Test { private int t; public static void main(String[] args)...

    Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT