Python Programming — C20 DCME Fifth Semester (Board Exam Scope): 18 MCQs with answers
All 18 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 7 questions
Q1. In Python, what is a variable?
BeginnerShow answer & explanation
A name that refers to a value in memory
A Python variable is a name bound to a value in memory. No type declaration is required, and the name can later refer to a value of a different type.
Q2. Which statement about Python's typing is correct?
BeginnerShow answer & explanation
Types are determined at runtime, not declared in advance
Python uses dynamic typing: the type of a value is determined at runtime, so the same name can later refer to a value of a different type.
Q3. A student writes `x = 10` and then `x = "hello"` in the same program. What happens?
BeginnerShow answer & explanation
It runs fine; `x` now refers to a string
Because of dynamic typing, reassigning `x` to a string is perfectly legal. The name simply points to a new value.
Q4. Which operator assigns a value rather than comparing two values?
BeginnerShow answer & explanation
`=`
`=` is the assignment operator. `==` compares two values for equality, and `!=` and `is` are also comparison-style operators.
Q5. Which family does the `in` operator belong to?
BeginnerShow answer & explanation
Membership
`in` and `not in` are membership operators; they check whether a value is present in a sequence or collection.
Q6. What sequence does `range(5)` generate?
BeginnerShow answer & explanation
0, 1, 2, 3, 4
`range(5)` starts at 0 and stops before 5, producing 0, 1, 2, 3, 4.
Q7. Python uses dynamic typing, so variable types are determined at runtime rather than declared in advance.
BeginnerShow answer & explanation
True
Python uses dynamic typing, so variable types are determined at runtime rather than declared in advance.
Building up · 6 questions
Q8. Which loop is best when you must repeat a block until a condition becomes false?
IntermediateShow answer & explanation
A `while` loop
A `while` loop repeats as long as its condition stays true, which suits condition-driven repetition. A `for` loop is meant for iterating over a sequence.
Q9. Inside a loop, you want to skip even numbers and keep processing the rest. Which statement fits?
IntermediateShow answer & explanation
`continue`
`continue` skips the rest of the current iteration and moves to the next one, so even numbers are skipped while the loop keeps running.
Q10. What does `*args` collect inside a function?
IntermediateShow answer & explanation
Extra positional arguments, as a tuple
`*args` gathers extra positional arguments into a tuple. It is `**kwargs` that gathers extra keyword arguments into a dictionary.
Q11. Which pair of collections is ordered and allows duplicates?
IntermediateShow answer & explanation
Lists and tuples
Lists and tuples are both ordered and allow duplicate values. Sets are unordered with no duplicates, and dictionary keys must be unique.
Q12. A list `a` exists. Which line creates an independent copy so that changing `b` leaves `a` untouched?
IntermediateShow answer & explanation
`b = a[:]`
`b = a[:]` (or `b = list(a)`) makes an independent copy. `b = a` only creates a second name for the same list, so changes show up in both.
Q13. Which statement about strings is correct?
IntermediateShow answer & explanation
Strings are immutable sequences of characters
A string is an immutable sequence of characters. Methods like `upper()` return new strings rather than changing the original.
Topper level · 5 questions
Q14. Which statement about `break` and `continue` is correct?
AdvancedShow answer & explanation
`break` exits the loop; `continue` skips to the next iteration
`break` leaves the loop entirely, while `continue` abandons only the current iteration and proceeds to the next one.
Q15. A program must respond gracefully when a user enters text where a number was expected. Which structure should it use?
AdvancedShow answer & explanation
`try` with `except` and `finally`
Exception handling with `try`, `except`, and `finally` lets the program catch the runtime error and continue instead of crashing. `assert` only raises an error; it does not handle one.
Q16. Which statement about data hiding in Python is correct?
AdvancedShow answer & explanation
A leading underscore signals restricted access by convention
Python uses naming conventions: `_name` signals internal use and `__name` triggers name mangling. The restriction is by convention and mangling, not a hard access block.
Q17. You are tasked with writing a function that takes a list of integers and returns a new list containing only the odd integers, while also handling any potential errors gracefully. Which implementation correctly achieves this?
AdvancedShow answer & explanation
```python def filter_odds(numbers): odds = [] for n in numbers: if n % 2 == 0: continue odds.append(n) return odds ```
The correct option uses a `for` loop with a `continue` statement to skip even numbers and append only odd integers to the list. This effectively filters out the even numbers.
Q18. Given the following class definition, what will be the output of the code snippet when executed? ```python class BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = BankAccount(100) account.deposit(50) print(account.get_balance()) ```
AdvancedShow answer & explanation
150
The output is 150 because the `deposit` method successfully adds 50 to the initial balance of 100, demonstrating correct encapsulation and method functionality in the class.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages