1. Home
  2. Explore
  3. Computer Science
  4. Modular Arithmetic in Python

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Modular Arithmetic in Python

  • 38Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Modulo Fundamentals

What does the modulo operation return when one number is divided by another?

38 questions

  1. 1

    What does the modulo operation return when one number is divided by another?

    1. A

      The sum of the dividend and divisor

    2. B

      The number of times the divisor fits into the dividend

    3. C

      The remainder left after division

    4. D

      The decimal portion of the quotient only

    Correct answer
    C

    The remainder left after division

    Explanation

    Modulo returns the remainder left after dividing the dividend by the divisor.

  2. 2

    In the expression a mod ba \bmod bamodb, what role does aaa play?

    1. A

      It is the quotient, the number of complete groups

    2. B

      It is the dividend, the number being divided

    3. C

      It is the modulus, which sets the output range

    4. D

      It is the remainder left after division

    Correct answer
    B

    It is the dividend, the number being divided

    Explanation

    The content identifies aaa as the dividend, or the number being divided.

  3. 3

    In the expression a mod ba \bmod bamodb, what does bbb represent?

    1. A

      The quotient and dividend

    2. B

      The remainder and quotient

    3. C

      The divisor and modulus

    4. D

      The dividend and remainder

    Correct answer
    C

    The divisor and modulus

    Explanation

    The content refers to bbb as both the divisor and the modulus; it sets the range of possible remainders.

  4. 4

    How are the quotient and remainder related in a division operation?

    1. A

      The quotient and remainder are both counts of complete groups

    2. B

      The quotient is the divisor multiplied by the remainder

    3. C

      The quotient is what is left, and the remainder counts complete groups of the divisor

    4. D

      The quotient counts complete groups of the divisor, and the remainder is what is left

    Correct answer
    D

    The 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.

  5. 5

    What are the quotient and remainder when 666 is divided by 222?

    1. A

      Quotient 333, remainder 111

    2. B

      Quotient 333, remainder 000

    3. C

      Quotient 444, remainder 000

    4. D

      Quotient 222, remainder 333

    Correct answer
    B

    Quotient 333, remainder 000

    Explanation

    Since 222 fits into 666 three times exactly, the quotient is 333 and the remainder is 000.

  6. 6

    What are the quotient and remainder when 555 is divided by 222?

    1. A

      Quotient 222, remainder 000

    2. B

      Quotient 333, remainder 111

    3. C

      Quotient 111, remainder 333

    4. D

      Quotient 222, remainder 111

    Correct answer
    D

    Quotient 222, remainder 111

    Explanation

    The divisor 222 fits into 555 twice, accounting for 444, with 111 left over.

  7. 7

    Why is 6 mod 26 \bmod 26mod2 equal to zero?

    1. A

      Because the divisor is smaller than the dividend

    2. B

      Because 666 is the quotient and 222 is the remainder

    3. C

      Because modulo always returns zero for even dividends

    4. D

      Because 222 divides 666 evenly, leaving nothing over

    Correct answer
    D

    Because 222 divides 666 evenly, leaving nothing over

    Explanation

    The modulo result is the remainder, and dividing 666 by 222 leaves no remainder.

  8. 8

    Why is 5 mod 25 \bmod 25mod2 equal to one?

    1. A

      Because the quotient is 111 and modulo returns the quotient

    2. B

      Because 222 fits into 555 exactly twice with no remainder

    3. C

      Because two groups of 222 use 444 of the 555, leaving 111

    4. D

      Because subtracting 222 from 555 once leaves the modulo result

    Correct answer
    C

    Because two groups of 222 use 444 of the 555, leaving 111

    Explanation

    Two complete groups of 222 account for 444, leaving a remainder of 111.

  9. 9

    Which Python operator is used for a modulo operation in the examples?

    1. A

      The double slash, //

    2. B

      The slash sign, /

    3. C

      The exponent sign, **

    4. D

      The percent sign, %

    Correct answer
    D

    The percent sign, %

    Explanation

    Python's built-in modulo operator is written with the percent sign, %.

  10. 10

    According to the content, Python's % modulo operator works with which kinds of values?

    1. A

      Dates and time zones only

    2. B

      Strings and lists only

    3. C

      Integers only

    4. D

      Integers and floating-point numbers

    Correct answer
    D

    Integers 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.

  11. 11

    Why does the tutorial focus on integer modulo examples?

    1. A

      Because Python does not allow modulo with floating-point numbers

    2. B

      For simplicity, even though the operator also works with floating-point numbers

    3. C

      Because floating-point modulo always returns the quotient

    4. D

      Because integer modulo requires an external package

    Correct answer
    B

    For 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.

  12. 12

    Which alternative is mentioned for performing modulo operations in Python?

    1. A

      A function called floor from a time-zone list

    2. B

      A function called fmod from a package

    3. C

      A method called remainder from pytz

    4. D

      A function called quotient from datetime

    Correct answer
    B

    A function called fmod from a package

    Explanation

    The content mentions fmod as another function that can be used for modulo operations, without detailing its differences from %.

  13. 13

    What does floor division provide that is useful when constructing a modulo function?

    1. A

      The modulus value used to set the output range

    2. B

      The whole-number quotient showing how many times the divisor fits

    3. C

      The decimal part of the quotient

    4. D

      The remainder left after the division

    Correct answer
    B

    The 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.

  14. 14

    Which expression correctly constructs a remainder from a dividend aaa and a positive modulus mmm using floor division?

    1. A

      m−(a//m)×am - (a // m) \times am−(a//m)×a

    2. B

      (a//m)−a×m(a // m) - a \times m(a//m)−a×m

    3. C

      a+(a//m)×ma + (a // m) \times ma+(a//m)×m

    4. D

      a−(a//m)×ma - (a // m) \times ma−(a//m)×m

    Correct answer
    D

    a−(a//m)×ma - (a // m) \times ma−(a//m)×m

    Explanation

    Floor division gives the whole-number quotient a//ma // ma//m. Multiplying it by mmm gives the amount accounted for by complete groups; subtracting that from aaa leaves the remainder.

  15. 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?

    1. A

      It changes the divisor into the dividend

    2. B

      It converts the quotient into a floating-point number

    3. C

      It gives the portion of the dividend accounted for by complete groups of the modulus

    4. D

      It ensures the remainder is always equal to the modulus

    Correct answer
    C

    It 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.

  16. 16

    How does the content verify the scratch-built modulo function across a range of values?

    1. A

      It compares the function only with the fmod function for floating-point values

    2. B

      It compares the custom function's results with Python's modulo operator in nested loops

    3. C

      It tests a single clock time in each available time zone

    4. D

      It checks whether each result is larger than the dividend

    Correct answer
    B

    It 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.

  17. 17

    What does range(-10, 10) include in the described Python test?

    1. A

      Integers from −10-10−10 through 999

    2. B

      Integers from −9-9−9 through 101010

    3. C

      Only the integers −10-10−10 and 101010

    4. D

      Integers from −10-10−10 through 101010

    Correct answer
    A

    Integers from −10-10−10 through 999

    Explanation

    In Python, the stop value is excluded, so range(-10, 10) includes −10-10−10 up to but not including 101010.

  18. 18

    If a comparison between the custom modulo function and Python's % operator produces False, what would that indicate in the described test?

    1. A

      There may be an issue with the custom function

    2. B

      The tested value is necessarily a floating-point number

    3. C

      The two operations have been verified to match

    4. D

      The clock has crossed midnight

    Correct answer
    A

    There may be an issue with the custom function

    Explanation

    The content explains that a False comparison would indicate a possible issue because the custom and built-in results should match in the tested cases.

  19. 19

    What is the result of 9 mod 99 \bmod 99mod9?

    1. A

      000

    2. B

      888

    3. C

      999

    4. D

      111

    Correct answer
    A

    000

    Explanation

    Nine fits into nine once exactly, leaving a remainder of zero.

  20. 20

    What is the result of 9 mod 89 \bmod 89mod8?

    1. A

      999

    2. B

      888

    3. C

      000

    4. D

      111

    Correct answer
    D

    111

    Explanation

    Eight fits into nine once, leaving a remainder of one.

  21. 21

    What is the clock function designed to report?

    1. A

      A date and time in seconds, always in UTC

    2. B

      A decimal quotient representing the current minute

    3. C

      A military-time hour and minute, using the current time or an adjusted hour

    4. D

      A time-zone offset without the local hour

    Correct answer
    C

    A 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.

  22. 22

    Why does the clock calculation use modulo 242424 when adjusting the hour?

    1. A

      To make the clock display hours from 111 through 242424

    2. B

      To wrap hour values around after 232323 so the time cycles back to 000

    3. C

      To convert minutes into groups of 242424

    4. D

      To calculate the time-zone offset in minutes

    Correct answer
    B

    To wrap hour values around after 232323 so the time cycles back to 000

    Explanation

    A 24-hour clock wraps after hour 232323; modulo 242424 maps an hour adjustment that passes the end of the day back into the range beginning at 000.

  23. 23

    If the current hour is 232323 and the clock function adds one hour, what hour should the modulo-242424 calculation produce?

    1. A

      242424

    2. B

      111

    3. C

      000

    4. D

      232323

    Correct answer
    C

    000

    Explanation

    The adjusted value is 23+1=2423 + 1 = 2423+1=24, and 24 mod 24=024 \bmod 24 = 024mod24=0, so the hour wraps to 000.

  24. 24

    If the clock function adds five hours to hour 212121, what hour results after wrapping modulo 242424?

    1. A

      222

    2. B

      262626

    3. C

      212121

    4. D

      555

    Correct answer
    A

    222

    Explanation

    The adjusted hour is 21+5=2621 + 5 = 2621+5=26. Taking 26 mod 2426 \bmod 2426mod24 gives 222.

  25. 25

    How can the clock function be used to estimate the hour one hour earlier?

    1. A

      Subtract 242424 from the current minute

    2. B

      Pass an hour adjustment of −1-1−1

    3. C

      Pass a modulus of −1-1−1

    4. D

      Set the hour adjustment to 111 and reverse the time zone

    Correct answer
    B

    Pass an hour adjustment of −1-1−1

    Explanation

    The function accepts an hour adjustment, and a negative adjustment such as −1-1−1 moves the displayed hour backward by one.

  26. 26

    What does leaving the clock function's hour adjustment at its default value of zero do?

    1. A

      It sets the hour to midnight regardless of the current time

    2. B

      It subtracts one hour from the current time

    3. C

      It displays the current hour without shifting it forward or backward

    4. D

      It changes the displayed time to UTC

    Correct answer
    C

    It 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.

  27. 27

    How are the minutes handled when the clock function adjusts the hour?

    1. A

      They are converted into the hour using modulo 242424

    2. B

      They are reset to zero whenever the hour changes

    3. C

      They are increased by the same number as the hour adjustment

    4. D

      They are displayed from the current time and are not incremented by the hour adjustment

    Correct answer
    D

    They 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.

  28. 28

    What is the stated default time zone for the clock function?

    1. A

      Addis Ababa

    2. B

      The user's local time zone, detected automatically

    3. C

      Eastern Standard Time

    4. D

      Universal Time

    Correct answer
    C

    Eastern Standard Time

    Explanation

    The example sets Eastern Standard Time as the default time zone, while allowing another time zone to be specified.

  29. 29

    Why does the clock function request a time zone when obtaining the current date and time?

    1. A

      To make the function return a date instead of a time

    2. B

      To obtain the current time for the selected zone rather than defaulting to universal time

    3. C

      To determine the divisor used in the modulo calculation

    4. D

      To convert the minute into a two-digit hour

    Correct answer
    B

    To 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.

  30. 30

    How does the clock output preserve a leading zero for a one-digit hour or minute?

    1. A

      It changes the modulo from 242424 to 606060

    2. B

      It rounds the time down using floor division

    3. C

      It adds 101010 to every one-digit value before printing

    4. D

      It converts the value to a string and prefixes 0 when the string has one character

    Correct answer
    D

    It converts the value to a string and prefixes 0 when the string has one character

    Explanation

    The formatting logic checks whether the string representation has length one and, if so, concatenates a leading zero.

  31. 31

    What is the effect of the clock function's output formatting for an hour such as 333 and a minute such as 777?

    1. A

      It displays them as 3.7

    2. B

      It omits the hour and minute because they are single digits

    3. C

      It displays them with leading zeros, as 03 and 07

    4. D

      It changes them to 30 and 70

    Correct answer
    C

    It displays them with leading zeros, as 03 and 07

    Explanation

    The function adds a leading zero to one-digit hour and minute strings so that each component is displayed with two digits.

  32. 32

    According to the content, does the example clock function return a time value for further processing?

    1. A

      Yes; it returns the hour as a floating-point number

    2. B

      No; it returns the time-zone name instead

    3. C

      No; it prints the formatted time rather than returning a value

    4. D

      Yes; it returns a date-time object in all cases

    Correct answer
    C

    No; 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.

  33. 33

    Which Python packages are named for working with the clock and time zones?

    1. A

      datetime and pytz

    2. B

      cryptography and datetime

    3. C

      fmod and math

    4. D

      isbn and upc

    Correct answer
    A

    datetime and pytz

    Explanation

    The clock example uses datetime to obtain the current date and time and pytz to specify a time zone.

  34. 34

    What kind of cryptographic task is Diffie–Hellman described as supporting?

    1. A

      Securely exchanging cryptographic keys over a public channel

    2. B

      Formatting a time as a four-digit military-time string

    3. C

      Checking whether a grocery barcode has the correct digits

    4. D

      Assigning a unique value to every printed page

    Correct answer
    A

    Securely 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.

  35. 35

    How is modular arithmetic used in the UPC example?

    1. A

      It converts the barcode into a time-zone offset

    2. B

      A checksum uses it to help verify that a UPC is valid

    3. C

      It generates the quotient that identifies the store's inventory count

    4. D

      It determines how many digits are printed on a cereal box

    Correct answer
    B

    A 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.

  36. 36

    What role does the UPC checksum serve in the described application?

    1. A

      It helps verify that the UPC is valid

    2. B

      It encrypts the UPC for a public key exchange

    3. C

      It determines the item's expiration date

    4. D

      It converts the UPC into an ISBN

    Correct answer
    A

    It helps verify that the UPC is valid

    Explanation

    The stated purpose of the UPC checksum is to verify whether the UPC is valid.

  37. 37

    How does the content characterize the use of modular arithmetic in ISBNs?

    1. A

      It is used to wrap book publication times through a 24-hour cycle

    2. B

      It is used to calculate the number of pages in each book

    3. C

      It is used to exchange encryption keys between book sellers

    4. D

      It is used in forming unique values that help verify books

    Correct answer
    D

    It 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.

  38. 38

    Which pair of examples illustrates modular arithmetic in both cryptography and product identification?

    1. A

      Diffie–Hellman key exchange and UPC checksums

    2. B

      Clock formatting and time-zone selection

    3. C

      Floor division and datetime extraction

    4. D

      ISBN values and military-time minutes

    Correct answer
    A

    Diffie–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.

Question 1 of 38
Question 1 of 38

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent