0% found this document useful (0 votes)
15 views13 pages

Python QA

The document contains a series of Python programming questions and their corresponding answers, covering topics such as list manipulation, function calls, and error handling. Each question is presented with a code snippet, and the answers provide explanations or outputs based on the given code. The questions range from basic syntax to more complex programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Python QA

The document contains a series of Python programming questions and their corresponding answers, covering topics such as list manipulation, function calls, and error handling. Each question is presented with a code snippet, and the answers provide explanations or outputs based on the given code. The questions range from basic syntax to more complex programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Here are all the questions and answers from the PDF in a text format:

Question 1

Question: Consider the following Python code snippet. What will the contents of
my_numbers be after execution?

Python

my_numbers = [10, 20, 30, 40, 50]

for i in range(4):

my_numbers.insert(i, my_numbers[-1])

print(my_numbers)

Answer: [50,10, 50, 20,50, 30,50, 40]

Question 2

Question: In the context of a function call in Python, what determines the meaning of a
positional argument?

Answer: Its position within the argument list

Question 3

Question: Analyze the following Python code snippet

Python

alpha = [4, 8, 15, 16, 23, 42]

beta = alpha[:]

beta[2] = 99

Which of the following statements are true regarding the code above? (Select two answers)

Answers:
• The beta list is of the same length as the alpha list, but their contents are not
identical.

• alpha and beta are separate lists; the alpha list remains unchanged after the
modification.

Question 4

Question: An operator able to check whether two values are not equal is coded as

Answer: !=

Question 5

Question: Given the Python code segment below:

Python

def compute_square(x):

return x * x

def compute_quad(x):

return compute_square(x) * compute_square(None)

print(compute_quad(4))

What will be the output of the code?

Answer: The code will cause a runtime error.

Question 6

Question: The result of the following division: 11/12

Answer: is equal to 0.9166666666666666

Question 7
Question: Consider the Python function defined below:

Python

def raise_power(base, exponent):

return base ** exponent

print(raise_power(exponent=3, 2))

What is the result of executing this code?

Answer: The code will output 8.

Question 8

Question: What value will be assigned to the variable x?

Python

z=0

y = 10

x = y < z and z > y or y < z and z < y

Answer: False

Question 9

Question: Review the following identifiers and select the ones that are reserved keywords
in Python and would cause a SyntaxError if used as variable names. (Select two answers)

Answers:

• continue

• class

Question 10

Question: What is the expected output of the following Python code?


Python

my_values = [3 * i for i in range(5)]

def modify_list(values):

del values[values[2] // 3]

return values

print(modify_list(my_values))

Answer: [0, 3, 9, 12]

Question 11

Question: What is the output of the following piece of code?

Python

x=1

y=2

x, y, z = x, x, y

z, y, z = x, y, z

print(x, y, z)

Answer: 1 2 2

Question 12

Question: What will be the output of the following snippet?

Python

a=1

b=0

a=a^b
b=a^b

a=a^b

print(a, b)

Answer: 0 1

Question 13

Question: What is the output of the following Python code snippet?

Python

def custom_function(value):

if value % 3 == 0:

return 1

else:

return 2

print(custom_function(custom_function(4)))

Answer: 2

Question 14

Question: Consider the following Python code snippet and determine which statement is
correct:

Python

inventory = ['apple', 'banana', 'cherry']

backup_inventory = inventory

del backup_inventory[:]

Answer: Both inventory and backup_inventory contain the same number of items.
Question 15

Question: Consider the following Python code, where only integer inputs are allowed:

Python

first_integer = int(input("Enter an integer: "))

second_integer = int(input("Enter another integer: "))

first_integer = first_integer + second_integer

first_integer = first_integer - second_integer

second_integer = second_integer % first_integer

print(second_integer)

Which condition1 will ensure that the code runs successfully without a runtime error?

Answer: first_integer must be larger than second_integer (at the point of the modulo
operation, first_integer cannot be zero).

Question 16

Question: Assuming the user enters 5 on the first line and 0 on the second line, what is the
output of the following Python code?

Python

first_input = input("Enter first number: ")

second_input = input("Enter second number: ")

print(second_input + first_input)

Answer: 05

Question 17

Question: What is the output of the following piece of code?

Python

print("a", "b", "c", sep="sep")

Answer: asepbsepc
Question 18

Question: What is the output of the following piece of code?

Python

x = 1 // 5 + 1 / 5

print(x)

Answer: 0.2

Question 19

Question: What is the result of trying to execute the following code snippet if sample_tuple
is a tuple that has been previously defined with at least two elements?

Python

sample_tuple[0] = 5

print(sample_tuple)

Answer: A TypeError will be raised because elements of a tuple cannot be reassigned.

Question 20

Question: What is the output of the following piece of code if the user enters two lines
containing 2.0 and 4.0 respectively?

Python

x = float(input())

y = float(input())

print(y ** (1 / x))

Answer: 2.0

Question 21

Question: What is the expected output of the following code snippet?


Python

my_list = [1, 2, 3, 4, 5]

value = my_list[2]

for i in range(my_list[value - 1]):

value = my_list[value - 1]

print(value)

Answer: 4

Question 22

Question: How many elements are in the list my_list?

Python

my_list = [i for i in range(5)]

Answer: 5

Question 23

Question: Which of the following function calls correctly invoke the function named
my_function? (Select two answers)

Python

def my_function(x, y):

return x + y

Answers:

• my_function(y=1, x=2)

• my_function(1, 2)

Question 24

Question: What is the output of the following snippet?


Python

x = 10

if x == 10:

print("Ten")

else:

print("Not Ten")

print(x + 1)

Answer: The snippet will cause a runtime error.

Question 25

Question: How many times will the following snippet loop?

Python

i=0

while i < 1:

i += 1

print(i)

else:

print("Done")

Answer: One

Question 26

Question: What is the output of the following Python code snippet?

Python

my_tuple = (1, 2, 3)

my_tuple = (my_tuple[0], my_tuple[2], my_tuple[1])

print(my_tuple)
Answer: (1, 3, 2)

Question 27

Question: Examine the following Python code: print("Hello, world!", "Python is fun!")

Which of the following statements is correct regarding this code?

Answer: The print() function with two arguments displays two values.

Question 28

Question: What is the output of the following code?

Python

dct = {}

dct[1] = 10

dct[2] = 20

for k, v in dct.items():

print(k, end=" ")

Answer: 1 2

Question 29

Question: What is the output of the following snippet?

Python

my_list = [1, 2, 3]

del my_list[3]

print(my_list)

Answer: IndexError (This would be a runtime error, specifically an IndexError, not a


SyntaxError).
Question 30

Question: How many times will the * symbol be emitted onto the console by the following
code snippet?

Python

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

count = 0

for row in matrix:

for element in row:

if element % 2 == 0:

count += 1

print(count * '*')

Answer: Zero

Question 31

Question: What is the expected output of the following Python program?

Python

my_list = ['a', 'b', 'c']

my_list.insert(0, my_list.pop(2))

print(my_list)

Answer: ['c', 'a', 'b']

Question 32

Question: What is the expected output of the following Python snippet?

Python

x = 10
y=3

x=y/x

print(x)

Answer: The value of x will be 0.3

Question 33

Question: What is the expected behavior of the following program?

Python

a = 10

b=0

c=a/b

print(c)

Answer: The program will cause a ZeroDivisionError exception.

Question 34

Question: What is the output of the following?

Python

print(())

Answer: ()

Question 35

Question: What is the expected behavior of the following program?

Python

my_tuple = (1, 2)

my_tuple.append(3)

print(my_tuple)
Answer: The code will raise a TypeError exception.

You might also like