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

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

# パッケージ構造:
# package/
# ├── __init__.py
# ├── module.py
# module.py
value = 42

# main.py
from package.module import value
value = value + 1
print(value)

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

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

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

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

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

try:
raise NameError("Variable not defined")
except ValueError as e:
print("Caught ValueError:", e)
except NameError as e:
print("Caught NameError:", e)

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

queue = [5, 10, 15]
queue.pop(0)
queue.append(20)
queue.pop(0)
queue.append(25)

6. 
次のコードについて、クラス変数へのアクセス方法として正しいものを選んでください。

class MyClass:
class_variable = "shared"

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

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

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

def calculate_total(price, tax=0.1, discount=0):
return price + (price * tax) - discount

print(calculate_total(100, discount=10))

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

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

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

num = 16
result = "4の倍数" if num % 4 == 0 else "4の倍数ではありません"
print(result)

11. 
リストdataがNoneでなく、かつ空でない場合にTrueを、Noneであるか空である場合にFalseを出力するコードはどれでしょうか?

12. 
次のコードにおいて、クラス変数として正しい記述を選んでください。 python コードをコピーする

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

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

try:
open("nonexistent_file.txt", "r")
except FileNotFoundError:
print("File not found")
finally:
print("Execution complete")

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

try:
raise KeyError("Key not found")
except KeyError as e:
print("Caught KeyError:", e)
raise

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

import json
data = {"numbers": [10, 20, 30, 40]}
json_data = json.dumps(data, separators=(",", ":"))
print(json_data)

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

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

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

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

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

x = 10

def outer_function():
x = 20

def inner_function():
return x + 5

return inner_function()

print(outer_function())
print(x)

19. 
変数xがNoneである場合にTrueを、そうでない場合にFalseを出力するコードはどれでしょうか?

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

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

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

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

class A:
pass

class B(A):
pass

class C(A):
pass

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

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

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

def greet(self):
return f"Hello, {self.name}!"

obj1 = MyClass("Alice")
obj2 = MyClass("Bob")
print(obj1.greet())
print(obj2.greet())

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

def multiply_values(x, y=2, z=3):
return x * y * z

print(multiply_values(4))
print(multiply_values(4, 5))
print(multiply_values(4, 5, 6))

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

reverse_concat = lambda s1, s2: s2 + s1
print(reverse_concat("Hello", "World"))

26. 
関数greetに名前を渡し、「Hello, 名前!」と出力する関数を正しく定義する方法はどれでしょうか?

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

score = 75
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("D")

28. 
次のコードで、数値を2倍にして返す関数doubleを正しく定義する方法はどれでしょうか?

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

x = 5

def outer_function():
x = 10
def inner_function():
global x
x += 5
inner_function()
print(x)

outer_function()
print(x)

30. 
次のコードで、with文を使用した場合に発生しない問題を選んでください。

f = open('sample.txt', 'w')
f.write("Test content")
# プログラムがここで終了した場合

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

with open('sample.txt', 'w') as f:
f.write("First Line\n")
raise Exception("An error occurred")

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

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

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

x = 5

def modify_variable():
global x
x += 10
return x

print(modify_variable())
print(x)

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

x = 10

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

modify_variable()
print(x)

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

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

36. 
次のコードについて、selfを使う理由として適切な説明を選んでください。

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

37. 
Pythonの標準ライブラリosモジュールで、現在の作業ディレクトリを変更する関数として正しいものを選んでください。

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

x, y = 3, 4
result = "奇数" if x % 2 != 0 and y % 2 != 0 else "どちらかが偶数"
print(result)

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

import math
print(math.log(100, 10))

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

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

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