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

1. 
次のうち、文字列の結合に使われないメソッドはどれですか?

2. 
文字列フォーマットのf-string(フォーマット文字列)の記述方法として正しいものはどれですか?

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

x = 10

def test_function():
x = 20
def modify_variable():
global x
x += 10
modify_variable()
print(x)

test_function()
print(x)

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

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

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

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

7. 
次のコードの出力として正しいものを選んでください。

try:
raise Exception("Generic error")
except Exception as e:
print(type(e).__name__, ":", e)

8. 
文字列の中に改行を含めるにはどの方法を使うべきですか?

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

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)

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

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)

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

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

x = 10

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

print(shadow_variable())
print(x)

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

x = 10

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

print(modify_variable())
print(x)

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

x = 10

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

modify_variable()
print(x)

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

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)

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

raise KeyError("Key not found")

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

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

x = [1, 2, 3]

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

modify_list()
print(x)

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

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

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

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

x = 5

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

outer_function()
print(x)

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

x = 1

def first_function():
global x
x += 1

def second_function():
global x
x *= 3

first_function()
second_function()
print(x)

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

x = 5

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

example_function()
print(x)

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

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)

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

26. 
Pythonのsplitlines()メソッドはどのような機能を持っていますか?

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

28. 
次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか?

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

text = """Line1
Line2
Line3"""
print(text.splitlines()[0])

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

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

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

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

x = 0

def increment():
global x
x += 1
return x

print(increment())
print(increment())

32. 
"Python3"という文字列から「3」を削除して"Python"にするコードはどれですか?

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

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

x = 5

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

print(modify_variable())
print(x)

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

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

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

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

print(my_function())

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

print("Missing parenthesis in print statement"

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

40. 
グローバル変数を関数内で変更するために使用するキーワードは何ですか?

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