Python Coding Interview Questions for Test Automation and SDET Roles Quiz

Test your Python skills with this quiz covering key programming concepts and techniques commonly asked in coding interviews for test automation and SDET positions. Assess your proficiency in Python with practical questions on lists, strings, loops, and more.

  1. Second Largest Element in a List

    Given the list [10, 20, 4, 45, 99, 45], what is the correct way to find the second largest unique element?

    1. Sort the list in descending order after removing duplicates and return the element at index 1.
    2. Return the maximum value of the list and subtract one.
    3. Reverse the list and pick the first element.
    4. Use list.sort() and get the second to last element.

    Explanation: To find the second largest unique element, duplicates must first be removed. Then, sorting the list in descending order allows access to the next largest value at index one. Returning max minus one or sorting without removing duplicates can give incorrect results if there are duplicate maximums. Reversing the list does not guarantee order by magnitude.

  2. String Reversal in Python

    Which slicing syntax would reverse the string 'Python' correctly?

    1. s[::-2]
    2. s[-2:]
    3. s[::-1]
    4. s[1:]

    Explanation: The slicing syntax s[::-1] reverses the string by stepping through it backwards. s[1:] omits the first character without reversing, s[::-2] reverses with a step of two, capturing every other character, and s[-2:] gets the last two characters only.

  3. Palindrome Check

    How can you check if a string is a palindrome in Python?

    1. Compare the string to its reverse using s == s[::-1].
    2. Check if the first and last characters are equal.
    3. Use the reverse() method on the string.
    4. Count vowels and consonants for symmetry.

    Explanation: Comparing the string to its reverse using s == s[::-1] is an efficient and direct method for palindrome checking. Comparing only the first and last characters is insufficient unless the entire string is checked. Strings do not have a reverse() method; it's for lists. Counting vowels and consonants does not determine palindromes.

  4. List Comprehensions

    Which syntax creates a new list containing the squares of numbers from 0 to 4?

    1. [x*x for x in range(5)]
    2. [x for x in range(5)]
    3. [x^2 for x in range(5)]
    4. (x*x for x in range(5))

    Explanation: [x*x for x in range(5)] uses list comprehension to create a list of squares. [x for x in range(5)] simply lists the numbers unmodified. (x*x for x in range(5)) produces a generator, not a list. [x^2 for x in range(5)] uses bitwise XOR instead of exponentiation.

  5. Finding Unique Elements

    Which approach would you use to get all unique elements from a Python list?

    1. Sort the list and remove elements manually.
    2. Remove duplicates by iterating and deleting.
    3. Convert the list to a set using set(my_list).
    4. Use the unique() method of list.

    Explanation: Converting a list to a set removes duplicates automatically. The list type has no unique() method in Python. Sorting and manually removing elements is more complex and slower. Iterating and deleting can work but is less efficient than using a set.

  6. Python Looping to Sum List Items

    What is the most straightforward way to sum all numbers in the list [1, 2, 3, 4] in Python?

    1. Multiply all items together.
    2. Concatenate the numbers as strings.
    3. Use the built-in sum() function.
    4. Subtract the smallest from the largest.

    Explanation: The sum() function efficiently totals all numbers in the list. Multiplying gives the product, not the sum. Concatenation results in a string, offering no sum. Subtracting just finds the difference between extremes, not the total.

  7. List Indexing

    What will be the output of numbers[-2] if numbers = [4, 11, 13, 29, 35]?

    1. 11
    2. 13
    3. 35
    4. 29

    Explanation: The index -2 accesses the second-to-last element, which is 29. 13 is at index 2, 35 is the last element, and 11 is at index 1. Negative indices count from the end of the list.

  8. Dictionary Value Access

    Given the dictionary record = {'id': 10, 'name': 'Sam'}, how would you retrieve the value 10?

    1. record.id
    2. record[10]
    3. record['id']
    4. record('id')

    Explanation: In Python, record['id'] accesses the value for the key 'id', returning 10. record.id is not valid; that's for attribute access in objects. record('id') tries to call record as a function, which is incorrect. record[10] would try to find a key 10, which does not exist.

  9. Set Operations in Python

    If set1 = {1, 2, 3} and set2 = {3, 4, 5}, which expression finds common elements?

    1. set1 - set2
    2. set1 u0026 set2
    3. set1 ^ set2
    4. set1 | set2

    Explanation: set1 u0026 set2 returns the intersection, which is {3}. set1 | set2 is the union, including all unique elements. set1 - set2 removes elements of set2 from set1, and set1 ^ set2 gives the symmetric difference, not the intersection.

  10. String Case Conversion

    Which method would you use to convert the string 'TestAutomation' to all lowercase letters?

    1. capitalize()
    2. lower()
    3. swapcase()
    4. uppercase()

    Explanation: The lower() method safely converts every character to lowercase. capitalize() only changes the first letter to uppercase and the rest to lowercase. uppercase() is not a Python string method. swapcase() toggles the case of each character.