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

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

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

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

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

values = [1, 2, 3, 4]
for v in values:
if v > 2:
print(v)

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# ---
with open('sample.txt', 'r') as f:
print(f.writable())

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

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

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

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

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

$ pip show requests

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

names = ["Alice", "Bob", "Charlie"]
for name in names:
if len(name) > 3:
print(name)

9. 
特定のパッケージがインストールされているかを確認するコマンドはどれですか?

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

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'rb') as f:
print(f.read(5))

12. 
次のコードを実行した場合、requirements.txtファイルに保存される内容として正しいものを選んでください。

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

13. 
pipを使用してパッケージを最新バージョンに更新するための正しいコマンドはどれですか?

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

$ python -c "import nonexistent"

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

for i in range(2, 8, 2):
print(i)

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

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

17. 
open()関数における第2引数で指定するファイルモードとして正しい組み合わせを選んでください。

18. 
次のコードを実行した場合、エラーが発生する原因として正しいものを選んでください。

$ pip uninstall nonexistent

19. 
次のコマンドについて、requirements.txtに基づいてパッケージをインストールするための正しい形式を選んでください。

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

items = [(2, "b"), (3, "c"), (1, "a")]
sorted_items = sorted(items)
print(sorted_items)

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

# ファイル名: 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())

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

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

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

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

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

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

numbers = [1, 4, 3, 2]
numbers.sort()
numbers.reverse()
print(numbers)

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'a+') as f:
f.write("\nNew Line")
f.seek(0)
print(f.read())

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

total = 0
for i in range(1, 5):
total += i
print(total)

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

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

29. 
文字列text = "hello"の文字を昇順に並べ替えて出力するコードはどれでしょうか?

30. 
次のコードで使用されているファイルモードに対応する操作を選んでください。

with open('sample.txt', 'a') as f:
f.write("Appended content")

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

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

32. 
次のコードで、文字列リストwords = ["apple", "banana", "cherry"]をアルファベット順に逆順(降順)で並べ替えて出力するコードはどれでしょうか?

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

34. 
次のコードについて、with文が持つ特性として正しいものを選んでください。

with open('logfile.txt', 'a') as log:
log.write("Log entry\n")

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

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

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

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

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

$ pip install requests==2.28.1
$ pip show requests

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

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

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

fruits = ["banana", "apple", "cherry"]
print(sorted(fruits, key=lambda x: x[-1]))

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

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])

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