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

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

def test_function():
y = 5
y += 1
return y

print(test_function())
print(y)

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

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

x = [1, 2, 3

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

x = 3

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

print(modify_variable())
print(x)

5. 
リスト["Python", "Java", "C++"]を結合して"Python and Java and C++"とするためのコードはどれですか?

6. 
Pythonの文字列で改行を表すエスケープシーケンスはどれですか?

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

x = 5

def outer_function():
x = 10

def inner_function():
return x

return inner_function()

print(outer_function())

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

if True print("This will not work")

9. 
文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか?

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

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

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

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

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

def outer_function():
x = 5

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

return inner_function()

print(outer_function())

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

x = 5

def modify_variable():
global x
x += 10
print(x)

modify_variable()
print(x)

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

def reset_counter():
count = 0

def increment():
nonlocal count
count += 1
return count

def reset():
nonlocal count
count = 0

return increment, reset

increment, reset = reset_counter()
print(increment())
print(increment())
reset()
print(increment())

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

x = 10

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

print(shadow_variable())
print(x)

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

19. 
次のコードのnonlocalキーワードの用途として正しいものはどれですか?

def outer_function():
x = 10

def inner_function():
nonlocal x
x += 5

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

for i in range(3):
print(i)

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

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

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

try:
raise IndexError("Index out of range")
except KeyError as e:
print("Caught KeyError:", e)
except Exception as e:
print("Caught Exception:", e)

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

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

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

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

x = 10

def modify_variable():
x = 20
return x

print(modify_variable())
print(x)

25. 
次のうち、文字列の結合に使われないメソッドはどれですか?

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

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

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

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

def modify_string(s):
s += " World"
return s

my_string = "Hello"
result = modify_string(my_string)
print(result)
print(my_string)

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

try:
raise ValueError("Test error")
except Exception as e:
print("Caught an exception")
raise e

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

x = 5

def multiply_by_two(x):
x = x * 2
return x

print(multiply_by_two(x))
print(x)

31. 
次のコードでraise文の動作として正しい説明を選んでください。

if not isinstance(x, int):
raise TypeError("x must be an integer")

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

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

def check_divisible(a, b):
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a / b

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

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

try:
raise TypeError("Custom type error")
except KeyError as e:
print("Caught KeyError:", e)
except Exception as e:
print("Caught Exception:", e)

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

while True
print("Looping")

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

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

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

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

z = [10, 20, 30]

def modify_list():
global z
z[1] = 99

modify_list()
print(z)

39. 
文字列の結合にjoin()メソッドを使用する場合、リスト["apple", "banana", "cherry"]をカンマ区切りの文字列にする正しいコードはどれですか?

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

print "Hello, World!"

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