Tuples are immutable, while lists, dicts, and sets can be changed after creation.
ข้อ 2
What does len("hello") return?
The string "hello" contains five characters, so len() returns 5.
ข้อ 3
Which operator checks whether two values are equal?
Python uses == to compare values for equality and = to assign a value.
ข้อ 4
What is the result of 2 ** 3?
The ** operator performs exponentiation, so 2 ** 3 means 2 multiplied by itself 3 times: 2 x 2 x 2 = 8. Options 6 and 9 come from multiplication or squaring, so option b is correct.
ข้อ 5
Which method removes the last item from a list and returns it?
list.pop() removes and returns the last item by default.
ข้อ 6
What does print(type([])) output?
An empty pair of square brackets is list literal syntax, so type([]) returns the list class. Tuples use parentheses, dictionaries use braces with colons, and arrays require a separate module.
ข้อ 7
Which keyword defines a function?
The def keyword begins a function definition followed by the function name and parameters. Option function is not a Python keyword, and lambda creates an anonymous expression rather than a named function.
ข้อ 8
Which values does list(range(3)) produce?
range(3) starts at 0 and stops before 3, producing [0, 1, 2].
ข้อ 9
Which built-in function converts a string to an integer?
int("42") converts the string "42" to the integer 42.
ข้อ 10
What is the result of "a" + "b"?
The + operator concatenates two strings into one new string, so "a" + "b" produces "ab". Option a is correct; "a + b" would require inserting a plus sign as literal text, and Python does not raise an error here.
ข้อ 11
Which collection stores only unique values?
A set stores only unique values and automatically removes duplicates, while lists and tuples keep every item in order and dictionaries store key-value pairs. Therefore option b is correct.
ข้อ 12
What is the result of 5 // 2?
The // operator performs floor division, which divides 5 by 2 to get 2.5 and then rounds down to the nearest whole number 2. The / operator would return 2.5, so option b is correct.
ข้อ 13
What is the result of 5 % 2?
% returns the remainder, and 5 divided by 2 leaves a remainder of 1.
ข้อ 14
Which keyword exits a loop immediately?
The break keyword exits the current loop immediately and continues with the code after the loop. The continue keyword skips only the current iteration, and exit is not the standard loop-control keyword, so option c is correct.
ข้อ 15
Which method adds one item to the end of a list?
list.append(item) adds the item to the end of the list.
ข้อ 16
What is the output of bool(0)?
Zero is treated as falsy in Python, so bool(0) is False.
ข้อ 17
Which statement handles exceptions?
Python uses try and except blocks to catch and handle exceptions.
ข้อ 18
What is the result of [1, 2] + [3]?
The + operator concatenates two lists by joining their elements into one new list, so [1, 2] + [3] becomes [1, 2, 3]. Option b would require append semantics, and option d is a tuple.
ข้อ 19
Which syntax creates a dictionary?
Curly braces with key-value pairs separated by colons create a dict.
ข้อ 20
What does dict.get("missing", 0) return when the key is missing?
get() returns the default value when the key is not present.
ข้อ 21
Which keyword creates a class?
The class keyword defines a new class, and its body contains methods and attributes. Option object is a built-in base, while def defines functions and new is not a Python keyword.
ข้อ 22
What does self represent inside a Python class method?
self is the conventional name for the current instance of the class.
ข้อ 23
Which method is called automatically when a class instance is created?
When an instance is created, Python automatically calls __init__ to initialize the new object. The other options are not special method names, so option a is correct.
ข้อ 24
What is the output of "abc".upper()?
str.upper() returns a copy of the string in uppercase.
ข้อ 25
Which function returns the largest item in a list?
max() returns the largest value in an iterable or among arguments.
ข้อ 26
What is the result of sum([1, 2, 3])?
sum() adds the items in the iterable, giving 1 + 2 + 3 = 6.
ข้อ 27
Which slice returns the last item of list x?
A negative index counts from the end, so x[-1] is the last item.
ข้อ 28
What is the output of sorted([3, 1, 2])?
sorted() returns a new list ordered from smallest to largest.
ข้อ 29
Which keyword defines an anonymous function?
lambda creates a small anonymous function, such as lambda x: x + 1.
ข้อ 30
What is the result of list(map(str, [1, 2]))?
map(str, ...) converts each number to a string, producing ["1", "2"].
ข้อ 31
Which data structure gives the fastest average membership test?
Set membership uses hashing and is O(1) on average, while list membership is O(n).
ข้อ 32
What is the worst-case time complexity of searching for a value in a list?
A linear search over a list checks items one by one, so worst case is O(n).
ข้อ 33
What is the average time complexity of looking up a key in a dict?
Dictionary lookups use a hash table and are O(1) on average.
ข้อ 34
Which statement is true about tuples?
Tuples are immutable ordered sequences, so their elements cannot be changed in place after creation. Lists are mutable, but tuples are not, which makes option b correct.
ข้อ 35
What does isinstance(3, int) return?
isinstance() checks whether 3 is an instance of int and returns True.
ข้อ 36
Which function returns the number of items in a container?
len() returns the number of items in a container or iterable.
ข้อ 37
What is the output of "hello".count("l")?
The count method counts non-overlapping occurrences of the substring, and the letter l appears at positions 2 and 3 in hello. That gives two matches, so option b is correct.
ข้อ 38
Which built-in function returns index-value pairs from an iterable?
enumerate() returns an iterator of tuples, each containing an index and the corresponding value from the iterable. zip() pairs separate iterables, and index() finds a position, so option a is correct.
ข้อ 39
What is list(zip([1, 2], ["a", "b"]))?
zip() pairs the first item of each iterable, then the second item, and so on, producing tuples. Converting to a list gives [(1, "a"), (2, "b")], which matches option a.
ข้อ 40
Which keyword is used inside a function to create a generator?
A function that contains the yield keyword is a generator function, and calling it returns a generator iterator that produces values lazily. The return keyword ends a normal function, so option b is correct.
ข้อ 41
What does next(iter([1, 2])) return?
next() returns the first item from the iterator, which is 1.
ข้อ 42
Which exception is raised by 1 / 0?
Division by zero is not a valid arithmetic operation in Python, so 1 / 0 raises ZeroDivisionError. ValueError covers invalid values, and IndexError covers sequence positions, making option b correct.
ข้อ 43
Which exception is raised by [1, 2][5]?
A list with two elements has valid indexes 0 and 1, so accessing index 5 is out of range and raises IndexError. KeyError is for dictionaries, and ValueError is for invalid values.
ข้อ 44
Which exception is raised by {"a": 1}["b"]?
The dictionary {"a": 1} has only the key a, so looking up b with square brackets raises KeyError. Using the get method would return None instead of raising an error.
ข้อ 45
What is the output of print(1 == 1.0)?
Python compares numeric values across types, and 1 equals 1.0.
ข้อ 46
Which expression evaluates to True?
not False is True; the other expressions evaluate to False or 0.
ข้อ 47
What is the result of "x" in "example"?
The substring "x" appears in "example", so the membership test is True.
ข้อ 48
Which method joins a list of strings into one string?
A separator string calls join(), such as "-".join(["a", "b"]).
ข้อ 49
What does set([1, 1, 2]) produce?
Converting a list with duplicates to a set keeps only unique values.
ข้อ 50
Which operator performs floor division?
The // operator performs floor division, meaning it divides and rounds down to the nearest whole number. The / operator returns a float, and % returns a remainder, so option b is correct.
ข้อ 51
What is the result of 3 * "ab"?
Multiplying a string by an integer repeats it three times.
ข้อ 52
Which built-in function applies a function to every item and returns an iterator?
map() applies the given function to every item in the iterable and returns a map iterator of the results. apply() and each() are not built-in functions in the same way, so option a is correct.
ข้อ 53
What is list(filter(lambda x: x > 2, [1, 2, 3]))?
filter() keeps only items for which the lambda returns True, so only 3 remains.
ข้อ 54
Which keyword imports a module?
The import keyword loads a module and makes its names available in the current namespace. require and include are used by other languages, and load is not Python syntax, so option b is correct.
ข้อ 55
What is the purpose of a docstring?
A docstring is a string literal placed at the start of a module, function, class, or method to document its purpose. It does not change execution speed, delete variables, or create a class, so option a is correct.
ข้อ 56
Which file commonly marks a directory as a Python package?
__init__.py is the conventional package marker inside a directory.
ข้อ 57
What does the global keyword do inside a function?
global tells Python that the name refers to a module-level variable.
ข้อ 58
Which built-in function returns an object identity?
id() returns the identity of an object, often its memory address.
ข้อ 59
What is the output of bool([])?
In Python, an empty list is falsy, so bool([]) converts it to False. Non-empty containers such as [0] are truthy, and None is a separate value, so option b is correct.
ข้อ 60
Which is a valid variable name in Python?
Variable names can start with an underscore and may not start with a digit or contain spaces.
ข้อ 61
Which of the following built-in types are immutable?
Tuples, strings, and frozensets cannot be modified after creation, so they are immutable. Lists can be changed with methods like append, which makes option d incorrect; options a, b, and c are correct.
ข้อ 62
Which methods can add items to a list?
append(), extend(), and insert() add items; remove() deletes an item.
ข้อ 63
Which expressions create a set?
set() with or without an iterable creates a set; {} creates an empty dict.
ข้อ 64
Which statements are true about dictionaries?
Dict keys must be hashable, values can be any type, and insertion order is preserved in Python 3.7+.
ข้อ 65
Which built-in functions return iterables?
enumerate(), zip(), and map() return iterator objects that produce their values lazily. print() returns None rather than an iterable, so options a, b, and c are correct.
ข้อ 66
Which keywords are used for exception handling?
try, except, finally, and raise are all part of Python exception handling.
ข้อ 67
Python lists are zero-indexed.
The first item of a list is at index 0.
ข้อ 68
A Python string can be changed in place.
Python strings are immutable, so an operation such as concatenation creates a new string object instead of changing the original. The statement is therefore false, and option b is correct.
ข้อ 69
None is a valid value in Python.
None represents the absence of a value and is a valid object.
ข้อ 70
A while loop always runs at least once.
A while loop checks its condition before each iteration and may run zero times.
ข้อ 71
Dictionaries preserve insertion order in Python 3.7 and later.
Starting with Python 3.7, dictionaries officially preserve the order in which keys were inserted. This behavior was an implementation detail earlier, so the statement is true.
ข้อ 72
The is operator compares object identity, not value.
is checks whether two names refer to the same object; == compares values.
ข้อ 73
A function without a return statement returns None.
Functions that do not return a value implicitly return None.
ข้อ 74
Python uses && for logical AND.
Python uses the word and for logical AND, while && is used in languages like JavaScript and C. The statement is false, so option b is correct.
ข้อ 75
What is the output of print(2 + 3 * 4)? Type the number only.
Multiplication runs before addition, so the result is 2 + 12 = 14.
ข้อ 76
Type the keyword used to define a function.
A function definition starts with the def keyword, followed by the function name, parentheses, and a colon. Typing def correctly creates a named function, so the answer is def.
ข้อ 77
Type the method used to add one item to the end of a list.
list.append(item) adds an item to the end of the list.
ข้อ 78
Type the keyword that raises an exception manually.
The raise keyword manually triggers an exception with an optional message. It is commonly used to signal invalid input or an impossible state, so the correct keyword is raise.
ข้อ 79
Type the keyword used to produce values from a generator.
yield pauses the function and produces one value at a time.
ข้อ 80
What does type(3.5) return? Type the short class name.
The value 3.5 is a floating-point number, and type(3.5) returns the float class. The short class name printed by Python is float, so the answer is float.