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

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

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

x = 5

def outer_function():
x = 10
def inner_function():
global x
x += 5
inner_function()
print(x)

outer_function()
print(x)

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

def validate_input(value):
if not isinstance(value, int):
raise TypeError("Value must be an integer")
return value * 2

try:
print(validate_input("text"))
except TypeError as e:
print("Error:", e)

4. 
次のコードのエラー原因として正しいものを選んでください。

def calculate():
total = total + 1
print(total)

calculate()

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

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

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

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

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

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

try:
raise KeyError("Key not found")
except KeyError as e:
print("Caught KeyError:", e)
raise

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

def count_calls():
count = 0

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

return increment

counter = count_calls()
print(counter())
print(counter())

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

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

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

def set_variable():
y = 20
return y

set_variable()
print(y)

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

x = 10

def my_function():
x = 20
return x

print(my_function())
print(x)

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

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

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

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

print(my_function())

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

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

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

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

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

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

x = 10

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

example_function()
print(x)

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

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

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

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

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

def outer_function():
x = 5

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

return inner_function()

print(outer_function())

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

while True
print("Looping")

24. 
次のコードの結果を選択してください。

x = 10

def my_function():
global x
x += 5

my_function()
print(x)

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

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

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

x = 10

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

print(modify_variable())

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

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

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

def add(a, b)
return a + b

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

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

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

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

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

def outer_function():
x = 10

def inner_function():
nonlocal x
x += 5

33. 
Pythonで「Hello」と「World」を改行付きで結合し、「Hello\nWorld」と表示させるコードはどれですか?

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. 
次のコードの実行結果を選んでください。

while True print("Infinite loop")

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

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

print "Hello, World!"

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

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

if True:
print("This is true")

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

x = 5

def outer_function():
x = 10

def inner_function():
return x

return inner_function()

print(outer_function())

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