Python Interview Questions
  • Python - Lists
    1. What is the result of the following operation: 'A,B,C,D'.split(',')
    A) ['A','B','C','D']
    B) ('A','B','C','D')
    C) 'A','B','C','D'

    Answer: ['A','B','C','D']

    Explanation: Split returns a list of the words in the string, separated by the delimiter string, in this case, a comma.
    2. The method append does the following:
    A) adds one element to a list
    B) merges two lists
    C) inserts multiple elements to a list

    Answer: adds one element to a list

    Explanation: append-only adds one element.
    3. Lists are mutable
    A) True
    B) False

    Answer: True

    Explanation: Lists are mutable tuples are not
    4. Consider the following list : A=["hard rock",10,1.2] what will list A contain affter the following command is run: del(A[1])
    A) [10,1.2]
    B) ["hard rock" 1.2]
    C) ["hard rock", 10]

    Answer: ["hard rock" 1.2]

    Explanation: We will delete element 1
    5. What is the syntax to clone the list A and assign the result to list B
    A) B = A
    B) B=A[:]
    C) a = b

    Answer: B=A[:]

    Explanation: The [:] method is used to clone a list.
    6. Create num_list that contains the numbers 1 through 3?
    A) num_list = (1,2,3)
    B) num_list = [1,2,3]
    C) num_list = {1,2,3}

    Answer: num_list = [1,2,3]
    7. Create the list one_to_ten that holds values 1 through 10 with range()?
    A) one_to_ten = list(range(1,11))
    B) one_to_ten = list(range(10))
    C) one_to_ten = list[range(10)]

    Answer: one_to_ten = list(range(1,11))
    8. Print the 1st value in rand_list?
    A) rand_list[0]
    B) print(rand_list[1])
    C) print(rand_list[0])

    Answer: print(rand_list[0])
    9. Get the number of values in rand_list?
    A) len(rand_list)
    B) length(rand_list)
    C) size(rand_list)

    Answer: len(rand_list)
    10. Check if dog is in rand_list?
    A) dog in rand_list
    B) find("dog", rand_list)
    C) "dog" in rand_list

    Answer: "dog" in rand_list
    11. Get the index for dog in rand_list?
    A) rand_list.index("dog")
    B) index("dog", rand_list)
    C) "index(rand_list, "dog")

    Answer: rand_list.index("dog")
    12. Create even_list which will contain even numbers up till 10?
    A) even_list = [i*2 for i in range(5)]
    B) even_list = [i*2 range(10)]
    C) even_list = [i%2 for i in range(10)]

    Answer:even_list = [i*2 for i in range(5)]
    13. Create a multidimensional list list_table with 10 rows and columns?
    A) list_table = [10 for i in range(10)]
    B) list_table = [[0] * 10 for i in range(10)]
    C) list_table = [[0] % 10 for i in range(10)]

    Answer: list_table = [[0] * 10 for i in range(10)]
    14. What method here does not add one or more elements to a list?
    A) extend
    B) add
    C) append
    D) insert

    Answer: add

    Explanation: There is no add method
    15. Given a list, numbers = [1,2,3], how would you access the first element in the list?
    A) numbers[-1]
    B) numbers[1]
    C) numbers[0]

    Answer: numbers[0]
    16. Given a list numbers = [1,2,3] what would the result of numbers.pop(5) be?
    A) None
    B) IndexError
    C) 3

    Answer: IndexError
    17. Which of the following is not true about lists:
    A) You can loop over them using a for loop or a while loop
    B) The index starts at 1
    C) They are ordered collection of elements

    Answer: The index starts at 1
    18. Given a list numbers = [1,2,3,4] - what does numbers[::-1] return?
    A) [1,2,3,4]
    B) [1,4]
    C) [4,3,2,1]

    Answer: [4,3,2,1]
    19. Given a list numbers = [1,2,3,4] - what does numbers[1:3] return?
    A) [2,3]
    B) [1,2,3]
    C) [2,3,4]
    D) [3]

    Answer: [2,3]
    20. Given a list numbers = [1,2,3,4] - what does numbers[-2] return?
    A) [3]
    B) 3
    C) [1,2,3]
    D) [4]

    Answer: 3
  • python bootcamp 2021
datai analytics