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

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Python is fun.
# ---
with open('sample.txt', 'r+') as f:
f.seek(10)
f.write("awesome!")
f.seek(0)
print(f.read())

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

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

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

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

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

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

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

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

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

numbers = [4, 1, 3, 2]
sorted_numbers = sorted(numbers)
print(numbers)
print(sorted_numbers)

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

$ python -c "import nonexistent"

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

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

9. 
with文を使用する主な目的として正しいものを選んでください。

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

$ pip install requests
$ pip list | grep requests

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

12. 
文字列text = "Python"の各文字を1文字ずつ出力するコードはどれでしょうか?

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

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

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

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

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

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

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

$ pip list | grep requests

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

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

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

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

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

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

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

22. 
次のコードを実行したとき、with文を使用する理由として最も適切なものを選んでください。

with open('data.txt', 'w') as f:
for i in range(5):
f.write(f"Line {i}\n")

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

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

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

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

25. 
次のコードで、ファイルを読み書きするために適切なモードを選んでください。

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

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

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

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

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

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

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

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

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

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

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

32. 
リストitems = ["apple", "banana", "cherry"]の要素をインデックス付きで出力するコードはどれでしょうか?

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

values = [10, 5, 20, 15]
values.sort(reverse=True)
print(values)

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

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

35. 
次のコマンドについて、特定のバージョンの外部パッケージをインストールするために正しい形式を選んでください。

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

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

37. 
次のコマンドについて、すべてのインストール済みパッケージとそのバージョンを表示するために使用する正しい形式を選んでください。

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

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

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

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

40. 
次のコードで、with文を使うメリットとして正しいものを選んでください。

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

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