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

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

# sample.txt
# ---
# Python Programming
# File Handling
# ---
with open('sample.txt', 'r') as f:
print(f.read(15))
print(f.read())

2. 
変数numが偶数である場合にTrueを、奇数である場合にFalseを出力するコードはどれでしょうか?

3. 
仮想環境の無効化(deactivate)のためのコマンドとして正しいものを選んでください。

4. 
仮想環境を使用する主な利点として正しいものを選んでください。

5. 
変数xがNoneである場合にTrueを、そうでない場合にFalseを出力するコードはどれでしょうか?

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

# sample.txt の初期内容:
# ---
# Old Content
# ---
with open('sample.txt', 'w') as f:
f.write("New Content")

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

text = "Python"
result = "非空文字列です" if text else "空文字列です"
print(result)

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

a, b = 5, None
result = "成立" if a > 0 or b is not None else "不成立"
print(result)

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

# empty.txt は空のファイル
with open('empty.txt', 'r') as f:
print(f.read())

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

value = 123.456789
formatted = "{:.2f}".format(value)
print(formatted)

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

# ファイル名: sample.txt
with open('sample.txt', 'w') as f:
f.write("Line1\n")
f.write("Line2\n")
print(f.tell())

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

$ python -m venv myenv
$ source myenv/bin/activate
$ which python

13. 
変数aがゼロでない場合にTrueを、ゼロである場合にFalseを出力するコードはどれでしょうか?

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

items = [None, 0, "", False]
result = "有効な要素が含まれていません" if not any(items) else "有効な要素が含まれています"
print(result)

15. 
次のコードを実行したときの出力を選んでください。

# sample.txt
# ---
# Hello, World!
# ---
with open('sample.txt', 'r') as f:
print(f.read())

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

value = 0.1234
formatted = "{:.2%}".format(value)
print(formatted)

17. 
次のコードにおいて、テキストファイルにデータを書き込むための正しい方法を選んでください。

# ファイル名: sample.txt
with open('sample.txt', ???) as f:
f.write("Hello, World!")

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

data = None
result = "値がありません" if data is None else "値があります"
print(result)

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

value = 12345
formatted = "{:b}".format(value)
print(formatted)

20. 
変数aが5の倍数で、変数bが2の倍数である場合に「条件を満たします」と表示するコードはどれでしょうか?

21. 
仮想環境を作成する際に別のPythonバージョンを指定する方法として正しいものを選んでください。

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

# sample.txt
# ---
# Python Programming
# File Handling
# ---
with open('sample.txt', 'r') as f:
print(f.read(7))
print(f.readline())

23. 
次のコードを実行したときのsample.txtの内容を選んでください。

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

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

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

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r+') as f:
f.write("New")
f.seek(6)
f.write("Content")

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

data = [None, False, 0]
result = "全ての要素が無効です" if not any(data) else "有効な要素があります"
print(result)

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

# sample.txt
# ---
# Initial Content
# ---
with open('sample.txt', 'w') as f:
f.truncate(5)

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

number = 0
result = "ゼロです" if not number else "ゼロ以外の数です"
print(result)

29. 
次のコードでf.readline()を使用した場合の出力を選んでください。

# sample.txt
# ---
# Hello, Python!
# Welcome to the world of programming.
# ---
with open('sample.txt', 'r') as f:
print(f.readline())

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

username = None
result = "ユーザー名が設定されています" if username else "ユーザー名が設定されていません"
print(result)

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

value = 1234
formatted = "{:08}".format(value)
print(formatted)

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

formatted = "{:*>10}".format("test")
print(formatted)

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

# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
print(f.read())
print(f.read())

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

values = [1.2345, 2.3456, 3.4567]
formatted = ["{:.1f}".format(v) for v in values]
result = ", ".join(formatted)
print(result)

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

value = 255
formatted = "{:X}".format(value)
print(formatted)

36. 
変数aが正で、変数bが負である場合に「条件を満たします」と表示するコードはどれでしょうか?

37. 
仮想環境を有効化するための正しいコマンドとして選んでください(Linux/Macの場合)。

38. 
変数nameが空でない場合にTrueを、空である場合にFalseを出力するコードはどれでしょうか?

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

items = [1, 2, 3]
result = "リストに要素があります" if items else "リストは空です"
print(result)

40. 
変数aが10以上、または変数bが5以下の場合に「条件を満たします」と表示するコードはどれでしょうか?

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