Python Quiz

Questions: 16 · 10 minutes
1. What is printed by this code? class Counter: def __init__(self, start=0): self.value = start def increment(self): self.value += 1 a = Counter(2) b = Counter() a.increment() print(a.value, b.value)
2 0
3 1
2 1
3 0
2. What is assigned to ordered? Python's sort is stable, so equal-length words retain their original relative order. words = [""pear"", ""fig"", ""kiwi"", ""plum""] ordered = sorted(words, key=len)
[""pear"", ""kiwi"", ""plum"", ""fig""]
[""fig"", ""kiwi"", ""pear"", ""plum""]
[""fig"", ""pear"", ""kiwi"", ""plum""]
[""plum"", ""kiwi"", ""pear"", ""fig""]
3. A program must generate one million squared numbers and process them one at a time without first storing every result. Which expression is most suitable?
A generator expression: (n * n for n in range(1_000_000))
A set comprehension: {n * n for n in range(1_000_000)}
A list comprehension: [n * n for n in range(1_000_000)]
An eagerly constructed tuple: tuple(n * n for n in range(1_000_000))
4. What does this expression print? print(-7 // 3)
The floating-point quotient -2.3333333333333335
The floor-divided integer -3
The integer -2 after truncation toward zero
The positive integer 3
5. What is printed by this code? def add_item(item, bucket=None): if bucket is None: bucket = [] bucket.append(item) return bucket print(add_item(""a""), add_item(""b""))
['a', 'b'] ['a', 'b']
['a'] ['b']
['b'] ['a']
['a'] ['a', 'b']
6. Which statement correctly describes the usual difference between Python's == and is operators?
#ИМЯ?
#ИМЯ?
#ИМЯ?
#ИМЯ?
7. What output is produced by this code? def check(): print(""checked"") return True result = False and check() print(result)
It prints `checked` and then `False`.
It prints only `False`.
It prints only `True`.
It prints `checked` and then `True`.
8. What is printed by this slice operation? text = ""python"" print(text[1:5:2])
yto
yh
pto
yth
9. What value is assigned to pairs? names = [""Ada"", ""Linus"", ""Guido""] scores = [9, 8] pairs = list(zip(names, scores))
[(""Ada"", 9), (""Linus"", 8)]
[(""Ada"", 9), (""Linus"", 8), (""Guido"", 9)]
[(""Ada"", ""Linus""), (9, 8)]
[(""Ada"", 9), (""Linus"", 8), (""Guido"", None)]
10. What is printed when this code runs? try: value = int(""3.5"") except ValueError: value = 0 print(value)
3
Nothing, because the unhandled error stops the program.
3.5
0
11. What value is assigned to result? nums = [1, 2, 3, 4, 5] result = [n * n for n in nums if n % 2 == 0]
[1, 9, 25]
[2, 4]
[4, 16]
[1, 4, 9, 16, 25]
12. What is a primary benefit of opening a file with a with statement, as in `with open(""notes.txt"") as file:`?
The file is closed automatically when the block ends, including when many exceptions occur.
The entire file is automatically converted into a list.
The file becomes writable even if it was opened in read mode.
The file contents remain permanently cached after the block ends.
13. What is printed by this code? x = 10 def change(): x = 3 return x change() print(x)
An UnboundLocalError occurs.
3
None
10
14. You need to remove duplicates from values while preserving the order of each value's first appearance. Which expression does that? values = [""b"", ""a"", ""b"", ""c"", ""a""]
list(set(values))
sorted(set(values))
list(dict.fromkeys(sorted(values)))
list(dict.fromkeys(values))
15. What is printed by this code? items = [1, 2] alias = items alias.append(3) print(items)
A list containing only 3: [3]
The original two-item list: [1, 2]
The updated list: [1, 2, 3]
An error occurs because alias is not a list.
16. What is printed after this loop counts the letters? counts = {} for ch in ""aba"": counts[ch] = counts.get(ch, 0) + 1 print(counts[""a""], counts[""b""])
1 1
2 2
2 1
3 1
Popular tests
Narcissistic Personality Inventory (NPI)
This self-report measure is used to assess narcissism as a personality trai…
Start Test
Yale-Brown Obsessive Compulsive Scale (Y-BOCS)
This measure is used to rapidly quantify the current severity of obsessive…
Start Test
CRAFFT Screening Test (CRAFFT 2.1)
This brief screening measure is designed to identify potential alcohol and…
Start Test
Patient Health Questionnaire-9 (PHQ-9)
This measure is commonly used to quickly screen for the presence and severi…
Start Test
Maslach Burnout Inventory (MBI)
This self-report measure is used to assess occupational burnout symptoms in…
Start Test
Adolescent Anxiety Questionnaire
This measure is designed to support a brief appraisal of anxiety symptoms a…
Start Test
Emotional Creativity Inventory (ECI)
This self-report measure assesses individual differences in the originality…
Start Test
Horne–Ostberg Morningness–Eveningness Questionnaire (MEQ)
Circadian preferences influence typical patterns of alertness and sleep tim…
Start Test
Ambivalent Sexism Inventory (ASI)
This measure is designed to assess attitudes toward women, including both o…
Start Test
Internalized Misogyny Scale (IMS)
This measure is designed to assess internalized negative beliefs and stereo…
Start Test
Perceived Stress Scale (PSS-10)
This self-report measure assesses the degree to which individuals appraise…
Start Test
Impulsive Behavior Scale (SUPPS-P)
Impulsivity is a multidimensional construct that is often assessed with bri…
Start Test
Clinical Institute Withdrawal Assessment for Alcohol, Revised (CIWA-Ar)
This rating scale is used to rapidly assess the severity of alcohol withdra…
Start Test
Positive and Negative Affect Schedule (PANAS)
This measure provides a brief self-report assessment of current or typical…
Start Test
Light Triad Scale (LTS)
This self-report measure assesses prosocial personality tendencies and orie…
Start Test
Suicidal Ideation Scale
In clinical settings, the Suicidal Ideation Scale is used to structure an i…
Start Test
Body Dysmorphic Disorder Scale (BDD-D)
This brief self-report measure is designed to screen for and quantify distr…
Start Test
Beck Anxiety Inventory (BAI)
This measure is a brief self-report inventory used to screen for anxiety sy…
Start Test
Differential Test of Perfectionism
This instrument is used to screen for perfectionism-related attitudes and t…
Start Test
Locus of Control Scale
This measure assesses generalized expectancies regarding the degree to whic…
Start Test
New Apathy Scale
This brief self-report measure is used to screen for apathy-related symptom…
Start Test
Perth Alexithymia Questionnaire (PAQ)
This measure assesses individual differences in alexithymia, including diff…
Start Test
Social Intelligence Scale
This brief self-report measure is designed to support rapid screening of in…
Start Test
Fear Test
This measure is designed to evaluate individual differences in fear-related…
Start Test
Neuroticism Level Scale
The measure is intended for brief screening of an individual’s propensity t…
Start Test
Aggressiveness Indicators Screening Questionnaire
This screening tool is designed to quickly identify behavioral indicators a…
Start Test