PCEP 30 02 PCEP Certified Entry Level Python Programmer · Free Practice Question Medium
Question 15
What is the output of the following code snippet?
- def cars_disp(new, clist=[]):
- clist.append(new)
- return clist
- top_cars = cars_disp("Pagani",["Ferrari","Aston", "Lamborghini"])
- print(cars_disp("McLaren", top_cars))
-
A
['Pagani', 'Ferrari', 'Aston', 'Lamborghini', 'McLaren'] -
B
['Ferrari', 'Aston', 'Lamborghini', 'Pagani', 'Pagani'] -
C
['McLaren', ['Ferrari', 'Aston', 'Lamborghini'], 'McLaren'] -
D
['Ferrari', 'Aston', 'Lamborghini', 'Pagani', 'McLaren']
Reveal correct answer
Correct answer: D
Explanation
First, cars_disp() is called and "Pagani" is added after the last element of ["Ferrari","Aston", "Lamborghini"] so it is ["Ferrari","Aston", "Lamborghini", "Pagani"]
Again, cars_disp() is called and "McLaren" is added after the last element of ["Ferrari","Aston", "Lamborghini", "Pagani"] so it is ["Ferrari","Aston", "Lamborghini", "Pagani", "McLaren"]
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
