LearnFrenzy
Sign in Take a free test

Python Programming · Topic 24 of 25

Tricky Python Output Questions Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Interview Questions

Tricky Python Output Questions · Study material

Tricky Python – Interview Questions

1. Why does a list default argument keep growing between calls?

Defaults are evaluated once when def runs, so every call without that argument shares the same list. Use None and create the list inside.

2. Why does [lambda: i for i in range(3)] give functions that all return 2?

Late binding: each lambda looks up i when called, after the loop has finished. Capture with lambda i=i: i.

3. What is the difference between a = a + [x] and a += [x] for lists?

+ creates a new list and rebinds a; += extends the existing list in place, so every alias sees the change.

4. Why does round(2.5) return 2?

Python rounds halves to the nearest even number (banker's rounding).

5. Why is -7 // 2 equal to -4?

// is floor division, which rounds toward minus infinity.

6. Why is [] == False False when [] is falsy?

Truthiness (bool([])) and equality are different questions; a list never equals a bool.

7. What does print(nums.sort()) print?

None – sort() sorts in place and returns None.

8. Why is {1: "a", True: "b"} a one-key dictionary?

1 == True and hash(1) == hash(True), so they are the same key.

9. Why does a second sum() over a generator return 0?

The first sum exhausted the generator; it cannot be restarted.

10. Does finally run if try returns?

Yes; finally runs before the function returns to its caller.

11. Why can changing one object change "another" object?

Both names refer to the same object (aliasing) – common with lists, dicts and class attributes.

12. Why does x = 10 inside a function not change a global x?

Assignment creates a local variable unless the name is declared global (or nonlocal).

13. What is printed by print(print("a"))?

"a" on one line, then None – the inner print returns None.

14. Why is 0.1 + 0.2 == 0.3 False?

Binary floating point cannot represent these decimals exactly; the sum is 0.30000000000000004.

15. Why does modifying a list while looping over it skip items?

Removing an item shifts the later items left, while the loop's internal index moves on.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links