java - Does anyone know how to work this out? -


what output produced following program? answer says 7 have trouble working out.

public class practice {     public static void main(string[] args){         int = 5;         int b = g(i);         system.out.println(b+i);      }      public static int f(int i) {         int n = 0;         while (n * n <= i) {n++;}         return n-1;     }      public static int g(int a) {         int b = 0;         int j = f(a);         b = b + j;         return b;     }   } 

i assume main getting called. here list of steps happen

  1. g gets called 5 parameter.
  2. then in function g, f gets called g's parameter, 5
  3. in function f n set zero, while loop called , every time n*n less or equal parameter, 5, n incremented. below outlines while loop.
    1. 0*0 less 5, increment n 0 1 , continue.
    2. 1*1 less 5, increment n 1 2 , continue.
    3. 2*2 less 5, increment n 2 3 , continue.
    4. 3*3 not less 5, break out of loop.
  4. n-1, 3-1=2, gets returned called, in variable j in function g.
  5. b gets assigned b+j 0+2.
  6. b gets returned variable b in function main.
  7. b+i, 5+2, 7, gets printed answer.

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -