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

1+(2*3)+(4*5*6)+(7*8*9*10)+... specified input value

1 Answers   MAQ,


A game that has five levels of play has the score for each level stored in an array. You are to write a program that goes through that array and finds: a) the minimum score, and the level at which it occurred b) the maximum score, and the level at which it occurred c)the average score for all five levels The score data you must use for this program are as follows: Game Level Score 1 450 2 316 3 148 Stack implementations is mandatory.

0 Answers  


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,


is public static void main() work in java?is "String arg[]" needed as argument?

4 Answers   Tech Mahindra,


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

1 Answers   Tech Mahindra,






Why the program getting error if we don't use String args[] in main(), even in the case of not getting any arguments from command line?

1 Answers  


How can we get the details for printing the employee details at run time using JDBC connectivity? can u provide the coding for that? Its urgent?

2 Answers  


does anyone know the code to display a triangle using a applet?

1 Answers  


I am trying to pass the string firstName from a Servlet called SampleServet. I am running this on eclipse and it tells me that "the value for annotation attribute must be a constant expression. I don't understand why it is giving me this error. @PersonAnnotation(name = SampleServlet.firstName) public class AnnotationClass{

0 Answers  


how to create a (*)pyramid using java codes???

5 Answers   Infosys,


write a c program which takes 20 numbers in an array as input from user and rearrange them in two different array defining even or odd

0 Answers  


write a java program to create a Frame with three scrolls, change the back ground color of the frame using functions with values of scrolls.

0 Answers  


Categories