1 For the following program segment give the output produced
Solution
Solution for the Problem Snip 1
2 4 6 8 10 12
2 4 6 8 10
2 4 6
2 4
How did this Output come?
So let us understand the Program execution Flow,
So the First statement we declare i=13, Now in a statement while(i>0), That is while(13>0) means this while will be kept on getting executed till the value of i is less than 13.
So inside the First, we define j=0, in second while loop these will be the condition 2<13, 4<13, 6<13, 8<13, 10<13,12<13. So for all the above statements, the inner while will be executed.
So for every execution of while we print the j followed by a space which will provide a result of 2 4 6 8 10 12. Now I will be reduced by 3 and the same sequence of j will be executed. before we reduce the value of j we make sure we have a new line.
Solution for Problem 2
+++
++++++
+++++++++
++++++++++++
Here there is a outer while where these can be the possible condition for the loop (1<5)(2<5)(3<5)(4<5)
so now we have 4 run on a outer while. Now for an inner while(j<i*3) for every print of \'+\' j is incremented by 1. And when i is incremented by 1 we tend to x3 the value of the while. ie ( j<1*3)(j<2*3)(j<3*3)(j<4*3)
so for every inner loop condition, we print +.
for every update of outer loop, a new blank space is created.
Solution of Problem 3
Output : pottatto
a and b are two different strings where we tend to use these strings for counting the number of character it has for a while loop.
we have outer loop which says while(0<6 (a.length)) so now the outer while can have all these possible values.
while ( 0<6) (1<6)(2<6)(3<6)(4<6)(5<6)
now for the inner loop, we calculate the possible case of an inner loop. For that b.length provides us an answer as 6-1 as \"tophat\" has six characters. So the value initially will be 5 for every completion of inner while 5 will be reduced by 2. so the case of inner while will be while(5,3,1>=0)
Now let us understand the sequence of the program below
is pos<a.length()
0<6
int pos2 = b.length()-1;
pos2 = 6-1 = 5
pos2 = 5 >=0
is a.charAt(pos) == b.charAt(pos2)
pos = 0, pos2 = 5 (no)
now, pos2=pos2-1
pos2= 5-1 = 4
pos = 0
pos2 = 4 >=0
is a.charAt(pos) == b.charAt(pos2) ?
pos = 0, pos2 = 4 (no)
now, pos2=pos2-1
pos2= 4-1 = 3
pos = 0
pos2 = 3 >=0
is a.charAt(pos) == b.charAt(pos2) ?
pos = 0, pos2 = 3 (no)
now, pos2=pos2-1
pos2= 3-1 = 2
pos = 0
pos2 = 2 >=0
is a.charAt(pos) = b.charAt(pos2) ?
pos = 0, pos2 = 2 (yes)
output=output+a.charAt(pos)
output=\"\"+\"p\" = \"p\"
after three more cycles,
pos2= -1
pos2<0
BREAK WHILE LOOP
while (pos<a.length()) loop
pos = pos + 1
pos = 1
pos<a.length()) when pos=1
pos2= 5, again
pos2>=0, again
is a.charAt(pos) == b.charAt(pos2) for pos=1 and pos2=5
(nope)
now, pos2=pos2-1
pos2 = 5-1 = 4
pos = 1
is a.charAt(pos) = b.charAt(pos2) for 1 and 4?
(nope)
now, pos2=pos2-1
pos2 = 4-1 = 3
pos = 1
is a.charAt(pos) = b.charAt(pos2) for 1 and 3?
(nope)
now, pos2=pos2-1
pos2 = 3-1 = 2
pos = 1
is a.charAt(pos) = b.charAt(pos2) for 1 and 2?
(nope)
pos2 = 2-1 =1
pos = 1
is a.charAt(pos) == b.charAt(pos2) for 1 and 1?
(yup)
output=\"p\" + \"o\"
[since \'t\' !== \'p\' we\'ll skip the rest of this loop, like we did above]
pos = pos + 1
pos = 1 + 1 = 2
pos<a.length() for pos = 2
pos2 = 5, again
pos2>=0
is a.charAt(pos) == b.charAt(pos2) for 2 and 5?
(yup)
output=\"p\" + \"o\" + \"t\"
pos2=5-1=4
is a.charAt(pos) == b.charAt(pos2) for 2 and 4?
(nope)
[repeat until pos2=1-1=0]
is a.charAt(pos) == b.charAt(pos2) for 2 and 0?
(yup)
output=\"pot\" + \"t\"
INVALID CASE pos2>=0
BREAK LOOP\'
pos = 2+1 = 3
3<a.length()
pos2 = 5
is a.charAt(pos) == b.charAt(pos2) for 3 and 5?
(nope)
pos2 = 5-1= 4
pos = 3
is a.charAt(pos) == b.charAt(pos2) for 3 and 4?
yes
output = \"pott\" + \"a\"
[run loop until break] // end loop
pos = 3+1 =4
pos2 = 5
is a.charAt(pos) == b.charAt(pos2) for 4 and 5?
(yep)
output = \"potta\" + \"t\"
[run loop until pos2 = 0]
is a.charAt(pos) == b.charAt(pos2) for 4 and 0?
(yea)
output = \"pottat\" + \"t\"
pos = 4+1 = 5
there will be a match at pos = 5 and pos2 = 1
output = \"pottatt\" + \"o\"
pos = 6, break loop
FINAL OUTPUT \"The final output is: pottatto\"



