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

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

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

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

3. 
次のコードで、文字列"apple"の各文字を逆順に出力するコードはどれでしょうか?

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

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

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

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

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

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

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

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

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

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

9. 
次のコードで、'x'モードを使用した場合の動作を選んでください。

with open('sample.txt', 'x') as f:
f.write("Exclusive content")

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

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

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

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

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

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

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

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

words = ["one", "two", "three"]
for word in words:
print(word.upper())

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

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

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

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

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

18. 
次のコードで、with文を使用した場合に発生しない問題を選んでください。

f = open('sample.txt', 'w')
f.write("Test content")
# プログラムがここで終了した場合

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

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

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

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

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

words = ["apple", "banana", "cherry"]
words.sort(key=len)
print(words)

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

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

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

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

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

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

25. 
次のコードについて、with文を使用して書き換えた正しい方法を選んでください。

f = open('sample.txt', 'w')
f.write("Hello, World!")
f.close()

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

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

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

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

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

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

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

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

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

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

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

32. 
複数のパッケージを一括でインストールするためのコマンドはどれですか?

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

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

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

$ pip install numpy
$ python -c "import numpy as np; print(np.zeros((2, 2)))"

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

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

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

$ pip uninstall nonexistent

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

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

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

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

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

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

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

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