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

1. 
次のコードの出力として正しいものはどれですか?

increment = lambda x: x + 1
print(increment(10))

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

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

3. 
次のコードについて、os.remove()の用途として正しい説明を選んでください。

import os
os.remove("example.txt")

4. 
次のコードについて、正しい説明を選んでください。

class MyClass:
def instance_method(self):
print("This is an instance method")

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

value = ""
result = "空でない値です" if bool(value) else "空の値です"
print(result)

6. 
次のコードについて、正しい説明を選んでください。

class A:
pass

class B(A):
pass

class C(B):
pass

obj = C()
print(isinstance(obj, A))

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

class MyClass:
class_variable = 0

@classmethod
def increment(cls):
cls.class_variable += 1

obj1 = MyClass()
obj2 = MyClass()

obj1.increment()
obj2.increment()
print(MyClass.class_variable)

8. 
raise文の役割として正しいものを選んでください。

9. 
次のosモジュールの関数のうち、ディレクトリを削除するために使用されるものはどれですか?

10. 
次のコードについて、正しい説明を選んでください。

class MyClass:
class_variable = "shared"

obj = MyClass()
print(obj.class_variable)

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

age = 17
result = "未成年" if age < 20 else "成人"
print(result)

12. 
次のコードに関する正しい説明はどれですか?

def append_item(item, items=[]):
items.append(item)
return items

13. 
次のコードについて、インスタンスメソッドの正しい説明を選んでください。

class MyClass:
def greet(self):
return "Hello!"

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

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

15. 
次のコードにおいて、相対インポートを使用して親パッケージ内のモジュールをインポートする正しい方法を選んでください。

# パッケージ構造:
# package/
# ├── __init__.py
# ├── module1.py
# ├── subpackage/
# │ ├── __init__.py
# │ ├── module2.py
# module2.py から module1 をインポートする

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

def check_value(x):
if x < 0:
raise ValueError("Negative value not allowed")
return x * 2

try:
print(check_value(-5))
except ValueError as e:
print("Error:", e)

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

add = lambda x, y=10: x + y
print(add(5))

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

from datetime import datetime

date = datetime(2024, 2, 29)
days_to_add = 365
new_date = date.replace(year=date.year + 1) + timedelta(days=days_to_add - 1)
print(new_date.strftime("%Y-%m-%d"))

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

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

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

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

21. 
次のコードについて、親クラスのメソッドを子クラスから呼び出す正しい方法を選んでください。

class Parent:
def greet(self):
return "Hello from Parent!"

class Child(Parent):
def greet(self):
return super().greet() + " and Child!"

obj = Child()
print(obj.greet())

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

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

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Line1
# Line2
# ---
with open('sample.txt', 'w') as f:
f.writelines(["New1\n", "New2\n", "New3\n"])

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

x = 10

def modify_variable():
global x
x = "Hello"

modify_variable()
print(x)

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

my_dict = {i: i**2 for i in range(3)}
print(my_dict)

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

score = 85
result = "優秀" if score >= 90 else "合格" if score >= 60 else "不合格"
print(result)

27. 
Pythonで単体テストを記述するために使用される標準ライブラリとして正しいものを選んでください。

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

def format_text(text, uppercase=False, exclamation=False):
if uppercase:
text = text.upper()
if exclamation:
text += "!"
return text

print(format_text("hello", uppercase=True, exclamation=True))

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

import unittest

class TestExample(unittest.TestCase):
def test_exception(self):
with self.assertRaises(ZeroDivisionError):
result = 1 / 0

if __name__ == "__main__":
unittest.main()

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

num = 12
if num % 3 == 0:
print("3の倍数")
elif num % 4 == 0:
print("4の倍数")
else:
print("3でも4でもない")

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

32. 
次のコードを実行した場合、headers変数に含まれるデータとして正しいものを選んでください。

import urllib.request

req = urllib.request.Request("https://www.example.com")
response = urllib.request.urlopen(req)
headers = dict(response.getheaders())
print(headers)

33. 
次のコードの出力は何ですか?

data = [["x", "y"], ["z", ["a", "b", "c"]], ["d", "e"]]
print(data[1][1][2])

34. 
変数tempが20以下なら「寒いです」と表示し、20より大きく30未満なら「快適です」と表示するコードはどれでしょうか?

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── subpackage/
# │ ├── __init__.py
# │ ├── module.py
# subpackage/__init__.py
from .module import hello

# main.py
from package.subpackage import hello
print(hello())

36. 
Pythonにおいて、標準ライブラリモジュールmathを使用するための適切なコードを選んでください。

37. 
次のコードの実行結果として正しいものを選んでください。

def my_function():
y = 7
print(y)

my_function()
print(y)

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

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

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

age = 18
result = "成人" if age >= 20 else "未成年"
print(result)

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

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