ABHYAS PREP: PROGRAMMING MCQS (PYTHON AND SQL OUTPUT) · PYTHON OUTPUT QUESTIONS

Python Output Questions

Predict what short Python programs print: slicing, aliasing, defaults, scope and more.

Loading practice…

Worked examples

Example 1 · level 1

What is the output of the following program?

  1. 10 [20, 30, 40] 50
  2. 10 20 30 40 50
  3. 10 [20, 30, 40, 50] 50
  4. 10 [] 50
Show solution
  1. Tuple of five integers.
  2. Unpack first to a, last to b, middle to list mid.
  3. Print a, mid, b.

Answer: 10 [20, 30, 40] 50

Example 2 · level 2

What is the output of the following program?

  1. [99, 2, 3, 4]
  2. [1, 2, 3, 4]
  3. [99, 2, 3]
  4. [1, 2, 3]
Show solution
  1. alias points to same list as lst.
  2. append adds 4 to the shared list.
  3. lst[0]=99 changes first element of shared list.
  4. Printing alias shows all mutations.

Answer: [99, 2, 3, 4]

Example 3 · level 3

What is the output of the following program?

  1. ['Mumbai', 'Delhi', 'Chennai']
  2. ['Delhi', 'Mumbai', 'Chennai']
  3. ['Chennai', 'Delhi', 'Mumbai']
  4. ['Mumbai', 'Chennai', 'Delhi']
Show solution
  1. List of (city, population) tuples.
  2. Sort by population using lambda.
  3. Extract city names in sorted order.
  4. Print list.

Answer: ['Mumbai', 'Delhi', 'Chennai']

Search by task, format or tool name.