PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium

Question 30

Q197 - Data Aggregates


Which of the following invocations are valid?


(Select two answers)


  • A

    print(sorted("python"))

  • B

    print("python".find(''))

  • C

    "python".sort()

  • D

    sort("python")

Reveal correct answers

Correct answers: A, B

Explanation

Q197 (Please refer to this number, if you want to write me about this question.)

A. The sorted() function in Python is used to return a new sorted list from the elements of any iterable object, such as a string. In this case, the string "python" is being sorted alphabetically, resulting in a list of characters ['h', 'n', 'o', 'p', 't', 'y']. Therefore, print(sorted("python")) is a valid invocation.

B. The find() method in Python is used to find the first occurrence of a substring within a string. In this case, an empty string is being searched within the string "python", which will return 0 as the index of the first occurrence. Therefore, print("python".find('')) is a valid invocation.

C. The sort() method is used to sort a list in place and does not return a new sorted list. However, in this case, the method is being called on a string object "python", which does not have a sort() method. Therefore, "python".sort() is an invalid invocation.

D.

The sort() method is used to sort a list in place, but it cannot be directly called on a string object like "python". The correct way to sort a string by the sort() method would be to convert it to a list first. Therefore, sort("python") is an invalid invocation.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need