Python Interview Questions
  • Python - Strings
    1. In Python, if you executed name = 'Lizz', what would be the output of print(name[0:2])?
    A) Lizz
    B) L
    C) Li

    Answer: Li

    Explanation: We only retrieve the first two elements.
    2. In Python, if you executed var = '01234567', what would be the result of print(var[::2]).
    A) 0246
    B) 1357
    C) 1234567

    Answer: 0246

    Explanation: A stride value of 2 selects even elements.
    3. In Python, what is the result of the following operation: '1'+'2'
    A) 3
    B) '3'
    C) '12'

    Answer: '12'

    Explanation: The '+' applied to strings does not add strings but concatenates them
    4. What is the result of the following: 'hello'.upper()
    A) 'HELLO'
    B) 'Hello'
    C) 'hello'

    Answer: 'HELLO'

    Explanation: The .upper returns a copy of the string in which all case-based characters have been converted to uppercase.
    5. Consider the string Name="ABCDE", what is the result of the following operation Name.find("B")
    A) 1
    B) 2
    C) 0

    Answer: 1

    Explanation: The method finds the starting index of a substring
    6. What is the result of the following : str(1)+str(1)
    A) '11'
    B) 2
    C) 2

    Answer: '11'

    Explanation: The integers are cast to a string, and the strings are concatenated
    7. What is the result of the following: "123".replace("12", "ab")
    A) 'ab3'
    B) '123ab'
    C) 123

    Answer: 'ab3'

    Explanation: The method replace returns a copy of the string with all occurrences of the old substring
    8. A series of characters between quotes is called what?
    A) Data type
    B) A string
    C) A variable

    Answer: A string
    9. What code would turn the integer 1 into a string?
    A) str(1)
    B) int("1")
    C) float("1")

    Answer: str(1)
    10. Code used to get the length of samp_str?
    A) length(samp_str)
    B) len(samp_str)
    C) size(samp_str)

    Answer: len(samp_str)
    11. Get the 1st character in samp_str?
    A) samp_str[1]
    B) get(samp_str, 1)
    C) samp_str[0]

    Answer: samp_str[0]
    12. Get the 1st 4 characters in samp_str?
    A) samp_str[0:4]
    B) samp_str[1:4]
    C) samp_str[4]

    Answer: samp_str[0:4]
    13. Get the last character in samp_str?
    A) samp_str[last]
    B) samp_str[-1]
    C) samp_str[0]

    Answer: samp_str[-1]
    14. Code used to turn 4 into a string?
    A) str(4)
    B) string(4)
    C) str("4")

    Answer: str(4)
    15. Get the unicode for A?
    A) char("A")
    B) unicode("A")
    C) ord("A")

    Answer: ord("A")
    16. Convert the unicode 65 into a character string?
    A) chr(65)
    B) char(65)
    C) str(65)

    Answer: chr(65)
  • python bootcamp 2021
datai analytics