120 free Python Institute PCEP-30-02 practice questions from the CertStash pack of 481, each with the correct answer and a full explanation. The rule is the same for every exam: ten free on the product page, 120 free here, all 481 in the pack, and each step includes the one before it.
Get all 481 questions (US$39) · Download the first 10 as a PDF
Question 1
Insert the correct snippet so that the program produces the expected output. Expected output: Code:
Show answer and explanation
Correct answer: C. b = 0 in list
The expected output is 'True', which means the code snippet must produce a boolean True value that gets printed. Option C, 'b = 0 in list', checks if the integer 0 is a member of the list. Since list[0] is False (which is equivalent to 0 in Python), the expression '0 in list' evaluates to True because False == 0 in Python's type system, making this the correct answer.
Why the other options are wrong
- A. The expression '0 not in list' would evaluate to False (since 0 is in the list via False), not True.
- B. Assigning b = list[0] sets b to False, and printing False outputs 'False', not 'True'.
- D. Assigning b = False directly outputs 'False' when printed, not 'True'.
Question 2
Assuming that the tuple is a correctly created tuple, the fact that tuples are immutable means that the following instruction:
Show answer and explanation
Correct answer: A. is illegal
Tuples in Python are immutable, meaning their elements cannot be modified after creation. The instruction attempts to reassign my_tuple[1] by adding to it, which violates the immutability constraint. Python will raise a TypeError: 'tuple' object does not support item assignment, making this operation illegal regardless of the tuple's contents or length.
Why the other options are wrong
- B. The type of elements (strings or otherwise) is irrelevant; the immutability of tuples themselves prevents any item reassignment.
- C. The tuple can have any number of elements (including zero or one), but the operation remains illegal due to tuple immutability, not based on element count.
- D. The code is not correct; attempting to modify a tuple element will always raise a TypeError in Python.
Question 3
Take a look at the snippet, and choose the true statements: (Choose two.)
Show answer and explanation
Correct answer: B, D. nums and vals are of the same length; nums and vals refer to the same list
Line 1 creates nums as [1, 2, 3]. Line 2 assigns vals = nums, which makes vals reference the same list object as nums, not a copy. Line 3 deletes the element at index 1 (value 2) from vals, resulting in [1, 3]. Since vals and nums refer to the same list object, this deletion affects both variables equally. After the deletion, both nums and vals contain [1, 3], making them the same length (2 elements) and referring to the same list object in memory.
Why the other options are wrong
- A. nums is not longer than vals; they are the same length after the deletion operation.
- C. vals is not longer than nums; after deletion, both contain 2 elements.
Question 4
What is the output of the following snippet?
Show answer and explanation
Correct answer: D. 21
The code creates a dictionary with string keys '1' and '2', where dct['1'] = (1, 2) and dct['2'] = (2, 1). The loop iterates through dct.keys() in insertion order, so x takes values '1' then '2'. For each iteration, print(dct[x][1], end='') prints the second element of the tuple at index [1]. When x='1', dct['1'][1] = 2; when x='2', dct['2'][1] = 1. With end='' (no spaces or newlines between prints), the output is '2' followed by '1', resulting in '21'.
Why the other options are wrong
- A. This would require printing only single digits without the second iteration or concatenation logic.
- B. This is the value of dct['2'], not what gets printed by accessing the [1] index of each tuple.
- C. This is the value of dct['1'], not what the loop prints; the loop iterates through both keys.
Question 5
What will be the output of the following code snippet?
Show answer and explanation
Correct answer: A. [1, 3, 5, 7, 9]
The slice notation `a[::2]` means start from the beginning (default 0), go to the end (default length), with a step of 2. This selects every second element starting from index 0. Applied to the list [1, 2, 3, 4, 5, 6, 7, 8, 9], this returns elements at indices 0, 2, 4, 6, 8, which are [1, 3, 5, 7, 9].
Why the other options are wrong
- B. [8, 9] represents the last two elements, which would be a[7:] or a[-2:], not a[::2]
- C. [1, 2, 3] represents the first three elements, which would be a[:3], not a[::2]
- D. [1, 2] represents the first two elements, which would be a[:2], not a[::2]
Question 6
What will be the output of the following code snippet?
Show answer and explanation
Correct answer: C. 4
The code initializes an empty dictionary d, then sets d[1] = 1, d['1'] = 2, and d[1] += 1. In Python, the keys 1 (integer) and '1' (string) are distinct, so after these operations d contains {1: 2, '1': 2}. The for loop iterates over the dictionary keys and sums their corresponding values: d[1] + d['1'] = 2 + 2 = 4. Therefore, the output is 4.
Why the other options are wrong
- A. 3 would be incorrect because it doesn't account for both the integer key 1 and string key '1' being separate entries in the dictionary.
- B. 2 would only be the value of a single key, not the sum of both values in the dictionary.
- D. 1 is incorrect because the code increments d[1] from 1 to 2, and there are two keys with values to sum.
Question 7
What is the output of the following snippet?
Show answer and explanation
Correct answer: A. two
The code assigns v = dictionary['one'], which is 'two'. The loop runs len(dictionary) = 3 times, and each pass replaces v with dictionary[v] (the loop variable k is not used). Pass 1: dictionary['two'] = 'three'. Pass 2: dictionary['three'] = 'one'. Pass 3: dictionary['one'] = 'two'. The print statement outputs the final value of v, which is two.
Why the other options are wrong
- B. v holds 'one' after the second pass, but the third pass changes it to 'two'.
- C. print(v) outputs the single string stored in v, not a tuple of the dictionary keys.
- D. v holds 'three' after the first pass, but two more passes follow and end with 'two'.
Question 8
Which of the following sentences is true?
Show answer and explanation
Correct answer: B. str1 and str2 are different names of the same strings.
Strings are immutable, so a full slice such as str1[:] has no reason to build a copy. In CPython, str1[:] returns a reference to the very same string object, so after str2 = str1[:] both names refer to one string ('Peter'), and str1 is str2 evaluates to True. str1 and str2 are therefore different names of the same string.
Why the other options are wrong
- A. No second string object is created; str1 is str2 is True, so they are not two different strings that merely compare equal.
- C. str1 and str2 have identical lengths; both are 'Peter' with 5 characters.
- D. str1 and str2 have identical lengths; both are 'Peter' with 5 characters.
Question 9
The fact that tuples belong to sequence types means:
Show answer and explanation
Correct answer: D. they can be indexed and sliced like lists
Tuples are sequence types, which means they support indexing and slicing operations just like lists. However, being immutable means they cannot be modified (no del, append, or extend), and they are not lists. The defining characteristic of sequence types is the ability to access elements by index and extract sub-sequences through slicing.
Why the other options are wrong
- A. While del can be used to delete a tuple variable itself, it cannot modify tuple contents; tuples are immutable.
- B. Tuples do not have an .append() method; they are immutable and cannot be extended.
- C. Tuples are a distinct type from lists, though both are sequences.
Question 10
What is the output of the following code?
Show answer and explanation
Correct answer: C. [3, 1, 1]
Starting with my_list = [3, 1, -1], line 2 executes my_list[-1] = my_list[-2], which assigns the value at index -2 (the middle element, which is 1) to index -1 (the last element). This replaces -1 with 1, resulting in [3, 1, 1]. Line 3 prints this final list.
Why the other options are wrong
- A. Incorrect because it ignores the first element which remains 3 after the assignment operation.
- B. Incorrect because [3, -1, 1] would require the last two elements to be swapped; line 2 only copies the value at index -2 (1) into index -1, so the middle element stays 1 and the list becomes [3, 1, 1].
What people say about the full PCEP-30-02 pack 4.8 from 10
Passed my Python Institute PCEP-30-02 exam today. About 70% of the questions were similar to the real exam. The PCEP-30-02 dumps must be the essential learning resource for preparation. Thanks!
I took my PCEP-30-02 exam yesterday and passed. Your questions helped me a lot.
Updated version for PCEP-30-02 exam is valid. I passed my test smoothly. Thanks a lot.
Question 11
Select the true statements? (Choose two.)
Show answer and explanation
Correct answer: B, C. Python is a good choice for creating and executing tests for applications; Python is free, open-source, and multiplatform
Python is indeed an excellent choice for creating and executing tests for applications, with extensive testing frameworks available (pytest, unittest). Python is also free, open-source, and multiplatform, available on Windows, Linux, macOS, and other systems. Option A is incorrect because Python is a high-level language not suitable for low-level programming like driver implementation. Option D is incorrect because Python 3 is explicitly NOT backwards compatible with Python 2, significant breaking changes were introduced.
Why the other options are wrong
- A. Python is a high-level language and is not a good choice for low-level programming like driver implementation, which requires languages like C or assembly.
- D. Python 3 introduced breaking changes and is not backwards compatible with Python 2; code written for Python 2 often requires modification to run on Python 3.
Question 12
What is the output of the following snippet?
Show answer and explanation
Correct answer: C. [1, 1, 1, 1, 2, 3]
The code starts with l1 = [1, 2, 3]. The for loop iterates through range(len(l1)), which is range(3), so v takes values 0, 1, 2. On each iteration, l1.insert(1, l1[v]) inserts the element at index v into position 1. Iteration 0: insert l1[0]=1 at position 1 → [1, 1, 2, 3]. Iteration 1: insert l1[1]=1 at position 1 → [1, 1, 1, 2, 3]. Iteration 2: insert l1[2]=1 at position 1 → [1, 1, 1, 1, 2, 3]. The final output is [1, 1, 1, 1, 2, 3].
Why the other options are wrong
- A. This would be the result if insert simply appended elements without modifying the list structure during iteration.
- B. This reverses the original list first, which doesn't happen in the code; the loop always inserts the current l1[v] value which changes as the list grows.
- D. This would require the loop to reverse the insertion order or the original list, neither of which occurs in this code.
Question 13
What is the expected behavior of the following program?
Show answer and explanation
Correct answer: A. The program will output Hello! to the screen
The print() function in Python outputs the string passed to it without including the quotation marks in the output. When print("Hello!") is executed, Python evaluates the string literal "Hello!" and outputs only the text content Hello! to the screen, not the quotation marks themselves.
Why the other options are wrong
- B. The syntax is valid Python; no error will be generated.
- C. The print function outputs the string value, not the literal representation including parentheses and quotes.
- D. The output will not include the quotation marks; it will be just Hello! without quotes.
Question 14
What is the output of the following snippet if the user enters two lines containing 3 and 6 respectively?
Show answer and explanation
Correct answer: A. 333333
When the user enters '3' and '6', x becomes the string '3' (from input()) and y becomes the integer 6 (from int(input())). The expression x * y performs string repetition in Python, which repeats the string x exactly y times. Therefore, '3' * 6 produces '333333' (the string '3' repeated 6 times), which is then printed.
Why the other options are wrong
- B. This would be the result of integer multiplication (3 * 6 = 18), but x is a string, not an integer, so string repetition occurs instead.
- C. 36 would be the result of concatenating the strings '3' and '6', but the code multiplies a string by an integer, which repeats the string.
- D. This would occur if the variables were reversed (6 * 3 as a string), but x is '3' and y is 6, not the other way around.
Question 15
What is true about tuples? (Choose two.)
Show answer and explanation
Correct answer: B, C. The for loop can be used to iterate through a tuple; A one-element tuple can be coded as <1, > or 1, .
Option B: Tuples are iterable and for loops work with tuples. Option C: A one-element tuple requires a trailing comma to distinguish it from a grouped expression; both (1,) and 1, are valid syntax for a one-element tuple. Options A and D are false because tuples do not support subtraction with the - operator and lack an .append() method (tuples are immutable).
Why the other options are wrong
- A. Tuples do not support the subtraction operator; this operation is not defined for tuple objects.
- D. Tuples are immutable and do not have an .append() method; only lists have this method.
Question 16
You are writing a Python program that evaluates an arithmetic formular. The formular is described as b equals a multiplied by negative one, then raised to the second power, where a is the value that will be input and b is the result. a = eval(input('Enter a number for the equation: ')) Which of the following is a valid expression for the given requirement?
Show answer and explanation
Correct answer: A. b = (-a) ** 2
The requirement is to multiply 'a' by negative one and raise the result to the second power. This means b = (-a) ** 2, which correctly negates a first, then squares it. Option B has invalid syntax (a-). Option C raises to the power of -2 instead of squaring. Option D applies the negation after exponentiation due to operator precedence, giving -(a**2) instead of (- a)**2.
Why the other options are wrong
- B. This has invalid syntax; (a-) is not a valid expression.
- C. This raises to the power of -2, which is reciprocal of the square, not the requirement.
- D. This applies negation after squaring (-(a**2)) due to operator precedence, not before.
Question 17
What is the output of the following snippet?
Show answer and explanation
Correct answer: D. No output, the snippet is erroneous
Line 1 binds my_list to a list, but the def statement on line 4 rebinds the same name my_list to a function object. On line 9, both the called name and the argument refer to that function, so the function receives itself as its parameter. The statement del my_list[3] then tries to delete an item from a function object, which raises a TypeError, so nothing is printed.
Why the other options are wrong
- A. This would be the original list, but the code doesn't preserve the original list reference after defining the function with the same name.
- B. This assumes the deletion works but the assignment doesn't, which doesn't match the actual error that occurs.
- C. This assumes both operations succeed on a list, but the code has a fundamental error preventing any valid output.
Question 18
The ** operator:
Show answer and explanation
Correct answer: D. performs exponentiation
The ** operator performs exponentiation (raising a number to a power). For example, 2 ** 3 = 8. This is a standard arithmetic operator in Python.
Why the other options are wrong
- A. The ** operator does not perform duplicated multiplication; it performs exponentiation.
- B. The ** operator definitely exists in Python.
- C. The ** operator performs exponentiation, not floating-point multiplication.
Question 19
What is the result of the following operation?
Show answer and explanation
Correct answer: A. 2.0
In Python, the operation `1 + 1.0` performs addition between an integer (1) and a float (1.0). When an integer is added to a float, Python automatically promotes the integer to a float and performs the addition, resulting in a float value of 2.0. This is standard type coercion behavior in Python's numeric operations.
Why the other options are wrong
- B. 11.0 would result from string concatenation of '1' and '1.0', but the operands are numeric types, not strings.
- C. The operation is completely legal in Python; mixed-type numeric operations between int and float are fully supported.
- D. While the mathematical result is 2, Python returns 2.0 (a float) not 2 (an int) due to the presence of the float operand 1.0.
Question 20
What is the output of the following snippet?
Show answer and explanation
Correct answer: D. 1
The list is [3, 1, -2], so my_list[-1] refers to the last element, which is -2. That value is then used as the index in the outer subscription, making the expression my_list[-2], which refers to the second element from the end of the list. Counting backwards, my_list[-1] is -2 and my_list[-2] is 1, so print outputs 1.
Why the other options are wrong
- A. -2 is only the value of the inner expression my_list[-1], which serves as the index, not the final printed result.
- B. 3 would be my_list[0] or my_list[-3], but the computed index is -2, not 0 or -3.
- C. -1 is a literal index written in the code, not any element stored in the list.
Question 21
UNICODE is a standard:
Show answer and explanation
Correct answer: D. like ASCII, but much more expansive.
Unicode is a character encoding standard that, like ASCII, assigns numerical values to characters. However, Unicode is far more expansive than ASCII, supporting over 1 million characters from virtually all written languages and scripts in the world, whereas ASCII is limited to 128 basic characters.
Why the other options are wrong
- A. Unicode is a character encoding standard, not a standard for coding floating-point numbers.
- B. Unicode is an international standard used by coders worldwide, not limited to universities.
- C. While Unicode is widely used, it is not accurately described as being 'honored by the whole universe.'
Question 22
Which of the following sentences are true about the code? (Choose two.)
Show answer and explanation
Correct answer: C, D. nums and vals are different names of the same list; nums and vals have the same length
In Python, when you assign one variable to another using the syntax `vals = nums`, you are not creating a new list; instead, you are creating a new reference to the same list object in memory. Both `nums` and `vals` point to the identical list `[1, 2, 3]`. Therefore, they are different names for the same list object (option C is true), and they have the same length since they refer to the same underlying list (option D is true). This is a fundamental concept in Python called reference assignment, as opposed to creating a copy of the list.
Why the other options are wrong
- A. nums and vals are not different lists; they are two references to the same list object in memory.
- B. vals is not longer than nums because both refer to the same list with length 3.
Question 23
What is the data type of x, y, z after executing the following snippet?
Show answer and explanation
Correct answer: D. int, str, str
Line 1: x = 23 + 42 adds two integers, so x is an int (65). Line 2: y = '23' + '42' concatenates two strings, so y is a str ('2342'). Line 3: z = '23' * 7 repeats the string '23' seven times, so z is a str ('23232323232323'). Therefore x is int, y is str and z is str.
Why the other options are wrong
- A. y and z are strings, not integers; the quoted operands make the + and * operations string operations.
- B. y and z are valid assignments; string concatenation and string repetition are both valid Python operations.
- C. z is not an int: multiplying the string '23' by the integer 7 performs string repetition and produces a str.
Question 24
The result of the following division:
Show answer and explanation
Correct answer: B. is equal to 1.0
The expression 1 / 1 evaluates to 1.0 because in most modern programming languages and mathematical contexts, division operations return a floating-point result. Even though both operands are integers (1 and 1), the division operator produces a float type result. The division of 1 by 1 equals 1, but when expressed as a floating-point number, it is represented as 1.0. This is the standard behavior in languages like Python 3, Java, and JavaScript, where the division operator (/) returns a float rather than performing integer division.
Why the other options are wrong
- A. The division can be evaluated; 1 divided by 1 is a valid mathematical operation with a defined result.
- C. While mathematically 1÷1=1, the result of the division operator in most programming contexts returns a float type (1.0) rather than an integer (1).
- D. The result is predictable and consistent; 1/1 always equals 1.0 in standard division operations.
Question 25
What is the expected behavior of the following program?
Show answer and explanation
Correct answer: A. The program will generate an error message on the screen
The code calls prin(), not print(). There is no function named prin, so Python raises a NameError and shows an error message instead of printing Goodbye!
Why the other options are wrong
- B. Nothing is printed, because prin is not defined.
- C. Nothing is printed, because prin is not defined.
- D. Nothing is printed, because prin is not defined.
Question 26
A way of passing arguments in which the order of the arguments determines the initial parameter's values is referred to as:
Show answer and explanation
Correct answer: A. positional
Positional arguments are the standard term in programming for arguments where the position in the argument list determines which parameter they are assigned to. This is the formal and widely recognized terminology used in Python documentation and computer science. Sequential and ordered are colloquial alternatives but not the correct technical term.
Why the other options are wrong
- B. While sequential describes the concept, positional is the correct technical term used in Python documentation.
- C. Ordered is too vague and not the formal terminology; positional is the precise term used in function definition contexts.
Question 27
Python is an example of which programming language category?
Show answer and explanation
Correct answer: C. interpreted
Python is an interpreted programming language, meaning the source code is translated into machine code and executed line-by-line at runtime by an interpreter, rather than being compiled into an executable file beforehand.
Why the other options are wrong
- A. Assembly is a low-level language much closer to machine code.
- B. Machine language is binary code directly executed by processors.
- D. Python is not a compiled language; it is interpreted.
Question 28
Right-sided binding means that the following expression will be evaluated:
Show answer and explanation
Correct answer: A. from right to left
Right-sided binding, also known as right associativity, means that operators are evaluated from right to left. When an operator has right associativity, expressions are grouped from the rightmost operator first, then proceed toward the left. For example, in an expression like a op b op c with right associativity, it is evaluated as a op (b op c) rather than (a op b) op c, which means processing starts from the right side and moves leftward.
Why the other options are wrong
- B. Random order evaluation would be unpredictable and is not how operator associativity works in any programming language.
- C. Left to right evaluation describes left-sided binding (left associativity), which is the opposite of right-sided binding.
Question 29
A data structure described as LIFO is actually a:
Show answer and explanation
Correct answer: A. stack
LIFO stands for Last-In-First-Out, which is the defining characteristic of a stack data structure. In a stack, the last element added is the first one to be removed, like a stack of plates where you take from the top.
Why the other options are wrong
- B. Trees are hierarchical structures, not LIFO-based.
- C. Lists are ordered collections but do not have LIFO semantics; they allow access to any element.
- D. Heaps are specialized tree structures used for priority queues, not LIFO structures.
Question 30
What is the output of the following snippet?
Show answer and explanation
Correct answer: A. 9
The function fun(x, y, z) returns x + 2*y + 3*z. On line 4, it's called with fun(0, z=1, y=3), which uses keyword arguments. Despite the parameter order in the function definition being (x, y, z), keyword arguments can be passed in any order. Therefore: x=0, y=3, z=1. The calculation is 0 + 2*3 + 3*1 = 0 + 6 + 3 = 9.
Why the other options are wrong
- B. The snippet is syntactically valid; Python supports keyword arguments in any order, so there is no error.
- C. The calculation yields 9, not 0, since y=3 and z=1 are properly assigned via keyword arguments.
- D. The result is 9, not 3; the formula x + 2*y + 3*z with the given values produces 0 + 6 + 3 = 9.
Question 31
What do you call a computer program which directly executes instructions written in a programming language?
Show answer and explanation
Correct answer: B. An interpreter
An interpreter is a program that directly executes instructions written in a programming language line by line at runtime, without requiring a separate compilation step. A compiler, by contrast, converts source code into machine code before execution, and a translator is a more general term that could apply to either.
Why the other options are wrong
- A. A compiler converts code to machine code first rather than directly executing it.
- C. Translator is too vague and general; the specific term for direct execution is interpreter.
Question 32
An operator able to check whether two values are not equal is coded as:
Show answer and explanation
Correct answer: B. !=
In Python, the inequality operator is `!=`. This checks whether two values are not equal and returns a boolean. Option B is the standard Python syntax for the "not equal" comparison operator.
Why the other options are wrong
- A. <> was used in older languages like Pascal but is not valid Python syntax.
- C. =/= is not valid Python syntax for any operator.
- D. While `not ==` works as a logical expression, it is not the standard operator for inequality; `!=` is the proper operator.
Question 33
What are the four fundamental elements that make a language?
Show answer and explanation
Correct answer: A. An alphabet, a lexis, a syntax, and semantics
The four fundamental elements that comprise any language are: an alphabet (the set of characters used), a lexis (vocabulary or lexicon), a syntax (rules for combining words into valid expressions), and semantics (the meaning of expressions). These elements apply to both natural and programming languages.
Why the other options are wrong
- B. Morphology and phonetics are linguistic concepts related to natural languages, not fundamental language elements; programming languages do not have phonetics.
- C. Phonetics is a property of spoken natural languages and is not a fundamental element of programming languages.
- D. Phonetics and phonology are properties of natural languages and are not applicable to programming languages as fundamental elements.
Question 34
The += operator, when applied to strings, performs:
Show answer and explanation
Correct answer: B. Concatenation
The += operator applied to strings performs concatenation, combining two strings together. For example, 'hello' += ' world' results in 'hello world'. This is string concatenation, not subtraction or multiplication.
Why the other options are wrong
- A. The += operator does not perform subtraction on strings.
- C. While * performs repetition on strings, += performs concatenation.
Question 35
Which one of the lines should you put in the snippet below to match the expected output? Expected output: Code:
Show answer and explanation
Correct answer: C. list.reverse()
The expected output shows the original list ['A', 2, 7, 1, 4] in reverse order: [4, 1, 7, 2, 'A']. The list.reverse() method reverses the items of the list in place, so the list object itself is changed. The method returns None, but its return value is not used here; print(list) then displays the reversed list.
Why the other options are wrong
- A. reverse() is not a built-in function in Python; it would raise a NameError.
- B. list.reversed() is not a valid Python method; the correct method name is reverse() without the 'd'.
- D. reversed(list) returns a new reverse iterator and does not change the list. Because its result is discarded, print(list) would output the unchanged ['A', 2, 7, 1, 4].
Question 36
Which of the following is the correct order of operator precedence?
Show answer and explanation
Correct answer: C. Parentheses Exponents Unary positive, negative, not Multiplication and Division Addition and Subtraction And
Python operator precedence from highest to lowest is: Parentheses, Exponents, Unary operators (positive, negative, not), Multiplication and Division (left to right), Addition and Subtraction (left to right), and Boolean And. Option C correctly lists this order.
Why the other options are wrong
- A. This places Parentheses too late in the order; they should have the highest precedence.
- B. This incorrectly places Addition and Subtraction before Multiplication and Division, violating standard precedence.
- D. This places Parentheses at the end, which is incorrect; they should have the highest precedence.
Question 37
You develop a Python application for your company. You have the following code. Which of the following expressions is equivalent to the expression in the function?
Show answer and explanation
Correct answer: B. (a + (b * c)) - d)
The function calculates `value = a + b * c - d`. Following Python's operator precedence rules, multiplication and division are performed before addition and subtraction, from left to right. Therefore, `b * c` is evaluated first, then the result is added to `a`, and finally `d` is subtracted from that sum. This gives us `(a + (b * c)) - d`, which matches option B exactly.
Why the other options are wrong
- A. This expression uses parentheses to force addition before multiplication, which violates standard operator precedence and would produce an incorrect result.
- C. This option has unbalanced parentheses (extra closing parenthesis) and is syntactically invalid.
- D. Option B is the correct equivalent expression, so D is incorrect.
Question 38
A keyword is a word: (Choose two.)
Show answer and explanation
Correct answer: A, B. that cannot be used as a variable name; that cannot be used as a function name
Keywords are reserved words in Python with special meaning that cannot be used as variable names (e.g., if, while, def) or function names. Option A is correct because keywords are protected identifiers. Option B is correct because keywords cannot be reassigned as function names either. Option C is incorrect because keywords are not necessarily 'the most important word', they simply have syntactic roles reserved by the language.
Why the other options are wrong
- C. Keywords have designated language functions, not universal importance in program logic.
Question 39
Which of the following expressions evaluate to a non-zero result? (Choose two.)
Show answer and explanation
Correct answer: A, D. 4 / 2 ** 3 ‒ 2; 2 ** 3 / 4 ‒ 1
Evaluating each expression: A) 4 / 2 ** 3, 2 = 4 / 8, 2 = 0.5, 2 = –1.5 (non-zero) ; B) 2 ** ✓ 3 / 4, 2 = 8 / 4, 2 = 2, 2 = 0 (zero); C) 1 * 4 // 2 ** 3 = 4 // 8 = 0 (zero); D) 2 ** 3 / 4, 1 = 8 / 4, 1 = 2, 1 = 1 (non-zero) . Expressions A and D both evaluate to non-zero results. ✓
Why the other options are wrong
- B. This expression evaluates to 0: exponentiation first (2**3=8), then division (8/4=2), then subtraction (2-2=0).
- C. This expression evaluates to 0: exponentiation first (2**3=8), then multiplication and floor division (1*4//8=4//8=0).
Question 40
How would you remove all the items from the d dictionary? Expected output: Code:
Show answer and explanation
Correct answer: D. d.clear()
The clear() method is the correct way to remove all items from a dictionary in Python. When you call d.clear(), it empties the dictionary while keeping the dictionary object itself intact. This is the standard and proper method for this operation in Python.
Why the other options are wrong
- A. del() is not a valid dictionary method; there is no such method as d.del()
- B. remove() is a method for lists and sets, not dictionaries; it does not exist for dict objects
- C. del d would delete the entire dictionary variable itself, not just clear its contents, and would cause a NameError if d is referenced afterward
Question 41
What is the output of the following snippet?
Show answer and explanation
Correct answer: C. 17.0
Multiplication has higher precedence than addition, so the expression is evaluated as 2 + (3 * 5.). The literal 5. is a float (a decimal point with an empty fraction part is equivalent to 5.0), so 3 * 5. gives 15.0, and 2 + 15.0 gives 17.0. The snippet prints 17.0.
Why the other options are wrong
- A. 25.0 would require (2 + 3) * 5., but multiplication is evaluated before addition.
- B. The snippet is valid Python; 5. is a legal float literal and runs without error.
- D. 17 would be printed only if all operands were integers, but 5. is a float, so the result is the float 17.0.
Question 42
The second assignment:
Show answer and explanation
Correct answer: B. doesn't change the list's length
The second line is a tuple assignment: the right-hand side (vals[1], vals[2]) is evaluated first, giving (1, 2), and then those values are assigned from left to right to vals[0] and vals[1]. Only existing positions are replaced; no element is added or removed. After execution vals is [1, 2, 2], and its length is still 3.
Why the other options are wrong
- A. Tuple unpacking assignment does not add new elements to the list; it only modifies existing values.
- C. The assignment does not remove any elements from the list; all three original positions remain.
Question 43
How many elements does the L list contain?
Show answer and explanation
Correct answer: D. zero
The range(-1, -2) function creates a range starting at -1 and ending before -2. Since -1 is already greater than -2, the range is empty and produces no values. Therefore, the list comprehension [i for i in range(-1, -2)] iterates over zero elements, resulting in an empty list L with zero elements.
Why the other options are wrong
- A. The range(-1, -2) does not produce any single element; it produces nothing because the start value is already past the stop value.
- B. The range(-1, -2) is empty, not a two-element range. With the default step of 1 the start value -1 is already past the stop value -2; a negative step, as in range(-1, -2, -1), would be needed to get any element.
- C. The range(-1, -2) does not contain three or any elements; the start value -1 is not less than the stop value -2.
Question 44
A process in which the source code is translated into machine code in order to be executed later is called:
Show answer and explanation
Correct answer: D. compilation
Compilation is the process where source code is translated into machine code by a compiler, creating an executable file that can be run later. This is distinct from interpretation, where code is translated and executed line-by-line at runtime.
Why the other options are wrong
- A. Linking is a separate step that follows compilation, combining compiled code with libraries.
- B. Edition is not a standard term for code translation processes.
- C. Interpretation is the opposite process, where code is translated and executed simultaneously rather than translated first.
Question 45
What value will be assigned to the x variable?
Show answer and explanation
Correct answer: A. False
Given z=3 and y=7, the expression evaluates as follows: x = y == z and y > z or z > y and z != y. First, evaluate the comparisons: y == z is False (7 ≠ 3), y > z is True (7 > 3), z > y is False (3 < 7), and z != y is True (3 ≠ 7). The expression becomes: x = False and True or False and True. Evaluating with proper precedence (and before or): x = False or False, which results in False.
Why the other options are wrong
- B. The integer 0 is not assigned; the expression evaluates to a boolean value, not an integer.
- C. The integer 1 is not assigned; the expression evaluates to a boolean value, not an integer.
- D. The expression evaluates to False, not True, because both sides of the final 'or' operation produce False.
Question 46
Consider the following code. Which statement will print True to the monitor? (Choose two.)
Show answer and explanation
Correct answer: A, C. print(languages == more_languages); print(more_languages is extra_languages)
Option A is correct because `languages` and `more_languages` contain identical list values with the same elements in the same order, so the `==` operator returns True. Option C is correct because `extra_languages` is assigned the reference to `more_languages` (line 3), making them point to the same object in memory, so the `is` operator returns True. Option B is incorrect because `languages` and `more_languages` are separate list objects with different memory addresses despite having equal contents, so `is` returns False. Option D is incorrect because `languages` and `extra_languages` are different objects, `extra_languages` points to the same object as `more_languages`, not `languages`, so `is` returns False.
Why the other options are wrong
- B. Although languages and more_languages have equal values, they are different list objects in memory, so the `is` operator returns False.
- D. extra_languages references the same object as more_languages, not languages, so `languages is extra_languages` returns False.
Question 47
How many elements does the my_list list contain?
Show answer and explanation
Correct answer: C. two
The list comprehension `[0 for i in range(1, 3)]` creates a list by iterating through `range(1, 3)`. The `range(1, 3)` function generates integers from 1 up to but not including 3, which means it produces the values 1 and 2. For each iteration, the value 0 is appended to the list. Therefore, the list contains two elements: `[0, 0]`.
Why the other options are wrong
- A. The range(1, 3) produces two values (1 and 2), not one, so the list has two elements, not one.
- B. The range(1, 3) excludes 3, so it only generates two values (1 and 2), resulting in a list with two elements, not three.
Question 48
After execution of the following snippet, the sum of all vals elements will be equal to:
Show answer and explanation
Correct answer: A. 4
The list starts as [0, 1, 2]. The call vals.insert(0, 1) places the value 1 at index 0, shifting the other elements right, producing [1, 0, 1, 2]. The statement del vals[1] removes the element at index 1, which is the 0, leaving [1, 1, 2]. Summing those remaining elements gives 1 + 1 + 2 = 4.
Why the other options are wrong
- B. No step of the snippet produces a sum of 5: after the insert the list [1, 0, 1, 2] sums to 4, and deleting the 0 at index 1 leaves [1, 1, 2], which still sums to 4.
- C. A sum of 2 would mean only the final element survived; the deletion removes just a single item at index 1.
- D. A sum of 3 would result from deleting the 1 rather than the 0, but index 1 after insertion holds the value 0.
Question 49
What is true about compilation? (Choose two.)
Show answer and explanation
Correct answer: A, B. The code is converted directly into machine code executable by the processor; It tends to be faster than interpretation
Compilation converts source code directly into machine code that can be executed by the processor, and compiled code typically runs faster than interpreted code because the translation happens once before execution rather than during runtime. Only the developer needs the source code; end users receive the compiled executable.
Why the other options are wrong
- C. Compilation tends to be faster than interpretation, not slower.
- D. Only the developer must have the code; the end user receives the compiled executable.
Question 50
A value returned by the input() function is:
Show answer and explanation
Correct answer: C. a string
The input() function in Python returns user input as a string, regardless of what the user types. Even if the user enters a number, it is returned as a string and must be explicitly converted using int() or float() if needed.
Why the other options are wrong
- A. input() returns a string, not an integer, even if numeric digits are entered.
- B. input() returns a string, not a float, even if decimal values are entered.
Question 51
What is the default return value for a function that does not explicitly return any value?
Show answer and explanation
Correct answer: D. None
In Python, when a function does not explicitly return a value using a return statement, it automatically returns None, which is Python's null value. None is a special object that represents the absence of a value.
Why the other options are wrong
- A. void is used in languages like Java and C, not Python.
- B. Null is used in some other languages; Python uses None.
- C. public is an access modifier keyword, not a return value.
- E. int is a data type, not a default return value.
Question 52
An alternative name for a data structure called a stack is:
Show answer and explanation
Correct answer: A. LIFO
A stack is a LIFO (Last-In-First-Out) data structure. LIFO is the fundamental principle and alternative name for a stack, where the most recently added element is removed first.
Why the other options are wrong
- B. FIFO stands for First-In-First-Out, which describes a queue, not a stack.
- C. FOLO is not a recognized data structure acronym.
Question 53
Insert the correct snippet to convert the t tuple to a dictionary named d. Expected output: Code:
Show answer and explanation
Correct answer: B. d = dict(t)
The dict() constructor in Python accepts an iterable of key-value pairs (like a tuple of tuples) and converts it to a dictionary. Given t = (('A', 1), ('B', 2), ('C', 3)), calling dict(t) creates a dictionary with keys 'A', 'B', 'C' mapped to values 1, 2, 3 respectively. This is the standard and correct Python syntax for this conversion.
Why the other options are wrong
- A. The >> operator is a bitwise right-shift operator in Python and is not used for type conversion or dictionary creation.
- C. This syntax d.dict(t) attempts to call a method named 'dict' on the variable d, but d doesn't exist yet and dict is not a method of dictionary objects.
- D. This syntax d = t(dict) attempts to call t as a function with dict as an argument, but tuples are not callable in this manner.
Question 54
The print() function can output values of:
Show answer and explanation
Correct answer: A. any number of arguments (including zero)
The print() function can accept any number of arguments, including zero arguments (which would print just a blank line). This flexibility allows print() to output nothing, one value, or multiple comma-separated values.
Why the other options are wrong
- B. print() can be called with zero arguments, producing a blank line.
- C. There is no limit of five arguments; print() accepts any number of arguments.
- D. print() can output multiple arguments, not just one.
Question 55
Which of the following expressions evaluate to a non-zero result? (Choose two.)
Show answer and explanation
Correct answer: B, D. 2 +, 2 / 2 + 2; 2 + 2 * 2 // 2
Evaluating each expression: A) 2, 2 * 2 + 2 = 2, 4 + 2 = 0 (zero); B) 2 + (–2 / 2) + 2 = 2 + (–1) + 2 = 3 (non-zero) ; C) 2 // 2 * 2, 2 = 1 * 2, 2 = 0 (zero); D) 2 + 2 * 2 // 2 = 2 + 4 // 2 = ✓ 2 + 2 = 4 (non-zero) . Expressions B and D both evaluate to non-zero results. ✓
Why the other options are wrong
- A. This expression evaluates to 0: following order of operations, multiplication occurs first (2*2=4), then subtraction and addition from left to right (2-4+2=0).
- C. This expression evaluates to 0: floor division and multiplication occur first (2//2*2=2), then subtraction (2-2=0).
Question 56
isalnum() checks if a string contains only letters and digits, and this is:
Show answer and explanation
Correct answer: C. A method
isalnum() is a method because it is called on a string object using dot notation (e.g., my_string.isalnum()). Methods are functions that belong to objects and are invoked with the dot operator. It is neither a standalone module nor a function, but rather a method of the string class.
Why the other options are wrong
- A. A module is a file or library that contains code; isalnum() is not a module.
- B. While isalnum() is callable like a function, it is specifically a method because it belongs to the string class and is invoked on string objects.
Question 57
The escape character owes its name to the fact that it:
Show answer and explanation
Correct answer: C. changes the meaning of the character next to it
The escape character owes its name to the fact that it changes the meaning of the character that immediately follows it. For example, the backslash (\) in '\n' escapes the 'n' to represent a newline instead of the literal character 'n'. The escape character modifies the interpretation of the next character in a string.
Why the other options are wrong
- A. The escape character is not related to speed or being 'caught'; this is a metaphorical misinterpretation.
- B. The escape character does not escape from source files into memory; it escapes the literal meaning of characters within code.
Question 58
What is the output of the following code?
Show answer and explanation
Correct answer: B. 2
Starting with a=1 and b=0: x evaluates to (a or b) = (1 or 0) = 1. Then y evaluates to not(a and b) = not(1 and 0) = not(0) = 1. Finally, print(x + y) outputs 1 + 1 = 2.
Why the other options are wrong
- A. The output is completely predictable given the deterministic values and logical operations.
- C. The code is syntactically valid and will execute without errors.
- D. The result is 2, not 1, because both x and y evaluate to 1 and are added together.
Question 59
A set of rules which defines the ways in which words can be coupled in sentences is called:
Show answer and explanation
Correct answer: C. syntax
Syntax refers to the set of rules that define how words and symbols can be properly combined to form valid statements in a language. It governs the structure and arrangement of code elements, determining what constitutes valid or invalid code.
Why the other options are wrong
- A. A dictionary is a collection of words and their definitions, not rules for combining words.
- B. Semantics refers to the meaning of code, not the rules for structuring it.
- D. Lexis refers to vocabulary or the collection of words, not the rules for combining them.
Question 60
Which of the following lines correctly invoke the function defined below: (Choose two.)
Show answer and explanation
Correct answer: A, C. fun(b=0, a=0); fun(0, 1, 2)
The function needs the parameters a and b. Only c has a default value. fun(b=0, a=0) passes both by name, and fun(0, 1, 2) passes all three by position. fun(b=1) is missing a, and fun() is missing a and b, so both raise a TypeError.
Why the other options are wrong
- B. a has no default value, so fun(b=1) raises a TypeError.
- D. a and b have no default values, so fun() raises a TypeError.
Question 61
Which of the following variable names are illegal? (Choose two.)
Show answer and explanation
Correct answer: A, C. True; and
In Python, both 'True' (capitalized) and 'and' are reserved keywords and cannot be used as variable names. 'True' is a boolean keyword, and 'and' is a logical operator keyword. While 'true' and 'TRUE' are not reserved words in Python, they can be assigned as variables since Python is case-sensitive and only the exact keyword 'True' is reserved.
Why the other options are wrong
- B. 'true' is not a Python keyword; only 'True' with capital T is reserved.
- D. 'TRUE' is not a Python keyword; only 'True' with capital T is reserved.
Question 62
The folder created by Python used to store pyc files is named:
Show answer and explanation
Correct answer: C. __pycache__
Python stores compiled bytecode files (.pyc) in a folder named __pycache__. This directory is automatically created in the same location as the source files and contains optimized versions of imported modules, allowing Python to skip recompilation on subsequent runs if the source hasn't changed.
Why the other options are wrong
- A. __pyc__ is not the correct directory name used by Python.
- B. __pycfiles__ is not the correct directory name used by Python.
- D. __cache__ is not the specific directory name Python uses for bytecode storage.
Question 63
What value will be assigned to the x variable?
Show answer and explanation
Correct answer: D. True
Line 3 contains a complex boolean expression that evaluates x. Given z = 2 and y = 1, we evaluate: (x = y < z) or (z > y and y > z) or (z < y). First part: 1 < 2 = True. Second part: 2 > 1 and 1 > 2 = True and False = False. Third part: 2 < 1 = False. Overall: True or False or False = True. Therefore, x is assigned the boolean value True.
Why the other options are wrong
- A. False would result if all conditions evaluated to false, but the first condition (y < z) is true since 1 < 2.
- B. The integer 1 is not assigned; the expression evaluates to a boolean value, not an integer.
- C. The integer 0 is not assigned; the expression evaluates to a boolean value, not an integer.
Question 64
What would you insert instead of ???, so that the program prints True to the monitor?
Show answer and explanation
Correct answer: C. x is y
In Python, both x and y reference the same string literal 'Peter'. Due to string interning, Python caches small strings and reuses them in memory, so x and y point to the same object in memory. The `is` operator checks object identity (whether two variables reference the exact same object), while `==` checks value equality. Since x and y reference the identical string object, `x is y` evaluates to True, which is what will be printed.
Why the other options are wrong
- A. x < y compares string values lexicographically; since both strings are identical, this returns False, not True.
- B. x is not y checks if the objects are different; since they reference the same interned string object, this returns False, not True.
- D. x != y checks value inequality; since both variables contain the same string value 'Peter', this returns False, not True.
Question 65
The digraph written as #! is used to:
Show answer and explanation
Correct answer: B. tell a Unix or Unix-like OS how to execute the contents of a Python file.
The #! digraph is called a shebang or hashbang and is used in Unix and Unix-like operating systems (Linux, macOS, etc.) to specify the interpreter that should execute a script file. When placed on the first line of a script, it tells the OS which program to use to run the file. Windows does not use shebangs for this purpose.
Why the other options are wrong
- A. MS Windows uses file extensions and the registry to determine how to execute files, not shebangs.
- C. A docstring in Python is created using triple quotes (''' or """), not the #! digraph.
- D. Privacy conventions in Python use a single underscore prefix (_variable), not the #! digraph.
Question 66
Evaluate the following Python arithmetic expression: (3 * (1 + 2) ** 2 - (2 ** 2) * 3) What is the result?
Show answer and explanation
Correct answer: D. 15
Parentheses are resolved first: (1 + 2) becomes 3 and (2 ** 2) becomes 4. Exponentiation binds tighter than multiplication, so 3 ** 2 gives 9, then 3 * 9 = 27. The second term is 4 * 3 = 12. Subtracting, 27 - 12 = 15.
Why the other options are wrong
- A. 13 would result from mis-multiplying the terms; the correct products are 27 and 12.
- B. 3 would come from ignoring the exponentiation entirely.
- C. 69 would require applying the exponent after the multiplication, e.g. (3*3)**2 style grouping, which the parentheses forbid.
Question 67
What is the output of the following snippet?
Show answer and explanation
Correct answer: B. 0
The code initializes x to 1, then iterates through my_list = [0, 1, 2, 3]. In the first iteration, elem = 0, so x *= 0 makes x = 1 * 0 = 0. In all subsequent iterations, x remains 0 because 0 multiplied by any number is 0. Therefore, the final value of x printed is 0.
Why the other options are wrong
- A. x equals 1 only before the loop; after multiplying by 0 in the first iteration, x becomes 0 and stays 0.
- C. 6 would be the sum of the list elements (0+1+2+3), but the operation is multiplication (*=), not addition, and the starting value is 1, not 0.
Question 68
What is the output of the following code?
Show answer and explanation
Correct answer: D. True
The variable c is assigned the result of the comparison a > b, which evaluates 10 > 20 and yields False. The not operator inverts the truth value of its operand, so not c becomes not False, which is True. print() therefore outputs True.
Why the other options are wrong
- A. All statements are valid Python: an integer comparison and a logical negation both execute without raising any exception.
- B. False is the value stored in c; the not operator flips it before printing, so False is not what appears.
- C. None would only be printed if a value of None were produced, but a comparison always returns a Boolean and not always returns a Boolean.
Question 69
Take a look at the snippet and choose one of the following statements which is true:
Show answer and explanation
Correct answer: B. nums and vals are of the same length
Line 1 initializes nums as an empty list. Line 2 assigns vals = nums, which in Python creates a reference to the same list object (not a copy). Line 3 appends 1 to vals. Since vals and nums reference the identical list object, appending to vals modifies the list that nums also points to. After execution, both nums and vals reference [1], giving them equal length of 1.
Why the other options are wrong
- A. vals is not longer than nums because they reference the same list object; the append affects both equally.
- C. nums is not longer than vals; they reference the same list and have identical length.
Question 70
Consider the following code. What does the second assignment do?
Show answer and explanation
Correct answer: B. It reverses the list.
The second assignment evaluates the right-hand side first, giving the pair (2, 0), and then assigns 2 to x[0] and 0 to x[2]. The first and last items are swapped, so x changes from [0, 1, 2] to [2, 1, 0]. For this three-item list, swapping the first and last items reverses the list.
Why the other options are wrong
- A. No items are removed; the list still has three items.
- C. The list does change: its contents go from [0, 1, 2] to [2, 1, 0].
- D. No items are added; the list still has three items.
Question 71
You create a function to calculate the power of a number by using Python. You need to ensure that the function is documented with comments. You create the following code. Line numbers are included for reference only. Which of the following statements are true? (Choose two.)
Show answer and explanation
Correct answer: C, D. Line 07 contains an inline comment; Lines 01 through 04 will be ignored for syntax checking.
Line 07 contains an inline comment because the '#' character appears after executable code on the same line (after 'return x ** y'), making it an inline comment that explains that specific line of code. Lines 01 through 04 are all comment lines (prefixed with '#') and will be ignored during syntax checking, Python only checks syntax for actual code statements, not comments. These comments serve as documentation but don't affect the program's execution or syntax validation.
Why the other options are wrong
- A. The text in Line 06 is a string literal assigned to the variable comment. The # inside the quotes is part of the string, not the start of a comment, so the line is ordinary code.
- B. The pound sign (#) is mandatory for lines 02 and 03; without it, those lines would be interpreted as invalid Python syntax, not as comments.
Question 72
Assuming that the following assignment has been successfully executed: my_list = [5, 4, 3, 2] Select the expressions which will not raise any exception. (Choose two.)
Show answer and explanation
Correct answer: C, D. my_list[my_list[–1]]; my_list[1:1]
Given my_list = [5, 4, 3, 2], my_list[1:1] (option D) returns an empty list without raising an exception because slicing gracefully handles out-of-range indices. my_list[my_list[-1]] evaluates to my_list[2], which equals 3 and is valid (option C). my_list[4] is out of range (option A raises IndexError), and my_list[-5] is also out of range (option B raises IndexError).
Why the other options are wrong
- A. Index 4 is out of bounds for a 4-element list, raising an IndexError.
- B. Index -5 is out of bounds; only indices -4 to -1 are valid for a 4-element list, raising an IndexError.
Question 73
What do you call a command-line interpreter which lets you interact with your OS and execute Python commands and scripts?
Show answer and explanation
Correct answer: A. A console
A console (or command-line interpreter/shell) is the interface that allows users to interact with the operating system and execute commands and scripts, including Python commands. A compiler translates code; Jython is a Python implementation for the Java platform; and an editor is a text tool, not an execution environment.
Why the other options are wrong
- B. A compiler translates code rather than providing an interactive command-line interface.
- C. Jython is a Python implementation for Java, not a command-line interpreter for the OS.
- D. An editor is a tool for writing code, not for executing commands interactively.
Question 74
What is the output of the following code?
Show answer and explanation
Correct answer: B. 22
x is the string "2". Multiplying a string by an integer repeats it, so 2 * x is "22", and print(y) outputs 22.
Why the other options are wrong
- A. x is a variable name, not text; its value "2" is what gets repeated.
- C. Multiplying a string by an integer is valid Python and raises no error.
- D. x is a string, not a number, so 2 * x repeats the text instead of doing arithmetic.
Question 75
What is the result of the following code?
Show answer and explanation
Correct answer: B. 2
The code executes as follows: Line 1 sets x = 7. Line 2 calculates y = x % 2, which is 7 % 2 = 1 (remainder when 7 is divided by 2). Line 3 increments y by 1, so y += 1 makes y = 1 + 1 = 2. Line 4 prints y, which outputs 2.
Why the other options are wrong
- A. This would be the value of y after line 2 before the increment operation on line 3.
- C. This is not produced by any calculation in this code sequence.
- D. This is not produced by any calculation in this code sequence.
Question 76
Python is an example of:
Show answer and explanation
Correct answer: C. a high-level programming language
Python is a high-level programming language, meaning it abstracts away low-level hardware details and provides a syntax closer to human-readable English. It is interpreted and dynamically typed, making it accessible to programmers while maintaining powerful capabilities.
Why the other options are wrong
- A. Machine language consists of binary code (1s and 0s) that directly executes on processors; Python is much higher-level than this.
- B. Natural languages are human languages used for communication; programming languages like Python have formal, strict syntax rules and are designed for computers.
Question 77
What is the expected behavior of the following snippet? It will:
Show answer and explanation
Correct answer: E. print 8
The code executes as follows: x is initialized to 1 on line 1. On line 8, x is reassigned to 2 + a(x), where a(x) calls the function a with x=1. The function a(1) returns 2*1=2. Therefore, x becomes 2 + 2 = 4. On line 9, a(x) is called with x=4, which returns 2*4=8. This value 8 is printed.
Why the other options are wrong
- A. Line 9 does not cause a runtime exception; the function call a(4) executes successfully.
- B. Line 8 does not cause a runtime exception; the assignment and function call execute without error.
- C. The value printed is not 6; a(4) returns 2*4=8, not 6.
- D. The value printed is not 4; line 9 prints the result of a(x) where x is now 4, which is 8, not 4.
Question 78
The print() function is an example of:
Show answer and explanation
Correct answer: A. a Python built-in function
The print() function is a built-in function provided by Python as part of its standard library and is always available without needing to be defined. It is not user-defined, not anonymous, and while it can be called as a method on objects in some contexts, the primary classification is as a built-in function.
Why the other options are wrong
- B. print() is provided by Python itself, not defined by the user.
- C. print() is a named function with a specific name and documentation, not anonymous.
- D. While print() can be used in method-like contexts, it is fundamentally a built-in function, not primarily a method.
Question 79
What code would you insert instead of the comment to obtain the expected output? Expected output: Code:
Show answer and explanation
Correct answer: A. print(k[0])
Each dictionary value is a one-item tuple such as ('a',), so k holds that tuple. The expression k[0] is the string inside it, and the loop prints a, b and c on separate lines, which matches the expected output.
Why the other options are wrong
- B. print(k) prints the whole tuple, so the output starts with ('a',).
- C. A tuple index must be an integer or slice, so k['0'] raises TypeError.
- D. A tuple index must be an integer or slice, so k["0"] raises TypeError.
Question 80
You have the following file. index.py: You run the file by executing the following command in the terminal. python index.py Hello What is the expected oputput?
Show answer and explanation
Correct answer: A. index.py
sys.argv is a list of the command-line arguments passed to the interpreter, and by convention argv[0] holds the name of the script being executed. Running python index.py Hello therefore populates argv as ['index.py', 'Hello'], so printing argv[0] displays index.py, while the user-supplied value 'Hello' occupies index 1.
Why the other options are wrong
- B. from sys import argv is a valid import of a standard-library attribute, so no ImportError occurs.
- C. argv always contains at least one element (the script name), so accessing index 0 never raises IndexError.
- D. 'Hello' is stored at argv[1]; argv[0] is reserved for the script name.
Question 81
What value will be assigned to the x variable?
Show answer and explanation
Correct answer: C. True
Line 3 evaluates the conditional expression `x = z > y or z == y`. With z=10 and y=0, the expression `z > y` evaluates to `10 > 0` which is True. In a logical OR operation, if the first operand is True, the entire expression short-circuits to True without evaluating the second operand. Therefore, x is assigned the boolean value True.
Why the other options are wrong
- A. The value 1 might seem related to True in some languages, but in Python and most modern languages, the assignment operator returns the actual boolean True, not the integer 1.
- B. False would only be assigned if both `z > y` (10 > 0) and `z == y` (10 == 0) were false, but z > y is true, making the OR expression evaluate to True.
Question 82
The following snippet:
Show answer and explanation
Correct answer: C. is erroneous
The call func(b=2, 2) puts a positional argument after a keyword argument. Python reports 'positional argument follows keyword argument' as a SyntaxError, so the code is erroneous and prints nothing.
Why the other options are wrong
- A. The program does not run, so nothing is printed.
- B. The program does not run, so nothing is printed.
- D. The program does not run, so nothing is printed.
Question 83
What will be the output of the following code snippet?
Show answer and explanation
Correct answer: D. 2 1
The code performs a classic three-step swap using a temporary variable. After x = 1 and y = 2, the line z = x stores 1 in z. Then x = y assigns 2 to x, and y = z assigns the saved value 1 to y. At the print statement x holds 2 and y holds 1, so the output is "2 1".
Why the other options are wrong
- A. This would be the result only if no assignments occurred after the initial two; the swap changes both values.
- B. y is restored from z (which holds the original x, 1), so y is not 2.
- C. x is reassigned to y's value 2 before y is changed, so x is not 1.
Question 84
What is machine code?
Show answer and explanation
Correct answer: B. A low-level programming language consisting of binary digits/bit that the computer reads and understands
Machine code is a low-level programming language consisting of binary digits (0s and 1s) that the computer processor directly reads, understands, and executes. It is the lowest level of programming language that directly corresponds to CPU instructions and is what all higher-level languages ultimately compile to.
Why the other options are wrong
- A. Assembly code is a medium-level language, not machine code; machine code is binary, not assembly.
- C. This describes high-level programming languages, not machine code.
- D. Machine code consists of binary digits, not hexadecimal, though hex is used to represent binary for readability.
Question 85
What is the output of the following code snippet?
Show answer and explanation
Correct answer: C. 3 2
The function test() is called with arguments test(2, 1), so x=2 and y=1 initially. Line 2 executes x = x + y, making x = 2 + 1 = 3. Line 3 executes y += 1, making y = 1 + 1 = 2. Line 4 prints x and y, which outputs '3 2'.
Why the other options are wrong
- A. 2 3 does not match the arithmetic: x becomes 2 + 1 = 3, not 2, and y becomes 1 + 1 = 2, not 3.
- B. The code is valid and runs without errors.
- D. 1 3 would need x to be 1 and y to be 3, but the arguments make x start at 2 and y start at 1, and the function changes them to 3 and 2.
- E. 3 3 is what the function prints when called with the defaults, test(), or with test(1, 2). With test(2, 1), y starts at 1 and becomes 2.
Question 86
Consider the following code. x = float('23.42') Which of the following expressions will evaluate to 2?
Show answer and explanation
Correct answer: B. bool(x) + True
Given x = float('23.42'), we evaluate each option: A) int(x) + False = 23 + 0 = 23; B) bool(x) + True = True + True = 1 + 1 = 2 (non-zero floats are True); C) str(x) returns the string '23.42', not 2; D) bool(x) returns True (which equals 1), not 2. Only option B evaluates to 2.
Why the other options are wrong
- A. int(23.42) + False equals 23 + 0 = 23, not 2.
- C. str(23.42) returns a string '23.42', not the integer 2.
- D. bool(23.42) returns True, which has a numeric value of 1, not 2.
Question 87
How did Python, the programming language, get its name?
Show answer and explanation
Correct answer: B. Guido van Roddum named it to honor Monty Python's Flying Circus, a BBC comedy series popular in the 1970s
Guido van Rossum, the creator of Python, named the language after Monty Python's Flying Circus, a popular BBC comedy series from the 1970s. This is well-documented in Python's official history and PEPs (Python Enhancement Proposals). Van Rossum chose the name because he was a fan of the show and wanted a name that was short, unique, and slightly mysterious.
Why the other options are wrong
- A. Python of Catana was an ancient dramatic poet, but this is not the source of Python's name.
- C. While Pythonidae is a family of snakes, this is not the actual origin of the programming language's name.
Question 88
The compiled Python bytecode is stored in files having names ending with:
Show answer and explanation
Correct answer: C. pyc
Python compiled bytecode is stored in files with the .pyc extension, typically in the __pycache__ directory. These files contain the compiled intermediate representation that the Python interpreter executes, allowing faster loading on subsequent runs.
Why the other options are wrong
- A. The .py extension is for source code files, not compiled bytecode.
- B. .pyb is not a standard Python file extension.
- D. .pc is not a standard Python file extension for bytecode.
Question 89
What is the output of the following snippet?
Show answer and explanation
Correct answer: C. [0, 1, 4, 9]
The list comprehension creates my_list = [0, 1, 4, 9, 16]. Inside fun(), lst[2] is 4, so del lst[lst[2]] is del lst[4], which removes the element at index 4 (the value 16). The function returns [0, 1, 4, 9], which is printed.
Why the other options are wrong
- A. [0, 1, 9, 16] would result from deleting index 2 itself; the code deletes at index lst[2], which is 4.
- B. [0, 1, 4, 16] would result from deleting index 3; the code deletes index 4.
- D. [1, 4, 9, 16] would result from deleting index 0; the code deletes index 4.
Question 90
The \n digraph forces the print() function to:
Show answer and explanation
Correct answer: C. break the output line
The \n digraph is an escape sequence that represents a newline character, which causes the print() function to break the output line and continue on the next line. It is interpreted as a single special character, not as two literal characters.
Why the other options are wrong
- A. The \n escape sequence does not duplicate the next character.
- B. The \n escape sequence does not stop the execution of print(); it only breaks the line.
- D. The \n escape sequence is interpreted as a single newline character, not as two literal characters.
Question 91
If a list passed into a function as an argument, deleting any of its elements inside the function using the del instruction:
Show answer and explanation
Correct answer: C. will affect the argument
In Python, lists are mutable objects passed by reference. When a list is passed as an argument to a function, the function receives a reference to the original list object, not a copy. Using the del instruction to delete elements modifies the original list directly, so changes made inside the function will affect the argument in the calling scope.
Why the other options are wrong
- A. This is incorrect because lists are mutable and passed by reference, so deletions do affect the original argument.
- B. Using del on a list element does not cause a runtime error; it is valid Python syntax for removing elements.
Question 92
What do you call a tool that lets you launch your code step-by-step and inspect it at each moment of execution?
Show answer and explanation
Correct answer: B. A debugger
A debugger is a development tool that allows a programmer to execute code step-by-step while inspecting variables, memory states, and program behavior at each execution point. This enables the identification and fixing of bugs and logical errors in the code.
Why the other options are wrong
- A. An editor is a tool for writing and editing source code, but it does not provide step-by-step execution and inspection capabilities.
- C. A console is an interface for entering commands and viewing output, but it does not provide the step-by-step execution and inspection features of a debugger.
Question 93
By which variable of the sys module can we access command line arguments?
Show answer and explanation
Correct answer: D. argv
The sys module in Python provides the argv variable, which is a list containing command-line arguments passed to a Python script. sys.argv[0] contains the script name, and subsequent elements contain the arguments provided by the user.
Why the other options are wrong
- A. argsv is not a valid variable name in the sys module.
- B. arguments is not the correct variable name in the sys module.
- C. args is not the correct variable name; the sys module uses argv.
Question 94
Which of the following statements is false?
Show answer and explanation
Correct answer: D. The result of the / operator is always an integer value.
Statement D is false because the result of the / operator is always a float in Python 3, not always an integer. Statements A, B, and C are all true: the right argument of % cannot be zero, the ** operator has right-to-left associativity, and multiplication precedes addition in order of operations.
Why the other options are wrong
- A. This statement is true; the modulo operator's right argument cannot be zero.
- B. This statement is true; the ** operator has right-to-left associativity.
- C. This statement is true; multiplication has higher precedence than addition.
Question 95
A code point is:
Show answer and explanation
Correct answer: B. A number which makes up a character.
A code point is a numerical value assigned to each character in the Unicode standard. Each character has a unique code point (represented in hexadecimal, such as U+0041 for the letter 'A'). This numeric identifier is fundamental to character encoding and representation. Option A is too literal and vague. Option C is imprecise and doesn't accurately describe what a code point is.
Why the other options are wrong
- A. This is overly literal and doesn't describe the technical meaning of code point.
- C. A code point is not 'a code containing a point', it is a numeric value assigned to a character.
Question 96
Take a look at the snippet and choose the true statement:
Show answer and explanation
Correct answer: C. nums and vals have the same length
Line 1 creates nums as a list with 3 elements [1, 2, 3]. Line 2 assigns vals = nums, which creates an alias, both variables reference the same list object in memory. Line 3 executes del vals[:], which deletes all elements from the list using slice notation. Since vals and nums reference the same object, this operation empties the underlying list, leaving both nums and vals as empty lists with length 0. Therefore, they have the same length both before and after the deletion operation.
Why the other options are wrong
- A. After del vals[:] executes, both lists are empty with length 0, so vals is not longer than nums.
- B. nums and vals are aliases to the same object, so they always have identical length; nums is never longer than vals.
- D. The snippet executes without error; del vals[:] is valid Python syntax for clearing a list in-place.
Question 97
Which one of the lines should you put in the snippet below to match the expected output? Expected output: Code:
Show answer and explanation
Correct answer: C. list.sort()
The code needs to sort the list [2, 7, 1, 4] to produce the expected output [1, 2, 4, 7]. The correct method is list.sort(), which is a built-in list method that sorts the list in-place and modifies the original list object. This method takes the list object itself and sorts it directly, which is why it's called as a method on the list variable.
Why the other options are wrong
- A. sorted(list) returns a new sorted list but does not modify the original list variable, so the print statement would still output [2, 7, 1, 4].
- B. sort(list) is not valid Python syntax; sort() is not a standalone function but rather a method that must be called on a list object.
- D. list.sorted() is not a valid Python method; there is no sorted() method on list objects, only the sorted() built-in function.
Question 98
The // operator:
Show answer and explanation
Correct answer: A. performs integer division
The // operator in Python performs floor division, which divides two numbers and rounds down to the nearest integer. For example, 7 // 2 = 3 and -7 // 2 = -4. This is a standard and well-defined operator in Python.
Why the other options are wrong
- B. Regular division is performed by the / operator, which returns a float; // is different.
- C. The // operator definitely exists and is a core part of Python's arithmetic operators.
Question 99
You have the following file. You want the following output: The average score for Peter is 200.00. Which command do you have to execute in the command line?
Show answer and explanation
Correct answer: B. python index.py Peter 100 200 300
The script skips argv[0] (the script name) and treats argv[1] as the name, then loops from index 2 through the end, summing all remaining arguments as floats and dividing by len(argv)-2, the count of score arguments. Running `python index.py Peter 100 200 300` gives argv = ['index.py','Peter','100','200','300'], so the loop adds 100 + 200 + 300 = 600 and divides by 5-2 = 3, producing 200.0, which is formatted with two decimals as "The average score for Peter is 200.00".
Why the other options are wrong
- A. With only one score, the sum is 100 divided by 1, printing an average of 100.00.
- C. The code runs correctly: argv indexing, the float conversion and the format placeholders are all valid, and it produces the intended output with the right arguments.
- D. Two scores give (100 + 200) / 2 = 150, printing 150.00 instead of 200.00.
Question 100
Which of the following variable names is illegal?
Show answer and explanation
Correct answer: D. in
'in' is a reserved keyword in Python used for membership testing in loops and conditionals (e.g., 'for x in list'). Therefore, it cannot be used as a variable name. The other options, 'In', 'IN', and 'in_', are all legal variable names because Python is case-sensitive and 'in_' contains an underscore making it distinct from the keyword.
Why the other options are wrong
- A. 'In' with a capital I is not the reserved keyword and is a legal variable name.
- B. 'IN' in all capitals is not the reserved keyword and is a legal variable name.
- C. 'in_' with an underscore is not the reserved keyword and is a legal variable name.
Question 101
What is true about tuples? (Choose two.)
Show answer and explanation
Correct answer: B, D. Tuples are immutable, which means that their contents cannot be changed during their lifetime; Tuples can be indexed and sliced like lists.
Tuples are immutable sequences, meaning their contents cannot be changed after creation (option B is correct). Like all sequences, tuples support indexing and slicing operations identical to lists (option D is correct). The len() function works on tuples, and empty tuples are written as (), but these are not what makes tuples true in the context of the question.
Why the other options are wrong
- A. The len() function works perfectly on tuples, returning the number of elements.
- C. While it is true that empty tuples are written as (), this is not one of the most defining characteristics of tuples in general.
Question 102
What is the output of the following snippet?
Show answer and explanation
Correct answer: C. [3, 2, 1]
The code iterates through my_list_1 = [1, 2, 3] and inserts each value v at index 0 of my_list_2. On the first iteration, v=1 is inserted at index 0, making my_list_2 = [1]. On the second iteration, v=2 is inserted at index 0, shifting 1 to the right, making my_list_2 = [2, 1]. On the third iteration, v=3 is inserted at index 0, making my_list_2 = [3, 2, 1]. The final print statement outputs [3, 2, 1].
Why the other options are wrong
- A. This would be the result if the code appended values instead of inserting them at index 0.
- B. This would only occur if all values were the same or if the iteration logic were fundamentally different.
- D. This is incorrect; the values inserted are 1, 2, and 3 from the original list, not all 1s.
Question 103
What does the following code do?
Show answer and explanation
Correct answer: D. Defines a function, which does nothing.
The code defines a function named 'a' that accepts three parameters (b, c, d). The function body contains only the 'pass' statement, which is a null operation in Python, when executed, nothing happens. This is a valid function definition that does nothing when called. It's not initializing a list (A), not passing parameters through to another function (B), and not defining a class (E).
Why the other options are wrong
- A. The code does not define or initialize a list; it defines a function with parameters.
- B. While the function has parameters, 'pass' does not pass them through anywhere; it simply does nothing.
- C. Option C is incorrect because option D correctly describes the code.
- E. The code uses 'def' keyword, which defines a function, not a class (which would use 'class' keyword).
Question 104
What value will be assigned to the x variable?
Show answer and explanation
Correct answer: B. True
Comparison operators bind more tightly than and, and and binds more tightly than or, so line 3 is evaluated as x = (y < z and z > y) or (y > z and z < y). With z = 3 and y = 7, the first group is 7 < 3 and 3 > 7, which is False and False, giving False. The second group is 7 > 3 and 3 < 7, which is True and True, giving True. False or True evaluates to True, so x is assigned True.
Why the other options are wrong
- A. The variable x is assigned a boolean result, not the integer 0.
- C. While parts of the expression evaluate to False, the overall compound expression evaluates to True.
- D. The variable x is assigned a boolean value (True), not the integer 1, though they may be equivalent in some contexts.
Question 105
What is the default value of encoding in the string function encode()?
Show answer and explanation
Correct answer: D. utf-8
The default encoding for the string encode() method in Python is UTF-8. UTF-8 is a variable-width character encoding that can represent any character in the Unicode standard and is the most commonly used encoding in Python 3 for text processing.
Why the other options are wrong
- A. 'qwerty' is not a valid encoding scheme and is not the default for encode().
- B. ASCII is a valid encoding but not the default; UTF-8 is the default in Python 3.
- C. UTF-16 is a valid encoding but not the default for the encode() method.
Question 106
Which expression evaluates to 7?
Show answer and explanation
Correct answer: A. 9 % 3 + 7
Evaluating option A: 9 % 3 + 7. The modulo operator % is evaluated first: 9 % 3 = 0 (remainder when 9 is divided by 3). Then 0 + 7 = 7, which is the target value.
Why the other options are wrong
- B. 9 - 3 * 7 = 9 - 21 = -12 (multiplication is evaluated first by precedence).
- C. 9 // 3 - 7 = 3 - 7 = -4 (integer division evaluated first).
- D. 9 / 3 * 7 = 3.0 * 7 = 21.0 (division and multiplication are left-associative).
Question 107
An operator able to check whether two values are equal, is coded as:
Show answer and explanation
Correct answer: A. ==
In Python, the == operator is used to check whether two values are equal. The === operator does not exist in Python (it exists in JavaScript). The 'is' operator checks identity, not equality. The = operator is for assignment, not comparison.
Why the other options are wrong
- B. The === operator does not exist in Python; it is a JavaScript construct.
- C. The 'is' operator checks object identity, not value equality.
- D. The = operator is used for assignment, not comparison.
Question 108
Which of the following statements are correct? (Choose two.)
Show answer and explanation
Correct answer: B, C. True or False evaluates to True; True + 1 evaluates to 2
Option B: True or False uses the 'or' operator and evaluates to True because at least one operand is True. Option C: True + 1 evaluates to 2 because in Python, True is equivalent to 1 in numeric contexts, so 1 + 1 = 2. Both statements are correct.
Why the other options are wrong
- A. 7 + False evaluates to 7, not False, because False is treated as 0 in arithmetic operations, so 7 + 0 = 7.
- D. True and False evaluates to False, not True, because the 'and' operator returns False when at least one operand is False.
- E. type('') returns <class 'str'>, not <class 'bool'>, because an empty string is still a string type.
Question 109
Insert the correct piece of code, so that the program produces the expected output. Expected output: Code:
Show answer and explanation
Correct answer: D. print("Andy\nBrown")
The expected output shows 'Andy' on line 1 and 'Brown' on line 2, requiring a newline character between them. The escape sequence \n in a Python string creates a newline. Option D uses print("Andy\nBrown"), which correctly inserts a newline between the two words, producing the exact output shown in the exhibit.
Why the other options are wrong
- A. Lacks quotes around the string, causing a syntax error; \n without quotes is not interpreted as an escape sequence.
- B. The end=" " parameter only changes the line terminator to a space, keeping both words on the same line separated by a space.
- C. The sep=" " parameter controls the separator between multiple arguments in print(), not the content within a single string argument.
Question 110
You develop a Python application for your company. A list named employees contains 200 employee names, the last five being company management. You need to slice the list to display all employees excluding management. Which code segments can you use? (Choose two.)
Show answer and explanation
Correct answer: B, C. employees[0:-5]; employees[:-5]
To exclude the last 5 elements from a 200-element list, use negative indexing with -5 as the stop index. Both employees[0:-5] (explicitly starting at index 0) and employees[:-5] (implicitly starting at 0) produce the same result: all elements except the last 5. Slicing with -5 as the endpoint includes elements from the start up to but not including index -5.
Why the other options are wrong
- A. This starts at index 1, excluding the first employee, which is not required.
- D. Using -4 excludes only the last 4 elements, not the last 5.
- E. This starts at index 1 and uses -4, excluding both the first employee and only 4 management members.
Question 111
What is the output of the following snippet?
Show answer and explanation
Correct answer: A. 4
The code defines a function `fun(x)` that increments x by 1 and returns the new value. Starting with x=2 on line 6, line 7 calls `fun(x + 1)` which is `fun(3)`. Inside the function, x becomes 3+1=4, and this value is returned and assigned back to x. Therefore, x=4 when printed on line 8.
Why the other options are wrong
- B. 5 would need 1 to be added twice to 3, or the function to receive 4. The call fun(x + 1) passes 3, and the function adds 1 only once, so it returns 4.
- C. The code is syntactically and logically valid Python with no errors.
- D. This would result if only `fun(x)` were called without the +1 argument, giving 2+1=3, but the actual call is `fun(x + 1)` with x starting at 2.
Question 112
The pyc file contains ...
Show answer and explanation
Correct answer: C. compiled Python bytecode.
A .pyc file contains compiled Python bytecode, which is an intermediate representation of the Python source code. This bytecode is generated when a Python module is imported and is cached for faster loading on subsequent runs. It is not human-readable source code but an intermediate form that the Python virtual machine interprets.
Why the other options are wrong
- A. A .pyc file does not contain the Python interpreter itself.
- B. A .pyc file does not contain the original Python source code.
- D. A .pyc file does not contain the Python compiler; it is the output of compilation, not the compiler itself.
Question 113
Which of the following enclose the input parameters or arguments of a function?
Show answer and explanation
Correct answer: D. Parentheses
In Python, parentheses () enclose the input parameters or arguments in both function definitions and function calls. For example, def function(param1, param2): defines a function with parameters enclosed in parentheses, and function_name(arg1, arg2) calls it with arguments in parentheses.
Why the other options are wrong
- A. Brackets [] are used for list literals and indexing, not for enclosing function parameters.
- B. Quotation marks are used for string literals, not for enclosing function parameters.
- C. Curly braces {} are used for dictionaries and sets, not for enclosing function parameters.
Question 114
Consider the following code. The value eventually assigned to x is equal to:
Show answer and explanation
Correct answer: C. True
Line 2 is a single assignment whose right-hand side is the comparison x == x. The comparison is evaluated first (1 == 1), returning True, and then this True value is assigned back to x. Therefore, x ultimately holds the boolean value True, not the integer 1.
Why the other options are wrong
- A. While x starts as 1, the final assignment in line 2 overwrites it with the boolean result True from the comparison x == x.
- B. The comparison x == x where x equals 1 evaluates to True, not False or 0.
- D. The comparison 1 == 1 evaluates to True, not False.
Question 115
What will be the output of the following code snippet? print(3 / 5)
Show answer and explanation
Correct answer: C. 0.6
In Python 3, the `/` operator performs true division and always returns a float. 3 / 5 = 0.6. This is different from Python 2 where division of integers would return an integer.
Why the other options are wrong
- A. Option C is correct; there is a valid answer among the choices.
- B. 0 would be the result of integer division (//), not true division (/).
- D. 6/10 is a fraction representation, not the numerical output; Python evaluates the division and prints the decimal result.
Question 116
What is the output of the following snippet?
Show answer and explanation
Correct answer: B. [1, 1, 1, 2]
The call insert(-1, x) places x before the last element. When v is 0, my_list[0] is 1 and the list becomes [1, 1, 2]. When v is 1, my_list[1] is read from the changed list, so it is also 1, and the list becomes [1, 1, 1, 2].
Why the other options are wrong
- A. This assumes my_list[1] is still 2, but the first insert already moved 2 to index 2.
- C. Both inserted values are 1, so the list cannot hold a second 2.
- D. Both inserted values are 1, so the list cannot hold three 2s.
Question 117
What is CPython?
Show answer and explanation
Correct answer: B. It's a default, reference implementation of the Python language, written in C.
CPython is the default and reference implementation of the Python language, written in C. It is the most widely used Python interpreter and serves as the standard against which other Python implementations are measured. The name 'CPython' derives from the fact that it is implemented in the C language.
Why the other options are wrong
- A. CPython is not a superset of C; it is an implementation of Python written in C.
- C. CPython is not a superset of Python; it is the standard implementation of Python itself.
- D. CPython is not an implementation of C; it is an implementation of Python written in C.
Question 118
You develop a Python application for your company. You want to add notes to your code so other team members will understand it. What should you do?
Show answer and explanation
Correct answer: B. Place the notes after the # sign on any line.
In Python, comments are created by placing a # symbol before the text. This allows notes to be added on any line of code, either at the end of a line of code or on their own line. The # character tells the Python interpreter to ignore everything after it on that line, making it the correct way to add notes for team members.
Why the other options are wrong
- A. Comments do not need to be placed only after the last line of code; they can appear anywhere.
- C. Parentheses are used for grouping expressions and function arguments, not for creating comments.
- D. Comments can appear anywhere in code, not just before the first line.
Question 119
Which of the literals below is not a valid function name?
Show answer and explanation
Correct answer: D. 1function
In Python, function names must follow identifier rules: they can start with a letter (a-z, A-Z) or underscore, but cannot start with a digit. Option D, '1function', violates this rule by beginning with the digit 1, making it an invalid function name. All other options follow proper naming conventions.
Why the other options are wrong
- A. Function_1 is valid; identifiers can contain underscores and digits after the first character.
- B. Function1 is valid; identifiers can contain digits as long as they don't start with one.
- C. _function1 is valid; identifiers can start with an underscore.
- E. Func_1_tion is valid; underscores can appear anywhere in an identifier after the first character.
Question 120
Which function does in-place reversal of objects in a list?
Show answer and explanation
Correct answer: D. list.reverse()
The list.reverse() method performs an in-place reversal of a list, modifying the original list object without returning a new list. This is the standard Python method for reversing list contents.
Why the other options are wrong
- A. list.sort() sorts elements in place but does not reverse them.
- B. list.pop() removes and returns an element; it does not reverse the list.
- C. list.remove() removes a specific element but does not reverse the list.
That was 120 of 481.
The full Python Institute PCEP-30-02 pack has all 481 questions, each with the answer, the explanation and why the other options are wrong, plus a questions-only copy for timed runs. US$39, paid once, with free monthly updates and a pass-or-your-money-back guarantee.
Not ready yet? Get the first 240 free as one PDF by email. Get the Half Pack →
