Question

Review the following code: public class Looping {    public static void main(String[] args) {      ...

Review the following code:

public class Looping {

   public static void main(String[] args) {

      for (int i = 1; i <= 5; i++) {

         for (int j = 1; j <= 5; j++) {

            System.out.println(i + " x " + j + " = " + (i * j));

         }

      }

   }

}

  1. What is the output from the code above?

Replace this text with your solution

  1. What happens if you change the print statement to be:

System.out.println(i + " + " + j + " = " + i + j);

Replace this text with your solution

  1. Why do you get that particular output for part (b) above?

Replace this text with your solution

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

Ans

a)

answer of first part is: both loop will run from 1 to 5 and perform operation i*j so initially it starts with value of i from 1 and at i=1 j will iterate through j=1 to j=5 so it prints table of 1,2,3,4,5 till 5th term of each because second loop will iterate only five times 
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25

Ans b) if we replace it with System.out.println(i + " + " + j + " = " + i + j); then it simply append the value of i and j while i vary from 1 to 5 for every j from 1 to 5.

for e.g at i=1 and  for (int j = 1; j <= 5; j++) will execute so here j changes from 1 to 5(for every value of j in range 1 to 5)

so output of this is

1 + 1 = 11

1 + 2 = 12

1 + 3 = 13

1 + 4 = 14

1 + 5 = 15

it will give output for every i=1,2,3,4,5

so final output is:

1 + 1 = 11
1 + 2 = 12
1 + 3 = 13
1 + 4 = 14
1 + 5 = 15
2 + 1 = 21
2 + 2 = 22
2 + 3 = 23
2 + 4 = 24
2 + 5 = 25
3 + 1 = 31
3 + 2 = 32
3 + 3 = 33
3 + 4 = 34
3 + 5 = 35
4 + 1 = 41
4 + 2 = 42
4 + 3 = 43
4 + 4 = 44
4 + 5 = 45
5 + 1 = 51
5 + 2 = 52
5 + 3 = 53
5 + 4 = 54
5 + 5 = 55

Ans c) we get this particular result for part(b) because in print statement the logic i+j is written without parenthesis so compiler simply treat it as a string and perform append operation instead of performing addition operation.

compiler will try to solve first () parenthesis in the print() statement so here we are typecasting integer to string initially e.g i+"x" typecasting integer to string.

whenever we are passing arguements in System.out.println() by default toString() method will call so in that case it will typecast integer to string .

And in first part it perform multiplicative operation instead of append operation because compiler first simply (i* j) because of higher priority of () operator so it first simply i*j then it will append result of that with the string in the print statement.

for e.g (i + " + " + j +" = "+ i+j) will be (1 + " +" + 1 + " = " +1+1) =1 + 1 =11 (appending string)

Add a comment
Know the answer?
Add Answer to:
Review the following code: public class Looping {    public static void main(String[] args) {      ...
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