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

1. 
次のコードについて、親クラスのコンストラクタを子クラスで明示的に呼び出す正しい方法を選んでください。

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(obj.name, obj.age)

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

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

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

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

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

lst = [1, 2, 3, 4]
new_lst = lst[::-1]
print(new_lst)

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

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

my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict)

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

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

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

class MyClass:
def my_method(self):
pass

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

t = (1, 2, 3, 4)
print(t.index(3))

10. 
リストnumbers = [1, 2, 3, 4, 5]のすべての要素を取得するにはどうすればよいでしょうか?

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

lst = [1, 2, 3]

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

fruits = ["apple", "banana", "cherry"]
print(fruits[1])

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

class Parent:
def __init__(self):
self.value = 10

class Child(Parent):
pass

obj = Child()
print(obj.value)

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

t = (1, 2, 3)
t2 = t[:2] + (4,)
print(t2)

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

words = ["hi", "hello", "hey"]
result = [w.upper() for w in words if len(w) == 3]
print(result)

16. 
次のコードについて、Childクラスが継承している親クラスを判定する方法として正しいものを選んでください。

class Parent:
pass

class Child(Parent):
pass

print(issubclass(Child, Parent))

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

words = ["apple", "banana", "cherry"]
result = [word[0].upper() + word[1:] for word in words]
print(result)

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

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)

19. 
次のリスト内包表記の出力は何でしょうか?

squares = [x**2 for x in range(5)]
print(squares)

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

data = [5, 3, 9, 1]
data.sort(reverse=True)
print(data)

21. 
以下のコードの出力結果を選んでください。

my_dict = {'a': 10, 'b': 20, 'c': 30}
print(my_dict.get('d', 'Not Found'))

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

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

obj = MyClass()
obj.set_value(42)
print(obj.value)

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. 
次のコードの出力は何ですか?

data = [10, 20, 30, 40]
result = [data[i] + data[i+1] for i in range(len(data)-1)]
print(result)

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

nums = [1, 2, 3, 4, 5]
nums[1:4] = [10, 20]
print(nums)

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

nums = [2, 4, 6, 8, 10]
result = [x for x in nums if x % 3 == 0]
print(result)

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

my_dict = {'x': 10, 'y': 20, 'z': 30}
filtered = {k: v for k, v in my_dict.items() if k != 'y'}
print(filtered)

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

numbers = [2, 4, 6, 8, 10]
result = [str(x) for x in numbers if x % 4 == 0]
print(result)

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

values = [1, 2, 3, 4, 5]
values[2] = 10
print(values)

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

nums = [10, 20, 30, 40, 50]
print(nums[-2])

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

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

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

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

obj = MyClass()
obj.set_value(42)
print(obj.value)

33. 
次のコードに関して、xに格納される値として正しいものを選んでください。

my_dict = {'key1': 1, 'key2': 2, 'key3': 3}
x = my_dict.keys()

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

t = (1, 2, 3)
a, b, c = t
print(b)

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

doubles = [x*2 for x in range(3)]
print(doubles)

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

t = (1, 2, 3) + (4, 5)
print(t)

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

t = ("a", "b", "c")
print("b" in t)

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

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

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

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

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

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = [row[0] + row[2] for row in matrix]
print(result)

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