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

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

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

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

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

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

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

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

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

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

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

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

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

8. 
次のコマンドについて、仮想環境を作成する正しい手順として選んでください。

python -m venv env

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

x = 10
result = "非ゼロ" if x and x % 2 == 0 else "ゼロまたは奇数"
print(result)

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

# sample.txt は存在しない
try:
with open('sample.txt', 'w+') as f:
f.write("Testing w+ mode")
f.seek(0)
print(f.read())
except Exception as e:
print(f"Error: {e}")

11. 
変数xが負でない場合に「非負の数」と表示し、負の場合に「負の数」と表示するコードはどれでしょうか?

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

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

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

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

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

# sample.txt
# ---
# Hello, Python!
# ---
with open('sample.txt', 'r') as f:
for line in f:
print(line, end="")

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

value = "Python"
formatted = "{:^10}".format(value)
print(formatted)

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

# sample.txt
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'r') as f:
for line in f:
print(line.strip())

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# ---
with open('sample.txt', 'w') as f:
f.write("New Line")
f.seek(0)
f.write("Old")

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

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

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

n = 10
result = "偶数かつ5の倍数" if n % 2 == 0 and n % 5 == 0 else "条件を満たさない"
print(result)

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

# sample.txt
# 初期内容: なし
try:
with open('sample.txt', 'x') as f:
f.write("New File Content")
f.write("\nSecond Line")
except FileExistsError:
print("File already exists")

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

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

23. 
次のコードを実行した場合、仮想環境の削除後にwhich pythonを実行した場合の出力として正しいものを選んでください。

$ python -m venv env
$ source env/bin/activate
$ deactivate
$ rm -rf env
$ which python

24. 
仮想環境内に特定のライブラリをインストールするコマンドとして正しいものを選んでください。

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

# ファイル名: sample.txt
with open('sample.txt', 'w') as f:
f.write("First Line\n")
f.flush()
f.write("Second Line\n")

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

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

27. 
リストmy_listが空である場合にTrueを、要素が含まれている場合にFalseを出力するコードはどれでしょうか?

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

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

29. 
次のコードを実行した場合、仮想環境の有効化が確認できる出力として正しいものを選んでください。

$ python -m venv env
$ source env/bin/activate

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

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

# sample.txt は存在しない
try:
with open('sample.txt', 'r+') as f:
f.write("Hello, World!")
except FileNotFoundError:
print("File not found")

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

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

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

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

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

x = 0
result = "ゼロ以外" if x or x + 5 else "ゼロです"
print(result)

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

a, b = 10, 0
result = "実行されました" if a > 5 or b / 0 else "実行されませんでした"
print(result)

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

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

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

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

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

# sample.txt
# ---
# Python is fun.
# File Handling is useful.
# ---
with open('sample.txt', 'r') as f:
print(f.readlines()[-1])

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

elements = [True, True, False]
result = "すべてがTrueです" if all(elements) else "Falseが含まれています"
print(result)

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

data = {"name": "Alice", "age": 25, "score": 92.5}
formatted = "{name} is {age} years old and scored {score:.1f}.".format(**data)
print(formatted)

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