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.
Given the list [10, 20, 4, 45, 99, 45], what is the correct way to find the second largest unique 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.
Which slicing syntax would reverse the string 'Python' correctly?
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.
How can you check if a string is a palindrome in Python?
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.
Which syntax creates a new list containing the squares of numbers from 0 to 4?
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.
Which approach would you use to get all unique elements from a Python 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.
What is the most straightforward way to sum all numbers in the list [1, 2, 3, 4] in Python?
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.
What will be the output of numbers[-2] if numbers = [4, 11, 13, 29, 35]?
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.
Given the dictionary record = {'id': 10, 'name': 'Sam'}, how would you retrieve the value 10?
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.
If set1 = {1, 2, 3} and set2 = {3, 4, 5}, which expression finds common elements?
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.
Which method would you use to convert the string 'TestAutomation' to all lowercase letters?
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.