8. More on Strings and Special Methods - 23 Questions


8. More on Strings and Special Methods

8. More on Strings and Special Methods

23 Questions
Created by Y. Daniel Liang - http://www.cs.armstrong.edu/liang/index.html
Free
Strings are among the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.
  1. What is len("Good")?



    1
    2
    3
    4

    View Answer | Discuss in forum
  2. What is max("Programming is fun")?



    P
    r
    a blank space character
    u

    View Answer | Discuss in forum
  3. What is min("Programming is fun")?



    P
    r
    a blank space character
    u

    View Answer | Discuss in forum
  4. What is "Programming is fun"[4: 6]?



    ram
    ra
    r
    pr

    View Answer | Discuss in forum
  5. What is "Programming is fun"[-1]?



    Pr
    P
    fun
    n

    View Answer | Discuss in forum
  6. What is "Programming is fun"[1:1]?



    P
    r
    Pr
    ''

    View Answer | Discuss in forum
  7. What is "Programming is fun"[-3:-1]?



    un
    Pr
    P
    fun

    View Answer | Discuss in forum
  8. What is "Programming is fun"[:-1]?



    Programming
    Programming is fu
    Programming is f
    Programming is

    View Answer | Discuss in forum
  9. What is "Programming is fun"[:2]?



    Pr
    P
    Pro
    Programming

    View Answer | Discuss in forum
  10. Given a string s = "Welcome", which of the following code is incorrect?

    hint: a str object is immutable!



    print(s[0])
    print(s.lower())
    s[1] = 'r'
    print(s.strip())

    View Answer | Discuss in forum
  11. What will be displayed by the following code?

    class Count:
        def __init__(self, count = 0):
           self.__count = count

    c1 = Count(2)
    c2 = Count(2)
    print(id(c1) == id(c2), end = " ")

    s1 = "Good"
    s2 = "Good"
    print(id(s1) == id(s2))

     

    hint: Two objects with the same contents in the Python library actually share the same object. This behavior is not true for custom-defined immutable classes.



    True False
    True True
    False True
    False False

    View Answer | Discuss in forum
  12. Given a string s = "Welcome", what is s.count('e')?



    1
    2
    3
    4

    View Answer | Discuss in forum
  13. Given a string s = "Programming is fun", what is s.find('ram')?



    1
    2
    3
    4

    View Answer | Discuss in forum
  14. Given a string s = "Programming is fun", what is s.find('rom')?



    -1
    1
    2
    3

    View Answer | Discuss in forum
  15. Given a string s = "Programming is fun", what is s.rfind('m')?



    8
    7
    6
    5

    View Answer | Discuss in forum
  16. Given a string s = "Programming is fun", what is s.find('m')?



    8
    7
    6
    5

    View Answer | Discuss in forum
  17. Given a string s = "Programming is fun", what is s.startswith('m')?



    1
    -1
    True
    False

    View Answer | Discuss in forum
  18. Given a string s = "Programming is fun", what is s.endswith('fun')?



    1
    -1
    True
    False

    View Answer | Discuss in forum
  19. Given a string s = "Programming is fun", what is s.endswith('m')?



    1
    -1
    True
    False

    View Answer | Discuss in forum
  20. What is "Good".replace("o", "e")?



    Gd
    Geed
    Good
    Ged

    View Answer | Discuss in forum
  21. Analyze the following code:

    class Name:
        def __init__(self, firstName, mi, lastName):
            self.firstName = firstName
            self.mi = mi
            self.lastName = lastName

    firstName = "John"
    name = Name(firstName, 'F', "Smith")
    firstName = "Peter"
    name.lastName = "Pan"
    print(name.firstName, name.lastName)



    The program displays Peter Pan.
    The program displays John Pan.
    The program displays Peter Smith.
    The program displays John Smith.

    View Answer | Discuss in forum
  22. Suppose i is 2 and j is 4, i + j is same as _________.



    i.__add(j)
    i.__add__(j)
    i.__Add(j)
    i.__ADD(j)

    View Answer | Discuss in forum
  23. Analyze the following code:

    class MyDate:
        def __init__(self, year, month, day):
            self.year = year
            self.month = month
            self.day = day

    class Name:
        def __init__(self, firstName, mi, lastName, birthDate):
            self.firstName = firstName
            self.mi = mi
            self.lastName = lastName
            self.birthDate = birthDate

    birthDate = MyDate(1990, 1, 1)
    name = Name("John", 'F', "Smith", birthDate)
    birthDate = MyDate(1991, 1, 1)
    birthDate.year = 1992
    print(name.birthDate.year)



    The program displays 1990.
    The program displays 1991.
    The program displays 1992.
    The program displays nothing.

    View Answer | Discuss in forum