Skip to Content

PCAP-31-03: Python String Comparison Which Expressions Evaluate to True

Explore complex Python string comparisons for the PCAP-31-03 certification exam. Learn about string concatenation, multiplication, and comparison operators in this expert analysis.

Table of Contents

Question

Which of the following expressions evaluate to True? (Choose two.)

A. ‘1’ + ‘1’ + ‘1’ < ‘1’ * 3′
B. 121 + 1 != ‘1’ + 2 * ‘2’
C. ‘AbC’.lower() < ‘AB’
D. ‘3.14’ != str(3.1415)

Answer

B. 121 + 1 != ‘1’ + 2 * ‘2’
D. ‘3.14’ != str(3.1415)

Explanation

Let’s analyze each option:

A. ‘1’ + ‘1’ + ‘1’ < ‘1’ * 3′
This is False.

  • Left side: ‘1’ + ‘1’ + ‘1’ concatenates to ‘111’
  • Right side: ‘1’ * 3 repeats ‘1’ three times, resulting in ‘111’
  • ‘111’ < ‘111’ is False, as they are equal

B. 121 + 1 != ‘1’ + 2 * ‘2’
This is True.

  • Left side: 121 + 1 = 122 (integer)
  • Right side: ‘1’ + 2 * ‘2’ = ‘1’ + ’22’ = ‘122’ (string)
  • 122 != ‘122’ is True because an integer is not equal to a string

C. ‘AbC’.lower() < ‘AB’
This is False.

  • ‘AbC’.lower() results in ‘abc’
  • ‘abc’ < ‘AB’ is False because lowercase letters are greater than uppercase in ASCII/Unicode ordering

D. ‘3.14’ != str(3.1415)
This is True.

  • ‘3.14’ is a string
  • str(3.1415) converts 3.1415 to the string ‘3.1415’
  • ‘3.14’ != ‘3.1415’ is True because these are different strings

The key concepts tested here are:

  1. String concatenation and multiplication
  2. Comparison of strings (lexicographic ordering)
  3. Type comparison (int vs str)
  4. String methods (lower())
  5. String representation of floating-point numbers

Understanding these concepts is crucial for the PCAP-31-03 certification exam and Python programming in general.

Python Institute PCAP-31-03 certification exam practice question and answer (Q&A) dump with detail explanation and reference available free, helpful to pass the Python Institute PCAP-31-03 exam and earn Python Institute PCAP-31-03 certification.