Question

Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

Java will be printed 10. can you explain step by step why?
public class WhatsPrinted2 {
    public static void whatHappens(int A[]) {
        int []B = new int[A.length];

        for (int i=0; i<A.length; i++) {
            B[i]=A[i]*2;
        }
        A=B;
    }
    
    public static void main(String args[]) {
        int A[] = {10,20,30};
        whatHappens(A);
        System.out.println(A[0]);
    }
}

will print 10. explain how it's works. Thanks

public class WhatsPrinted3 {
    public static int[] whatHappens(int A[]) {
        int []B = new int[A.length];

        for (int i=0; i<A.length; i++) {
            B[i]=A[i]*2;
        }
        return B;
    }
    
    public static void main(String args[]) {
        int A[] = {10,20,30};
        whatHappens(A);
        System.out.println(A[0]);
    }
}
public class WhatsPrinted4 {
    public static int[] whatHappens(int A[]) {
        int C[]=new int[A.length];
        for (int i=0; i<A.length; i++) {
            C[i]=A[i];
        }
        return C;
    }

    public static void main(String args[]) {
        int A[]={10,20,30};
        int B[]=whatHappens(A);

        if (A==B) {
            System.out.println("equal");
        } else {
            System.out.println("not equal");
        }
    }
}

step by step please. thanks

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

1.

The program prints 10 because array A is passed as value to the function. So whatever changes made to the array A inside the function will not get reflected in main method

=> printing A[0] in main method will give 10 as 10 is the first element in the actual A

A[0] will be 20 inside the whatHappens function

2.

whatHappens function returns B which is not handled by the main function

=> values of B are not getting reflected to A

If A is assigned with B in main function, then 20 will be printed.

3.

array C is assigned with array A's values and returned which is handled by array B.

In if condition, the address of A and B are checked which is different from each other and produces "Not equal" as the output

values have to be compared to check whether both the arrays are equal.

Add a comment
Know the answer?
Add Answer to:
Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...
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
  • Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

    Java 1. Can you explain it how it will be printed step by step? Thanks!! What is printed by the following? for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) { if (i%2==0) { i++; System.out.println(i + " " + c); } else { c++; System.out.println(c + " " + i); } } } 2. Is there a compiler error in this code? If so, what is it? If not, what’s printed? Is there a compiler error in...

  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

  • What are the first five lines printed from this code and why? public class LoopQ {...

    What are the first five lines printed from this code and why? public class LoopQ { public static void main(String[] args) { for(char c = 'A'; c < 'Z'; c++) { for(int i = 0; i < 2; i++) { System.out.println("seat "+c+i); } } } }

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

  • Draw a UML class diagram (with associations) to show the design of the Java application. public...

    Draw a UML class diagram (with associations) to show the design of the Java application. public class OOPExercises {     public static void main(String[] args) {         //A objA = new A();         B objB = new B();         System.out.println("in main(): ");         //System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         //objA.setA (222);         objB.setB (333.33);         //System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } public class A {     int a = 100;     public A() {...

  • Explain in detail what the code below does: public class MyClass {       public static void...

    Explain in detail what the code below does: public class MyClass {       public static void main(String args[]) {              System.out.println(isUniqueChars("something"));       }       public static boolean isUniqueChars(String str) {             int checker = 0;                                                                                               for (int i = 0; i < str.length(); ++i) {                         int val = str.charAt(i) - 'a';                         if ((checker & (1 << val)) > 0) return false;                         checker |= (1 << val);             }             return true;...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

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
ADVERTISEMENT