Python: Quiz Part 2

Conditional Statement Questions

1. What will be the output of this program?


	x=44
	y=101
	z=200
	if x > 44:
		print(x)
	else:
		if y == 100:
			print(y)
		else:
			print(z)
	
  There will be no output
  44
  101
  200


2. What will be the output of this program?


	# Grade calculator program
	grade = 99

	if grade == 100:
		print("A+")
	if grade >= 90:
		print("A")
	if grade >= 80:
		print("B")
	if grade >= 70:
		print("C")
	if grade >= 60:
		print("D")
	else:
		print("E")
	
  A
  A
            B
            C
            D
  B
  There will be no output


The example above has no syntax errors, but it does have a semantic (logic) error. The output is incorrect. The program should not be written with multiple independent if statements, but one related if-elseif-else statement. The grades (A, B, etc.) are mutually exclusive (it can be only be done of them).


3. What will be the output of this program?


	# Grade calculator program
	grade = 99

	if grade == 100:
		print("A+")
	elif grade >= 90 and grade < 100:
		print("A")
	elif grade >= 80 and grade < 90:
		print("B")
	elif grade >= 70 and grade < 90:
		print("C")
	elif grade >= 60 and grade < 70:
		print("D")
	elif grade >= 0 and grade < 60:
		print("E")
	
  A+
  A
  B
  C
  D


The example above has neither syntax errors or semantic errors. It produces the correct output. However, the program is written efficiently with unnecessary code.


4. What will be the output of this program?


	# Grade calculator program
	grade = 99

	if grade == 100:
		print("A+")
	elif grade >= 90:
		print("A")
	elif grade >= 80:
		print("B")
	elif grade >= 70:
		print("C")
	elif grade >= 60:
		print("D")
	else:
		print("E")
	
  A+
  A
  B
  C
  D


The example above has neither syntax errors or semantic errors, produces the correct output, and is written more efficiently.



Repetition (Loop) Statement Questions

5. What will the first line of output be when this Python program runs?


	for x in range(5):
		print(x)
	
  0
  1
  5


6. How many lines of output will this Python program produce?


	for x in range(1,6):
		print(x)
	
  0
  1
  4
  5
  6


7. How many lines of output will this Python program produce?


	for x in range(10,3,2):
		print(x)
	
  0
  1
  4
  5
  10


8. How output will appear on the last line of output when this Python program runs?


	for x in range(10,3,-2):
		print(x)
	
  0
  2
  3
  4
  10


9. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	for x in range(0,4):
		for y in range(0,2):
			print(x)
	

 


10. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	x = 10
	while x < 10:
		print(x)
	

 


11. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	x = 5
	while x < 10
		print(x)
	

 


12. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	x = 5
	while x < 10:
		print(x)
	

 


13. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	x = 5
	while x < 25:
		print(x)
		x += 6
	
 


14. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	start = 1
	end = 10
	step = 3
	for x in range(start,end,step):
		if x % 2 == 0:
			print("a")
		else:
			print("b")
	

 


15. What will be the output when this Python program runs?


	start = 1
	end = 10
	step = 3
	total = 0
	while end > start:
		total += step
		start += step
	print(total)
	
  0
  6
  9
  12
  15


16. Write down on a piece of paper what you believe the output of this program would be and then check your answer below.


	x = 1
	while x < 5:
		print(x)