How Can I Trace A Java Program . Please Give Me Step by Step
Process



How Can I Trace A Java Program . Please Give Me Step by Step Process..

Answer / shanu zabeen

//consider the follwing program part

for(int k = 0; k < 10; k++);
System.out.println("hi");
int k = 9
while(k < 10)
{
System.out.println("world");
k++;
}
//now lets trace the program as follows
start, at the for loop, k is initialized to 0 with a scope
local to the for loop itself.
k = 0
Is it less than 10? yup, so we enter the loop.
Next, we have output,
hi.
Since there is no brackets, the for loop applies only to the
very next line. In this case, the output. So, return to the
top of the loop, k increments by 1
k = 1
and we proceed again since 1 < 10.
The output "hi" is printed again.
Go back to the top of the loop, increment k, and recheck the
loop condition of k < 10. The above cycle repeats until k is
incremented and equals 10. Since 10 is not less than 10,
exit the for loop.
:
k = 10
next
Now, declare an integer k to be equal to 9.
k = 9
Note it has local scope to this part of the program but,
since the for for loop previous one is gone from memory, so
too is the integer k that it (the loop) declared.
Thus, there is no conflict. So, an integer variable k (one
different from the last) is now = 9

Check the while boolean condition and k is less than 10 so
enter the while loop.
Next, print the output
"world"
Next, increment k by 1 ("k++") so
k = 10
Check the while loop conditional again but, this time, it's
false. k is not less than 10 anymore. So, exit the while loop.

Is This Answer Correct ?    3 Yes 5 No

Post New Answer

More J2SE Code Interview Questions

Can we run Applet in Web browser with security policy files

0 Answers  


Write a program to convert a decimal number to binary form?

1 Answers   Oracle,


write a program in java to find the moving average of all prime numbers between 2 and 100.

0 Answers  


how to print a message to console without using main() function?(do not use even static blocks also.)

14 Answers   Google, Zoho,


How to create Date method to set the date in Ms Access

0 Answers  






Why we r using String args[] in main() even though v r not passing any arguments in command line?

2 Answers  


Is it possible to define marker interface in java.If possible then how to define user defined marker interface?

1 Answers   Tech Mahindra,


write a program that will ask the user to enter a number n and display the product of all numbers from 1 to n.

1 Answers  


In java, why do we set thread priority, when we know that there is no guarantee by which a thread should be execute?

0 Answers   Google, Microsoft,


what are the other loops except for for,while,do while and until?

0 Answers   Aricent,


write a program in java to solve a system of n-variabled simultaneous equations using the guassian elimination method. let the maximum possible value of n be 100. run the program using hypothetical values for a set of 10- variables simultaneous equations. print out the program, the input equation and the results generated by the program.

0 Answers   TCS,


How Can I Trace A Java Program . Please Give Me Step by Step Process

1 Answers   IBM,


Categories