Q5 1-7 Flashcards
/* 1 What output does the following program fragment produce? i = 1; while (i { printf (“%d “, i ); i *= 2; } */
1 2 4 8 16 32 64 128
/* 2 What output does each of the following two statements produce? Statement 1: for (i = 5, j = i – 1; i > 0; –i, j = i - 1) printf(“%d %d”, i, j); Statement 2: for (i = 5, j = i – 1; i > 0, j > 0; –i, j = i - 1) printf(“%d %d”, i, j); */
Statement 1: 5 44 33 22 11 0 Statement 2: 5 44 33 22 1
/* 3 Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same)? • while (i < 10) {…} • for (; i < 10;) {…} • do {…} while (i < 10) ; */
do while, because do while will always iterate at least once (checks the condition after one iteration)
/* 4 What output does the following for statement produce? for (i = 10; i >= 1; i /= 2) printf(“%d “, –i); */
9 3 0
/* 5 What output does the following for statement produce? for (i = 10; i >= 1; i %= 2) printf(“%d “, i++); */
10 1
/* 6 What output does the following for statement produce? for (i = 10; i >= 1; i *= 2) printf(“%d “, (i-=6)); */
4 2 -2
/* 7 What output does the following for statement produce? for (i = 10; i >= 1; i -= 1) printf(“%d “,(i/=2)); */
5 2 0