Modular Arithmetic in Python
- 38Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does the modulo operation return when one number is divided by another?
What does the modulo operation return when one number is divided by another?
38 questions
- 1
What does the modulo operation return when one number is divided by another?
- A
The sum of the dividend and divisor
- B
The number of times the divisor fits into the dividend
- C
The remainder left after division
- D
The decimal portion of the quotient only
Correct answer
CThe remainder left after division
Explanation
Modulo returns the remainder left after dividing the dividend by the divisor.
- A
- 2
In the expression amodb, what role does a play?
- A
It is the quotient, the number of complete groups
- B
It is the dividend, the number being divided
- C
It is the modulus, which sets the output range
- D
It is the remainder left after division
Correct answer
BIt is the dividend, the number being divided
Explanation
The content identifies a as the dividend, or the number being divided.
- A
- 3
In the expression amodb, what does b represent?
- A
The quotient and dividend
- B
The remainder and quotient
- C
The divisor and modulus
- D
The dividend and remainder
Correct answer
CThe divisor and modulus
Explanation
The content refers to b as both the divisor and the modulus; it sets the range of possible remainders.
- A
- 4
How are the quotient and remainder related in a division operation?
- A
The quotient and remainder are both counts of complete groups
- B
The quotient is the divisor multiplied by the remainder
- C
The quotient is what is left, and the remainder counts complete groups of the divisor
- D
The quotient counts complete groups of the divisor, and the remainder is what is left
Correct answer
DThe quotient counts complete groups of the divisor, and the remainder is what is left
Explanation
The quotient tells how many times the divisor fits into the dividend; the remainder is the leftover amount.
- A
- 5
What are the quotient and remainder when 6 is divided by 2?
- A
Quotient 3, remainder 1
- B
Quotient 3, remainder 0
- C
Quotient 4, remainder 0
- D
Quotient 2, remainder 3
Correct answer
BQuotient 3, remainder 0
Explanation
Since 2 fits into 6 three times exactly, the quotient is 3 and the remainder is 0.
- A
- 6
What are the quotient and remainder when 5 is divided by 2?
- A
Quotient 2, remainder 0
- B
Quotient 3, remainder 1
- C
Quotient 1, remainder 3
- D
Quotient 2, remainder 1
Correct answer
DQuotient 2, remainder 1
Explanation
The divisor 2 fits into 5 twice, accounting for 4, with 1 left over.
- A
- 7
Why is 6mod2 equal to zero?
- A
Because the divisor is smaller than the dividend
- B
Because 6 is the quotient and 2 is the remainder
- C
Because modulo always returns zero for even dividends
- D
Because 2 divides 6 evenly, leaving nothing over
Correct answer
DBecause 2 divides 6 evenly, leaving nothing over
Explanation
The modulo result is the remainder, and dividing 6 by 2 leaves no remainder.
- A
- 8
Why is 5mod2 equal to one?
- A
Because the quotient is 1 and modulo returns the quotient
- B
Because 2 fits into 5 exactly twice with no remainder
- C
Because two groups of 2 use 4 of the 5, leaving 1
- D
Because subtracting 2 from 5 once leaves the modulo result
Correct answer
CBecause two groups of 2 use 4 of the 5, leaving 1
Explanation
Two complete groups of 2 account for 4, leaving a remainder of 1.
- A
- 9
Which Python operator is used for a modulo operation in the examples?
- A
The double slash,
// - B
The slash sign,
/ - C
The exponent sign,
** - D
The percent sign,
%
Correct answer
DThe percent sign,
%Explanation
Python's built-in modulo operator is written with the percent sign,
%. - A
- 10
According to the content, Python's
%modulo operator works with which kinds of values?- A
Dates and time zones only
- B
Strings and lists only
- C
Integers only
- D
Integers and floating-point numbers
Correct answer
DIntegers and floating-point numbers
Explanation
The content states that Python's built-in modulo expression works on integers and floating-point numbers, although the examples focus on integers.
- A
- 11
Why does the tutorial focus on integer modulo examples?
- A
Because Python does not allow modulo with floating-point numbers
- B
For simplicity, even though the operator also works with floating-point numbers
- C
Because floating-point modulo always returns the quotient
- D
Because integer modulo requires an external package
Correct answer
BFor simplicity, even though the operator also works with floating-point numbers
Explanation
The stated reason is simplicity; the
%operator can also be used with floating-point numbers. - A
- 12
Which alternative is mentioned for performing modulo operations in Python?
- A
A function called
floorfrom a time-zone list - B
A function called
fmodfrom a package - C
A method called
remainderfrompytz - D
A function called
quotientfromdatetime
Correct answer
BA function called
fmodfrom a packageExplanation
The content mentions
fmodas another function that can be used for modulo operations, without detailing its differences from%. - A
- 13
What does floor division provide that is useful when constructing a modulo function?
- A
The modulus value used to set the output range
- B
The whole-number quotient showing how many times the divisor fits
- C
The decimal part of the quotient
- D
The remainder left after the division
Correct answer
BThe whole-number quotient showing how many times the divisor fits
Explanation
Floor division returns the quotient as a whole number rather than returning the remainder.
- A
- 14
Which expression correctly constructs a remainder from a dividend a and a positive modulus m using floor division?
- A
m−(a//m)×a
- B
(a//m)−a×m
- C
a+(a//m)×m
- D
a−(a//m)×m
Correct answer
Da−(a//m)×m
Explanation
Floor division gives the whole-number quotient a//m. Multiplying it by m gives the amount accounted for by complete groups; subtracting that from a leaves the remainder.
- A
- 15
In the scratch-built modulo function, what is the purpose of multiplying the floor-division result by the modulus before subtracting it from the dividend?
- A
It changes the divisor into the dividend
- B
It converts the quotient into a floating-point number
- C
It gives the portion of the dividend accounted for by complete groups of the modulus
- D
It ensures the remainder is always equal to the modulus
Correct answer
CIt gives the portion of the dividend accounted for by complete groups of the modulus
Explanation
The quotient multiplied by the modulus gives the total amount covered by complete groups; subtracting this amount from the dividend yields the leftover remainder.
- A
- 16
How does the content verify the scratch-built modulo function across a range of values?
- A
It compares the function only with the
fmodfunction for floating-point values - B
It compares the custom function's results with Python's modulo operator in nested loops
- C
It tests a single clock time in each available time zone
- D
It checks whether each result is larger than the dividend
Correct answer
BIt compares the custom function's results with Python's modulo operator in nested loops
Explanation
The example uses nested loops over integer values and checks whether the custom function agrees with Python's built-in modulo result.
- A
- 17
What does
range(-10, 10)include in the described Python test?- A
Integers from −10 through 9
- B
Integers from −9 through 10
- C
Only the integers −10 and 10
- D
Integers from −10 through 10
Correct answer
AIntegers from −10 through 9
Explanation
In Python, the stop value is excluded, so
range(-10, 10)includes −10 up to but not including 10. - A
- 18
If a comparison between the custom modulo function and Python's
%operator producesFalse, what would that indicate in the described test?- A
There may be an issue with the custom function
- B
The tested value is necessarily a floating-point number
- C
The two operations have been verified to match
- D
The clock has crossed midnight
Correct answer
AThere may be an issue with the custom function
Explanation
The content explains that a
Falsecomparison would indicate a possible issue because the custom and built-in results should match in the tested cases. - A
- 19
What is the result of 9mod9?
- A
0
- B
8
- C
9
- D
1
Correct answer
A0
Explanation
Nine fits into nine once exactly, leaving a remainder of zero.
- A
- 20
What is the result of 9mod8?
- A
9
- B
8
- C
0
- D
1
Correct answer
D1
Explanation
Eight fits into nine once, leaving a remainder of one.
- A
- 21
What is the clock function designed to report?
- A
A date and time in seconds, always in UTC
- B
A decimal quotient representing the current minute
- C
A military-time hour and minute, using the current time or an adjusted hour
- D
A time-zone offset without the local hour
Correct answer
CA military-time hour and minute, using the current time or an adjusted hour
Explanation
The function is designed to display the current time or a time adjusted by a number of hours, formatted as military time and including minutes.
- A
- 22
Why does the clock calculation use modulo 24 when adjusting the hour?
- A
To make the clock display hours from 1 through 24
- B
To wrap hour values around after 23 so the time cycles back to 0
- C
To convert minutes into groups of 24
- D
To calculate the time-zone offset in minutes
Correct answer
BTo wrap hour values around after 23 so the time cycles back to 0
Explanation
A 24-hour clock wraps after hour 23; modulo 24 maps an hour adjustment that passes the end of the day back into the range beginning at 0.
- A
- 23
If the current hour is 23 and the clock function adds one hour, what hour should the modulo-24 calculation produce?
- A
24
- B
1
- C
0
- D
23
Correct answer
C0
Explanation
The adjusted value is 23+1=24, and 24mod24=0, so the hour wraps to 0.
- A
- 24
If the clock function adds five hours to hour 21, what hour results after wrapping modulo 24?
- A
2
- B
26
- C
21
- D
5
Correct answer
A2
Explanation
The adjusted hour is 21+5=26. Taking 26mod24 gives 2.
- A
- 25
How can the clock function be used to estimate the hour one hour earlier?
- A
Subtract 24 from the current minute
- B
Pass an hour adjustment of −1
- C
Pass a modulus of −1
- D
Set the hour adjustment to 1 and reverse the time zone
Correct answer
BPass an hour adjustment of −1
Explanation
The function accepts an hour adjustment, and a negative adjustment such as −1 moves the displayed hour backward by one.
- A
- 26
What does leaving the clock function's hour adjustment at its default value of zero do?
- A
It sets the hour to midnight regardless of the current time
- B
It subtracts one hour from the current time
- C
It displays the current hour without shifting it forward or backward
- D
It changes the displayed time to UTC
Correct answer
CIt displays the current hour without shifting it forward or backward
Explanation
With an adjustment of zero, no hours are added or subtracted, so the function displays the current time in the selected time zone.
- A
- 27
How are the minutes handled when the clock function adjusts the hour?
- A
They are converted into the hour using modulo 24
- B
They are reset to zero whenever the hour changes
- C
They are increased by the same number as the hour adjustment
- D
They are displayed from the current time and are not incremented by the hour adjustment
Correct answer
DThey are displayed from the current time and are not incremented by the hour adjustment
Explanation
The function adjusts the hour but obtains and displays the current minutes separately; it does not increment the minutes along with the hour adjustment.
- A
- 28
What is the stated default time zone for the clock function?
- A
Addis Ababa
- B
The user's local time zone, detected automatically
- C
Eastern Standard Time
- D
Universal Time
Correct answer
CEastern Standard Time
Explanation
The example sets Eastern Standard Time as the default time zone, while allowing another time zone to be specified.
- A
- 29
Why does the clock function request a time zone when obtaining the current date and time?
- A
To make the function return a date instead of a time
- B
To obtain the current time for the selected zone rather than defaulting to universal time
- C
To determine the divisor used in the modulo calculation
- D
To convert the minute into a two-digit hour
Correct answer
BTo obtain the current time for the selected zone rather than defaulting to universal time
Explanation
The content explains that a specific time zone is supplied so the function gets the time for that zone rather than returning universal time by default.
- A
- 30
How does the clock output preserve a leading zero for a one-digit hour or minute?
- A
It changes the modulo from 24 to 60
- B
It rounds the time down using floor division
- C
It adds 10 to every one-digit value before printing
- D
It converts the value to a string and prefixes
0when the string has one character
Correct answer
DIt converts the value to a string and prefixes
0when the string has one characterExplanation
The formatting logic checks whether the string representation has length one and, if so, concatenates a leading zero.
- A
- 31
What is the effect of the clock function's output formatting for an hour such as 3 and a minute such as 7?
- A
It displays them as
3.7 - B
It omits the hour and minute because they are single digits
- C
It displays them with leading zeros, as
03and07 - D
It changes them to
30and70
Correct answer
CIt displays them with leading zeros, as
03and07Explanation
The function adds a leading zero to one-digit hour and minute strings so that each component is displayed with two digits.
- A
- 32
According to the content, does the example clock function return a time value for further processing?
- A
Yes; it returns the hour as a floating-point number
- B
No; it returns the time-zone name instead
- C
No; it prints the formatted time rather than returning a value
- D
Yes; it returns a date-time object in all cases
Correct answer
CNo; it prints the formatted time rather than returning a value
Explanation
The content explicitly notes that the example prints the formatted result and does not return data for further use.
- A
- 33
Which Python packages are named for working with the clock and time zones?
- A
datetimeandpytz - B
cryptographyanddatetime - C
fmodandmath - D
isbnandupc
Correct answer
AdatetimeandpytzExplanation
The clock example uses
datetimeto obtain the current date and time andpytzto specify a time zone. - A
- 34
What kind of cryptographic task is Diffie–Hellman described as supporting?
- A
Securely exchanging cryptographic keys over a public channel
- B
Formatting a time as a four-digit military-time string
- C
Checking whether a grocery barcode has the correct digits
- D
Assigning a unique value to every printed page
Correct answer
ASecurely exchanging cryptographic keys over a public channel
Explanation
The content describes Diffie–Hellman as allowing two parties to securely exchange cryptographic keys over a public channel and notes that it uses modular arithmetic.
- A
- 35
How is modular arithmetic used in the UPC example?
- A
It converts the barcode into a time-zone offset
- B
A checksum uses it to help verify that a UPC is valid
- C
It generates the quotient that identifies the store's inventory count
- D
It determines how many digits are printed on a cereal box
Correct answer
BA checksum uses it to help verify that a UPC is valid
Explanation
The content says UPCs have ten digits and use a checksum involving modular arithmetic to verify validity.
- A
- 36
What role does the UPC checksum serve in the described application?
- A
It helps verify that the UPC is valid
- B
It encrypts the UPC for a public key exchange
- C
It determines the item's expiration date
- D
It converts the UPC into an ISBN
Correct answer
AIt helps verify that the UPC is valid
Explanation
The stated purpose of the UPC checksum is to verify whether the UPC is valid.
- A
- 37
How does the content characterize the use of modular arithmetic in ISBNs?
- A
It is used to wrap book publication times through a 24-hour cycle
- B
It is used to calculate the number of pages in each book
- C
It is used to exchange encryption keys between book sellers
- D
It is used in forming unique values that help verify books
Correct answer
DIt is used in forming unique values that help verify books
Explanation
The content states that ISBNs are used to verify books and that unique values are made using modular arithmetic.
- A
- 38
Which pair of examples illustrates modular arithmetic in both cryptography and product identification?
- A
Diffie–Hellman key exchange and UPC checksums
- B
Clock formatting and time-zone selection
- C
Floor division and
datetimeextraction - D
ISBN values and military-time minutes
Correct answer
ADiffie–Hellman key exchange and UPC checksums
Explanation
Diffie–Hellman is presented as a cryptographic use, while UPC checksums are presented as a product-identification validation use.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.