Question



What is the output of the following codes? Line1: public class ArrayCompare { Line2: public static void main (String[] args)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :-

public class ArrComp
{
public static void main(String args[])
{
int arr1[] = {1,2,3};
int arr2[] = {1,2,3};
if(arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not Same");
}

}

The output of the code is Not same.

Explanation :-

The output of the above code will be Not same. Although the elements in both the arrays is same i.e 1,2,3 but in this case when we compare both the arrays by using if decision statement i.e if(arr1 = = arr2), this will compare the base address of both the arrays. The arrays(arr1 and arr2) are stored in different memory locations and hence their base address will be different respectively.

Thus when the control of the program checks the condition if(arr1 = = arr2) it will be false and the control jumps to the else block and prints "Not same." Thus the output of the above code is Option b i.e Not same.

Add a comment
Know the answer?
Add Answer to:
What is the output of the following codes? Line1: public class ArrayCompare { Line2: public static...
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
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