Python 3エンジニア認定基礎試験~模擬試験②~

1. 
次のコードの実行結果として正しいものを選んでください。

x = 5

def example_function():
x = 10
print(x)

example_function()
print(x)

2. 
次のコードを実行したときの出力結果は何でしょうか?

def modify_list(values):
values.append(4)

numbers = [1, 2, 3]
modify_list(numbers)
print(numbers)

3. 
次のコードの出力結果を求めてください。

words = ["data", "science", "python"]
sentence = " ".join(words)
print(sentence)

4. 
次のコードの実行結果を選んでください。

try:
raise TypeError("Invalid type")
except Exception as e:
print("Caught Exception:", e)

5. 
次のコードの実行結果を選んでください。

for i in range(3)
print(i)

6. 
次のコードの出力を1行にするために使うべき引数はどれですか?

print("Hello")
print("Python")

7. 
次のコードを実行したときの出力結果は何でしょうか?

x = 10

def my_function():
x = 20
return x

print(my_function())
print(x)

8. 
次のコードを実行したときの出力結果は何でしょうか?

x = 0

def increment():
global x
x += 1
return x

print(increment())
print(increment())

9. 
次のコードを実行した場合の出力は何ですか?

items = ["apple", "banana", "cherry"]
output = " - ".join(items)
print(output)

10. 
次のコードを実行した場合の結果として正しいものを選んでください。

try:
raise ValueError("Invalid value")
except ValueError as e:
print("Caught exception:", e)

11. 
次のコードを実行したときの出力結果は何でしょうか?

y = 100

def modify_variable():
global y
y = "Changed"

modify_variable()
print(y)

12. 
"Python3"という文字列から「3」を削除して"Python"にするコードはどれですか?

13. 
次のコードの実行結果を選んでください。

print("Missing parenthesis in print statement"

14. 
次のコードの問題点として正しいものを選んでください。

if 5 > 3:
print("5 is greater than 3")

15. 
次のコードの問題点として正しいものを選んでください。

if x = 10:
print("x is 10")

16. 
次のコードを実行したときの出力結果は何でしょうか?

x = 10

def modify_variable():
global x
x = "Hello"

modify_variable()
print(x)

17. 
次のコードの出力結果を求めてください。

text = "Python"
result = text + " " + text[::-1]
print(result)

18. 
次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか?

print("Hello")
print("World")

19. 
次のコードの問題点として正しいものを選んでください。

if True
print("This is true")

20. 
Pythonで文字列を結合するために使用される演算子はどれですか?

21. 
次のコードを実行したときの出力結果は何でしょうか?

def modify_list(lst):
lst.append(10)

my_list = [1, 2, 3]
modify_list(my_list)
print(my_list)

22. 
次のコードで発生する例外の種類として正しいものを選んでください。

raise KeyError("Key not found")

23. 
次のコードの実行結果を選んでください。

try:
raise ValueError("Invalid data provided")
except KeyError as e:
print("Caught KeyError:", e)
except ValueError as e:
print("Caught ValueError:", e)

24. 
次のコードを実行したときの出力結果は何でしょうか?

x = 5

def outer_function():
global x
x = x * 2

outer_function()
print(x)

25. 
次のコードを実行したときの出力結果は何でしょうか?

def extend_list(item, target=None):
if target is None:
target = []
target.append(item)
return target

list1 = extend_list(1)
list2 = extend_list(2, [])
list3 = extend_list(3)

print(list1)
print(list2)
print(list3)

26. 
次のコードを実行したときの出力結果は何でしょうか?

x = 5

def outer_function():
x = 10

def inner_function():
return x

return inner_function()

print(outer_function())

27. 
次のコードの実行結果を選んでください。

def check_value(x):
if x < 0:
raise ValueError("Negative value not allowed")
return x * 2

try:
print(check_value(-5))
except ValueError as e:
print("Error:", e)

28. 
次のコードの実行結果として正しいものを選んでください。

print("Hello

29. 
次のコードの実行結果として正しいものを選んでください。

def my_function():
y = 7
print(y)

my_function()
print(y)

30. 
次のコードを実行したときの出力結果は何でしょうか?

def my_function():
x = 5
x += 10
return x

print(my_function())

31. 
次のコードを実行したときの出力結果は何でしょうか?

def outer_function():
x = 10

def inner_function():
nonlocal x
x = x + 5
return x

return inner_function()

print(outer_function())

32. 
次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか?

33. 
次のコードを実行したときの出力結果は何でしょうか?

x = 10

def outer_function():
x = 20

def inner_function():
return x + 5

return inner_function()

print(outer_function())
print(x)

34. 
次のコードを実行したときの出力結果は何でしょうか?

def reset_list(values):
values = [0, 0, 0]

numbers = [1, 2, 3]
reset_list(numbers)
print(numbers)

35. 
次のリスト["apple", "banana", "cherry"]の各要素を改行で区切って表示するために、適切なコードはどれですか?

36. 
文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか?

37. 
次のコードを実行したときの出力結果は何でしょうか?

x = 10

def shadow_variable():
x = 5
def inner():
return x
return inner()

print(shadow_variable())
print(x)

38. 
グローバル変数に関する正しい説明はどれですか?

39. 
次のコードの実行結果を選んでください。

try:
raise NameError("Variable not defined")
except ValueError as e:
print("Caught ValueError:", e)
except NameError as e:
print("Caught NameError:", e)

40. 
次のコードを実行したときの出力結果は何でしょうか?

def append_item(value, items=None):
if items is None:
items = []
items.append(value)
return items

print(append_item(1))
print(append_item(2))

コメントを残すにはログインしてください。