Practica 80 preguntas gratis de entrevista de programación en Python con respuestas y explicaciones.
Nivel: Coding InterviewDificultad: medium80 preguntas60 min
Responde cada pregunta de Python y comprueba tu puntuación. Los errores se guardan localmente para repasar.
Racha de días: 0 díasGuardado solo en este dispositivo
Progreso0 / 80
Tiempo restante: 00:00
Aún no hay errores guardados.
Ninguna pregunta coincide con tus filtros.
Pregunta 1
Which data type is immutable in Python?
Tuples are immutable, while lists, dicts, and sets can be changed after creation.
Pregunta 2
What does len("hello") return?
The string "hello" contains five characters, so len() returns 5.
Pregunta 3
Which operator checks whether two values are equal?
Python uses == to compare values for equality and = to assign a value.
Pregunta 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.
Pregunta 5
Which method removes the last item from a list and returns it?
list.pop() removes and returns the last item by default.
Pregunta 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.
Pregunta 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.
Pregunta 8
Which values does list(range(3)) produce?
range(3) starts at 0 and stops before 3, producing [0, 1, 2].
Pregunta 9
Which built-in function converts a string to an integer?
int("42") converts the string "42" to the integer 42.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 13
What is the result of 5 % 2?
% returns the remainder, and 5 divided by 2 leaves a remainder of 1.
Pregunta 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.
Pregunta 15
Which method adds one item to the end of a list?
list.append(item) adds the item to the end of the list.
Pregunta 16
What is the output of bool(0)?
Zero is treated as falsy in Python, so bool(0) is False.
Pregunta 17
Which statement handles exceptions?
Python uses try and except blocks to catch and handle exceptions.
Pregunta 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.
Pregunta 19
Which syntax creates a dictionary?
Curly braces with key-value pairs separated by colons create a dict.
Pregunta 20
What does dict.get("missing", 0) return when the key is missing?
get() returns the default value when the key is not present.
Pregunta 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.
Pregunta 22
What does self represent inside a Python class method?
self is the conventional name for the current instance of the class.
Pregunta 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.
Pregunta 24
What is the output of "abc".upper()?
str.upper() returns a copy of the string in uppercase.
Pregunta 25
Which function returns the largest item in a list?
max() returns the largest value in an iterable or among arguments.
Pregunta 26
What is the result of sum([1, 2, 3])?
sum() adds the items in the iterable, giving 1 + 2 + 3 = 6.
Pregunta 27
Which slice returns the last item of list x?
A negative index counts from the end, so x[-1] is the last item.
Pregunta 28
What is the output of sorted([3, 1, 2])?
sorted() returns a new list ordered from smallest to largest.
Pregunta 29
Which keyword defines an anonymous function?
lambda creates a small anonymous function, such as lambda x: x + 1.
Pregunta 30
What is the result of list(map(str, [1, 2]))?
map(str, ...) converts each number to a string, producing ["1", "2"].
Pregunta 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).
Pregunta 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).
Pregunta 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.
Pregunta 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.
Pregunta 35
What does isinstance(3, int) return?
isinstance() checks whether 3 is an instance of int and returns True.
Pregunta 36
Which function returns the number of items in a container?
len() returns the number of items in a container or iterable.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 41
What does next(iter([1, 2])) return?
next() returns the first item from the iterator, which is 1.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 45
What is the output of print(1 == 1.0)?
Python compares numeric values across types, and 1 equals 1.0.
Pregunta 46
Which expression evaluates to True?
not False is True; the other expressions evaluate to False or 0.
Pregunta 47
What is the result of "x" in "example"?
The substring "x" appears in "example", so the membership test is True.
Pregunta 48
Which method joins a list of strings into one string?
A separator string calls join(), such as "-".join(["a", "b"]).
Pregunta 49
What does set([1, 1, 2]) produce?
Converting a list with duplicates to a set keeps only unique values.
Pregunta 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.
Pregunta 51
What is the result of 3 * "ab"?
Multiplying a string by an integer repeats it three times.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 56
Which file commonly marks a directory as a Python package?
__init__.py is the conventional package marker inside a directory.
Pregunta 57
What does the global keyword do inside a function?
global tells Python that the name refers to a module-level variable.
Pregunta 58
Which built-in function returns an object identity?
id() returns the identity of an object, often its memory address.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 62
Which methods can add items to a list?
append(), extend(), and insert() add items; remove() deletes an item.
Pregunta 63
Which expressions create a set?
set() with or without an iterable creates a set; {} creates an empty dict.
Pregunta 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+.
Pregunta 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.
Pregunta 66
Which keywords are used for exception handling?
try, except, finally, and raise are all part of Python exception handling.
Pregunta 67
Python lists are zero-indexed.
The first item of a list is at index 0.
Pregunta 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.
Pregunta 69
None is a valid value in Python.
None represents the absence of a value and is a valid object.
Pregunta 70
A while loop always runs at least once.
A while loop checks its condition before each iteration and may run zero times.
Pregunta 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.
Pregunta 72
The is operator compares object identity, not value.
is checks whether two names refer to the same object; == compares values.
Pregunta 73
A function without a return statement returns None.
Functions that do not return a value implicitly return None.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 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.
Pregunta 79
Type the keyword used to produce values from a generator.
yield pauses the function and produces one value at a time.
Pregunta 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.