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

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

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

def outer_function():
x = 5

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

return inner_function()

print(outer_function())

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

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

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

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

x = [1, 2, 3]

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

modify_list(x)
print(x)

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

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)

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

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

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

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

print "Hello, World!"

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

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)

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

y = 100

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

outer_function()
print(y)

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

for i in range(5)
print(i)

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

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

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

example_function()

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

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

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

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

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

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

x = 10

def modify_variable():
global x
x = "Hello"

modify_variable()
print(x)

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

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

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

print(test_function())
print(y)

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

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

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

def outer_function():
x = 10

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

return inner_function()

print(outer_function())

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

x = 5

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

print(multiply_by_two(x))
print(x)

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

27. 
次のコードの出力結果は何ですか?

x = 10

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

my_function()
print(x)

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

raise KeyError("Key not found")

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

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

if True print("This will not work")

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

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

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

x = 10

def example_function():
print(x)

example_function()

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

x = 5

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

print(modify_variable())
print(x)

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

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

def add(a, b)
return a + b

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

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

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

if True
print("This is true")

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

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

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

x = 5

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

example_function()
print(x)

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

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

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