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

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

2. 
次のうち、unittestでテストを実行するために使用される標準的なコマンドとして正しいものを選んでください。

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

data = [[100, 200, 300], [400, [500, 600]], [700]]
print(data[1][1][1])

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

import urllib.request

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

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

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

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

a = 3
b = 5
result = "aとbは等しい" if a == b else "aはbより小さい" if a < b else "aはbより大きい"
print(result)

7. 
Pythonのディクショナリについて正しいものを選んでください。

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── subpackage/
# │ ├── __init__.py
# │ ├── module.py
# module.py
def greet():
return "Greetings from module!"

# main.py
from package.subpackage import module
print(module.greet())

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

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

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

text = "Python"
for char in text:
print(char, end="-")

11. 
次のコードの問題点として正しいものを選んでください。

if True
print("This is true")

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

try:
raise TypeError("Custom type error")
except KeyError as e:
print("Caught KeyError:", e)
except Exception as e:
print("Caught Exception:", e)

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

class MyClass:
@classmethod
def greet(cls):
return "Hello from class method"

print(MyClass.greet())

14. 
次のコードを実行した場合、出力は何ですか?

from collections import deque
dq = deque([1, 2, 3, 4], maxlen=4)
dq.append(5)
print(dq)

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

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

16. 
dequeで要素を先頭に追加するメソッドはどれですか?

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

18. 
次のコードの出力結果は何でしょうか?

queue = []
for i in range(1, 4):
queue.append(i)
for _ in range(2):
queue.pop(0)
print(queue)

19. 
次のコードについて、正しい実行結果を選んでください。

class MyClass:
def __init__(self, value):
self.value = value

obj = MyClass(5)
print(obj.value)

20. 
次のコードについて、glob.glob("data/**", recursive=True)が返す結果として正しい説明を選んでください。

import glob
result = glob.glob("data/**", recursive=True)

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

def extend_list(item, target=None):
if target is None:
target = []
target.append(item)
return target

list1 = extend_list(1)
list2 = extend_list(2, [])
list3 = extend_list(3)

print(list1)
print(list2)
print(list3)

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

x = 5

def modify_variable():
global x
x = x * 2
return x

print(modify_variable())
print(x)

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

value = []
result = "空のリストです" if not value else "リストに値があります"
print(result)

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── subpackage/
# │ ├── __init__.py
# │ ├── module2.py
# │ ├── module3.py
# module2.py
from . import module3
print(dir(module3))

# module3.py
def greet():
return "Hello from module3"

class Greeter:
def say_hello(self):
return "Hello, world!"

25. 
変数numが偶数ならば「偶数です」、奇数なら「奇数です」と表示する条件式はどれでしょうか?

26. 
次のコードについて、selfの適切な説明を選んでください。

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

27. 
次のコードを実行した場合、出力は何ですか?

lst = [1, 2, 3]
lst *= 2
print(lst)

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

class MyClass:
def __init__(self, value):
self.value = value

obj = MyClass(5)
print(obj.value)
obj.value = 10
print(obj.value)

29. 
次のコードについて、テストメソッド名に必要なルールとして正しいものを選んでください。

import unittest

class TestExample(unittest.TestCase):
def test_something(self):
self.assertTrue(True)

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

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

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

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

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

class MyClass:
def __init__(self, value):
self.value = value

obj1 = MyClass(10)
obj2 = obj1
obj2.value = 20
print(obj1.value)

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

import statistics
data = [1, 2, 3, 4, 5, 6, 7, 8]
variance = statistics.variance(data)
print(round(variance, 2))

34. 
次のコードのクラス定義として正しいものを選んでください。

class MyClass:
pass

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

set1 = {1, 2, 3}
set2 = {2, 3, 4}
result = set1.symmetric_difference(set2)
print(result)

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

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

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

def greet(name="Guest"):
return "Hello, " + name + "!"

print(greet())
print(greet("Alice"))

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

class MyClass:
class_variable = "shared"

obj1 = MyClass()
obj2 = MyClass()
MyClass.class_variable = "modified"

print(obj1.class_variable)
print(obj2.class_variable)

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

def check_divisible(a, b):
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a / b

try:
print(check_divisible(10, 0))
except ZeroDivisionError as e:
print("Error:", e)

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── subpackage/
# │ ├── __init__.py
# │ ├── module.py
# subpackage/module.py
def hello():
return "Hello from subpackage!"

# main.py
import package.subpackage.module as mod
print(mod.hello())

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