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

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

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

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

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

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

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

def outer_function():
x = 10

def inner_function():
nonlocal x
x += 5

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

raise KeyError("Key not found")

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

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

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

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

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

while True
print("Looping")

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

x = 0

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

print(example_function())
print(x)

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

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

y = 100

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

outer_function()
print(y)

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

x = 10

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

example_function()
print(x)

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

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

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

x = 10

def outer_function():
x = 20

def inner_function():
return x + 5

return inner_function()

print(outer_function())
print(x)

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

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

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

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

x = 10

def my_function():
global x
x += 5

my_function()
print(x)

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

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

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

x = [1, 2, 3]

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

modify_list()
print(x)

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

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

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

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

x = 5

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

example_function()
print(x)

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

def count_calls():
count = 0

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

return increment

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

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

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

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

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

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

28. 
次のコードを実行したときにエラーが発生する理由として正しいものを選んでください。

def example_function():
x += 1
print(x)

example_function()

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

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

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

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

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

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)

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

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

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

x = 5

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

outer_function()
print(x)

35. 
ミュータブル(変更可能)なオブジェクトに該当するものはどれですか?

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

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)

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

try:
raise RuntimeError("Runtime issue")
except RuntimeError as e:
print("Caught:", e)
raise

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

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

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

x = 10

def my_function():
x = 20
return x

print(my_function())
print(x)

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