PCAP-31-03 practice question 15 of 25

Which of the following invocations are valid? (Select two answers)

Full set: 147 questions
Answer options
A: rfind("python","r")
B: sorted("python") Correct answer
C: "python".sort ()
D: "python".index("th") Correct answer
Correct Answer:
B. sorted("python"); D. "python".index("th")
Explanation

sorted(iterable) is a built-in that accepts a string and returns a list of its characters, so sorted("python") is valid. str.index(sub[, start[, end]]) is a legal method invocation; "python".index("th") returns 2. The other two calls either lack the required object context or reference a non-existent method.

Why Incorrect

A. rfind is a string method (or string.rfind in the string module); calling it as a standalone function without import/object raises NameError.
C. str objects have no sort() method; invoking it raises AttributeError.

References

1. Python Documentation, Library Reference 3.12 – §4.7 “Built-in Types”: str.index description.

2. Python Documentation, Library Reference 3.12 – “Built-in Functions”, entry for sorted().

3. Python Documentation, Library Reference 3.12 – 4.7 “str methods”, entry for rfind (object method).

4. University of Michigan “Python 3 Programming” course notes, Week 4, section “Sorting”.

Scroll to Top