Courseiva

PCEP · domain

Computer Programming and Python Fundamentals

This domain covers the foundations every PCEP candidate needs: how Python source is structured, how names and literals are written, and how basic expressions evaluate. Questions are short code-reading and output-prediction items, plus PEP 8 style recognition and choosing the right built-in data structure for a described configuration or record.

122 questions37 easy45 medium40 hard

Focused practice

Practice Computer Programming and Python Fundamentals questions

Scored sessions drawing only from this domain — pick a length below.

Start 20-question practice test →

What this domain covers

What to know about Computer Programming and Python Fundamentals

You must read short snippets and state exactly what prints, and recognize idiomatic Python naming and structure choices. The single most important thing is to trace expression evaluation and slicing precisely, remembering that slice stops are exclusive and that ** has higher precedence than unary minus.

PEP 8 naming conventions for variables, constants, functions, classes, and modules

Python literal and operator behavior, including exponentiation, precedence, and integer versus float results

Indexing and slicing of sequences, including negative indices and slice end exclusivity

Selecting appropriate built-in types such as list, tuple, dict, and set for a given data shape

Why learners struggle

Why Computer Programming and Python Fundamentals questions are commonly missed

RAM questions are commonly missed because learners confuse physical form factors (DIMM vs SO-DIMM) and fail to distinguish between memory speed (MHz) and latency (CL).

  • ·DIMM vs SO-DIMM — desktop vs laptop form factor confusion
  • ·DDR3 vs DDR4 vs DDR5 — notch position and voltage differences
  • ·MHz vs CL — speed vs latency trade-offs in performance
  • ·Single-channel vs dual-channel — bandwidth impact misconception
  • ·ECC vs non-ECC — error correction support in servers vs desktops
  • ·32-bit vs 64-bit — maximum addressable RAM limit

Watch out for

Common Computer Programming and Python Fundamentals exam traps

  • ▸Assuming a slice like [-3:-1] includes the last element; the stop index is always excluded, so the final item is dropped.
  • ▸Forgetting that ** binds tighter than unary minus and multiplication, so -2 ** 2 evaluates to -4, not 4.
  • ▸Using camelCase or single-letter names for constants; PEP 8 expects UPPER_CASE_WITH_UNDERSCORES for module-level constants.

Question index

All Computer Programming and Python Fundamentals questions (122)

Click any question to see the full explanation, or start a practice session above.

1

You are a junior developer at a logistics company. Your team maintains a Python script that processes daily shipment data from a CSV file. The script reads the file, computes total weight per shipment, and writes results to a new CSV. Recently, the script started crashing sporadically with a 'ValueError: invalid literal for int() with base 10: 'NULL''. The CSV file sometimes contains the string 'NULL' in the weight column for missing values. The current code reads the weight column as: weight = int(row['weight']). Your team lead wants a robust fix that handles missing data gracefully without crashing, and also logs the line number for any problematic rows for later review. Which of the following approaches best meets these requirements?

Hard
2

A program uses a for loop to double each element in a list: numbers = [1, 2, 3, 4, 5]; for num in numbers: num = num * 2. After execution, numbers remains unchanged. Why?

Medium
3

Arrange the steps to read data from a text file in Python.

Medium
4

What does the following code print? text = 'Hello World'; print(text.replace('o', '0').upper())

Medium
5

You are a developer in a company that runs a Python script daily to generate reports. The script uses the os module to list files in a directory and process each. Recently, after a server migration, the script fails with 'PermissionError: [Errno 13] Permission denied'. The script runs under a service account that has read/write access to most folders, but the migration changed the permissions on certain subdirectories. The error is intermittent, occurring only for some files. You need to fix the script to continue processing other files even if one fails. Which approach should you take?

Medium
6

Refer to the exhibit. What is the most likely cause of this error?

Easy
7

A programmer needs a constant that identifies the maximum number of retry attempts for a network operation. The value must not change while the program runs. Which statement about naming and using this constant in Python is accurate?

Hard
8

Which two of the following are true about Python lists? (Choose two.)

Hard
9

What does the following code output? for i in range(3): if i == 1: continue; print(i, end=' ')

Medium
10

Consider code: def outer(): x = 1 def inner(): nonlocal x x = 2 inner() print(x) outer() What is printed?

Hard
11

A junior developer is writing a script to process a list of user IDs: ids = [101, 102, 103, 104]. The goal is to create a new list where each ID is increased by 10, without modifying the original list. The developer writes: new_ids = ids.append(10). However, the output shows None. The developer needs to correctly create the new list. Which code should the developer use to achieve this?

Medium
12

A developer writes code to calculate the area of a rectangle and prints it. The code is: length = 10 width = 5 area = length + width print('The area is', area) If the width is accidentally assigned a string '5', what error will occur?

Easy
13

A developer is writing a Python script and needs to create valid variable names. Which two of the following are valid variable names? (Choose two.)

Hard
14

A programmer wants to iterate over a list of strings and print each string in uppercase. Which of the following code snippets will accomplish this?

Easy
15

A trainee is reviewing how Python 3 handles basic data types and type conversion in a small inventory script. Which TWO statements about Python's built-in numeric and string types are correct? (Choose two.)

Medium
16

A beginner writes: x = 10; y = 3; print(x // y). What is the output?

Easy
17

Which THREE of the following are correct ways to create a list containing the numbers 1, 2, 3? (Choose three.)

Hard
18

A Python program contains the following code: print(10 / 4) What is displayed when this program runs?

Easy
19

A junior developer is working on a script that processes user data. The script reads a CSV file into a list of dictionaries. Each dictionary represents a user with keys 'name', 'age', and 'email'. The developer needs to filter out users under 18 and store their names in a list. The current code is: users = [{'name': 'Alice', 'age': 17, 'email': 'alice@example.com'}, {'name': 'Bob', 'age': 22, 'email': 'bob@example.com'}] minors = [] for user in users: if user['age'] < 18: minors.append(user['name']) print(minors) The code works, but the senior developer says it is not idiomatic and suggests a more concise solution. Which of the following approaches is the best replacement?

Hard
20

Which THREE of the following statements about Python data types are correct? (Choose three.)

Hard
21

A developer needs to iterate over the indices of a list named 'items' and print each index and its corresponding value. Which loop construct is most appropriate?

Medium
22

A developer wrote: a, b, c = 10, 20, 30; avg = a + b + c / 3; print(avg). What is the output?

Easy
23

A developer is reviewing how literals and operators behave in a Python script. Which TWO of the following statements about Python literals and operators are true? (Choose two.)

Hard
24

A Python developer is creating a function that processes a list of dictionaries and needs to ensure the original list remains unchanged. They write the following code: def process(data): for item in data: item['processed'] = True return data What is the best-practice critique of this function?

Hard
25

A Python program is designed to process user input and store results in a dictionary. The code uses the statement: my_dict[user_key] = value. Under which condition will this statement raise a TypeError?

Hard
26

A developer writes a short script to convert a numeric string entered at the console into a number for arithmetic: value = input('Enter a quantity: ') total = value * 2 print(total) A user types 7 and presses Enter. What is printed, and why?

Medium
27

You are an IT support specialist for a university. A professor uses a Python script that analyzes exam scores from a text file. The script calculates the average score and prints it. Recently, the script outputs 'NaN' instead of a number. The relevant code is: scores = [float(line.strip()) for line in open('scores.txt')]; average = sum(scores) / len(scores); print(average). You inspect the scores.txt file and find that one line contains the word 'Absent' and another line is blank. The professor wants the script to ignore non-numeric lines and blank lines, and also print a warning if any line was skipped. Which of the following modifications to the script best achieves this?

Hard
28

A developer writes a function that calculates the area of a rectangle and prints the result inside the function. Later, they need to use this area in another calculation. What should they do to make the function reusable and composable?

Easy
29

Which logical expression evaluates to True given that a = 5 and b = 10?

Medium
30

A developer writes the following code: x = 5 if x > 3: print('A') else: print('B') print('C') What is the output?

Easy
31

Which TWO of the following are valid variable names in Python?

Easy
32

A programmer is writing a Python script to calculate the area of a circle. They write the following code: radius = 5 area = 3.14 * radius ** 2 print(area) What is the output of this code?

Easy
33

Match each Python keyword to its use.

Medium
34

Refer to the exhibit. Which of the following is true about the output?

Hard
35

A developer writes the following code and runs it without any error: x = 5 y = 2.0 z = x + y What is the value and type of z?

Medium
36

A junior developer is writing a script to read a number from input and double it. They write: num = input("Enter a number: ") result = num * 2 print(result) When they test with input 5, the output is '55' instead of 10. What is wrong?

Easy
37

A function is defined as: def add(a, b=5): return a + b What is the result of add(10)?

Medium
38

What is the output?

Hard
39

A developer is writing a Python script and assigns a value to a variable named '2nd_place'. When the script is run, it raises a SyntaxError. What is the cause of this error?

Medium
40

You are a junior developer at a small startup. Your team has a Python script that automates daily data processing. The script reads a CSV file, processes each row, and writes results to a new file. Recently, the script started crashing with a 'ValueError: invalid literal for int()' error. The error occurs on a line that converts a field to an integer using int() on a string value. The CSV file comes from an external source that sometimes contains non-numeric values like 'N/A' or empty strings. Which course of action is best to handle this robustly without stopping the entire process?

Easy
41

You are maintaining a Python script that calculates team bonuses based on sales data. The script reads a dictionary where keys are employee names and values are total sales (float). It then applies a 10% bonus if sales exceed 5000. The code snippet is: def calculate_bonus(sales): for name, value in sales.items(): if value > 5000: print(f"{name} gets bonus") However, the manager wants the script to return a list of employees who qualify, not just print them. They also want to avoid side effects. What is the best way to modify this function?

Easy
42

Match each Python data type to its description.

Medium
43

A Python program needs to store the integer value 42 in a variable named count. Which line of code correctly assigns this value?

Easy
44

Which THREE of the following are valid ways to create a list in Python?

Easy
45

According to PEP 8, which of the following is the recommended way to name a constant representing the maximum number of retries?

Hard
46

A Python script calculates the area of a circle: radius = 5; area = 3.14 * radius ** 2; print(area). What is printed?

Easy
47

A QA engineer needs to run a test 5 times. Which loop construct is most appropriate?

Easy
48

You are a developer on a team that maintains a legacy Python 2 codebase being migrated to Python 3. One function reads a file in text mode and counts word frequencies. In Python 2, the code used the dict.iteritems() method to iterate over the dictionary. After migration, the code raises AttributeError: 'dict' object has no attribute 'iteritems'. You need to update the code to work in Python 3 while minimizing changes. Which action should you take?

Hard
49

A developer needs to check if a number is positive and even. Which conditional expression is correct?

Medium
50

A dictionary: d = {1: 'a', 2: 'b', 3: 'c'}. Which code will cause a KeyError?

Hard
51

A developer writes a function that modifies a global variable inside the function: count = 0 def increment(): count += 1 When called, an error occurs. What is the correct way to fix this?

Medium
52

An application requires different messages based on temperature. Given: temp = 25 if temp > 30: print('Hot') elif temp > 20: print('Warm') else: print('Cool') What is the output?

Easy
53

Which THREE of the following are valid ways to create a list with elements 1, 2, 3? (Choose Three)

Hard
54

A programmer needs to read a file line by line and process each line. Which of the following is the most memory-efficient and Pythonic approach?

Hard
55

A function is supposed to modify a list passed as argument by appending an element. However, after calling the function, the original list remains unchanged. Which is the most likely cause?

Hard
56

A student runs the following code in a Python 3.11 interactive session: >>> 7 // 2 What is displayed?

Easy
57

A team is developing a script that processes user input. They want to ensure that if the user enters a non-numeric value when asked for age, the program does not crash. Which approach should they use?

Medium
58

A script uses 'import math' then calls 'math.sqrt(-1)'. What is the outcome?

Hard
59

Which TWO of the following statements about Python's for loop are correct? (Choose Two)

Medium
60

Which of the following code snippets will correctly assign the integer 10 to the variable 'x'?

Easy
61

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?

Medium
62

Which TWO of the following expressions evaluate to True? (Choose two.)

Easy
63

A programmer writes the following code to convert a user's input to a number and add 5: age = input('Enter age: ') new_age = int(age) + 5 print(new_age) If the user enters 'twenty', what happens?

Hard
64

Which TWO of the following are immutable data types in Python?

Medium
65

A programmer is debugging a Python script that is supposed to concatenate a user's first and last names with a space between them. The code is: first = 'John' last = 'Doe' full = first + ' ' + last print(full) What is the output of this script?

Medium
66

Which FOUR of the following are valid ways to create a list with elements 1, 2, 3? (Choose four.)

Medium
67

A developer runs the code from the exhibit and gets the error shown. Which of the following is the most likely cause?

Hard
68

A programmer is writing a Python script and needs to create variables that hold a user's name, age, and whether they are a student. Which TWO of the following assignments are syntactically valid and will execute without error in Python? (Choose two.)

Medium
69

A developer writes: print('Hello' + 5). What is the result?

Medium
70

A programmer wants to create a function that can accept any number of keyword arguments and store them in a dictionary. Which function definition is correct?

Hard
71

A programmer writes: x = 10 y = 4 print(x // y, x % y, x / y) What is printed?

Hard
72

A Python script processes a large file and runs out of memory. Which solution is most appropriate?

Hard
73

A developer writes a loop to find the first even number in a list: numbers = [3, 5, 8, 10] for n in numbers: if n % 2 == 0: print(n) break else: print('No even number') What is printed?

Hard
74

A developer defines a function and calls it as shown: def greet(name, greeting='Hello'): return greeting + ', ' + name print(greet('Ana')) What is printed?

Medium
75

A developer wants to store a collection of unique employee IDs that will be checked frequently for membership. Duplicate IDs must be automatically ignored, and the order of IDs does not matter. Which built-in data type best fits this requirement?

Medium
76

Which THREE of the following are Python data types? (Choose three.)

Medium
77

A Python script contains the following code: x = 10 y = 3 result = x // y What is the value of result after execution?

Easy
78

A programmer needs to store the number of items in a shopping cart. The value will be a whole number and may be used in arithmetic operations. Which Python data type is most appropriate for this variable?

Easy
79

Which TWO of the following are valid Python variable names? (Choose two.)

Easy
80

What is the output of the following code? x = [1, 2, 3]; y = x; y.append(4); print(x)

Hard
81

The exhibit shows a JSON configuration. Which Python data structure is best suited to represent this configuration?

Easy
82

Consider the code: x = 10; def func(): x = 5; print(x); func(); print(x). What is the output?

Hard
83

A developer writes a function that returns multiple values. How should they return these values?

Medium
84

Consider the following function definition: def add(a, b): return a + b What is the value of add(3, '4')?

Medium
85

Which of the following is the correct way to define a function that takes no arguments and returns the value 42?

Easy
86

A programmer writes: x = 5; y = 2; result = x / y. What is the type of result?

Medium
87

What function is used to read input from the user in Python 3?

Easy
88

Which of the following statements about Python indentation is true?

Easy
89

A developer writes the following code to sum a list of scores: scores = [12, 7, 25, 18] total = 0 for s in scores: total += s print(total) What is printed?

Medium
90

A programmer writes the following code to classify a temperature reading: temp = 25 if temp > 30: print('Hot') elif temp > 20: print('Warm') else: print('Cool') What is printed when this code runs?

Hard
91

Given the code: my_list = [1, 2, 3, 4, 5]. What is the output of print(my_list[-3:-1])?

Hard
92

A junior developer writes a Python script to calculate the average of three numbers: avg = a + b + c / 3. What is the problem with this code?

Easy
93

Which TWO statements correctly describe Python's dynamic typing?

Hard
94

A student types the following into a Python 3 interactive session: >>> greeting = 'Hello' >>> greeting = greeting + ' World' >>> print(greeting) What is displayed?

Easy
95

Which TWO statements about Python lists are true?

Medium
96

Which of the following is an immutable data type in Python?

Easy
97

A system administrator is automating server configuration using Python. She has a dictionary: config = {'host': 'localhost', 'port': 8080, 'debug': True}. She needs to add a new key 'timeout' with value 30 if it does not already exist, but only if the 'debug' key is False. If 'debug' is True, she should not add 'timeout'. Additionally, she wants to ensure that the ordering of keys in the dictionary remains stable (insertion order). Which code snippet correctly implements this logic?

Hard
98

A programmer wants to iterate over a list and also access the index. Which built-in function should they use?

Medium
99

What is the result of the following expression? 3 + 4 * 2 ** 3 // 5

Medium
100

Which THREE of the following code snippets will successfully print the string 'Hello, World!'? (Choose three.)

Medium
101

What will be printed? ```python my_list = [] my_list.append('192.168.1.10') my_list.append('255.255.255.0') my_list.append('192.168.1.1') print(my_list) ```

Medium
102

What is the output of the following code? print('Hello'.upper())

Easy
103

Which of the following is a floating-point literal?

Easy
104

A junior developer writes the following code to swap two variables: a = 5; b = 10; a = b; b = a. When they print a and b, what is the output?

Easy
105

A developer wants to create a list of even numbers from 0 to 10 inclusive. Which code snippet will correctly produce [0, 2, 4, 6, 8, 10]?

Easy
106

Which keyword is used to define a function in Python?

Easy
107

A developer is writing a Python script that stores the number of active user sessions in a variable. They want the variable to hold the value 42 as an integer. Which of the following statements correctly assigns the integer 42 to a variable named session_count?

Medium
108

A developer writes: try: x = int('hello') except ValueError: x = 0 except TypeError: x = -1 finally: x = x + 1 What is the final value of x?

Hard
109

What is the output of the following code? try: print(1/0); except ZeroDivisionError: print('error'); finally: print('done')

Hard
110

A programmer is debugging a Python script that uses a while loop to process a list of numbers. The code is: numbers = [1, 2, 3, 4, 5] i = 0 while i < len(numbers): if numbers[i] % 2 == 0: print(numbers[i]) i += 1 What is the output of this code?

Hard
111

Arrange the steps to slice a list in Python.

Medium
112

A Python program prompts the user for their age and stores it in a variable. Which is the correct way to convert the input to an integer?

Easy
113

A developer is reviewing Python's built-in numeric types while preparing a data-processing script. Which TWO statements about Python's numeric types are accurate? (Choose two.)

Medium
114

Which two of the following are correct ways to create a dictionary in Python? (Choose two.)

Medium
115

Arrange the steps to write and run a Python script from the command line in the correct order.

Medium
116

What is the output of: print(2 ** 3 ** 2)?

Hard
117

What is the scope of a variable defined inside a function?

Hard
118

A developer is reviewing a script that processes user data. The script contains the following lines: name = "Alice" age = 30 print(name + " is " + age) When executed, the script raises a TypeError. Which of the following changes will make the script run without error and produce the output "Alice is 30"?

Medium
119

A developer is writing a Python script to compare two numbers. They write the following code: x = 10 y = 20 result = x > y or x < y and y > 15 print(result) What is printed?

Medium
120

A developer writes the following code: values = [10, 20, 30, 40, 50] result = values[1:4] What does the variable result contain after execution?

Hard
121

What is the result of the expression: (1 and 0) or (not False and True)?

Hard
122

A developer is creating a Python script and needs to assign a value to a variable. Which TWO of the following assignment statements are syntactically valid in Python? (Choose two.)

Medium

Frequently asked questions

What does the Computer Programming and Python Fundamentals domain cover on the PCEP exam?
You must read short snippets and state exactly what prints, and recognize idiomatic Python naming and structure choices. The single most important thing is to trace expression evaluation and slicing precisely, remembering that slice stops are exclusive and that ** has higher precedence than unary minus.
How many questions are in this domain?
This page lists all 122 Computer Programming and Python Fundamentals questions in the PCEP question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
What is the best way to practise this domain?
Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
Can I practise only Computer Programming and Python Fundamentals questions?
Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.
python-pcep PYTHON-PCEP python fundamentals Practice Questions