Python Institute PCEP-30-02 PCEP - Certified Entry-Level Python Programmer Exam Practice Test

Page: 1 / 14
Total 30 questions
Question 1

Python Is an example of which programming language category?



Answer : A

Python is an interpreted programming language, which means that the source code is translated into executable code by an interpreter at runtime, rather than by a compiler beforehand. Interpreted languages are more flexible and portable than compiled languages, but they are also slower and less efficient. Assembly and machine languages are low-level languages that are directly executed by the hardware, while compiled languages are high-level languages that are translated into machine code by a compiler before execution.


Question 2

What is true about tuples? (Select two answers.)



Answer : A, D

Tuples are one of the built-in data types in Python that are used to store collections of data. Tuples have some characteristics that distinguish them from other data types, such as lists, sets, and dictionaries. Some of these characteristics are:

Tuples are immutable, which means that their contents cannot be changed during their lifetime. Once a tuple is created, it cannot be modified, added, or removed. This makes tuples more stable and reliable than mutable data types. However, this also means that tuples are less flexible and dynamic than mutable data types.For example, if you want to change an element in a tuple, you have to create a new tuple with the modified element and assign it to the same variable12

Tuples are ordered, which means that the items in a tuple have a defined order and can be accessed by using their index. The index of a tuple starts from 0 for the first item and goes up to the length of the tuple minus one for the last item. The index can also be negative, in which case it counts from the end of the tuple. For example, if you have a tuplet = ('a', 'b', 'c'), thent[0]returns'a', andt[-1]returns'c'12

Tuples can be indexed and sliced like lists, which means that you can get a single item or a sublist of a tuple by using square brackets and specifying the start and end index. For example, if you have a tuplet = ('a', 'b', 'c', 'd', 'e'), thent[2]returns'c', andt[1:4]returns('b', 'c', 'd'). Slicing does not raise any exception, even if the start or end index is out of range.It will just return an empty tuple or the closest possible sublist12

Tuples can contain any data type, such as strings, numbers, booleans, lists, sets, dictionaries, or even other tuples. Tuples can also have duplicate values, which means that the same item can appear more than once in a tuple. For example, you can have a tuplet = (1, 2, 3, 1, 2), which contains two 1s and two 2s12

Tuples are written with round brackets, which means that you have to enclose the items in a tuple with parentheses. For example, you can create a tuplet = ('a', 'b', 'c')by using round brackets. However, you can also create a tuple without using round brackets, by just separating the items with commas. For example, you can create the same tuplet = 'a', 'b', 'c'by using commas.This is called tuple packing, and it allows you to assign multiple values to a single variable12

The len() function can be applied to tuples, which means that you can get the number of items in a tuple by using the len() function. For example, if you have a tuplet = ('a', 'b', 'c'), thenlen(t)returns 312

An empty tuple is written as (), which means that you have to use an empty pair of parentheses to create a tuple with no items. For example, you can create an empty tuplet = ()by using empty parentheses. However, if you want to create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple. For example, you can create a tuple with one itemt = ('a',)by using a comma12

Therefore, the correct answers are A. Tuples are immutable, which means that their contents cannot be changed during their lifetime. and D. Tuples can be indexed and sliced like lists.


Question 3

What is the expected output of the following code?



Answer : D

The code snippet that you have sent is using the count method to count the number of occurrences of a value in a list. The code is as follows:

my_list = [1, 2, 3, 4, 5] print(my_list.count(1))

The code starts with creating a list called ''my_list'' that contains the numbers 1, 2, 3, 4, and 5. Then, it uses the print function to display the result of calling the count method on the list with the argument 1. The count method is used to return the number of times a value appears in a list. For example, my_list.count(1) returns 1, because 1 appears once in the list.

The expected output of the code is 1, because the code prints the number of occurrences of 1 in the list. Therefore, the correct answer is D. 1.


Question 4
Question 5

What is the expected output of the following code?



Answer : B

The code snippet that you have sent is using the slicing operation to get parts of a string and concatenate them together. The code is as follows:

pizza = ''pizza'' pasta = ''pasta'' folpetti = ''folpetti'' print(pizza[0] + pasta[0] + folpetti[0])

The code starts with assigning the strings ''pizza'', ''pasta'', and ''folpetti'' to the variables pizza, pasta, and folpetti respectively. Then, it uses the print function to display the result of concatenating the first characters of each string. The first character of a string can be accessed by using the index 0 inside square brackets. For example, pizza[0] returns ''p''. The concatenation operation is used to join two or more strings together by using the + operator. For example, ''a'' + ''b'' returns ''ab''. The code prints the result of pizza[0] + pasta[0] + folpetti[0], which is ''p'' + ''p'' + ''f'', which is ''ppt''.

The expected output of the code is ppt, because the code prints the first characters of each string. Therefore, the correct answer is B. ppt.


Question 6

What happens when the user runs the following code?



Answer : B

The code snippet that you have sent is calculating the value of a variable ''total'' based on the values in the range of 0 to 3. The code is as follows:

total = 0 for i in range(0, 3): if i % 2 == 0: total = total + 1 else: total = total + 2 print(total)

The code starts with assigning the value 0 to the variable ''total''. Then, it enters a for loop that iterates over the values 0, 1, and 2 (the range function excludes the upper bound). Inside the loop, the code checks if the current value of ''i'' is even or odd using the modulo operator (%). If ''i'' is even, the code adds 1 to the value of ''total''. If ''i'' is odd, the code adds 2 to the value of ''total''. The loop ends when ''i'' reaches 3, and the code prints the final value of ''total'' to the screen.

The code outputs 2 to the screen, because the value of ''total'' changes as follows:

When i = 0, total = 0 + 1 = 1

When i = 1, total = 1 + 2 = 3

When i = 2, total = 3 + 1 = 4

When i = 3, the loop ends and total = 4 is printed

Therefore, the correct answer is B. The code outputs 2.


Question 7

What is the expected output of the following code?



Answer : C

The code snippet that you have sent is checking if two numbers are equal and printing the result. The code is as follows:

num1 = 1 num2 = 2 if num1 == num2: print(4) else: print(1)

The code starts with assigning the values 1 and 2 to the variables ''num1'' and ''num2'' respectively. Then, it enters an if statement that compares the values of ''num1'' and ''num2'' using the equality operator (==). If the values are equal, the code prints 4 to the screen. If the values are not equal, the code prints 1 to the screen.

The expected output of the code is 1, because the values of ''num1'' and ''num2'' are not equal. Therefore, the correct answer is C. 1.


Page:    1 / 14   
Total 30 questions