Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, September 29, 2026

Python file-handling

 ### Top 20 Python `open()` Function Quiz 🐍


Test your Python file-handling knowledge with 20 multiple-choice questions covering file modes, reading, writing, appending, `with` statements, and file pointers.


### Part 1: Basic `open()` Quiz

Q1. What is the default mode of open()?

A. 'w'

B. 'r'

C. 'a'

D. 'x'

Correct answer: B

The default mode is 'r', which opens a file for reading.


Q2. What happens if you open a non-existing file using open('test.txt', 'r')?

A. Creates the file

B. Returns None

C. Raises FileNotFoundError

D. Creates an empty file

Correct answer: C

Read mode requires the file to exist.


Q3. Which mode opens a file for writing and truncates existing content?

A. 'a'

B. 'r'

C. 'w'

D. 'x'

Correct answer: C

'w' creates a file if needed and clears existing content.


Q4. Which mode adds content to the end of a file?

A. 'r'

B. 'a'

C. 'w'

D. 'x'

Correct answer: B

Append mode writes at the end without removing existing content.


Q5. What does mode 'x' do?

A. Reads a file

B. Appends content

C. Creates a new file exclusively

D. Deletes a file

Correct answer: C

'x' raises FileExistsError if the file already exists.


Q6. What does file.read() return?

A. A list of lines

B. A string containing file content

C. A file object

D. An integer

Correct answer: B

read() returns the file contents as a string in text mode.


Q7. What does readline() return?

A. All lines

B. One line, including newline if present

C. A list

D. The last line only

Correct answer: B

readline() reads one line at a time.


Q8. What does readlines() return?

A. A string

B. A tuple

C. A list of lines

D. A dictionary

Correct answer: C

readlines() returns a list where each element is a line.


Q9. Which method writes a string to a file?

A. read()

B. write()

C. readline()

D. append()

Correct answer: B

write() writes a string and does not automatically add a newline.


Q10. What is the purpose of the with statement when opening files?

A. Makes file read-only

B. Automatically closes the file

C. Deletes the file

D. Opens only binary files

Correct answer: B

The context manager closes the file automatically, even if an exception occurs.


### Part 2: Output-Based Quiz

Q11. What is printed if file.txt contains Hello World?

f = open('file.txt', 'r') print(f.read(5))

A. Hello

B. Hello World

C. World

D. Error

Correct answer: A

read(5) reads the first five characters.


Q12. What is printed if file.txt contains Python?

f = open('file.txt', 'r') print(f.read()) print(f.read())

A. Python twice

B. Python followed by a blank line

C. Error

D. None twice

Correct answer: B

The first read consumes the content. The second read returns an empty string.


Q13. What is the content after execution if file.txt initially contains ABC?

f = open('file.txt', 'w') f.write('XYZ') f.close()

A. ABCXYZ

B. ABC

C. XYZ

D. XYZABC

Correct answer: C

Write mode truncates the original file before writing XYZ.


Q14. If file.txt contains Hi, what is its content after this?

f = open('file.txt', 'a') f.write('!') f.close()

A. !

B. Hi!

C. Hi

D. !Hi

Correct answer: B

Append mode adds ! at the end.


Q15. What does this print?

with open('file.txt', 'w') as f: f.write('Python') print(f.closed)

A. False

B. True

C. Python

D. Error

Correct answer: B

The with block closes the file when execution exits the block.


Q16. What is printed?

f = open('file.txt', 'w') print(f.write('Hello')) f.close()

A. Hello

B. 5

C. None

D. True

Correct answer: B


write() returns the number of characters written. Hello has five characters.


Q17. What is printed?

f = open('file.txt', 'w') f.write('ABC\\nDEF') f.close() f = open('file.txt', 'r') print(f.readline().strip())

A. ABC

B. DEF

C. ABC DEF

D. ABC\\nDEF

Correct answer: A

readline() reads the first line; strip() removes the newline.


Q18. What does seek(0) do?

f.seek(0)

A. Closes the file

B. Moves the file pointer to the beginning

C. Deletes the first character

D. Moves to the end

Correct answer: B

seek(0) moves the file pointer to byte/character position zero in ordinary text-file usage.


Q19. What is printed?

f = open('file.txt', 'w+') f.write('ABC') f.seek(0) print(f.read()) f.close()

A. ABC

B. Empty string

C. 3

D. Error

Correct answer: A

w+ permits reading and writing. seek(0) moves the pointer back to the beginning.


Q20. What happens when opening an existing file with mode 'x'?

f = open('existing.txt', 'x')

A. File is overwritten

B. File is appended

C. FileExistsError is raised

D. File is read

Correct answer: C

Exclusive creation fails if the file already exists.


### Quick revision: Python file modes


| Mode | Purpose |

| ---- | ---------------------------------------- |

| r | Read; file must exist |

| w | Write; truncates existing content |

| a | Append; creates file if missing |

| x | Create exclusively; fails i

f file exists |

| r+ | Read and write; file must exist |

| w+ | Read and write; truncates existing file |

| a+ | Read and append; creates if missing |


Remember: Use `with open("file.txt", "r") as f:` for automatic file closing.

Classes, Objects, Inheritance, Method Overriding & More

 ### Top 20 Python OOP Quiz 🐍


### Classes, Objects, Inheritance, Method Overriding & More


Test your Python knowledge with these 20 output-based MCQs covering classes, constructors, inheritance, method overriding, `super()`, polymorphism, encapsulation, and abstraction.


### Section 1: Classes and Objects

Q1. What is the output?

```python

class Student:

    def __init__(self, name):

        self.name = name


s = Student("Ravi")

print(s.name)

```

A. Student

B. Ravi

C. name

D. Error

Answer: B — Ravi. `self.name` stores the instance attribute.


Q2. What is the output?

```python

class Demo:

    x = 10


a = Demo()

b = Demo()

a.x = 20


print(b.x)

```


A. 10

B. 20

C. None

D. Error

Answer: A — 10. Assigning `a.x` creates an instance attribute; `b.x` still resolves to the class attribute.


Q3. What does `__init__()` do?

A. Destroys an object

B. Initializes an instance after it is created

C. Inherits a class

D. Defines a static method

Answer: B — It initializes the newly created instance.


Q4. What is the output?

```python

class A:

    def show(self):

        print("A")


obj = A()

obj.show()

```

A. A

B. show

C. None

D. Error

Answer: A — A.


Q5. What is the output?

```python

class Counter:

    count = 0


    def __init__(self):

        Counter.count += 1


a = Counter()

b = Counter()

print(Counter.count)

```

A. 0

B. 1

C. 2

D. Error

Answer: C — 2. The class variable is incremented for each instance.


### Section 2: Inheritance and Method Overriding

Q6. What is the output?

```python

class Parent:

    def show(self):

        print("Parent")


class Child(Parent):

    pass


Child().show()

```

A. Child

B. Parent

C. None

D. Error

Answer: B — Parent. Child inherits the method.


Q7. What is the output?

```python

class Parent:

    def show(self):

        print("Parent")


class Child(Parent):

    def show(self):

        print("Child")


Child().show()

```

A. Parent

B. Child

C. Parent Child

D. Error

Answer: B — Child. The child overrides the parent method.


Q8. What is the output?

```python

class A:

    def show(self):

        print("A")


class B(A):

    def show(self):

        super().show()

        print("B")


B().show()

```

A. B then A

B. A then B

C. B only

D. Error

Answer: B — A, then B. `super()` calls the next implementation in the method resolution order.


Q9. What is the output?

```python

class A:

    def __init__(self):

        print("A")


class B(A):

    def __init__(self):

        print("B")


B()

```

A. A then B

B. B then A

C. B only

D. A only

Answer: C — B only. The child constructor does not call the parent constructor.


Q10. What is the output?

```python

class A:

    def __init__(self):

        self.x = 10


class B(A):

    def __init__(self):

        super().__init__()

        self.y = 20


obj = B()

print(obj.x + obj.y)

```

A. 10

B. 20

C. 30

D. Error

Answer: C — 30. `super().__init__()` initializes `x`; the child initializes `y`.


Q11. Which inheritance type allows a class to inherit from two parent classes?

A. Single inheritance

B. Multiple inheritance

C. Multilevel inheritance

D. Hierarchical inheritance

Answer: B — Multiple inheritance.


Q12. What is the output?

```python

class A:

    def show(self):

        print("A")


class B:

    def show(self):

        print("B")


class C(A, B):

    pass


C().show()

```

A. A

B. B

C. A B

D. Error

Answer: A — A. Python searches base classes according to the MRO; A comes first.


### Section 3: Polymorphism, Encapsulation & Abstraction

Q13. What is the output?

```python

class Animal:

    def sound(self):

        print("Animal")


class Dog(Animal):

    def sound(self):

        print("Bark")


def make_sound(obj):

    obj.sound()


make_sound(Dog())

```

A. Animal

B. Bark

C. Both

D. Error

Answer: B — Bark. This demonstrates runtime polymorphism.


Q14. What is the output?

```python

class A:

    def show(self):

        return "Hello"


obj = A()

print(obj.show())

```

A. None

B. Hello

C. show

D. Error

Answer: B — Hello. The method returns a string.


Q15. What is the output?

```python

class Employee:

    def __init__(self):

        self.__salary = 50000


e = Employee()

print(e.__salary)

```

A. 50000

B. None

C. AttributeError

D. SyntaxError

Answer: C — AttributeError. Double-underscore attributes undergo name mangling.


Q16. What is the output?

```python

class A:

    def __init__(self):

        self.__x = 10


obj = A()

print(obj._A__x)

```

A. 10

B. None

C. AttributeError

D. SyntaxError

Answer: A — 10. Name mangling changes `__x` to `_A__x`; it is not strict privacy.


Q17. Which module provides support for abstract base classes?

A. os

B. abc

C. sys

D. math

Answer: B — `abc` (Abstract Base Classes).


Q18. What happens when you run this code?

```python

from abc import ABC, abstractmethod


class Shape(ABC):

    @abstractmethod

    def area(self):

        pass


s = Shape()

```

A. Object created successfully

B. Returns None

C. TypeError

D. SyntaxError

Answer: C — TypeError. A class with unimplemented abstract methods cannot be instantiated.


Q19. What is the output?

```python

class A:

    def greet(self):

        print("Hello")


class B(A):

    pass


class C(B):

    pass


C().greet()

```

A. Hello

B. A

C. None

D. Error

Answer: A — Hello. This is multilevel inheritance: A → B → C.


Q20. What is the output?

```python

class A:

    def show(self):

        print("A")


class B(A):

    def show(self):

        print("B")

        super().show()


class C(B):

    def show(self):

        print("C")

        super().show()


C().show()

```

A. A B C

B. C B A

C. C A B

D. C only

Answer: B — C, B, A. Each `super()` call invokes the next method in the MRO.


### Quick Revision – Python OOP


| Concept | Key point |

| ------------- | ---------------------------------------------------------------------- |

| Class | Blueprint for creating objects |

| Object | Instance of a class |

| Inheritance | Reuse parent class functionality |

| Overriding | Child provides its own implementation of an inherited method |

| super() | Accesses the next implementation in the MRO |

| Polymorphism | Same interface, different behavior |

| Encapsulation | Bundling data and methods; name mangling helps avoid accidental access |

| Abstraction | Define required interfaces using abstract classes and methods |

Friday, September 18, 2026

Python Boolean Truthiness & Logical Expressions Quiz

 1. What is the output of the following Python code?

x = [0]

print(bool(x))


A) True

B) False

C) 0

D) [0]

Answer: A) True

(A list containing elements, even 0, is non-empty and evaluates to True.)


2. What is the output of the following expression?

print([] or "Python")


A) []

B) False

C) "Python"

D) True

Answer: C) "Python"

(The or operator returns the first truthy operand, or the last operand if all are falsy.)


3. What does the following snippet print?

a = []

b = [1, 2]

print(a and b)


A) False

B) []

C) [1, 2]

D) None

Answer: B) []

(The and operator short-circuits on the first falsy value and returns the object itself, not a boolean.)


4. What is the evaluated output of this code?

x = [[]]

print(not x)


A) True

B) False

C) [[]]

D) Error

Answer: B) False

(x contains one element—an empty list—so x itself is non-empty and truthy. not x yields False.)


5. Which of the following expressions evaluates to True?

A) bool([])

B) bool("")

C) bool([False])

D) bool(0)

Answer: C) bool([False])

(A list with one element [False] has a length of 1, making it truthy.)


6. What does this code snippet print?

print(not [] == True)


A) True

B) False

C) None

D) SyntaxError

Answer: B) False

(Operator chaining applies here: not ([] == True). Since [] == True evaluates to False, the expression parses as not ([] == True) which produces False due to operator precedence where == binds tighter than not.)


7. What will be printed by the following expression?

print([1] and [] or [2])


A) [1]

B) []

C) [2]

D) True

Answer: C) [2]

([1] and [] evaluates to []. Then [] or [2] evaluates to [2].)


8. What is the output of the following condition?

items = []

result = "Non-empty" if items else "Empty"

print(result)


A) "Non-empty"

B) "Empty"

C) None

D) []

Answer: B) "Empty"

(An empty list evaluates to falsy in a ternary/conditional expression.)


9. What does the following comparison print?

print([] == False)


A) True

B) False

C) None

D) Error

Answer: B) False

(Falsiness does not mean equality. [] is a list, while False is a boolean; they are distinct objects with different types.)


10. What is the output of running this code?

x = []

print(not not x)


A) True

B) False

C) []

D) N

Answer: B) False

(not x evaluates to True, and the second not inverts it back to False.)

Thursday, September 17, 2026

Python Lambda Function Quiz

 Python Lambda Function Quiz


1. What is the output? f = lambda x: x * 2 print(f(5))

   A. 25

   B. 10

   C. 7

   D. 5


2. What is the output? add = lambda a, b: a + b print(add(3, 7))

   A. 10

   B. 4

   C. 37

   D. 21


3. What is the output? square = lambda x: x ** 2 print(square(4))

   A. 16

   B. 6

   C. 8

   D. 12


4. What is the output? nums = [1, 2, 3, 4] result = list(map(lambda x: x * 2, nums)) print(result)

   A. [2, 3, 4, 5]

   B. [1, 2, 3, 4]

   C. [2, 4, 6, 8]

   D. [1, 4, 9, 16]


5. What is the output? nums = [1, 2, 3, 4, 5] result = list(filter(lambda x: x % 2 == 0, nums)) print(result)

   A. [2, 4]

   B. [2, 3, 4]

   C. [1, 2, 3, 4]

   D. [1, 3, 5]


6. What is the output? nums = [3, 1, 2] result = sorted(nums, key=lambda x: x) print(result)

   A. [3, 2, 1]

   B. [2, 1, 3]

   C. [1, 2, 3]

   D. [3, 1, 2]


7. What is the output? f = lambda x: x + 10 print(f(2) * 3)

   A. 36

   B. 16

   C. 26

   D. 6


8. What is the output? multiply = lambda a, b: a * b print(multiply(4, 5))

   A. 9

   B. 45

   C. 1

   D. 20


9. What does a Python lambda expression return when its expression is evaluated?

   A. Always None

   B. Always a Boolean

   C. The evaluated expression result

   D. Always a string


10. What is the output? words = ["cat", "elephant", "dog"] result = sorted(words, key=lambda x: len(x)) print(result)

   A. ['cat', 'dog', 'elephant']

   B. ['cat', 'elephant', 'dog']

   C. ['dog', 'cat', 'elephant']

   D. ['elephant', 'cat', 'dog']

Wednesday, September 16, 2026

Python Lists, `append()`, Nested Lists & Self-Referencing Lists

 ### 🐍 Top 10 Python `append()` Quiz


Topic: Python Lists, `append()`, Nested Lists & Self-Referencing Lists


Test your Python knowledge with these output-based MCQs! 🚀


### Q1. What is the output?


A. 3


B. 4


C. 5


D. Error


### Q2. What is the output?


A. 2


B. 3


C. 4


D. Error


### Q3. What is the output?


A. \[1, 2, 3, 4\]


B. \[1, 2, \[3, 4\]\]


C. \[3, 4, 1, 2\]


D. \[1, 2, 4, 3\]


### Q4. What is the output?


A. 4


B. \[1, 2, 3, 4\]


C. None


D. True


### Q5. What is the output?


A. \[1, 2, 3, 4\]


B. \[\[1, 2\], \[3, 4\]\]


C. \[1, \[2, 3\], 4\]


D. Error


### Q6. What is the output?


A. 3


B. 4


C. 6


D. Error


### Q7. What is the output?


A. 2


B. 3


C. 4


D. 5


### Q8. What is the output?


A. 30


B. 40


C. 50


D. Error


### Q9. What is the output?


A. \[1, 2\]


B. \[3\]


C. \[1, 2, 3\]


D. Error


### Q10. What is the output?


A. True


B. False


C. None


D. Error


### 📌 Key Takeaways


| Method | Behavior |

| ------------------ | ------------------------- |

| append(x) | Adds one element |

| append([x, y]) | Adds a nested list |

| append(a) | Adds a self-reference |

| len(a) | Counts top-level elements |

| a.append(x) return | None |

Python List `insert()` Quiz

 ### 🐍 Top 10 Python List `insert()` Quiz


Test your Python knowledge with these 10 output-based multiple-choice questions.


### Q1. What is the output?


A. \[1, 2, 10, 3\]


B. \[1, 10, 2, 3\]


C. \[10, 1, 2, 3\]


D. \[1, 2, 3, 10\]


### Q2. What is the output?


A. \[5, 6, 7, 100\]


B. \[100, 5, 6, 7\]


C. \[5, 100, 6, 7\]


D. \[100, 7, 6, 5\]


### Q3. What is the output?


A. \[4, 1, 2, 3\]


B. \[1, 2, 4, 3\]


C. \[1, 2, 3, 4\]


D. Error


### Q4. What is the output?


A. \[99, 10, 20, 30\]


B. \[10, 20, 99, 30\]


C. \[10, 20, 30, 99\]


D. \[10, 99, 20, 30\]


### Q5. What is the output?


A. Error


B. \[5, 1, 2\]


C. \[1, 2, 5\]


D. \[1, 5, 2\]


### Q6. What is the output?


A. \[2, 8, 10, 4, 6\]


B. \[2, 8, 4, 10, 6\]


C. \[2, 4, 10, 8, 6\]


D. \[8, 2, 10, 4, 6\]


### Q7. What is the output?


A. \[1, 99, 2, 3\]


B. 99


C. None


D. True


### Q8. What is the output?


A. \[1, 2, 3, 9\]


B. \[9, 1, 2, 3\]


C. \[1, 9, 2, 3\]


D. Error


### Q9. What is the output?


A. \[1, 4, 5, 2, 3\]


B. \[1, \[4, 5\], 2, 3\]


C. \[4, 5, 1, 2, 3\]


D. Error


### Q10. What is the output?


A. \[10, 20, 15, 25, 30, 40\]


B. \[10, 20, 25, 15, 30, 40\]


C. \[10, 15, 20, 25, 30, 40\]


D. \[10, 20, 30, 15, 25, 40\]


### 📌 Key Takeaways


* `insert(index, value)` adds an element at a specific position.


* Existing elements shift to the right.


* `insert()` modifies the original list.


* `insert()` returns `None`.


* Large positive indexes append to the end.


* Negative indexes count from the end, with boundary clamping.


Share your score! How many did you get correct? 🐍

Tuesday, September 15, 2026

Python Lambda Quiz

 Python Lambda Quiz


1. What will be the output? x = 4 f = lambda x: x + 2 print(f(3) + x)

   A. 9

   B. 7

   C. 12

   D. 10


2. What will be the output? square = lambda x: x * x print(square(5))

   A. 25

   B. 20

   C. 10

   D. 15


3. What will be the output? add = lambda a, b: a + b print(add(4, 6))

   A. 24

   B. 10

   C. 2

   D. 46


4. What will be the output? f = lambda x: x * 2 print(f(3) + f(4))

   A. 11

   B. 14

   C. 24

   D. 12


5. What will be the output? f = lambda x: x - 1 print(f(10))

   A. 10

   B. 9

   C. 1

   D. 11

Monday, September 14, 2026

Python sorted() Quiz

 Python sorted() Quiz


1. What is the output? print(sorted([3, 1, 2]))

   A. [3, 2, 1]

   B. [1, 2, 3]

   C. None

   D. [2, 1, 3]


2. What is the output? print(sorted([3, 1, 2], reverse=True))

   A. [2, 3, 1]

   B. [3, 2, 1]

   C. None

   D. [1, 2, 3]


3. What is the output? words = ["banana", "fig", "apple"] print(sorted(words, key=len))

   A. ['fig', 'apple', 'banana']

   B. ['banana', 'apple', 'fig']

   C. ['apple', 'banana', 'fig']

   D. ['fig', 'banana', 'apple']


4. What is the output? nums = [10, 2, 30, 4] print(sorted(nums, key=lambda x: x))

   A. [30, 10, 4, 2]

   B. [4, 2, 10, 30]

   C. [10, 2, 4, 30]

   D. [2, 4, 10, 30]


5. What is the output? names = ["Bob", "alice", "Charlie"] print(sorted(names, key=str.lower))

   A. ['Charlie', 'Bob', 'alice']

   B. ['alice', 'Bob', 'Charlie']

   C. ['alice', 'Charlie', 'Bob']

   D. ['Bob', 'Charlie', 'alice']

______________________________


1. What is the output? nums = [7, 2, 9, 1] print(sorted(nums))

   A. [7, 2, 9, 1]

   B. [1, 2, 7, 9]

   C. [2, 1, 7, 9]

   D. [9, 7, 2, 1]


2. What is the output? nums = [4, 8, 1, 6] print(sorted(nums, reverse=True))

   A. [6, 8, 4, 1]

   B. [8, 6, 4, 1]

   C. [4, 8, 1, 6]

   D. [1, 4, 6, 8]


3. What is the output? words = ["pear", "apple", "banana"] print(sorted(words))

   A. ['banana', 'pear', 'apple']

   B. ['pear', 'banana', 'apple']

   C. ['apple', 'pear', 'banana']

   D. ['apple', 'banana', 'pear']


4. What is the output? words = ["cat", "elephant", "dog"] print(sorted(words, key=len))

   A. ['cat', 'elephant', 'dog']

   B. ['dog', 'cat', 'elephant']

   C. ['elephant', 'cat', 'dog']

   D. ['cat', 'dog', 'elephant']


5. What is the output? words = ["one", "three", "two"] print(sorted(words, key=len, reverse=True))

   A. ['one', 'two', 'three']

   B. ['three', 'one', 'two']

   C. ['two', 'one', 'three']

   D. ['three', 'two', 'one']


6. What is the output? items = [("B", 2), ("A", 3), ("C", 1)] print(sorted(items, key=lambda x: x[1]))

   A. [('C', 1), ('B', 2), ('A', 3)]

   B. [('A', 3), ('B', 2), ('C', 1)]

   C. [('A', 3), ('C', 1), ('B', 2)]

   D. [('B', 2), ('A', 3), ('C', 1)]


7. What is the output? students = [("John", 80), ("Amy", 95), ("Bob", 70)] print(sorted(students, key=lambda x: x[1], reverse=True))

   A. [('John', 80), ('Amy', 95), ('Bob', 70)]

   B. [('Amy', 95), ('John', 80), ('Bob', 70)]

   C. [('Bob', 70), ('John', 80), ('Amy', 95)]

   D. [('Amy', 95), ('Bob', 70), ('John', 80)]


8. What is the output? pairs = [(3, 1), (1, 5), (2, 2)] print(sorted(pairs))

   A. [(1, 5), (3, 1), (2, 2)]

   B. [(2, 2), (1, 5), (3, 1)]

   C. [(1, 5), (2, 2), (3, 1)]

   D. [(3, 1), (2, 2), (1, 5)]


9. What is the output? words = ["apple", "kiwi", "banana", "fig"] print(sorted(words, key=lambda x: (len(x), x)))

   A. ['banana', 'apple', 'kiwi', 'fig']

   B. ['fig', 'kiwi', 'apple', 'banana']

   C. ['fig', 'apple', 'kiwi', 'banana']

   D. ['kiwi', 'fig', 'apple', 'banana']

Python Exception Handling

 Python Exception Handling: try, except, else & finally


1. What is the output? try: print(10 / 0) except ZeroDivisionError: print("Error")

   A. 0

   B. Nothing

   C. Error

   D. 10


2. What is the output? try: x = 10 except: print("Error") else: print("Success")

   A. Error

   B. 10

   C. Success

   D. Nothing


3. What is the output? try: print(5 / 0) except ZeroDivisionError: print("Caught") finally: print("Done")

   A. Caught

   B. Done

   C. Error\nDone

   D. Caught\nDone


4. What is the output? try: print("A") except: print("B") finally: print("C")

   A. C\nA

   B. A

   C. A\nC

   D. B\nC


5. Which block is designed to execute whether or not an exception occurs? try: # risky operation except Exception: # handle error else: # no error finally: # cleanup

   A. except

   B. try

   C. finally

   D. else


6. What is the output? try: int("abc") except ValueError: print("Invalid") else: print("Valid") finally: print("Finished")

   A. Invalid

   B. Invalid\nFinished

   C. Valid\nFinished

   D. Finished\nInvalid


7. What happens to the else block when the try block raises an exception that is handled by except?

   A. It executes before except

   B. It is skipped

   C. It always executes

   D. It executes after finally


8. What is the output? try: x = 10 print(x) except Exception: print("Error") finally: print("Cleanup")

   A. Error\nCleanup

   B. 10\nCleanup

   C. Cleanup\n10

   D. 10


9. What is the main purpose of the else block in Python exception handling?

   A. Run code only when try fails

   B. Run code only when try succeeds

   C. Always clean up resources

   D. Catch every exception


10. What is the output? try: print("Start") raise Exception("Oops") except Exception: print("Handled") finally: print("End")

   A. Handled\nEnd

   B. Start\nEnd

   C. Start\nHandled

   D. Start\nHandled\nEnd

Python find() Quiz

Python find() Quiz


1. What is the output? s = "Python Programming" print(s.find("Pro"))

   A. 6

   B. 8

   C. -1

   D. 7


2. What is the output? s = "banana" print(s.find("na"))

   A. 3

   B. 2

   C. 1

   D. 4


3. What is the output? print("hello world".find("world"))

   A. 6

   B. 5

   C. 7

   D. -1


4. What is the output? print("Python".find("Java"))

   A. None

   B. -1

   C. 1

   D. 0


5. What is the output? s = "programming" print(s.find("g"))

   A. 10

   B. 3

   C. 5

   D. 7


6. What is the output? s = "banana" print(s.find("a", 2))

   A. 5

   B. 3

   C. -1

   D. 1


7. What is the output? s = "banana" print(s.find("a", 4))

   A. -1

   B. 3

   C. 5

   D. 4


8. What is the output? print("mississippi".find("iss"))

   A. 0

   B. 2

   C. 4

   D. 1


9. What is the output? print("PYTHON".find("python"))

   A. 0

   B. 6

   C. 1

   D. -1


10. What is the output? print("hello".find(""))

   A. -1

   B. None

   C. 0

   D. 1


11. What is the output? s = "abcabc" print(s.find("abc", 1))

   A. 1

   B. 3

   C. -1

   D. 0


12. What is the output? s = "abcdef" print(s.find("cd", 0, 4))

   A. -1

   B. 1

   C. 2

   D. 3


13. What is the output? s = "abcdef" print(s.find("ef", 0, 5))

   A. 4

   B. 3

   C. -1

   D. 5


14. What is the output? print("banana".find("ana"))

   A. 2

   B. 3

   C. 0

   D. 1


15. What is the output? s = "Python Python" print(s.find("Python", 1))

   A. 0

   B. -1

   C. 7

   D. 6


16. What is the output? print("developer".find("lop"))

   A. -1

   B. 4

   C. 2

   D. 3


17. What is the output? s = "abracadabra" print(s.find("abra", 1))

   A. 7

   B. 3

   C. -1

   D. 0


18. What is the output? s = "cat cat cat" print(s.find("cat", 4))

   A. 4

   B. 5

   C. 0

   D. 8


19. What is the output? s = "python programming" print(s.find("python"))

   A. 1

   B. -1

   C. 0

   D. 7


20. What is the output? s = "hello hello" print(s.find("hello", 1))

   A. 5

   B. 0

   C. -1

   D. 6

Python Control Flow Quiz

 Python Control Flow Quiz


1. What is the output? x = 10 if x > 5: print("A") else: print("B")

   A. B

   B. No output

   C. Error

   D. A


2. What is the output? x = 15 if x < 10: print("Low") elif x < 20: print("Medium") else: print("High")

   A. Low

   B. B

   C. Medium

   D. High


3. What is the output? for i in range(3): print(i) else: print("Done")

   A. 0 1 2

   B. Done 0 1 2

   C. 0 1 Done

   D. 0 1 2 Done


4. What is the output? for i in range(5): if i == 2: break print(i)

   A. 0 1 2

   B. 0 1 2 3 4

   C. 0 1

   D. 2 3 4


5. What is the output? for i in range(5): if i == 2: continue print(i)

   A. 0 1

   B. 2 3 4

   C. 0 1 2 3 4

   D. 0 1 3 4


6. What is the output? for i in range(3): if i == 1: break else: print("Done")

   A. 0 1

   B. Done

   C. 0 Done

   D. 0


7. What is the output? x = 0 if x: print("True") else: print("False")

   A. True

   B. Error

   C. 0

   D. False


8. What is the output? for i in range(4): if i % 2 == 0: continue print(i)

   A. 2 3

   B. 0 2

   C. 1 3

   D. 0 1 2 3


9. What is the output? for i in range(3): print(i) if i == 1: break else: print("Done")

   A. 1

   B. 0 1

   C. 0

   D. 0 1 Done


10. What is the output? x = 20 if x < 10: print("A") elif x < 20: print("B") elif x == 20: print("C") else: print("D")

   A. C

   B. D

   C. A

   D. B

Python Lists: Mutability Quiz

 Python Lists: Mutability Quiz


1. What is the output of this Python code? x = [1, 2, 3] x[0] = 10 print(x)

   A. [10, 2, 3]

   B. [1, 2, 3]

   C. [10, 1, 2, 3]

   D. TypeError


2. What is the output of this Python code? items = [1, 2] items.append(3) print(items)

   A. [1, 2]

   B. [1, 2, 3]

   C. [3, 1, 2]

   D. [1, 2, [3]]


3. What is the output of this Python code? a = [10, 20] b = a b[1] = 99 print(a)

   A. TypeError

   B. [99, 20]

   C. [10, 99]

   D. [10, 20]


4. Which statement correctly describes Python lists?

   A. Lists cannot contain duplicate values.

   B. Lists are mutable sequences.

   C. Lists can contain only numbers.

   D. Lists are immutable sequences.


5. What is the output of this Python code? numbers = [1, 2, 3] numbers[1] = 20 print(numbers)

   A. [1, 20, 3]

   B. [1, 2, 20, 3]

   C. [1, 2, 3]

   D. [20, 2, 3]

Python split() Method Quiz

Python:

Quiz 1: Custom Delimiters
What will be the output of the following code?

data = "apple,banana,cherry"
print(data.split(","))
  • A) ["apple", "banana", "cherry"]

  • B) "apple" "banana" "cherry"

  • C) ["apple,banana,cherry"]

  • D) Error

Quiz 2: Handling Extra Whitespace What will be the output of the following code?

text = "Hello World Python"
print(text.split())
  • A) ['Hello', '', '', 'World', '', 'Python']

  • B) ['Hello', 'World', 'Python']

  • C) ['Hello World Python']

  • D) Error


Quiz 3: The Opposite of Split What will be the output of the following code?

words = ["Python", "is", "fun"]
print(" ".join(words))
  • A) ["Python is fun"]

  • B) Python, is, fun

  • C) Python is fun

  • D) ['Python', 'is', 'fun']


-------------

1. What is the output of print("Python is easy".split())?
   A. Python is easy
   B. ['Python', 'is', 'easy']
   C. ['Python', 'is easy']
   D. ['Python is easy']

2. What is the output of print("a,b,c".split(","))?
   A. ['a', ',b', ',c']
   B. ['a', 'b', 'c']
   C. ['a', 'b,c']
   D. ['a,b,c']

3. What is the output of print("one two three".split(" ", 1))?
   A. ['one two three']
   B. ['one two', 'three']
   C. ['one', 'two', 'three']
   D. ['one', 'two three']

4. What is the output of print("hello".split("l"))?
   A. ['he', '', 'o']
   B. ['he', 'llo']
   C. ['hel', 'o']
   D. ['hello']

5. What is the output of print(" Python quiz ".split())?
   A. ['Python', 'quiz']
   B. [' Python', 'quiz ']
   C. ['Python', '', 'quiz']
   D. ['', 'Python', '', '', 'quiz', '']

Sunday, September 13, 2026

Python Dictionary Methods Quiz

Python Dictionary Methods Quiz


1. What is the output? d = {"a": 1, "b": 2} print(d.get("a"))

   A. 2

   B. KeyError

   C. None

   D. 1


2. What is the output? d = {"x": 10, "y": 20} print(list(d.keys()))

   A. [10, 20]

   B. [('x', 10), ('y', 20)]

   C. ['x', 'y']

   D. ['x', 10, 'y', 20]


3. What is the output? d = {"a": 5, "b": 7} print(list(d.values()))

   A. ['a', 'b']

   B. [5, 'a', 7, 'b']

   C. [('a', 5), ('b', 7)]

   D. [5, 7]


4. What is the output? d = {"a": 1, "b": 2} print(list(d.items()))

   A. [('a', 1), ('b', 2)]

   B. [1, 2]

   C. ['a', 'b']

   D. {'a': 1, 'b': 2}


5. What is the output? d = {"a": 1} d.update({"b": 2}) print(d)

   A. {'a': 2, 'b': 1}

   B. {'b': 2}

   C. {'a': 1}

   D. {'a': 1, 'b': 2}


6. What is the output? d = {"a": 10, "b": 20} x = d.pop("a") print(x, d)

   A. 20 {'a': 10}

   B. 10 {'a': 10}

   C. 10 {'b': 20}

   D. a {'b': 20}


7. What is the output? d = {"a": 1, "b": 2} x = d.popitem() print(x, d)

   A. ('b', 2) {'a': 1, 'b': 2}

   B. 2 {'a': 1, 'b': 2}

   C. ('a', 1) {'b': 2}

   D. ('b', 2) {'a': 1}


8. What is the output? d = {"a": 1} x = d.setdefault("b", 2) print(x, d)

   A. 1 {'a': 1, 'b': 2}

   B. 2 {'a': 1}

   C. None {'a': 1, 'b': 2}

   D. 2 {'a': 1, 'b': 2}


9. What is the output? d = {"a": 1} x = d.setdefault("a", 99) print(x, d)

   A. 99 {'a': 1}

   B. None {'a': 1}

   C. 99 {'a': 99}

   D. 1 {'a': 1}


10. What is the output? d = {"a": 1, "b": 2} d.clear() print(d)

   A. {'a': 1}

   B. {}

   C. None

   D. {'b': 2}


11. What is the output? d = {"a": 1} c = d.copy() c["b"] = 2 print(d)

   A. {'a': 1}

   B. {'b': 2}

   C. {'a': 1, 'b': 2}

   D. {}


12. What is the output? d = dict.fromkeys(["a", "b"], 0) print(d)

   A. {'a': 1, 'b': 1}

   B. {0: 'a', 0: 'b'}

   C. {'a': None, 'b': None}

   D. {'a': 0, 'b': 0}


13. What is the output? d = {"a": 1} print(d.get("b", 99))

   A. KeyError

   B. 99

   C. None

   D. 1


14. What is the output? d = {"a": 1, "b": 2} d.update({"a": 10}) print(d)

   A. {'a': 10, 'b': 2}

   B. {'a': 10}

   C. {'a': 1, 'b': 10}

   D. {'a': 1, 'b': 2}


15. What is the output? d = {"a": 1, "b": 2} print("a" in d)

   A. True

   B. False

   C. None

   D. 1


16. What is the output? d = {"a": 1, "b": 2} print(d.get("c") is None)

   A. True

   B. 0

   C. KeyError

   D. False


17. What is the output? d = {"a": 1} d.update(b=2) print(d)

   A. {'a': 1, 'b': None}

   B. {'a': 2, 'b': 1}

   C. {'a': 1, 'b': 2}

   D. {'b': 2}


18. What is the output? d = {"a": 1, "b": 2} x = d.pop("b") print(x)

   A. None

   B. 1

   C. 2

   D. b


19. What is the output? d = {"x": 1, "y": 2} print(len(d.keys()))

   A. 2

   B. 1

   C. 0

   D. 3


20. What is the output? d = {"a": 1} d.setdefault("b") print(d)

   A. {'a': 1, 'b': 0}

   B. {'b': None}

   C. {'a': 1}

   D. {'a': 1, 'b': None}

Python List Method Quiz

Python List Method Quiz


1. What is the output? x = [1, 2] x.append(3) print(x)

   A. [3, 1, 2]

   B. [1, 3, 2]

   C. [1, 2]

   D. [1, 2, 3]


2. What is the output? x = [1, 2] x.extend([3, 4]) print(x)

   A. [1, 2]

   B. [3, 4, 1, 2]

   C. [1, 2, [3, 4]]

   D. [1, 2, 3, 4]


3. What is the output? x = [10, 20, 30] x.insert(1, 99) print(x)

   A. [99, 10, 20, 30]

   B. [10, 20, 30, 99]

   C. [10, 20, 99, 30]

   D. [10, 99, 20, 30]


4. What is the output? x = [1, 2, 2, 3] x.remove(2) print(x)

   A. [1, 3]

   B. [1, 2, 2]

   C. [1, 2, 3]

   D. [2, 2, 3]


5. What is the output? x = [10, 20, 30] y = x.pop() print(y, x)

   A. 30 [10, 20, 30]

   B. 10 [20, 30]

   C. 30 [10, 20]

   D. 20 [10, 30]


6. What is the output? x = [1, 2, 3] x.pop(0) print(x)

   A. [2, 3]

   B. [1, 3]

   C. [1, 2, 3]

   D. [1, 2]


7. What is the output? x = [1, 2, 3] x.clear() print(x)

   A. [3]

   B. None

   C. [1]

   D. []


8. What is the output? x = [10, 20, 30, 20] print(x.index(20))

   A. 0

   B. 3

   C. 1

   D. 2


9. What is the output? x = [1, 2, 2, 3, 2] print(x.count(2))

   A. 1

   B. 2

   C. 3

   D. 4


10. What is the output? x = [3, 1, 2] x.sort() print(x)

   A. [3, 2, 1]

   B. [2, 1, 3]

   C. [3, 1, 2]

   D. [1, 2, 3]


11. What is the output? x = [1, 2, 3] x.reverse() print(x)

   A. [1, 3, 2]

   B. [1, 2, 3]

   C. [3, 2, 1]

   D. [2, 3, 1]


12. What is the output? x = [1, 2] y = x.copy() y.append(3) print(x)

   A. [1, 2, 3]

   B. [1, 2]

   C. []

   D. [3, 1, 2]


13. What is the output? x = [1, 2] x += [3, 4] print(x)

   A. [1, 2, [3, 4]]

   B. [1, 2]

   C. [3, 4, 1, 2]

   D. [1, 2, 3, 4]


14. What is the output? x = [1, 2] x.append([3, 4]) print(x)

   A. [[3, 4], 1, 2]

   B. [1, 2, [3, 4]]

   C. [1, 2, 3, 4]

   D. [1, 2]


15. What is the output? x = [5, 10, 15] print(x.pop(1))

   A. 1

   B. 5

   C. 10

   D. 15


16. What is the output? x = [4, 1, 3] y = x.sort() print(y)

   A. 3

   B. [1, 3, 4]

   C. [4, 1, 3]

   D. None


17. What is the output? x = [1, 2, 3] y = x.reverse() print(y)

   A. 3

   B. [3, 2, 1]

   C. None

   D. [1, 2, 3]


18. What is the output? x = [1, 2, 3] x.insert(0, 9) print(x)

   A. [9, 1, 2, 3]

   B. [1, 2, 9, 3]

   C. [1, 2, 3, 9]

   D. [1, 9, 2, 3]


19. What is the output? x = [1, 2, 3] x.remove(2) print(x)

   A. [1, 2]

   B. [2, 3]

   C. [1, 2, 3]

   D. [1, 3]


20. What is the output? x = [3, 1, 2] x.sort(reverse=True) print(x)

   A. [3, 1, 2]

   B. [2, 3, 1]

   C. [3, 2, 1]

   D. [1, 2, 3]

Saturday, September 12, 2026

Python String Format Quizzes

Python String Format Quizzes


1. What is the output? print("python".upper())

   A. PYTHON

   B. python

   C. Python

   D. pYTHON


2. What is the output? print("PYTHON".lower())

   A. pYthon

   B. python

   C. PYTHON

   D. Python


3. What is the output? print("hello world".title())

   A. Hello world

   B. HELLO WORLD

   C. hello world

   D. Hello World


4. What is the output? print("hello".capitalize())

   A. hello

   B. HELLO

   C. hELLO

   D. Hello


5. What is the output? print("PyThOn".swapcase())

   A. python

   B. PyThOn

   C. PYTHON

   D. pYtHoN


6. What is the output? print("banana".find("na"))

   A. 2

   B. 4

   C. 1

   D. 3


7. What is the output? print("banana".count("a"))

   A. 3

   B. 4

   C. 1

   D. 2


8. What is the output? print("python".startswith("py"))

   A. False

   B. 0

   C. None

   D. True


9. What is the output? print("report.pdf".endswith(".pdf"))

   A. None

   B. False

   C. True

   D. pdf


10. What is the output? print("hello world".replace("world", "Python"))

   A. helloPython

   B. hello Python

   C. hello world

   D. Python world


11. What is the output? print("a,b,c".split(","))

   A. ('a', 'b', 'c')

   B. a b c

   C. ['a', 'b', 'c']

   D. ['a,b,c']


12. What is the output? print("-".join(["2026", "09", "12"]))

   A. 2026/09/12

   B. 20260912

   C. 2026-09-12

   D. ['2026-09-12']


13. What is the output? print("Python123".isalnum())

   A. True

   B. False

   C. Python

   D. 123


14. What is the output? print("12345".isdigit())

   A. True

   B. 5

   C. 12345

   D. False


15. What is the output? print("abc".center(7, "-"))

   A. abc----

   B. abc

   C. ---abc-

   D. --abc--


16. What is the output? print("42".zfill(5))

   A. 00042

   B. 42000

   C. 0042

   D. 42.000


17. What is the output? print("abc123".isalpha())

   A. False

   B. abc

   C. 123

   D. True


18. What is the output? print("Hello".find("x"))

   A. -1

   B. None

   C. 0

   D. 1


19. What is the output? print("hello\tworld".expandtabs(4))

   A. hello\tworld

   B. helloworld

   C. hello world

   D. hello world

Friday, September 11, 2026

Python Slicing

 Python Slicing:


1. What is the output? s = "Python" print(s[0:3])

   A. 'Pyt'

   B. 'Pyth'

   C. 'Python'

   D. 'yth'


2. What is the output? s = "Python" print(s[2:5])

   A. 'yth'

   B. 'hon'

   C. 'thon'

   D. 'tho'


3. What is the output? nums = [10, 20, 30, 40, 50] print(nums[-3:])

   A. [40, 50]

   B. [30, 40]

   C. [20, 30, 40]

   D. [30, 40, 50]


4. What is the output? s = "abcdef" print(s[::2])

   A. 'ace'

   B. 'fed'

   C. 'abcd'

   D. 'bdf'


5. What is the output? s = "Python" print(s[::-1])

   A. 'nothP'

   B. 'Python'

   C. 'Pnohty'

   D. 'nohtyP'


6. What is the output? nums = [0, 1, 2, 3, 4, 5] print(nums[1:5:2])

   A. [0, 2, 4]

   B. [2, 4]

   C. [1, 3]

   D. [1, 3, 5]


7. What is the output? s = "Python" print(s[:4])

   A. 'ytho'

   B. 'Pytho'

   C. 'Python'

   D. 'Pyth'


8. What is the output? s = "Python" print(s[2:])

   A. 'tho'

   B. 'Python'

   C. 'thon'

   D. 'ython'


9. What is the output? s = "abcdef" print(s[1:6:2])

   A. 'bcdef'

   B. 'ace'

   C. 'bde'

   D. 'bdf'


10. What is the output? s = "abcdef" print(s[-1])

   A. 'f'

   B. 'e'

   C. 'd'

   D. 'a'


11. What is the output? s = "Python" print(s[-4:-1])

   A. 'yth'

   B. 'hon'

   C. 'tho'

   D. 'ytho'


12. What is the output? nums = [1, 2, 3, 4, 5, 6] print(nums[::3])

   A. [1, 4]

   B. [2, 5]

   C. [1, 4, 6]

   D. [1, 3, 5]


13. What is the output? s = "abcdef" print(s[5:1:-1])

   A. 'fedc'

   B. 'edcb'

   C. 'fed'

   D. 'fecd'


14. What is the output? s = "abcdef" print(s[::-2])

   A. 'fdb'

   B. 'bdf'

   C. 'eca'

   D. 'fed'


15. What is the output? s = "Python" print(s[1:5:3])

   A. 'yth'

   B. 'yt'

   C. 'to'

   D. 'yo'


16. What is the output? s = "Python" print(s[::])

   A. 'ython'

   B. 'Pyth'

   C. 'Python'

   D. 'nohtyP'


17. What is the output? nums = [10, 20, 30, 40, 50] print(nums[1:-1])

   A. [30, 40]

   B. [20, 30, 40]

   C. [20, 30, 40, 50]

   D. [10, 20, 30, 40]


18. What is the output? s = "abcdef" print(s[4:1:-1])

   A. 'fed'

   B. 'cde'

   C. 'edc'

   D. 'edcb'


19. What is the output? s = "abcdefgh" print(s[2:7:2])

   A. 'bdf'

   B. 'ceg'

   C. 'cdefg'

   D. 'ce'


20. What is the output? s = "Python" print(s[-5:-2])

   A. 'tho'

   B. 'yth'

   C. 'Pyt'

   D. 'ytho'

Python Modulus (%) Mastery Quiz

Python Modulus (%) Mastery Quiz

1. What is the output of the following Python snippet?

x = 17 % 5

print(x)


 * Options: A) 2, B) 3, C) 5, D) 1

 * Answer: A) 2

 * Explanation: 17 divided by 5 is 3 with a remainder of 2 (since 5 × 3 = 15, and 17 - 15 = 2).

2. What is the output of the following Python snippet?

y = 25 % 4

print(y)


 * Options: A) 0, B) 1, C) 2, D) 3

 * Answer: B) 1

 * Explanation: 25 divided by 4 is 6 with a remainder of 1 (since 4 × 6 = 24, and 25 - 24 = 1).

3. What is the output of the following Python snippet?

a = 17 % 5

b = 25 % 4

print(a + b)


 * Options: A) 5, B) 6, C) 3, D) 7

 * Answer: C) 3

 * Explanation: Variable a evaluates to 2 (17 \pmod 5) and variable b evaluates to 1 (25 \pmod 4). Adding them gives 2 + 1 = 3.

4. What is the output of the following Python snippet?

print(10 % 3)


 * Options: A) 0, B) 1, C) 2, D) 3

 * Answer: B) 1

 * Explanation: 10 divided by 3 leaves a remainder of 1 (3 \times 3 = 9, 10 - 9 = 1).

5. What is the output of the following Python snippet?

print(8 % 8)


 * Options: A) 8, B) 1, C) 0, D) 4

 * Answer: C) 0

 * Explanation: When a number is divided evenly by itself, there is no remainder, resulting in 0.

6. What is the output of the following Python snippet?

print(3 % 7)


 * Options: A) 0, B) 3, C) 4, D) 7

 * Answer: B) 3

 * Explanation: When the left operand is smaller than the right operand, the result of the modulo operation is simply the left operand itself.

7. What is the output of the following Python snippet?

print(-17 % 5)


 * Options: A) -2, B) 2, C) 3, D) -3

 * Answer: C) 3

 * Explanation: In Python, the modulo result always takes the sign of the divisor. Since the divisor is positive (5), -17 \pmod 5 yields 3 (5 \times -4 = -20, and -17 - (-20) = 3).

8. What is the output of the following Python snippet?

print(14 % 3 * 2)


 * Options: A) 4, B) 8, C) 6, D) 2

 * Answer: A) 4

 * Explanation: Operators % and * have the same precedence and associate from left to right. First, 14 \pmod 3 = 2, then 2 \times 2 = 4.

9. What is the output of the following Python snippet?

print(20 // 6)


 * Options: A) 3.33, B) 3, C) 4, D) 2

 * Answer: B) 3

 * Explanation: Floor division (//) divides the operands and rounds down to the nearest whole integer. 20 / 6 = 3.333\dots, rounded down to 3.

10. What is the output of the following Python snippet?

print(15 % 4 == 3)


 * Options: A) True, B) False, C) None, D) Error

 * Answer: A) True

 * Explanation: 15 \pmo

d 4 evaluates to 3, and checking equality 3 == 3 results in True.

---------------

**Q1. What does `15 % 4` evaluate to?**

A) 3 ✅

B) 1

C) 4

D) 0

*Answer: A — 4×3=12, 15-12=3*


**Q2. What is the result of `print(20 % 5)`?**

A) 4

B) 1

C) 0 ✅

D) 5

*Answer: C — 5×4=20 exactly, no remainder*


**Q3. Which operator gives the whole-number result of division (ignoring remainder)?**

A) `%`

B) `//` ✅

C) `/`

D) `**`

*Answer: B — floor division drops the remainder*


**Q4. What does `print(17 // 5)` output?**

A) 2

B) 3 ✅

C) 3.4

D) 2.5

*Answer: B — 5 fits into 17 three full times*


**Q5. A number is even if which expression equals 0?**

A) `n // 2`

B) `n % 2` ✅

C) `n / 2`

D) `n ** 2`

*Answer: B — classic even/odd



Python String Methods — Output Quiz

 Python String Methods — Output Quiz


1. What is the output? s = "PyThOn" print(s.swapcase())

   A. PYTHON

   B. python

   C. pYtHoN

   D. PyThOn


2. What is the output? s = "banana" print(s.find("an"), s.rfind("an"))

   A. 1 4

   B. 1 3

   C. 0 3

   D. 2 4


3. What is the output? s = "Python Python" print(s.replace("Python", "Java", 1))

   A. Python Python

   B. Java Python

   C. Python Java

   D. Java Java


4. What is the output? s = "one two three" print(s.split(" ")[1])

   A. one

   B. three

   C. two

   D. two three


5. What is the output? print("abc".isalpha(), "123".isdigit(), "abc123".isalnum())

   A. True True True

   B. True False True

   C. False True False

   D. True True False


6. What is the output? print("Hello".islower(), "HELLO".isupper(), " ".isspace())

   A. False False True

   B. True True False

   C. True False False

   D. False True True


7. What is the output? print("hi".center(6, "-"))

   A. -hi---

   B. hi----

   C. ---hi-

   D. --hi--


8. What is the output? print("42".zfill(5), "7".rjust(3, "0"))

   A. 42000 700

   B. 00042 007

   C. 00042 700

   D. 42 7


9. What is the output? s = "a-b-c" print(s.partition("-")) print(s.rpartition("-"))

   A. ('a', '-', 'b-c') and ('a-b', '-', 'c')

   B. ['a', '-', 'b-c'] and ['a-b', '-', 'c']

   C. ('a-b', '-', 'c') and ('a', '-', 'b-c')

   D. ('a', 'b', 'c') and ('a', 'b', 'c')

Python String Methods

 Python String Methods Quiz

1. What is the output? s = "Hello World" print(s.lower())

   A. Hello World

   B. hello world

   C. Hello world

   D. HELLO WORLD


2. What is the output? s = "banana" print(s.find("na"), s.rfind("na"))

   A. 2 4

   B. 1 4

   C. 2 3

   D. 1 3


3. What is the output? s = "hello" print(s.count("l"))

   A. 1

   B. 2

   C. 0

   D. 3


4. What is the output? s = " Python " print(s.strip())

   A. 'Python '

   B. 'Python'

   C. ' Python'

   D. ' Python '


5. What is the output? s = "a,b,c" print(s.split(","))

   A. a-b-c

   B. ['a,b,c']

   C. ['a', 'b', 'c']

   D. ['a', 'bc']


6. What is the output? words = ["Python", "is", "fun"] print("-".join(words))

   A. ['Python-is-fun']

   B. Python is fun

   C. Python-is-fun

   D. Python--is--fun


7. What is the output? s = "abc123" print(s.isalnum(), s.isalpha(), s.isdigit())

   A. False True False

   B. True True True

   C. True False False

   D. False False True


8. What is the output? s = "Python" print(s.startswith("Py"), s.endswith("on"))

   A. True True

   B. True False

   C. False False

   D. False True


9. What is the output? s = "hello world" print(s.title(), s.capitalize())

   A. HELLO WORLD Hello world

   B. Hello world Hello World

   C. hello world Hello World

   D. Hello World Hello world


10. What is the output? s = "42" print(s.zfill(5))

   A. 42

   B. 000042

   C. 42000

   D. 00042