You are on page 1of 3

Question 1

m=9 n=6

m=9+1=10

N=n-1(assume it was typo for uppercase N)

Hence, it became n=n-1

n=6-1=5

m=10+5=15

if (15>5), print m

m=15

Answer=15

Or answer=16 if we did not use assumption for uppercase N.

Question 2

a=56 b=876

initial t=0

While loop is inside the function, the loop will keep going on if condition is true.

For this case, the loop will stop if b=0.

First iteration,

t = 0+56

= 56

b=876-1

= 875

Second iteration,

t = 56+56

= 112

b = 875-1

= 874

The iterations will keep going until b = 0. There are 876 times with 56 which equal 49056. While loop will
end and return the value of t which is 49056. Answer = b
Question 3

Inside the for loop, initial value n = 3, the loop will stop if n = 0. Every execution of code block the n will
decrease by 1.

First loop,

It will print n which is 3. Then, n will decrease by 1 become 2. After that, it goes through n = n -1, which
will return the values of n=1. End first loop.

Second loop,

Initial n = 1, it will print n and decrease by 1 become 0. After that, it goes through n= n-1 and return the
values of n= -1. End second loop.

It will never end the for loop, because n value will never become 0, and it will keep decreasing.

Answer = infinite loop

Question 4,

By using ASCII values,

‘b’ = 98

‘a’ = 97

‘z’ = 122

Inside the while loop, we have (ch >= ‘a’ && ch <= ‘z’)

For first loop,

It means that (98 >= 97 && 98 <= 122) and it returns true for both conditions. Because of ch++, ASCII
values 98 increased by 1 to 99 which is ‘c’ after each execution.

The loop will keep going on from ‘b’ to ’z’.

Total 25 loops will be executed.

Answer = b
Question 5

f=6 g=9

initial sum = 0

if (9>6) return true, so it will execute for loop.

First loop,

for (n = 6; n<9; n=n+1)

sum = 0 + 6 = 6

After execution, n becomes 7.

Second loop,

for (n = 7; n<9; n=n+1)

sum = 6 + 7 = 13

After execution, n becomes 8.

Third loop,

for (n = 8; n<9; n=n+1)

sum = 13 + 8 = 21

After execution, n becomes 9. Loop will end because n cannot larger than 9.

It will print sum as 21.

Answer = a

You might also like