Jawab setiap soal Python, lalu periksa nilai Anda. Jawaban salah disimpan secara lokal untuk ditinjau.
Hari beruntun: 0 hariTersimpan hanya di perangkat ini
Kemajuan0 / 80
Waktu tersisa: 00:00
Belum ada jawaban salah tersimpan.
Tidak ada soal yang cocok dengan filter.
Soal 1
Which data type is immutable in Python?
Tuples are immutable, while lists, dicts, and sets can be changed after creation.
Soal 2
What does len("hello") return?
The string "hello" contains five characters, so len() returns 5.
Soal 3
Which operator checks whether two values are equal?
Python uses == to compare values for equality and = to assign a value.
Soal 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.
Soal 5
Which method removes the last item from a list and returns it?
list.pop() removes and returns the last item by default.
Soal 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.
Soal 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.
Soal 8
Which values does list(range(3)) produce?
range(3) starts at 0 and stops before 3, producing [0, 1, 2].
Soal 9
Which built-in function converts a string to an integer?
int("42") converts the string "42" to the integer 42.
Soal 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.
Soal 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.
Soal 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.
Soal 13
What is the result of 5 % 2?
% returns the remainder, and 5 divided by 2 leaves a remainder of 1.
Soal 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.
Soal 15
Which method adds one item to the end of a list?
list.append(item) adds the item to the end of the list.
Soal 16
What is the output of bool(0)?
Zero is treated as falsy in Python, so bool(0) is False.
Soal 17
Which statement handles exceptions?
Python uses try and except blocks to catch and handle exceptions.
Soal 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.
Soal 19
Which syntax creates a dictionary?
Curly braces with key-value pairs separated by colons create a dict.
Soal 20
What does dict.get("missing", 0) return when the key is missing?
get() returns the default value when the key is not present.
Soal 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.
Soal 22
What does self represent inside a Python class method?
self is the conventional name for the current instance of the class.
Soal 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.
Soal 24
What is the output of "abc".upper()?
str.upper() returns a copy of the string in uppercase.
Soal 25
Which function returns the largest item in a list?
max() returns the largest value in an iterable or among arguments.
Soal 26
What is the result of sum([1, 2, 3])?
sum() adds the items in the iterable, giving 1 + 2 + 3 = 6.
Soal 27
Which slice returns the last item of list x?
A negative index counts from the end, so x[-1] is the last item.
Soal 28
What is the output of sorted([3, 1, 2])?
sorted() returns a new list ordered from smallest to largest.
Soal 29
Which keyword defines an anonymous function?
lambda creates a small anonymous function, such as lambda x: x + 1.
Soal 30
What is the result of list(map(str, [1, 2]))?
map(str, ...) converts each number to a string, producing ["1", "2"].
Soal 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).
Soal 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).
Soal 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.
Soal 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.
Soal 35
What does isinstance(3, int) return?
isinstance() checks whether 3 is an instance of int and returns True.
Soal 36
Which function returns the number of items in a container?
len() returns the number of items in a container or iterable.
Soal 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.
Soal 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.
Soal 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.
Soal 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.
Soal 41
What does next(iter([1, 2])) return?
next() returns the first item from the iterator, which is 1.
Soal 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.
Soal 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.
Soal 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.
Soal 45
What is the output of print(1 == 1.0)?
Python compares numeric values across types, and 1 equals 1.0.
Soal 46
Which expression evaluates to True?
not False is True; the other expressions evaluate to False or 0.
Soal 47
What is the result of "x" in "example"?
The substring "x" appears in "example", so the membership test is True.
Soal 48
Which method joins a list of strings into one string?
A separator string calls join(), such as "-".join(["a", "b"]).
Soal 49
What does set([1, 1, 2]) produce?
Converting a list with duplicates to a set keeps only unique values.
Soal 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.
Soal 51
What is the result of 3 * "ab"?
Multiplying a string by an integer repeats it three times.
Soal 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.
Soal 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.
Soal 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.
Soal 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.
Soal 56
Which file commonly marks a directory as a Python package?
__init__.py is the conventional package marker inside a directory.
Soal 57
What does the global keyword do inside a function?
global tells Python that the name refers to a module-level variable.
Soal 58
Which built-in function returns an object identity?
id() returns the identity of an object, often its memory address.
Soal 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.
Soal 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.
Soal 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.
Soal 62
Which methods can add items to a list?
append(), extend(), and insert() add items; remove() deletes an item.
Soal 63
Which expressions create a set?
set() with or without an iterable creates a set; {} creates an empty dict.
Soal 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+.
Soal 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.
Soal 66
Which keywords are used for exception handling?
try, except, finally, and raise are all part of Python exception handling.
Soal 67
Python lists are zero-indexed.
The first item of a list is at index 0.
Soal 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.
Soal 69
None is a valid value in Python.
None represents the absence of a value and is a valid object.
Soal 70
A while loop always runs at least once.
A while loop checks its condition before each iteration and may run zero times.
Soal 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.
Soal 72
The is operator compares object identity, not value.
is checks whether two names refer to the same object; == compares values.
Soal 73
A function without a return statement returns None.
Functions that do not return a value implicitly return None.
Soal 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.
Soal 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.
Soal 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.
Soal 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.
Soal 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.
Soal 79
Type the keyword used to produce values from a generator.
yield pauses the function and produces one value at a time.
Soal 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.