(a) What is the output of the main method when the followingprogram is executed?
ID: 3618936 • Letter: #
Question
(a) What is the output of the main method when the followingprogram is executed?PLEASE GIVE A DETAILED EXPLANATION OF EACH OF OUTPUT NUMBERSFROM THIS CODE. PLEASE MAKE IT NICE AND CLEAR. ANSWER IS PROVIDED BELOW
18 POINTS WILL BE GIVEN
public class Prog06{ private static int i=2; public static void One(){ int i=2; i-=2; System.out.println(i); } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; return b+2; } public static void main(String[] args){ System.out.println(i+1); One(); i = Three(i); System.out.println(i); Three(i); Two(i+1); One(); } }
PLEASE GIVE A DETAILED EXPLANATION OF THE OUTPUT OF EACH OFTHE NUMBERS,
ANSWER: 3 0 7 10 0 0 (a) What is the output of the main method when the followingprogram is executed?
PLEASE GIVE A DETAILED EXPLANATION OF EACH OF OUTPUT NUMBERSFROM THIS CODE. PLEASE MAKE IT NICE AND CLEAR. ANSWER IS PROVIDED BELOW
18 POINTS WILL BE GIVEN
public class Prog06{ private static int i=2; public static void One(){ int i=2; i-=2; System.out.println(i); } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; return b+2; } public static void main(String[] args){ System.out.println(i+1); One(); i = Three(i); System.out.println(i); Three(i); Two(i+1); One(); } }
PLEASE GIVE A DETAILED EXPLANATION OF THE OUTPUT OF EACH OFTHE NUMBERS,
ANSWER: 3 0 7 10 0 0 3 0 7 10 0 0
Explanation / Answer
Please Rate.. I am rearranging your code. public class Prog06{ private static int i=2; public static void main(String[] args){ System.out.println(i+1); //2+1 = 3, 3will be printed One(); //controlgoes to the function named "one" i =Three(i); //controlgoes to the function named "three which will return value and thisvalue will assign to i. System.out.println(i); //7is come from three, value of i is set 7. print it, 7 will beprinted. Three(i); //7will be passed to function three which will change it to somegarbage value Two(i+1); //garbage+1 will be passed to function TWO, function two will called. One(); //zerowill be printed on the console. } public static voidOne(){ inti=2; //i=2 i-=2; //thisstatement means i=i-2 so it will be 0 System.out.println(i); //zerowill be printed and control goes to back in main. } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; //bvalue will be two, this expression will change it to2*2+1=5 returnb+2; //return5+2 means 7. this value will assign to i in the main. go back tomain. } } public class Prog06{ private static int i=2; public static void main(String[] args){ System.out.println(i+1); //2+1 = 3, 3will be printed One(); //controlgoes to the function named "one" i =Three(i); //controlgoes to the function named "three which will return value and thisvalue will assign to i. System.out.println(i); //7is come from three, value of i is set 7. print it, 7 will beprinted. Three(i); //7will be passed to function three which will change it to somegarbage value Two(i+1); //garbage+1 will be passed to function TWO, function two will called. One(); //zerowill be printed on the console. } public static voidOne(){ inti=2; //i=2 i-=2; //thisstatement means i=i-2 so it will be 0 System.out.println(i); //zerowill be printed and control goes to back in main. } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; //bvalue will be two, this expression will change it to2*2+1=5 returnb+2; //return5+2 means 7. this value will assign to i in the main. go back tomain. } public static voidOne(){ inti=2; //i=2 i-=2; //thisstatement means i=i-2 so it will be 0 System.out.println(i); //zerowill be printed and control goes to back in main. } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; //bvalue will be two, this expression will change it to2*2+1=5 returnb+2; //return5+2 means 7. this value will assign to i in the main. go back tomain. } public static int Three(int b){ b=2*b+1; //bvalue will be two, this expression will change it to2*2+1=5 returnb+2; //return5+2 means 7. this value will assign to i in the main. go back tomain. } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.