Python Quiz: 50 Questions

Q1. Which function prints text to the console?
Built-in function used in examples like Hello World.
Answer: print() — prints values to standard output; accepts multiple arguments and sep/end parameters.
Q2. Which file is commonly used to run Python code interactively in the browser?
Cloud notebook often used for data and AI demos.
Answer: Google Colab — a free cloud notebook environment for running Python in the browser with GPU support.
Q3. What keyword starts a conditional branch?
Used with a boolean expression and a colon.
Answer: if — `if condition:` begins a conditional block in Python.
Q4. Fill: Read a line of input into variable name.
name =
Built-in function to read from stdin.
Answer: input — `name = input()` reads a string from the user.
Q5. Which data type represents whole numbers?
Used for counting and indexing.
Answer: int — integer type for whole numbers (positive, negative, zero).
Q6. Which operator checks equality between two values?
Used in conditions like if x == y.
Answer: == — equality operator compares values; `is` checks identity.
Q7. Which loop iterates a fixed number of times using range?
Syntax: for i in range(n):
Answer: for — `for i in range(5):` iterates i from 0 to 4.
Q8. Fill: Reverse string s using slicing.
s = "abc"
rev = s[]
Use negative step in slice.
Answer: ::-1 — `s[::-1]` returns the reversed string.
Q9. Which method adds an element to the end of a list?
Common list method: nums.append(x).
Answer: append() — adds a single element to the end of the list.
Q10. Which type holds True or False?
Boolean type in Python.
Answer: bool — values True and False are of type bool.
Q11. Which built-in converts a value to string?
Used for printing non-string values.
Answer: str() — converts numbers, booleans, etc., to their string representation.
Q12. Which expression creates a set from list lst?
Removes duplicates.
Answer: set(lst) — constructs a set of unique elements from the iterable.
Q13. Fill: Access dictionary value for key "name".
student = {"name": "A", "age": 20}
print(student[])
Keys are strings in quotes.
Answer: "name" — `student["name"]` returns "A".
Q14. Which method returns list of dictionary keys?
Used in loops: for k in d.keys():
Answer: keys() — returns a view of the dictionary's keys.
Q15. Which operator checks membership in a collection?
Syntax: if x in my_list:
Answer: in — membership operator used with lists, sets, tuples, strings, etc.
Q16. Which method splits a string into words?
Default splits on whitespace.
Answer: split() — `s.split()` returns a list of substrings split by whitespace or a given separator.
Q17. Which method converts all characters to uppercase?
String method: s.upper().
Answer: upper() — returns a new string with all characters converted to uppercase.
Q18. Which built-in returns the type of a value?
Used for debugging and checks.
Answer: type() — returns the object's type, e.g., type(3) is int.
Q19. Which statement creates a tuple?
Immutable ordered sequence.
Answer: (1, 2, 3) — parentheses create a tuple; lists use square brackets.
Q20. Which keyword creates an empty set literal?
{} creates an empty dict, not a set.
Answer: set() — constructs an empty set; `{}` creates an empty dict.
Q21. Which statement removes and returns the last list item?
pop returns the removed element.
Answer: pop() — removes and returns the last item by default or the item at given index.
Q22. Which operator concatenates strings?
Use + between two strings.
Answer: + — `s1 + s2` concatenates strings; `join()` is used to join iterable of strings.
Q23. Which function returns an iterator from an iterable?
Used with next() to get successive items.
Answer: iter() — returns an iterator object from an iterable.
Q24. Which built-in returns successive pairs (index, value)?
Commonly used in loops: for i, v in enumerate(lst):
Answer: enumerate() — yields (index, value) pairs from an iterable.
Q25. Which function pairs elements from multiple iterables?
Stops at shortest iterable by default.
Answer: zip() — aggregates elements from each iterable into tuples.
Q26. Which module provides mathematical functions like sqrt?
Import with import math.
Answer: math — provides sqrt, sin, cos, pi, etc.; cmath is for complex numbers.
Q27. Which statement creates a list comprehension doubling numbers?
Comprehension returns a list.
Answer: [x*2 for x in nums] — list comprehension producing a new list with doubled values.
Q28. Which keyword defines a function?
Syntax: def foo():
Answer: def — used to define named functions; lambda creates anonymous functions.
Q29. Which expression creates an anonymous function that adds 1?
Used inline with map/filter.
Answer: lambda x: x + 1 — creates a small anonymous function returning x+1.
Q30. Which statement opens a file for reading?
Mode 'r' is default for reading.
Answer: open('file.txt', 'r') — opens file for reading; use with `with` for safe handling.
Q31. Which module provides JSON encoding/decoding?
Use json.dumps and json.loads.
Answer: json — standard library module for JSON serialization and parsing.
Q32. Which keyword raises an exception?
Used to signal errors programmatically.
Answer: raise — `raise ValueError("msg")` throws an exception in Python.
Q33. Which block handles exceptions?
Used with try: ... except Exception as e:
Answer: except — handles exceptions raised in the try block; finally runs regardless.
Q34. Which built-in returns the next item from an iterator?
Used with iterators returned by iter().
Answer: next() — returns the next item or raises StopIteration when exhausted.
Q35. Which statement creates a generator expression?
Parentheses produce a lazy iterator.
Answer: (x*x for x in range(5)) — generator expression yields values lazily.
Q36. Which module provides tools for regular expressions?
Use re.search, re.match, re.sub.
Answer: re — standard module for regular expressions in Python.
Q37. Which statement creates a virtual environment using venv?
Use -m to run module as script.
Answer: python -m venv env — creates a virtual environment directory named env.
Q38. Which built-in sorts a list in place?
sorted() returns a new list; sort() modifies original.
Answer: list.sort() — sorts the list in place and returns None; use sorted() to get a new sorted list.
Q39. Which statement checks if a string s is a palindrome?
Slicing with step -1 reverses string.
Answer: s == s[::-1] — compares string to its reverse; `''.join(reversed(s))` also works but is longer.
Q40. Which module helps with HTTP requests (third-party)?
Popular third-party library installed via pip.
Answer: requests — easy-to-use HTTP library for GET/POST and more (pip install requests).
Q41. Which statement creates a dictionary from two lists keys and vals?
zip pairs elements; dict builds mapping.
Answer: dict(zip(keys, vals)) — pairs keys and values into a dictionary.
Q42. Which built-in returns the absolute value?
Works for ints and floats.
Answer: abs() — returns the absolute value of a number.
Q43. Which statement creates a virtual environment activation on Windows (cmd)?
Windows uses Scripts folder.
Answer: env\Scripts\activate — activates the venv on Windows cmd; use `source env/bin/activate` on Unix shells.
Q44. Which built-in sorts and returns a new sorted list?
sorted(list) returns a new list.
Answer: sorted() — returns a new sorted list from any iterable.
Q45. Which operator is used for integer division (floor) in Python?
Returns floor of division result.
Answer: // — floor division operator; `/` returns float division result.
Q46. Which statement checks object identity (same object)?
Checks whether two references point to same object.
Answer: is — identity operator; `==` checks value equality.
Q47. Which standard library module helps with CSV files?
Use csv.reader and csv.writer.
Answer: csv — standard module for reading/writing CSV files; pandas offers higher-level tools.
Q48. Which built-in returns the maximum of an iterable?
max([1,2,3]) returns 3.
Answer: max() — returns the largest item in an iterable or the largest of two or more arguments.
Q49. Which statement creates a virtual environment activation on Unix/macOS?
Use source with the bin/activate script.
Answer: source env/bin/activate — activates the virtual environment in Unix-like shells.
Q50. Which practice helps avoid mutable default argument bugs?
Default evaluated once at function definition time.
Answer: Use None and create inside function — e.g., `def f(x=None): x = [] if x is None else x` avoids shared mutable defaults.
Your Score: 0 / 50

Post a Comment

0 Comments