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

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

y = 100

def outer_function():
y = 200
def inner_function():
global y
y += 50
inner_function()
print(y)

outer_function()
print(y)

2. 
ローカル変数が削除されるタイミングとして正しいものはどれですか?

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

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

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

text = "Line1\nLine2\nLine3"
print(text.replace("\n", " | "))

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

if True
print("This is true")

6. 
Pythonの文字列で改行を削除するための適切なメソッドはどれですか?

7. 
文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか?

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

a = "Python"
b = "Programming"
result = a + " " + b
print(result)

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

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

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

11. 
グローバル変数を関数内で変更するために使用するキーワードは何ですか?

12. 
次のうち、Pythonの文字列で改行を保持したまま文字列を定義する方法として正しいものはどれですか?

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

x = [1, 2, 3]

def modify_list():
global x
x.append(4)

modify_list()
print(x)

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

print("Hello

15. 
raise文の役割として正しいものを選んでください。

16. 
次のコードの出力結果は何でしょうか?

x = 0

def example_function():
global x
x = x + 2
return x

print(example_function())
print(x)

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

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

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

x = 5

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

outer_function()
print(x)

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

x = 3

def modify_variable():
global x
x = x * 2
return x

print(modify_variable())
print(x)

20. 
変数name = "Alice"とage = 25の内容を使って「Alice is 25 years old」という文字列を生成するために入力すべきコードはどれですか?

21. 
次のコードの出力として正しいものを選んでください。

try:
raise Exception("Generic error")
except Exception as e:
print(type(e).__name__, ":", e)

22. 
次のコードで出力を1行にまとめるために使用する引数はどれですか?

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

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

def check_divisor(y):
if y == 0:
raise ZeroDivisionError("Divisor cannot be zero")
return 10 / y

try:
print(check_divisor(0))
except ZeroDivisionError as e:
print("Error:", e)

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

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

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

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

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

text = "Line1\nLine2\nLine3"
lines = text.splitlines()
print(lines)

27. 
Pythonのsplitlines()メソッドはどのような機能を持っていますか?

28. 
Pythonで複数行にわたる文字列を作成するために使う方法として正しいものはどれですか?

29. 
次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか?

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

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)

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

y = 100

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

modify_variable()
print(y)

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

if True print("This will not work")

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

x = 10

def modify_variable():
x = 20
return x

print(modify_variable())
print(x)

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

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

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

35. 
次のコードで正しい出力を選んでください。

try:
raise RuntimeError("Unexpected error occurred")
except RuntimeError as e:
print(e)

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

if True:
print("This is true")

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

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

my_function()
print(y)

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

text = "Hello"
result = text + " " * 3 + "World"
print(result)

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