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

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

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

2. 
次のコードを実行した場合の出力結果を選んでください。

def set_variable():
y = 20
return y

set_variable()
print(y)

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

for i in range(3)
print(i)

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

def counter():
count = 0

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

return increment

incrementer = counter()
print(incrementer())
print(incrementer())

5. 
文字列text = "Line1\nLine2\nLine3"に含まれる改行ごとに分割し、リストにする方法はどれですか?

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

x = 5

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

modify_variable()
print(x)

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

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

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

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

text = "Welcome"
repeat_text = text * 3
print(repeat_text)

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

raise KeyError("Key not found")

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

x = 10
if x > 5
print("x is greater than 5")

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

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

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

names = ["John", "Paul", "George", "Ringo"]
result = ", ".join(names)
print(result)

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

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

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

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

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

17. 
ローカル変数とグローバル変数のスコープを区別するために使用されるキーワードはどれですか?

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

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

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

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

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

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

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

x = 10

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

print(modify_variable())

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

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

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

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

z = [10, 20, 30]

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

modify_list()
print(z)

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

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

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. 
次のコードの問題点として正しいものを選んでください。

for i in range(5)
print(i)

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

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)

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

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

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

32. 
変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか?

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

x = 5

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

print(multiply_by_two(x))
print(x)

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

def add(a, b)
return a + b

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

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

36. 
次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか?

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

x = 10

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

test_function()
print(x)

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

def create_multiplier(factor):
def multiplier(x):
return x * factor
return multiplier

times_three = create_multiplier(3)
times_five = create_multiplier(5)

print(times_three(10))
print(times_five(10))

39. 
次のコードに関する正しい説明はどれですか?

x = 10

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

example_function()
print(x)

40. 
グローバル変数を使用する際の注意点として適切なものはどれですか?

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