Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 8 More on Strings and Special Methods


Section 8.2 The str Class
8.1  What is len("Good")?
A. 1
B. 2
C. 3
D. 4
E. -1

8.2  What is max("Programming is fun")?
A. P
B. r
C. a blank space character
D. u
E. n

8.3  What is min("Programming is fun")?
A. P
B. r
C. a blank space character
D. u
E. n

8.4  What is "Programming is fun"[4: 6]?
A. ram
B. ra
C. r
D. pr
E. pro

8.5  What is "Programming is fun"[-1]?
A. Pr
B. P
C. fun
D. n
E. un

8.6  What is "Programming is fun"[1:1]?
A. P
B. r
C. Pr
D. ''
E. incorrect expression

8.7  What is "Programming is fun"[-3:-1]?
A. Pr
B. P
C. fun
D. un
E. fu

8.8  What is "Programming is fun"[:-1]?
A. Programming
B. rogramming is fun
C. Programming is f
D. Programming is fu
E. Programming is

8.9  What is "Programming is fun"[:2]?
A. Pr
B. P
C. Pro
D. Programming
E. Programming is

8.10  Given a string s = "Welcome", which of the following code is incorrect?
A. print(s[0])
B. print(s.lower())
C. s[1] = 'r'
D. print(s.strip())

8.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))
A. True False
B. True True
C. False True
D. False False

8.12  Given a string s = "Welcome", what is s.count('e')?
A. 1
B. 2
C. 3
D. 4

8.13  Given a string s = "Programming is fun", what is s.find('ram')?
A. 1
B. 2
C. 3
D. 4
E. -1

8.14  Given a string s = "Programming is fun", what is s.find('rom')?
A. 1
B. 2
C. 3
D. 4
E. -1

8.15  Given a string s = "Programming is fun", what is s.rfind('m')?
A. 8
B. 7
C. 6
D. 5
E. -1

8.16  Given a string s = "Programming is fun", what is s.find('m')?
A. 8
B. 7
C. 6
D. 5
E. -1

8.17  Given a string s = "Programming is fun", what is s.startswith('m')?
A. 0
B. 1
C. -1
D. True
E. False

8.18  Given a string s = "Programming is fun", what is s.startswith('Program')?
A. 0
B. 1
C. -1
D. True
E. False

8.19  Given a string s = "Programming is fun", what is s.endswith('fun')?
A. 0
B. 1
C. -1
D. True
E. False

8.20  Given a string s = "Programming is fun", what is s.endswith('m')?
A. 0
B. 1
C. -1
D. True
E. False

8.21  What is "Good".replace("o", "e")?
A. God
B. Good
C. Geed
D. Ged
E. Good

8.22  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)
A. The program displays Peter Pan.
B. The program displays John Pan.
C. The program displays Peter Smith.
D. The program displays John Smith.

8.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(199011)
name = Name("John"'F'"Smith", birthDate)
birthDate = MyDate(199111)
birthDate.year = 1992
print(name.birthDate.year)
A. The program displays 1990.
B. The program displays 1991.
C. The program displays 1992.
D. The program displays no thing.

Section 8.5 Operator Overloading and Special Methods
8.24  To concatenate two strings s1 and s2 into s3, use _________.
A. s3 = s1 + s2
B. s3 = s1.add(s2)
C. s3 = s1.__add(s2)
D. s3 = s1.__add__(s2)

8.25  To retrieve the character at index 3 from string s, use _________.
A. s[3]
B. s.getitem(3)
C. s.__getitem__(3)
D. s.getItem(3)

8.26  To return the length of string s, use _________.
A. s.__len__()
B. len(s)
C. size(s)
D. s.size()

8.27  If a class defines the __str__(self) method, for an object obj for the class, you can use ______ to invoke the __str__ method.
A. obj.__str__()
B. str(obj)
C. obj.str()
D. __str__(obj)

8.28  To check whether string s1 contains s2, use _________.
A. s1.__contains__(s2)
B. s1 in s2
C. s1.contains(s2)
D. si.in(s2)

8.29  Suppose i is 2 and j is 4, i + j is same as _________.
A. i.__add(j)
B. i.__add__(j)
C. i.__Add(j)
D. i.__ADD(j)