100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Previous Exam Questions on Python

Previously asked exam-style Python and CS-fundamentals questions with worked answers for revision.

Concurrency & Interview PrepIntermediate18 min readJul 7, 2026
Analogies

1. Overview

This section collects exam-style questions spanning Python syntax, core data structures, recursion, and algorithmic complexity -- the kind commonly asked in written or practical Python exams. Work through each question, attempt the code traces by hand before checking the explanation, and use the Quick Reference for last-minute revision.

🏏

Cricket analogy: Like a written cricket-coaching exam covering rules, field placements, and scoring math, this section mixes syntax questions with hand-traced calculations you check afterward, then a Quick Reference card for last-minute revision before the paper.

2. Frequently Asked Questions

Write a Python function to check if a number is prime. What is its time complexity?

A simple approach checks divisibility from 2 up to the square root of n: def is_prime(n): return n > 1 and all(n % i for i in range(2, int(n**0.5) + 1)). Checking divisors only up to sqrt(n) gives a time complexity of O(sqrt(n)) instead of the naive O(n).

🏏

Cricket analogy: Checking primality only up to sqrt(n) is like a fielding captain only checking for run-out chances up to the halfway mark of the pitch instead of scanning the entire ground -- far fewer checks, same correct result.

What is the difference between compiled and interpreted languages? Where does Python fit?

A compiled language translates source code into machine code ahead of time (e.g., C); an interpreted language executes source code (or an intermediate form) line by line at run time via an interpreter (e.g., classic BASIC). Python is a hybrid: source is first compiled to platform-independent bytecode (.pyc), which the CPython virtual machine then interprets at run time.

🏏

Cricket analogy: Compiled is like a bowling machine pre-programmed with a full sequence of deliveries before the session starts, while interpreted is like a live bowler reacting ball by ball; Python is hybrid -- a coach's pre-set plan (bytecode) that's still executed live.

What is the output of the following code?

python
for i in range(5):
    if i == 3:
        break
else:
    print("Loop completed")
print(i)

A for-loop's else clause runs only if the loop completes without hitting a break. Here, break fires when i == 3, so the else block is skipped. The loop variable i retains its last value, 3, when break executed. So the only output is 3.

🏏

Cricket analogy: A for-loop's else is like a 'target achieved' bonus that only triggers if the chase completes all 50 overs without a batsman being run out early -- since a wicket (break) fell at over 3, the bonus is skipped, and the scoreboard shows 3.

Explain the time complexity of common list operations.

Indexing (lst[i]) is O(1). Appending to the end (lst.append(x)) is O(1) amortized. Inserting at the front or middle (lst.insert(0, x)) is O(n) because subsequent elements must shift. Searching for a value (x in lst) is O(n) since it may scan the whole list.

🏏

Cricket analogy: Indexing the scorecard by over number is instant (O(1)), appending the latest ball to the end is instant, but inserting a forgotten over at the start means renumbering every later over (O(n)), and searching for a specific score means scanning the whole card (O(n)).

What is recursion? Write a recursive factorial function and trace factorial(4).

Recursion is when a function calls itself to solve smaller instances of the same problem, until a base case stops further calls. def factorial(n): return 1 if n <= 1 else n * factorial(n-1). Tracing factorial(4): 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * (3 * (2 * factorial(1))) = 4 * 3 * 2 * 1 = 24.

🏏

Cricket analogy: Recursion is like calculating a team's total run rate by asking 'what was the rate before this over, plus this over's runs' repeatedly back to over one -- factorial(4) unwinds the same way: 4*(3*(2*(1))) = 24.

Differentiate between a stack and a queue and how each can be implemented in Python.

A stack is LIFO (Last-In-First-Out) -- the most recently added item is removed first; a plain Python list works well via append()/pop(). A queue is FIFO (First-In-First-Out) -- the earliest added item is removed first; collections.deque is preferred over a list because popleft() is O(1), whereas list.pop(0) is O(n).

🏏

Cricket analogy: A stack is like a pile of bats where the last one placed on top is grabbed first (LIFO), while a queue is like the batting order where the first player padded up bats first (FIFO), and deque handles substitutions from the front efficiently.

What is the output of the following code?

python
a = [1, 2, 3]
b = a[:]
b.append(4)
print(a, b)

Slicing with a[:] creates a shallow copy -- a new list object with the same elements. Appending to b does not affect a. Output: [1, 2, 3] [1, 2, 3, 4].

🏏

Cricket analogy: Slicing a scorecard with a[:] is like photocopying the full innings card into a new sheet b -- adding a fifth entry to the photocopy doesn't touch the original, so a stays [1,2,3] and b becomes [1,2,3,4].

What is Big-O notation and why is it used?

Big-O notation describes how an algorithm's running time (or space) grows relative to input size n, in the worst case, ignoring constant factors. It lets you compare algorithms' scalability independent of hardware, e.g., O(n) linear search vs O(log n) binary search.

🏏

Cricket analogy: Big-O is like comparing bowling strategies by how many extra deliveries they need as the target grows, ignoring the exact stadium -- a well-set field (O(log n) binary search style) scales far better than searching every fielder position one by one (O(n)).

What is the difference between a syntax/compile-time error and a runtime exception in Python?

A syntax error is caught by Python's parser before any code runs, e.g., a missing colon after if x: -- the program never starts executing. A runtime exception (e.g., ZeroDivisionError, IndexError) occurs while the program is executing valid syntax, typically due to invalid operations on data encountered during execution.

🏏

Cricket analogy: A syntax error is like the umpire refusing to start the match because the pitch dimensions are wrong -- play never begins; a runtime exception is like a valid match that starts fine but a batsman gets run out mid-innings due to a bad call.

What does the following code output?

python
def fib(n):
    if n <= 1:
        return n
    return fib(n-1) + fib(n-2)

print(fib(6))

The Fibonacci sequence starting fib(0)=0, fib(1)=1 continues 1, 2, 3, 5, 8 for fib(2) through fib(6). So fib(6) evaluates to 8.

🏏

Cricket analogy: Fibonacci is like a run-rate sequence where each over's target is the sum of the previous two overs' scores -- starting 0, 1, it builds 1, 2, 3, 5, 8, so by the sixth over (fib(6)) the target sits at 8.

3. Quick Reference

  • List indexing is O(1); append is O(1) amortized; insert at front and linear search are O(n).
  • A for-loop's else runs only when the loop finishes without hitting break.
  • Slicing (a[:]) makes a shallow copy, not a reference to the same list.
  • Recursion always needs a base case to terminate; otherwise it causes infinite recursion / RecursionError.
  • Stacks are LIFO (use list.append/pop); queues are FIFO (use collections.deque).

4. Key Takeaways

  • Know the time complexity of common list operations cold -- it's a frequent exam topic.
  • Trace for...else and recursive functions carefully by hand; exams love these code-tracing questions.
  • Slicing produces a shallow copy, distinct from simple assignment which shares the same object.
  • Big-O describes worst-case growth rate, useful for comparing algorithm scalability.
  • Syntax errors are caught before execution; runtime exceptions occur during execution.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#PreviousExamQuestionsOnPython#Previous#Exam#Questions#Frequently#StudyNotes#SkillVeris

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse