Translated with AI · suggest a fix

Programming MCQsप्रोग्रामिंग एमसीक्यू · पायथन आउटपुट प्रश्न

Python Output Questionsपायथन आउटपुट प्रश्न questions and answersप्रश्न और उत्तर

Output questions test whether you can run code in your head. Every program on this page was actually run, so the keyed output is exactly what Python prints.

  • Unlimitedअसीमित fresh questionsनए प्रश्न
  • Answersउत्तर always one tap awayहमेशा एक टैप दूर
  • 1 subtopicउप-टॉपिक
  • TCS NQT ~2

Practice questions are in English for now.अभ्यास के प्रश्न अभी अंग्रेज़ी में हैं।

How python output questions worksपायथन आउटपुट प्रश्न को समझिए

Read the program line by line and keep a small table of variable values. Most traps are in what changes a value in place (append, sort, +=) versus what creates a new object.

Slices include the start index and stop just before the stop index. Negative indices count from the end, and a step of -1 reverses.

Default arguments are evaluated once, when the function is defined, so a mutable default keeps changes between calls.

Tricks that save timeसमय बचाने वाली ट्रिक्स

All tricks →सभी ट्रिक्स →

Each trick says when to use it, why it works and when it fails. Formula tricks are checked by code over their whole range.हर ट्रिक बताती है कि उसे कब इस्तेमाल करें, वह क्यों काम करती है और कब नहीं। सूत्र वाली ट्रिक्स को कोड से उनकी पूरी रेंज पर जाँचा गया है।

Formulaसूत्रRarely taughtकम सिखाई जाने वाली✓ Checked on 4,200 cases✓ 4,200 मामलों पर जाँची गई

Count a slice before you write it out

Use it when:कब इस्तेमाल करें: Slices with a step, like s[2:11:3], where an option differs by one character.

  1. Clip start and stop to the length of the sequence.
  2. Number of items = ceil((stop − start) / step) when stop > start, else 0.
  3. Then list the indices start, start + step, … below stop.
Exampleउदाहरण

"PLACEMENT"[2:7:2]

→ ceil(5 / 2) = 3 items: indices 2, 4, 6 → A, E, E

Why it works, and when it failsयह क्यों काम करती है, और कब नहीं

Why:क्यों: The slice takes start, start + step, … while the index stays below stop, which is exactly ceil((stop − start)/step) indices.

Limits:सीमाएँ: For negative steps the slice runs backwards and the rule is ceil((start − stop)/|step|) after clipping.

Exam strategyपरीक्षा रणनीति

Keep a trace table, and mark what changes in place

Use it when:कब इस्तेमाल करें: Any “what does this program print?” question with more than three lines.

  1. Write one column per variable and update it line by line.
  2. Star every operation that changes an object in place (append, extend, sort, +=, dict assignment): every name pointing to that object sees the change.
  3. Only then read the print line.
Exampleउदाहरण

a = [1, 2]; b = a; b += [3]; print(a)

→ += on a list changes it in place, and a and b are the same list → [1, 2, 3]

Why it works, and when it failsयह क्यों काम करती है, और कब नहीं

Why:क्यों: Most output traps come from aliasing and in-place mutation, which are invisible if you track values but not objects. Marking in-place operations makes them visible.

Limits:सीमाएँ: For recursion, use a call stack (one row per call) instead of one row per variable.

Solved examplesहल किए गए उदाहरण

Answers and full solutions are shown. Hide them to test yourself first.उत्तर और पूरे हल दिखाए गए हैं। पहले खुद को परखने के लिए इन्हें छिपाइए।

  1. Example 1उदाहरण 1 · Python Output Questions · Easyआसान

    What is the output of the following program?

    Python
    s = '  apple, banana , cherry '
    parts = s.strip().replace(' ', '').split(',')
    print('-'.join(parts))
    1. apple-banana-cherry
    2. apple,banana,cherry
    3. apple banana cherry
    4. apple-banana-cherry-
    Answer:उत्तर: apple-banana-cherry
    1. strip() removes leading/trailing spaces.
    2. replace(' ', '') deletes all internal spaces.
    3. split(',') breaks on commas.
    4. join('-') combines with hyphens.
  2. Example 2उदाहरण 2 · Python Output Questions · Mediumमध्यम

    What is the output of the following program?

    Python
    a = {1,2,3,4}
    b = {3,4,5,6}
    print(sorted(a & b), sorted(a | b), sorted(a - b))
    1. [3, 4] [1, 2, 3, 4, 5, 6] [1, 2]
    2. [3, 4] [1, 2, 3, 4, 5, 6] [3, 4]
    3. [1, 2] [1, 2, 3, 4, 5, 6] [1, 2]
    4. [3, 4] [3, 4, 5, 6] [1, 2]
    Answer:उत्तर: [3, 4] [1, 2, 3, 4, 5, 6] [1, 2]
    1. Create sets a and b.
    2. Compute intersection (&), union (|), difference (-).
    3. Sort each result for deterministic order.
    4. Print them space separated.
  3. Example 3उदाहरण 3 · Python Output Questions · Hardकठिन

    What is the output of the following program?

    Python
    nums = [1, 2, 3, 4]
    squares = [x*x for x in nums if x % 2 == 0]
    print(squares)
    1. [4, 16]
    2. [1, 4, 9, 16]
    3. [2, 4]
    4. [4, 9]
    Answer:उत्तर: [4, 16]
    1. Filter keeps even numbers 2 and 4.
    2. Square each: 2*2=4, 4*4=16.
    3. List becomes [4,16].
  4. Example 4उदाहरण 4 · Python Output Questions · Mediumमध्यम

    What is the output of the following program?

    Python
    a = [1, 2, 3]
    b = a
    b.append(4)
    print(a)
    1. [1, 2, 3, 4]
    2. [1, 2, 3]
    3. [1, 2, 3, 4, 4]
    4. []
    Answer:उत्तर: [1, 2, 3, 4]
    1. b = a makes b refer to same list object
    2. b.append(4) mutates that shared list
    3. print(a) shows the mutated list
  5. Example 5उदाहरण 5 · Python Output Questions · Mediumमध्यम

    What is the output of the following program?

    Python
    try:
        x = 10 // 0
    except ZeroDivisionError:
        print("caught")
    finally:
        print("done")
    1. caught done
    2. caught
    3. done
    4. ZeroDivisionError
    Answer:उत्तर: caught done
    1. Division by zero raises ZeroDivisionError.
    2. Except block catches it and prints 'caught'.
    3. Finally block always runs and prints 'done'.
  6. Example 6उदाहरण 6 · Python Output Questions · Mediumमध्यम

    What is the output of the following program?

    Python
    try:
        x = 10 // 0
    except ZeroDivisionError:
        print("Z")
    except Exception:
        print("E")
    finally:
        print("F")
    1. Z F
    2. Z E F
    3. E F
    4. F
    Answer:उत्तर: Z F
    1. 10//0 raises ZeroDivisionError
    2. caught by first except, prints Z
    3. finally prints F
    4. second except never runs

Common mistakesआम गलतियाँ

  • Treating b = a as a copy of a list.
  • Including the stop index in a slice.
  • Forgetting that // rounds down towards minus infinity.

Frequently asked questionsअक्सर पूछे जाने वाले प्रश्न

Are these questions checked?

Yes. Every program is executed with Python (Pyodide) before it is published, and the keyed option must match the real output exactly.

Which placement tests ask output questions?

Coding rounds and technical MCQ sections of service companies often include “what is the output” questions in C, Java or Python.

What version of Python is used?

Python 3 (current Pyodide). Older Python 2 behaviour such as integer division with / does not apply.

Search by task, format or tool name.