Python Institute · Free Practice Questions · Last reviewed May 2026
24real exam-style questions organised by domain, each with the correct answer highlighted and a plain-English explanation of why it's right — and why the others are wrong.
A developer writes a script that prompts the user for their age and stores it in a variable. Which code snippet correctly converts the input to an integer?
age = int(input)
age = int(input("Enter age: "))
Correctly converts the input string to an integer.
age = input("Enter age: ", int)
age = input(int("Enter age: "))
Which of the following is the correct way to define a function that takes no arguments and returns the value 42?
function f(): return 42
def f(): return 42
def f: return 42
def f(): return 42
Correct syntax for a function that returns 42.
A program uses a variable named 'list' that shadows the built-in list type. Later, the code tries to create a new list using list([1,2,3]) but gets a TypeError. What is the most likely cause?
The argument [1,2,3] is invalid because it contains integers.
The variable 'list' is now an integer or other non-callable type.
Because 'list' was reassigned, it no longer refers to the built-in function.
The list constructor expects a tuple, not a list.
The code is missing an import for the list type.
A programmer writes: x = 5; y = 2; result = x / y. What is the type of result?
int
float
True: / operator returns float.
str
complex
Given the code: a = [1, 2, 3]; b = a; b.append(4). What is the value of a?
[1, 2, 3, 4]
Both a and b refer to the same list.
Error
[1, 2, 3]
[1, 2, 3, [4]]
Which of the following statements about Python indentation is true?
Indentation is optional but recommended.
You can mix tabs and spaces freely.
Indentation must be consistent within a block.
Correct.
Indentation only matters for loops and conditionals.
Want more Computer Programming and Python Fundamentals practice?
Practice this domainA developer writes the following code: x = 5; y = 2; print(x // y). What is the output?
1
2
Floor division of 5 by 2 yields 2.
2.0
2.5
A junior developer writes: x = 10; y = 3; print(x % y). What will be printed?
3.33
1
Remainder when 10 is divided by 3.
0
3
A developer needs to convert a string '25' to an integer and then add 10. Which code correctly performs this?
print(int('25') + 10)
Correct conversion and addition.
print('25' + 10)
print(int('25') + '10')
print('25' + str(10))
What is the correct way to read a floating-point number from user input and store it in a variable?
x = input(float())
x = input() as float
x = float(input())
Reads input as string, then converts to float.
x = input().float()
A developer wants to assign the value 3.14 to a variable and later change it to the integer 3. Which of the following is true?
This is allowed only if the variable was declared as a float.
This causes a TypeError because you cannot change types.
This is allowed because Python is dynamically typed.
No type restrictions.
This is allowed only if you use the int() function during assignment.
Which of the following is a valid variable name in Python?
my-var
2nd_place
_count
Underscore is allowed at start.
class
Want more Data Types, Variables, Basic I/O and Operators practice?
Practice this domainA developer writes a loop to sum numbers from 1 to 10. The code outputs 55, but the expected sum is 55. However, the loop uses a range that includes 0. Which range should be used to achieve the correct sum?
range(0,11)
range(1,11)
Correctly generates 1..10
range(0,101)
range(1,10)
A programmer needs to iterate over a list of strings and print each string in uppercase. Which loop correctly accomplishes this?
for i, item in enumerate(mylist): print(item.upper())
Also correct, but the question expects exactly one correct; both A and C are correct. I'll adjust: make only A correct? No, both are valid. I need to change distractors so that only one is correct. Let's replace A with a wrong one.
for item in mylist: item = item.upper()
for item in mylist: print(item.upper)
for i in range(len(mylist)): print(mylist[i].upper())
Correctly accesses each element by index and prints uppercase.
A programmer writes code that uses a while loop to process user input until the user types 'exit'. The code currently prints 'Done' after the loop, but it never exits. What is the most likely cause?
The loop condition is 'while True'
The print statement is indented incorrectly
The input function is called outside the loop
The variable controlling the loop is not updated inside the loop
Classic infinite loop cause.
A list of numbers is defined as nums = [1, 2, 3, 4, 5]. Which expression returns the last element?
nums[5]
nums[-1]
Correct negative indexing.
nums[0]
nums[-2]
A list contains strings and numbers: items = ['apple', 10, 'banana', 20]. A programmer wants to create a new list that contains only the strings. Which approach is correct?
[item for item in items if item.isdigit()]
[item for item in items if type(item) is 'str']
[item for item in items if type(item) == str]
Correct, though isinstance is preferred.
[item for item in items if isinstance(item, str)]
Correct and best practice.
A developer writes the following code snippet:
for i in range(3):
for j in range(2):
if i == j:break else:
print(i, 'outer')
What is the output?
0 outer\n2 outer
2 outer
Only when i=2 does the inner loop complete without break.
0 outer\n1 outer\n2 outer
No output
Want more Control Flow, Loops, Lists and Logic practice?
Practice this domainA developer writes a function to calculate the average of a list of numbers, but the function sometimes returns a wrong result when the list contains non-numeric values. What is the best way to handle this?
Return None if any non-numeric value is encountered.
Use try-except to ignore non-numeric values and proceed with the remaining numbers.
Convert all values to string and concatenate them.
Check that all items are numeric before calculation, and raise TypeError otherwise.
Raising an exception is the standard way to handle invalid input.
A Python script uses a dictionary to store user session data. The developer writes `user = {'id': 101, 'name': 'Alice'}` and later tries to access `user['email']`. What is the outcome?
It returns an empty string.
It raises a KeyError.
Accessing a non-existent key directly raises KeyError.
It returns None.
It checks the 'in' operator automatically and returns False.
A server logs are stored as a list of tuples: `logs = [('2024-01-10', 'INFO', 'Started'), ('2024-01-10', 'ERROR', 'Disk full')]`. A developer wants to count how many ERROR logs exist. Which code snippet correctly counts them?
count = logs.count(('ERROR',))
count = sum(log[1] == 'ERROR' for log in logs)
Sum of booleans gives the count.
count = [log for log in logs if log[1] == 'ERROR']
count = len(logs)
A function `def process(data):` modifies the dictionary passed as argument by adding a new key. The developer wants to avoid modifying the original dictionary. What should the function do?
Create a copy of the dictionary at the start: `data = data.copy()`
copy() creates a shallow copy, avoiding modification of original.
Add the key, then delete it at the end.
Modify directly; changes to mutable objects are local only.
Convert the dictionary to a tuple before processing.
A developer writes a function that returns multiple values as a tuple. Which of the following is a valid way to unpack the result into separate variables?
result = func(); a, b = result[0], result[1]
a, b, c = func()
a = func()[0]; b = func()[1]
a, b = func()
This is direct tuple unpacking.
A Python script processes a list of tuples representing coordinates: `points = [(1,2), (3,4), (5,6)]`. The developer wants to create a dictionary mapping each coordinate to its distance from origin. Which code correctly creates the dictionary?
distances = {}; for point in points: distances[point] = (point[0]**2 + point[1]**2)
distances = {}; for point in points: distances[point] = (point[0]**2 + point[1]**2)**0.5
distances = {point: (point[0]**2 + point[1]**2)**0.5 for point in points}
Dictionary comprehension with correct calculation.
distances = {point: point[0]**2 + point[1]**2 for point in points}
distances = [(point, (point[0]**2 + point[1]**2)**0.5) for point in points]
Want more Functions, Tuples, Dictionaries and Exceptions practice?
Practice this domainThe PCEP exam has 30 questions and must be completed in 45 minutes. The passing score is 700/1000.
Scenario-based questions covering exam objectives with detailed answer explanations.
The exam covers 4 domains: Computer Programming and Python Fundamentals, Data Types, Variables, Basic I/O and Operators, Control Flow, Loops, Lists and Logic, Functions, Tuples, Dictionaries and Exceptions. Questions are weighted by domain — higher-weight domains appear more on your actual exam.
No. These are original exam-style practice questions written against the official Python Institute PCEP exam objectives. They are not copied from the real exam. Courseiva focuses on genuine understanding, not memorisation of braindumps.
Courseiva tracks your accuracy per domain and routes you toward weak areas automatically. Free, no account required.