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

1. 
次のコードについて、obj1.class_variable = "changed"の動作として正しいものを選んでください。

class MyClass:
class_variable = "shared"

obj1 = MyClass()
obj2 = MyClass()
obj1.class_variable = "changed"
print(obj2.class_variable)

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

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

try:
result = "text" + 5
except TypeError as e:
print("TypeError occurred:", e)

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

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

def multiply(self, factor):
self.value *= factor

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

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

def add(a, b, *args):
return a + b + sum(args)

print(add(1, 2))
print(add(1, 2, 3, 4))

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

def divide(a, b=1):
return a / b

print(divide(6))
print(divide(6, 2))

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

class MyClass:
class_attribute = "shared value"

8. 
次のコードについて、os.listdir()の正しい動作を選んでください。

import os
print(os.listdir("."))

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

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

stack = [1, 2, 3, 4]
stack.pop()
stack.append(5)
stack.pop()
stack.pop()
stack.append(6)

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

try:
with open("nonexistent.txt", "r") as f:
content = f.read()
except FileNotFoundError as e:
print("Error:", e)

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

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

13. 
変数aが正の数で、変数bが偶数である場合に「条件を満たします」と表示し、どちらかの条件が満たされない場合に「条件を満たしません」と表示するコードはどれでしょうか?

14. 
次のコードでraise文の動作として正しい説明を選んでください。

if not isinstance(x, int):
raise TypeError("x must be an integer")

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

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

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

x = 10

def modify_variable():
x = 20
return x

print(modify_variable())
print(x)

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

try:
x = 10 / 0
except ZeroDivisionError:
print("Division by zero")
except Exception:
print("General exception")

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

class MyClass:
class_variable = "shared"

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

print(obj.class_variable)

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

$ pip install requests
$ python -c "import requests; print(requests.get('https://httpbin.org/get').status_code)"

20. 
次のコードのnonlocalキーワードの用途として正しいものはどれですか?

def outer_function():
x = 10

def inner_function():
nonlocal x
x += 5

21. 
argparseでオプション引数を指定する正しい方法を選んでください。

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose mode")
args = parser.parse_args()
print(f"Verbose: {args.verbose}")

実行コマンド:python script.py -v

22. 
次のコードを実行した場合、仮想環境の削除後にwhich pythonを実行した場合の出力として正しいものを選んでください。

$ python -m venv env
$ source env/bin/activate
$ deactivate
$ rm -rf env
$ which python

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

class ???:
pass

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

class MyClass:
def my_method(self):
print("Hello")

25. 
次のコードについて、glob.glob("*[0-9].py")が返す結果として正しい説明を選んでください。

import glob
result = glob.glob("*[0-9].py")

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

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

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

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

def greet(name):
print("Hello,", name)

28. 
変数nが5と10の両方で割り切れる場合に「5と10の倍数」、そうでなければ「倍数ではない」と表示するコードはどれでしょうか?

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

30. 
変数numが偶数である場合に「偶数」、奇数である場合に「奇数」と表示するコードはどれでしょうか?

31. 
次のコードについて、Childクラスに追加された属性が正しく設定されているか確認する方法を選んでください。

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

class Child(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age

obj = Child("Alice", 10)
print(hasattr(obj, "age"))

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

def create_message(greeting, name="Guest", punctuation="."):
return f"{greeting}, {name}{punctuation}"

print(create_message("Hello"))
print(create_message("Hi", "Alice"))
print(create_message("Goodbye", "Bob", "!"))

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

y = 100

def outer_function():
y = 200
def inner_function():
global y
y += 50
inner_function()
print(y)

outer_function()
print(y)

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

def my_function():
x = 5
x += 10
return x

print(my_function())

35. 
次のコードについて、環境変数を取得するための正しい関数を選んでください。

import os
print(os.environ["HOME"])

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

# ファイル名: sample.txt
with open('sample.txt', 'w') as f:
lines = ["Line1\n", "Line2\n", "Line3\n"]
f.writelines(lines)

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

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

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

def add_items(item, items=None):
if items is None:
items = []
items.append(item)
return items

print(add_items("apple"))
print(add_items("banana"))

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

class A:
pass

class B(A):
pass

class C(A):
pass

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

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

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", required=True, help="Specify the name")
parser.add_argument("-a", "--age", type=int, help="Specify the age")
args = parser.parse_args()
print(f"Name: {args.name}, Age: {args.age}")

実行コマンド:python script.py -n Alice -a 30

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