Week 6 COMPSCI Quiz Questions
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 print(x)
128
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: break print(x, end=' ')
2 4 8 16 32
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: break print(x, end=' ') else: print(x)
2 4 8 16 32
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 if x == 64: continue print(x, end=' ')
2 4 8 16 32 128
What output is produced by the following code fragment? x = 1 while x < 100: if x == 64: break x *= 2 print(x, end=' ')
2 4 8 16 32 64
What output is produced by the following code fragment? x = 1 while x < 100: x *= 2 print(x, end=' ') else: print(x)
2 4 8 16 32 64 128 128
What output is produced by the following code fragment? num = 1; max = 20 while num < max: num += 4 print(num)
21
What output is produced by the following code fragment? str = "abc123_def456_$5.0" i = 0 while i< len(str): if str[i].isalnum(): print( str[i].upper(), end=' ') i += 1
A B C 1 2 3 D E F 4 5 6 5 0
What output is produced by the following code fragment? x = 0 while x < 100: x *= 2 print(x)
Error. Go to infinite loop
What output is produced by the following code fragment? x = 1 while x < 100: if x == 64: continue x *= 2 print(x, end=' ')
Error. Go to infinite loop
A while statement always executes its loop body at least once
False
What output is produced by the following code fragment? str = "abcdef" while i in str: print( i, end=' ')
NameError, i is not defined
What output is produced by the following code fragment? str = "abcdef" i = 0 while i< len(str): print( str[i], end=' ') i += 1
a b c d e f
What output is produced by the following code fragment? num = 1; max = 20 while num < max: if num%2 == 0: print( num, end=' ') num += 1
print even number from 2 to 18: 2, 4, 6, 8, 10, 12, 14, 16, 18
For str = "abc123_def456_$5.0", which code can print letters only? The output should be: a b c d e f
str = "abc123_def456_$5.0" i = 0 while i< len(str) -1 : if str[i].isalpha(): print( str[i], end=' ') elif str[i].isdigit(): pass i += 1