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

1. 
次のコマンドについて、外部パッケージのすべての依存関係を更新する正しい形式を選んでください。

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

# ファイル名: sample.txt
# 初期内容: なし(ファイルは存在しない)
try:
with open('sample.txt', 'r') as f:
print(f.read())
except FileNotFoundError:
print("File not found")

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Initial
# ---
with open('sample.txt', 'r+') as f:
f.truncate(4)
print(f.read())

4. 
リストnumbers = [3, 1, 4, 2]を昇順に並べ替えて出力するコードはどれでしょうか?

5. 
pipでパッケージをインストールする際に、その依存関係に問題がある場合に使用するコマンドはどれですか?

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

# ファイル名: sample.txt
# 初期内容: なし(ファイルは存在しない)
try:
with open('sample.txt', 'x') as f:
f.write("Exclusive Content")
except FileExistsError:
print("File already exists")

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Data Science
# ---
with open('sample.txt', 'r+') as f:
f.seek(5)
f.truncate()
f.seek(0)
print(f.read())

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line 1
# Line 2
# ---
with open('sample.txt', 'a') as f:
f.write("\nNew Line")
with open('sample.txt', 'r') as f:
print(f.read())

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

numbers = [1, 3, 2, 5, 4]
print(sorted(numbers)[::-1])

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

data = [0, "", False, None]
for value in data:
if value:
print("True value")
else:
print("False value")

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

with open('sample.txt', 'w') as f:
f.write("First Line\n")
raise Exception("An error occurred")

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

people = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35}]
sorted_people = sorted(people, key=lambda x: x["age"])
print([person["name"] for person in sorted_people])

13. 
次のコードで、リストitems = [("apple", 3), ("banana", 2), ("cherry", 5)]を数値の昇順で並べ替えて出力するコードはどれでしょうか?

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

mixed = ["10", "2", "30", "22"]
print(sorted(mixed, key=int))

15. 
次のコードで、リストnames = ["Alice", "Bob", "Charlie"]を文字数の降順に並べ替えて出力するコードはどれでしょうか?

16. 
特定のパッケージの詳細情報(例:バージョン、依存関係、ホームページURL)を表示するためのコマンドはどれですか?

17. 
次のコードで、0から3までの数字を使って「Index: X」を表示するコードはどれでしょうか?

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

$ pip install requests
$ python -c "import requests; print(requests.get('https://httpbin.org/get').status_code)"

19. 
次のコードで、ファイルが存在しない場合に新規作成し、読み書きできるモードを選んでください。

with open('sample.txt', ???) as f:
f.write("New content")
f.seek(0)
print(f.read())

20. 
0から10までの偶数を出力するコードはどれでしょうか?

21. 
辞書data = {"a": 3, "b": 1, "c": 2}のキーを昇順に並べ替えて出力するコードはどれでしょうか?

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line 1
# Line 2
# ---
with open('sample.txt', 'r+') as f:
f.seek(7)
f.write("Modified")
f.seek(0)
print(f.read())

23. 
辞書fruits = {"apple": 1, "banana": 2, "cherry": 3}のキーをすべて出力するコードはどれでしょうか?

24. 
次のコードについて、with文を使用しない場合に注意が必要な点を選んでください。

f = open('sample.txt', 'r')
content = f.read()
f.close()

25. 
Pythonのパッケージリポジトリとして正しいものを選んでください。

26. 
次のコードで、バイナリモードでファイルを開くために適切なモードを選んでください。

with open('image.png', ???) as f:
data = f.read()

27. 
Pythonで外部パッケージをインストールするために使用する標準的なツールとして正しいものを選んでください。

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

$ pip list | grep requests

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

text = "hello"
for i in range(len(text)):
if i % 2 == 0:
print(text[i])

30. 
次のコマンドについて、パッケージとその依存関係を完全に削除するために使用するコマンドはどれですか?

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

# sample.txt の初期内容:
# ---
# Line 1
# Line 2
# ---
with open('sample.txt', 'r') as f:
for line in f:
print(line.strip())

32. 
リストnumbers = [3, 5, 2, 4]を昇順に並べ替えた後に元のリストを変更せずに、並べ替えたリストを出力するコードはどれでしょうか?

33. 
次のコードで、0から4までの数字を出力するコードはどれでしょうか?

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

numbers = [1, 2, 3]
for i in range(len(numbers)):
numbers[i] *= 2
print(numbers)

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

# ファイル名: sample.txt
# 初期内容: なし
with open('sample.txt', 'w') as f:
print(f.readable())

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

# sample.txt の初期内容:
# ---
# Line 1
# Line 2
# ---
with open('sample.txt', 'r') as f:
content = f.read()
print(content)

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

$ pip freeze > requirements.txt
$ cat requirements.txt

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

numbers = [5, 10, 15]
for n in numbers:
print(n + 2)

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

$ pip install requests
$ python -c "import requests; print(requests.__version__)"

40. 
次のコードを実行したときに発生するエラーを選んでください。

with open('missing.txt', 'r') as f:
content = f.read()

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