Quiz for "Learning Python for PL/SQL Developers: Part 2"
by Arup Nanda
Questions
1. What will be the output of the following Python code:
n1=25
if (int(n1)%5):
print ('n1 is a multiple of 5')
else:
print ('n1 is not a multiple of 5')
2. What will be output of the following code:
n1=25
if (n1%5 == 0):
print ('I am at the right place')
print ('I am a multiple of 5')
else:
print ('I am at the wrong place')
print ('I am not a multiple of 5')
print ('End of program')
3. You want to display all even numbers between 30 and 20, in reverse order, for example, 30, 28, 26, and so on down to 20. You wrote the following code:
for n in (30,20,-2):
print n
Will the output match what you intended?
4. You want to print all even numbers between 20 and 30 (both numbers inclusive). You write the following:
for n in range(20,30, 2):
print n
Will it produce the desired outcome?
5. You want to display all numbers between 5 and 10, so you wrote the following program:
n1 = 5
while (n1<11):
print ('n1 = ',n1)
n1 = n1 + 1
Your logic is that you increment n1
by 1 and the while
loop will continue until (but not including) 11. Will the program produce the following output?
5
6
7
8
9
10
Answers
1. It will be n1 is not a multiple of 5
. Why? n1%5
will be 0, which means it's a multiple of 5. However, any zero value in a boolean comparison will equate to false; so the else
block will be executed.
2. It will be this:
I am at the right place
I am a multiple of 5
I am not a multiple of 5
End of program
Why was I am not a multiple of 5
printed even if n1
is 25, which is a multiple of 25? The line print ('I am not a multiple of 5')
is not indented. Therefore, it will not be executed as a part of the else
block. Only the line print ('I am at the wrong place')
is indented; so it will be executed as a part of the else
block.
3. No. The output will be this:
30
10
-2
The reason is simple. You didn't include the range
function. The expression (30,20,-2)
simply means these three values and i
will iterate between these three. You should have written range(30,20,-2)
.
4. It will produce the following output:
20
22
24
26
28
Note that 30 is not in the list. When you use the range
function, the second number (but not including it) is the higher bound This is very different from PL/SQL, where both bounds are included. If you want to include 30, use 31 as the upper bound.
5. It will not. The program will continue in an infinite loop with the value of n1
stuck at 5. Why? It's because the statement n1 = n1 + 1
is not indented at the while
loop level; so it will be executed after the completion of the while
loop. Since the n1
value will not be incremented, the while
loop condition (n1<11
) will never occur and, hence, the loop will continue infinitely.
Back to the Part 2 article.
About the Author
Arup Nanda (arup@proligence.com has been an Oracle DBA since 1993, handling all aspects of database administration, from performance tuning to security and disaster recovery. He was Oracle Magazine's DBA of the Year in 2003 and received an Oracle Excellence Award for Technologist of the Year in 2012.