Soal Latihan Pyhton

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5
At a glance
Powered by AI
Some of the key takeaways from the passages include defining functions, the properties of tuples as immutable sequences, and the difference between built-in and user-defined functions.

Functions are blocks of code that perform a specific task and are defined using the def keyword followed by the function name and parameters in parentheses. Functions may take parameters and return values. Parameters can have default values.

Tuples belonging to sequence types means they can be indexed and sliced like lists. However, unlike lists, tuples are immutable and cannot be modified once created.

PCAP – Programming Essentials in Python

Quizzes Module 3 Test Answers


1. Which of the following lines properly starts a parameterless
function definition?
 function fun():
 def fun:
 fun function():
 def fun():
2. A function defined in the following way:
def function(x=0):
return x
 may be invoked without any argument, or with just one
 must be invoked without arguments
 must be invoked with exactly one argument
 may be invoked with any number of arguments (including zero)
3. A built-in function is a function which:
 has to be imported before use
 is hidden from programmers
 has been placed within your code by another programmer
 comes with Python, and is an integral part of Python
4. The fact that tuples belong to sequence types means:
 they can be modified using the del instruction
 they can be indexed and sliced like lists
 they are actually lists
 they can be extended using the .append() method
5. What is the output of the following snippet?
def f(x):
if x == 0:
return 0
return x + f(x – 1)
print(f(3))

 the code is erroneous


 3
 1
 6
6. What is the output of the following snippet?
def fun(x):
x += 1
return x
x=2
x = fun(x+1)
print(x)

 the code is erroneous


 4
 5
 3
7. What code would you insert into the commented line to obtain
the output that reads:
a
b
c
Code:
dct = { }
lst = [‘a’,’b’,’c’,’d’]
for i in range(len(lst) – 1):
dct[lst[i]] = ( lst[i], )
for i in sorted(dct.keys()):
k = dct[i]
# insert your code
 print(k[0])
 print(k)
 print(k[“0”])
 print(k[‘0’])
8. The following snippet:
def func(a,b):
return a ** a
print(func(2))

 will output 2
 is erroneous
 will return None
 will output 4
9. The following snippet:
def func1(a):
return a ** a
def func2(a):
return func1(a)*func1(a)
print(func2(2))
 is erroneous
 will output 2
 will output 4
 will output 16
10. Which of the following lines properly starts a function
using two parameters, both with zeroed default values?
 def fun(a=b=0):
 fun fun(a,b=0):
 fun fun(a=0,b):
 def fun(a=0,b=0):
11. Which of the following statements is false?
 The None value cannot be used as an argument of arithmetic operators
 The None value can be assigned to variables
 The None value may not be used outside functions
 The None value can be compared with variables
12. What is the output of the following snippet?
def fun(x):
if x % 2 == 0:
return 1
else:
return
print(fun(fun(2)) + 1)
 the code will cause a runtime error
 1
 None
 2
13. What is the output of the following snippet?
def fun(x):
global y
y=x*x
return y
fun(2)
print(y)
 2
 4
 None
 the code will cause a runtime error
14. What is the output of the following snippet?
def any():
print(var + 1,end=”)
var = 1
any()
print(var)
 22
 21
 11
 12
15. Assuming that tuple is a correctly created tuple, the fact
that tuples are immutable means that the following instruction:
tuple[1] = tuple[1] + tuple[0]
 is illegal
 is fully correct
 can be executed if and only if the tuple contains at least two elements
 may be illegal if the tuple contains strings
16. What is the output of the following snippet?
list = [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
def list(L):
del L[3]
L[3] = ‘ram’
print(list(list))
 [‘Mary’, ‘had’, ‘a’, ‘ram’]
 [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]
 the snippet is erroneous
 [‘Mary’, ‘had’, ‘a’, ‘lamb’]
17. What is the output of the following snippet?
def fun(x,y,z):
return x+2*y+3*z
print(fun(0,z=1,y=3))
 3
 the snippet is erroneous
 0
 9
18. What is the output of the following snippet?
def fun(inp=2,out=3):
return inp * out
print(fun(out=2))
 6
 4
 2
 the snippet is erroneous
19. What is the output of the following snippet?
dct = { ‘one’:’two’, ‘three’:’one’, ‘two’:’three’ }
v = dct[‘one’]
for k in range(len(dct)):
v = dct[v]
print(v)
 two
 three
 (‘one’, ‘two’, ‘three’)
 one
20. What is the output of the following snippet?
tup = (1, 2, 4, 8)
tup = tup[1:-1]
tup = tup[0]
print(tup)
 the snippet is erroneous
 (2)
 (2,)
 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy