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

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

my_dict = {i: i**2 for i in range(3)}
print(my_dict)

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

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

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

class Child(Parent):
def greet(self):
return f"Hi, {self.name}!"

obj = Child("Alice")
print(obj.greet())

3. 
次のコードについて、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"))

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

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

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

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

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

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

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

nums = [1, 2, 3, 4, 5]
result = [x**2 for x in nums if x % 2 == 0]
print(result)

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

class MyClass:
def __init__(self):
self.data = []

def add_data(self, item):
self.data.append(item)

obj = MyClass()
obj.add_data(1)
obj.add_data(2)
print(obj.data)

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

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

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

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

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

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

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

def double(self):
self.x *= 2

obj1 = MyClass(3)
obj2 = MyClass(4)
obj1.double()
print(obj1.x, obj2.x)

12. 
次のコードについて、selfの使用方法として正しいものを選んでください。

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

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

13. 
次のコードに関して、正しい出力結果を選んでください。

my_dict = {'a': 1, 'b': 2}
print('c' in my_dict)

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

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

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

my_dict = {'x': 10, 'y': 20, 'z': 30}
result = {key: val * 2 for key, val in my_dict.items() if val > 15}
print(result)

16. 
次のコードを実行した場合、tの型は何ですか?

t = (5,)

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

class Parent:
def greet(self):
return "Hello from Parent!"

class Child(Parent):
def greet(self):
return "Hello from Child!"

obj = Child()
print(obj.greet())

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

my_dict = {'x': 10, 'y': 20}
copied_dict = my_dict.copy()
copied_dict['x'] = 100
print(my_dict, copied_dict)

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

t = (1, 2, 3)
t[1] = 4

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

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

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

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

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

t = (1, 2, 3)
print(t[1])

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

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

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

my_dict = {'a': 100, 'b': 200, 'c': 300}
removed_value = my_dict.pop('b', 'Key not found')
print(removed_value, my_dict)

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

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

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

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

def add(self, other_value):
return self.value + other_value

obj = MyClass(10)
print(obj.add(5))

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

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

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

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

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

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

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

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

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

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

def reset(self):
del self.value

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

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

odd_or_even = ["even" if x % 2 == 0 else "odd" for x in range(5)]
print(odd_or_even)

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

class Parent:
def greet(self):
return "Hello from Parent!"

class Child(Parent):
def greet(self):
return super().greet() + " and Child!"

obj = Child()
print(obj.greet())

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

my_dict = {'x': 10, 'y': 20}
result = [(k, v) for k, v in my_dict.items()]
print(result)

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

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

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

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

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

obj = Child()
print(obj.name, obj.age)

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

values = [1, 2, 3, 4, 5]
new_values = [val for val in values if val > 3]
print(new_values)

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

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

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

lst = [1, 2, 3]

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

lst = [1, [2, 3], [4, [5, 6]], 7]
print(lst[2][1][1])

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