(a) A and B both the array contain elements from 1 to 4 . For each element of A the script prints foo 4 times. Since there are 4 elements in array A. Thus the number of foo output is 4X4=16
(b) When $i is 1, bar will be printed 3 times when $j is 2,3,4
When $i is 2, bar will be printed 2 times when $j is 3,4
When $i is 3, bar will be printed 1 times when $j is 4
When $i is 4, bar will be printed 0 times
Thus number of times of bar output is (3+2+1) = 6
(c) Following the same logic as (b)
When $i is k, bar will be printed (n - k) times where k can take value in 1,2,...,n
Thus total number of "bar" output is
(n-1 ) + (n-2) + (n - 3) + ... + 1
= (n-1)n/2 [sum of first (n-1) natural numbers]
=