93.5k views
2 votes
What is the output of the following code snippet?int f1 = 0;int f2 = 1;int fRes;System.out.print(f1 + " ");System.out.print(f2 + " ");for (int i = 1; i < 10; i++) { fRes = f1 + f2; System.out.print(fRes + " "); f1 = f2; f2 = fRes;}System.out.println();Select one:a. 0 1 5 7 9 11 13 15 17 19b. 0 1 1 2 3 5 8 13 21 34c. 0 1 4 6 8 10 12 14 16 18d. 0 1 6 7 9 12 14 17 19 21

User Sappy
by
5.4k points

2 Answers

7 votes

Answer:

");System.out.print(f2 + " ");for (int i = 1; i < 10; i++) { fRes = f1 + f2;

Step-by-step explanation:

User Paul Andrew
by
5.2k points
0 votes

Answer:

b. 0 1 1 2 3 5 8 13 21 34

Step-by-step explanation:

  1. int f1 = 0; creates an integer variable named f1 and sets its value as 0
  2. int f2 = 1; creates an integer variable named f2 and sets its value as 1
  3. fRes; creates an integer variable with no value
  4. System.out.print(f1 + " "); prints the value of f1 (0)
  5. System.out.print(f2 + " "); prints the value of f2 (1)
  6. The for loop runs 9 times and sets the value of fRes to the sum of f1 and f2, prints fRes, changes the value of f1 to f2, and changes the value of f2 to the current value of fRes
  7. System.out.printIn() prints a new line

The result of the loop block will be

  1. (0 + 1) = 1
  2. 1 + 1 = 2
  3. 1 + 2 = 3
  4. 2 + 3 = 5
  5. 3 + 5 = 8
  6. 5 + 8 = 13
  7. 8 + 13 = 21
  8. 13 + 21 = 34
  9. 21 + 34 = 55

So, it first prints "0 1" (the value of the first 2 variables), then the result of the the loop block

That would be 0 1 1 2 3 5 8 13 21 34 55

User Pasquers
by
4.8k points