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

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

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

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

2. 
次のコードにおいて、finallyブロックの役割として正しいものを選んでください。

try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution complete")

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

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

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

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

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

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

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

import math
x = math.degrees(math.atan(1))
print(round(x, 2))

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

t = (1, 2, 3)
print(len(t))

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

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

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

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

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

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")

11. 
次のコマンドについて、requirements.txtに基づいてパッケージをインストールするための正しい形式を選んでください。

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

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

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

from collections import deque
dq = deque([1, 2, 3])
x = dq.popleft()
y = dq.pop()
print(dq)

14. 
グローバル変数を関数内で変更するために使用するキーワードは何ですか?

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

import unittest

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

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

16. 
次のコードを実行した場合、結果は何になりますか?

def add_numbers(a, b=10, c=5):
return a + b + c

print(add_numbers(1, c=2))

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

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

# ファイル名: sample.txt
# 初期内容: なし(ファイルは存在しない)
try:
with open('sample.txt', 'r+') as f:
f.write("Content")
except FileNotFoundError:
print("File not found")

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

lst = [1, 2, 3, 4]
x = lst.pop(2)
print(x, lst)

20. 
リストnumbers = [3, 1, 4, 2]を昇順に並べ替えて出力するコードはどれでしょうか?

21. 
次のコードを実行した後のstackの内容は何でしょうか?

stack = []
stack.append(1)
stack.append(2)
stack.pop()
stack.append(3)

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

class MyClass:
pass

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

lst = [1, 2, 3, 4]
lst.clear()
print(lst)

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

class MyClass:
def __init__(self):
self.count = 0

def increment(self):
self.count += 1

obj = MyClass()
obj.increment()
obj.increment()
print(obj.count)

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

x = 0

def example_function():
global x
x = x + 2
return x

print(example_function())
print(x)

26. 
次のうち、Pythonのstatisticsモジュールで最頻値(モード)を計算するための関数として正しいものを選んでください。

27. 
次のコードについて、テストスキップの用途として正しい説明を選んでください。

import unittest

class TestExample(unittest.TestCase):
@unittest.skip("Not implemented yet")
def test_case(self):
self.assertTrue(True)

28. 
次のコードの出力結果として正しいものを選んでください。

my_set = set([1, 2, 2, 3])
print(my_set)

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

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 & set2
print(result)

30. 
unittestのmockモジュールの用途として正しいものを選んでください。

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

marks = 55
result = "Excellent" if marks >= 90 else "Good" if marks >= 75 else "Average" if marks >= 50 else "Poor"
print(result)

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

class MyClass:
class_variable = "shared"

MyClass.class_variable = "modified"
print(MyClass.class_variable)

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

import urllib.request

try:
response = urllib.request.urlopen("https://www.example.com")
print(response.read().decode("utf-8")[:20])
except urllib.error.HTTPError as e:
print(f"HTTP Error: {e.code}")
except urllib.error.URLError as e:
print(f"URL Error: {e.reason}")

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

if 5 > 3:
print("5 is greater than 3")

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

a, b = 0, 10
result = "成立" if a != 0 and b / a > 2 else "不成立"
print(result)

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

def greet_many(times=1):
return "Hello! " * times

print(greet_many())
print(greet_many(3))

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

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

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

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

value = [123.456, 78.9, 4.321]
formatted = " | ".join("{:.1f}".format(v) for v in value)
print(formatted)

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

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

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

mod = lambda x, y: x % y
print(mod(10, 3))

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