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

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

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

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

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

uppercase = [char.upper() for char in "abc"]
print(uppercase)

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

my_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = {k: v for k, v in my_dict.items() if v % 2 == 0}
print(new_dict)

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

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

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

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

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

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

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

obj1 = MyClass(10)
obj2 = obj1
obj2.value = 20
print(obj1.value)

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

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

8. 
リストitems = ["pen", "pencil", "eraser", "sharpener"]の長さを取得するコードはどれでしょうか?

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

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

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

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

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

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

squares = [x**2 for x in range(1, 4)]
print(squares)

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

my_dict = {'x': 10, 'y': 20}
my_dict.update({'y': 30, 'z': 40})
print(my_dict)

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

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

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

lst = [1, 3, 5, 7]
result = [x * y for x in lst for y in lst if x != y]
print(result)

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

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

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

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)

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

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

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

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

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

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

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

class GrandChild(Child):
pass

obj = GrandChild()
print(obj.greet())

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

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

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

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

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

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

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

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

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

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

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

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

obj = GrandChild()
print(obj.greet())

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

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

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

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)

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

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

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

my_dict = {'a': 10, 'b': 20}
new_dict = {key: my_dict.get(key, 0) + 5 for key in ['a', 'c']}
print(new_dict)

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

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

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

def reset(self):
del self.value

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

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

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

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

animals = ["cat", "dog", "elephant"]
animals[1] = "fox"
print(animals)

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

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

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

lst = [1, 2, 3]

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

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

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

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

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

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

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

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

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

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

class Parent:
pass

class Child(Parent):
pass

print(issubclass(Child, Parent))

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