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

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

x = 10

def shadow_variable():
x = 5
def inner():
return x
return inner()

print(shadow_variable())
print(x)

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

# ファイル名: sample.txt
# 初期内容: なし
with open('sample.txt', 'w') as f:
print(f.readable())

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

import os
print(os.getcwd())

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

def customize_greeting(name="Guest", greeting="Welcome"):
return f"{greeting}, {name}!"

print(customize_greeting(greeting="Hello"))

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

class Parent:
def __init__(self):
self.data = [1, 2, 3]

class Child(Parent):
def __init__(self):
super().__init__()
self.data.append(4)

obj = Child()
print(obj.data)

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

my_set = {1, 2, 3}
my_set.update({3, 4, 5})
print(my_set)

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

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

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

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

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

sort_by_length = lambda lst: sorted(lst, key=lambda s: len(s))
print(sort_by_length(["apple", "fig", "banana", "kiwi"]))

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

import json
data = {"name": "David", "age": 40, "city": "Hiroshima"}
json_string = json.dumps(data, indent=4)
print(json_string)

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

class MyClass:
class_variable = [1, 2, 3]

obj1 = MyClass()
obj1.class_variable.append(4)

print(MyClass.class_variable)

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

my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = list(my_dict.keys())
values = list(my_dict.values())
print(keys[1], values[1])

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

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

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

values = [1, 2, 3, 4]
for v in values:
if v > 2:
print(v)

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

import os
os.makedirs("test_dir/sub_dir", exist_ok=True)
print("Directories created")

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── module1.py
# ├── module2.py
# __init__.py
from .module1 import func1
from .module2 import func2

# main.py
from package import func1, func2
print(func1(), func2())

# module1.py
def func1():
return "Function 1"

# module2.py
def func2():
return "Function 2"

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

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

obj = MyClass([1, 2, 3])
obj.values[0] = 10
print(obj.values)

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

print(__name__)

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

import math
value = math.factorial(5)
print(value)

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

21. 
次のコードについて、クラスメソッドを定義する方法として正しいものを選んでください。

class MyClass:
@???
def my_class_method(cls):
print("This is a class method")

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

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

23. 
次のコードについて、クラス名として適切なものを選んでください。

class ???:
pass

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

# ファイル名: sample.txt
# 初期内容:
# ---
# Data Science
# ---
with open('sample.txt', 'r+') as f:
f.seek(5)
f.truncate()
f.seek(0)
print(f.read())

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

def check_divisor(y):
if y == 0:
raise ZeroDivisionError("Divisor cannot be zero")
return 10 / y

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

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

my_set = {1, 2, 3}
another_set = {2, 3}
result = my_set.issuperset(another_set)
print(result)

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

queue = [1, 2, 3]
queue.append(4)
queue.pop(0)

28. 
次のコードで、__file__属性が表す内容として正しいものを選んでください。

import os
print(os.__file__)

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

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

30. 
次のコードについて、staticmethodの役割として正しいものを選んでください。

class MyClass:
@staticmethod
def my_static_method():
print("This is a static method")

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

t = (1, 2, 3, 4, 5)
print(t[-2])

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

stack = [10, 20, 30]
stack.append(40)
stack.pop()
stack.append(50)
stack.pop()
stack.append(60)
stack.pop()

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

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

obj = MyClass("Bob")
del obj.name
print(obj.name)

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

fruits = ["banana", "apple", "cherry"]
print(sorted(fruits, key=lambda x: x[-1]))

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

class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y

def add(self):
return self.x + self.y

obj = MyClass(3, 7)
print(obj.add())

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

from collections import deque
dq = deque([1, 2, 3])
dq.rotate(1)
print(dq)

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

print "Hello, World!"

38. 
文字列text = "Python"の各文字を1文字ずつ出力するコードはどれでしょうか?

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

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

40. 
次のコードの説明として正しいものはどれですか?

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

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