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

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

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

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

x = 5

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

outer_function()
print(x)

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

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

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

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

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

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

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

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

print("Hello

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

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

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

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

if True:
print("This is true")

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

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

print(test_function())
print(y)

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

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

for i in range(5)
print(i)

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

def counter():
count = 0

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

return increment

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

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

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

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

x = 0

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

print(example_function())
print(x)

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

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

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

19. 
変数animal = "cat"とsound = "meow"を使って、「The cat says meow」という文字列を生成するために入力すべきコードはどれですか?

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

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

def greet(name):
print("Hello,", name)

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

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

calculate()

23. 
改行を含む文字列"Hello\nWorld"をスペースで区切って一行にまとめるためのコードはどれですか?

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

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

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

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

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

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

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

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)

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

x = 10

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

print(shadow_variable())
print(x)

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

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

x = 10

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

print(modify_variable())
print(x)

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

x = 10

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

print(modify_variable())

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

def set_variable():
y = 20
return y

set_variable()
print(y)

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

x = 5

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

print(multiply_by_two(x))
print(x)

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

def check_positive(x):
if x < 0:
raise ValueError("Negative values are not allowed")
return x

try:
print(check_positive(-10))
except ValueError as e:
print(e)

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

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

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

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

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

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)

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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