Ask Question

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

+3
Answers (1)
  1. 9 November, 07:52
    0
    b. 0 1 1 2 3 5 8 13 21 34

    Explanation:

    int f1 = 0; creates an integer variable named f1 and sets its value as 0 int f2 = 1; creates an integer variable named f2 and sets its value as 1 fRes; creates an integer variable with no value System. out. print (f1 + " "); prints the value of f1 (0) System. out. print (f2 + " "); prints the value of f2 (1) 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 System. out. printIn () prints a new line

    The result of the loop block will be

    (0 + 1) = 1 1 + 1 = 2 1 + 2 = 3 2 + 3 = 5 3 + 5 = 8 5 + 8 = 13 8 + 13 = 21 13 + 21 = 34 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
Know the Answer?