exam questions

Exam 98-381 All Questions

View all questions & answers for the 98-381 exam

Exam 98-381 topic 1 question 6 discussion

Actual exam question from Microsoft's 98-381
Question #: 6
Topic #: 1
[All 98-381 Questions]

DRAG DROP -
You are building a Python program that displays all of the prime numbers from 2 to 100.
How should you complete the code? To answer, drag the appropriate code segments to the correct location. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
NOTE: Each correct selection is worth one point.
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:
References:
https://docs.python.org/3.1/tutorial/inputoutput.html
https://stackoverflow.com/questions/11619942/print-series-of-prime-numbers-in-python https://www.programiz.com/python-programming/examples/prime-number-intervals

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
devpool
Highly Voted 4 years, 1 month ago
p=2 while p<=100: is_prime=True for i in range(2, p): if p % i == 0: is_prime=False break if is_prime == True: print(p) p=p+1
upvoted 7 times
...
yasminek
Most Recent 4 years, 3 months ago
continue
upvoted 4 times
...
Dkadet159958
4 years, 6 months ago
why is_prime = True it needs to be inside while loop, can someone explain please.
upvoted 4 times
PavanKumpatlaAccountTwo
3 years, 5 months ago
The code asks for prime numbers between 2-100. So we use the while loop with "<=" so that the number is less than or equal to 100. After all, we don't want the number to be over 100 do we? And we use the while loop because it will repeatedly check until the condition is true.
upvoted 1 times
...
sadako11
4 years, 6 months ago
is_prime is what is called a flag. In our program its main purpose is to control which number to print. In our case only prime numbers when is_prime = True. if is_prime==True: print(p) When is_prime is set to False (when a non prime number is found) then the statement if is_prime==True: print(p) is not executed. is_prime must be located inside the while loop to reset its value for each number. If it is located outside the while loop it will not reset and will get stuck with the same value throughout the complete while loop. When the loop finds a non primal number (is_prime==False) is_prime will be stuck with False for the whole while loop and the code if is_prime==True: print(p) will never be executed.
upvoted 4 times
...
...
cono
4 years, 7 months ago
Would you not use 'continue' rather than 'break' so loop can be repeated?
upvoted 2 times
...
gargaditya
4 years, 8 months ago
Perfect answer,be careful with the 'break' statement which breaks the inner for loop the moment a non prime number is detected(after setting is_prime to false) Also note difference between division(/) and remainder after division(%) logic: eg a number like 6 start with 2,test divisibility by 2, 3,4,5 number like 10 start with 2,test divisibility by 2,3,4....9 If anywhere remainder(ie modulo operation->%) is zero ie fully divisble,then mark non prime and break further testing
upvoted 1 times
...
Vaibhavdhingra24
4 years, 11 months ago
Can someone explain the indentation??
upvoted 1 times
FuzzyDunlop
4 years, 11 months ago
The indentation represents the literal indentation you would use when writing the python code. The for loop is within the while loop and the break statement is within the if statement.
upvoted 2 times
...
Shanmahi
4 years ago
p = 2 while p <= 100: is_prime = True for i in range(2, p): if p % i == 0: is_prime = False break if is_prime: print(p) p = p+1
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...