Consider the following program public class Foopublic static

Consider the following program. public class Foo{public static int x - 3; public int y: public Foo(){y - 10;} public char foo(char c){char d = c + 1; return d;} public static void main(String[] arga){Foo f - nov Foo(); f.foo(\'a\');}} When the program is run and line 8 has just finished executing, for each of the following state where the variable lives and where the data for the variable lives: f, d, c, x, y.

Solution

When the program runs the control starts from main method which is creating object of class Foo and then function foo with argument as \'a\' , since it’s a char so control will go to public char foo(char c)

where char d = c +1 i.e. Ascii value of \'a\' which is 97

char d = 97+1 i.e. 98 Ascii code \'b\'

Now the values of variable at this line of execution

f is object hence in memory f will get some memory and we have one static variable x which will stored in this object so f will have x=3 and now when we are creating object f its constructor would be called which is initialising value of y so y=10 at that time . As we have called function foo with passing \'a\' as argument so c will be \'a\' and d after calculation becomes \'b\'

 Consider the following program. public class Foo{public static int x - 3; public int y: public Foo(){y - 10;} public char foo(char c){char d = c + 1; return d;

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site