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

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

try:
raise ValueError("Invalid data provided")
except KeyError as e:
print("Caught KeyError:", e)
except ValueError as e:
print("Caught ValueError:", e)

2. 
文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか?

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

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

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

while True print("Infinite loop")

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

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)

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

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

print(my_function())

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

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

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

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

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

x = 5

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

outer_function()
print(x)

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

x = 10

def outer_function():
x = 20

def inner_function():
return x + 5

return inner_function()

print(outer_function())
print(x)

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

x = 5

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

outer_function()
print(x)

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

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

x = 1

def outer_function():
def inner_function():
global x
x += 1
inner_function()

outer_function()
print(x)

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

for i in range(5)
print(i)

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

x = 10

def my_function():
x = 20
return x

print(my_function())
print(x)

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

text = "Line1\nLine2\nLine3"
result = text.split("\n")
print(result[1])

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

if True:
print("This is true")

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

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

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

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

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

20. 
Pythonの文字列で改行を表すエスケープシーケンスはどれですか?

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

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

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

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

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

x = 5

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

example_function()
print(x)

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

print("Hello

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

def counter():
count = 0

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

return increment

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

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

while True
print("Looping")

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

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

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

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

def count_calls():
count = 0

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

return increment

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

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

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)

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

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

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

try:
raise ValueError("This is a value error")
except ValueError as e:
print("Caught error:", e)

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

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

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

def reset_list(values):
values = [0, 0, 0]

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

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

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

print(test_function())
print(y)

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

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

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

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

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

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

if True print("This will not work")

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

try:
raise ValueError("Test error")
except Exception as e:
print("Caught an exception")
raise e

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