Python 3 エンジニア認定実践試験対策として、総仕上げ1としてChatGPTで生成した模擬問題です。
生成された問題の適切性と“正解”回答の正しさは一通り検証しておりますので、皆さんのご参考になれれば嬉しいです。
問題
問題は以下になります。問題の下に正解を隠してますので、クリックしたら確認できます。
あとは、検証した内容や補足を付け加えてますので参考にしてください。
- try と except ブロックを使用して例外を処理する際、どのようにして例外オブジェクトを取得できますか?
- a) exception()
 - b) catch()
 - c) get_exception()
 - d) as
 
クリックして正解と解説をチェック
正解: d) as>>> try: ... 1 / 0 ... except ZeroDivisionError as e: ... print(e) ... division by zero
 - モジュール random を使用して、1から10の間のランダムな整数を生成するために使用される関数は何ですか?
- 
- a) randint(1, 10)
 - b) random_int(1, 10)
 - c) random(1, 10)
 
 
- d) randrange(1, 10)
 
クリックして正解と解説をチェック
正解: a) randint(1, 10)テキストP155です。
>>> import random >>> random.randint(1, 10) 4 >>> random.randint(1, 10) 3 >>> random.randint(1, 10) 1
 - 
 - for ループを使用してリスト my_list の要素を反復処理する際、要素とそのインデックスを同時に取得するために使用される関数は何ですか?
- a) enumerate()
 - b) index()
 - c) range()
 - d) zip()
 
クリックして正解と解説をチェック
正解: a) enumerate()>>> my_list = ['a', 'b', 'c'] >>> for k, v in enumerate(my_list): ... print(f'{k}:{v}') ... 0:a 1:b 2:c
 - Pythonのファイルをバイナリモードで読み書きするために使用されるモード文字列は何ですか?
- a) t
 - b) b
 - c) text
 - d) binary
 
クリックして正解と解説をチェック
正解: b) b参考:Python File I/O – Read and Write Files
>>> f=open("binfile.bin","wb") >>> num=[5, 10, 15, 20, 25] >>> arr=bytearray(num) >>> f.write(arr) 5 >>> f.close()
 - リスト内包表記を使用して、0から9までの偶数のリストを生成するコードの正しい記述はどれですか?
- a) [x for x in range(0, 10) if x % 2 == 0]
 - b) [x for x in range(0, 10) when x % 2 == 0]
 - c) [x for x in range(0, 10) unless x % 2 != 0]
 - d) [x for x in range(0, 10) if even(x)]
 
クリックして正解と解説をチェック
正解: a) [x for x in range(0, 10) if x % 2 == 0]>>> [x for x in range(0, 10) if x % 2 == 0] [0, 2, 4, 6, 8]
 - Pythonのリスト my_list に対して、要素の数を取得するために使用される関数は何ですか?
- a) size(my_list)
 - b) length(my_list)
 - c) count(my_list)
 - d) len(my_list)
 
クリックして正解と解説をチェック
正解: d) len(my_list)>>> my_list = [x for x in range(0, 10) if x % 2 == 0] >>> len(my_list) 5
 - リストの要素をランダムに並び替えるために使用される関数は何ですか?
- a) shuffle()
 - b) randomize()
 - c) mix()
 - d) scramble()
 
クリックして正解と解説をチェック
正解: a) shuffle()randomライブラリの関数を使った場合はこうなります。
>>> import random >>> li = list(range(10)) >>> li [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> random.shuffle(li) >>> li [1, 3, 6, 7, 5, 4, 8, 9, 2, 0]
 - collections モジュールを使用して、要素のカウントを追跡するためのクラスは何ですか?
- a) Counter
 - b) Tracker
 - c) CounterDict
 - d) DictCounter
 
クリックして正解と解説をチェック
正解: a) Counter>>> from collections import Counter >>> c = Counter() >>> c['a'] += 2 >>> c['b'] += 1 >>> c Counter({'a': 2, 'b': 1})
 - Pythonのクラスメソッドを定義するために使用されるデコレーターは何ですか?
- a) @staticmethod
 - b) @classmethod
 - c) @instance_method
 - d) @classmethodstaticmethod
 
クリックして正解と解説をチェック
正解: b) @classmethodテキストはP87とP92で触れてます。datetime.now()には@classmethodが付けられています。
ライブラリソースはこちらです。@classmethod def now(cls, tz=None): "Construct a datetime from time.time() and optional time zone info." t = _time.time() return cls.fromtimestamp(t, tz)
 - Pythonのモジュールを別のモジュールからインポートするために使用されるキーワードは何ですか?
- a) use
 - b) import
 - c) include
 - d) require
 
クリックして正解と解説をチェック
正解: b) import
 - Pythonの with 文を使用してファイルを開く際、ファイルを自動的にクローズするために使用される特殊メソッドは何ですか?
- a) __init__()
 - b) __enter__()
 - c) __open__()
 - d) __exit__()
 
クリックして正解と解説をチェック
正解: d) __exit__()
 - Pythonの try ブロックで例外が発生しなかった場合に実行される部分はどこですか?
- a) else
 - b) except
 - c) finally
 - d) unless
 
クリックして正解と解説をチェック
正解: a) elseelse は列挙した例外種類がヒットしなかった場合ではなく、try ブロックで例外が送り出されなかった場合、つまり正常系の場合実行されるブロックになりますので、注意しましょう。
 - リスト内包表記を使用して、1から10までの整数の平方を含むリストを生成するコードの正しい記述はどれですか?
- a) [x * x for x in range(1, 11)]
 - b) [x ^ 2 for x in range(1, 11)]
 - c) [square(x) for x in range(1, 11)]
 - d) [x ** 2 for x in range(1, 11)]
 
クリックして正解と解説をチェック
正解: d) [x ** 2 for x in range(1, 11)]>>> [x ** 2 for x in range(1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
 - Pythonのセット(集合)内の要素を追加するために使用されるメソッドは何ですか?
- a) push()
 - b) append()
 - c) insert()
 - d) add()
 
クリックして正解と解説をチェック
正解: d) add()次のように、セットは
add()、リストはappendです。>>> s1 = {'a', 'b', 'c'} >>> s1.add('d') >>> s1 {'a', 'd', 'c', 'b'} >>> l1 = ['a', 'b', 'c'] >>> l1.append('d') >>> l1 ['a', 'b', 'c', 'd']
 - Pythonのリスト内の要素を削除するために、要素の値ではなくインデックスを使用するメソッドは何ですか?
- a) remove()
 - b) delete()
 - c) pop()
 - d) discard()
 
クリックして正解と解説をチェック
正解: c) pop()>>> l1 ['a', 'b', 'c', 'd'] >>> l1.pop(1) 'b' >>> l1 ['a', 'c', 'd']
 - Pythonのディクショナリから特定のキーに関連付けられた値を取得するために使用されるメソッドは何ですか?
- a) get()
 - b) fetch()
 - c) access()
 - d) retrieve()
 
クリックして正解と解説をチェック
正解: a) get()>>> d1 = {'a':1, 'b':2, 'c':3} >>> d1.get('a') 1
 - Pythonでファイルを読み取るために使用される組み込み関数は何ですか?
- a) open()
 - b) read()
 - c) load()
 - d) write()
 
クリックして正解と解説をチェック
正解: a) open()>>> f = open('binfile.bin', 'rb') >>> f.read() b'\x05\n\x0f\x14\x19' >>> f.close()
 - Pythonのモジュールをインポートする際に、別名を使用するためのキーワードは何ですか?
- a) as
 - b) import
 - c) with
 - d) alias
 
クリックして正解と解説をチェック
正解: a) as
 - Pythonのリストから重複した要素を削除するために使用されるデータ構造は何ですか?
- a) set
 - b) dictionary
 - c) tuple
 - d) array
 
クリックして正解と解説をチェック
正解: a) set>> s2 = set() >>> s2.add(1) >>> s2 {1}
 - Pythonのデコレーターの主な用途は何ですか?
- a) グラフィックスの描画
 - b) コンフィギュレーション管理
 - c) 関数の機能を拡張または修飾
 - d) テキスト処理
 
クリックして正解と解説をチェック
正解: c) 関数の機能を拡張または修飾
 - Pythonでコマンドライン引数をパースするために使用されるモジュールは何ですか?
- a) os
 - b) sys
 - c) getopt
 - d) argparse
 
クリックして正解と解説をチェック
正解: d) argparseargtest.pyは以下とします。
import argparse parser = argparse.ArgumentParser("Hello World") parser.add_argument("-s", "--name", type=str, required=True) args = parser.parse_args() print(args.name)実行するとこうなります。
% python argtest.py -s abc abc % python argtest.py --name abc abc
 - Pythonの文字列を逆順にするために使用される組み込み関数は何ですか?
- a) reverse()
 - b) flip()
 - c) reverse_string()
 - d) reversed()
 
クリックして正解と解説をチェック
正解: d) reversed()>>> list(reversed("abc")) ['c', 'b', 'a'] # ちなみに、これも同じ効果です。 >>> list(sorted("abc", reverse=True)) ['c', 'b', 'a']
 - Pythonでファイルから1行ずつ読み込むためのループ構造は何ですか?
- a) for line in file.read_lines():
 - b) for line in file.read():
 - c) for line in file.readlines():
 - d) for line in file:
 
クリックして正解と解説をチェック
正解: c) for line in file.readlines():>>> f = open('listtest.txt', 'r') >>> for line in f.readlines(): ... print(line) ... a b c d e
 - PythonでHTTPリクエストを送信するためのライブラリは何ですか?
- a) httprequest
 - b) httplib
 - c) request.urllib
 - d) urllib.request
 
クリックして正解と解説をチェック
正解: d) urllib.request>>> from urllib import request >>> res = request.urlopen('https://httpbin.org/get?key1=value1') >>> res.status 200
 - Pythonのデフォルト引数はどの位置に配置すべきですか?
- a) 引数リストの最後
 - b) 引数リストの最初
 - c) どこでも配置できる
 - d) 引数リストの中央
 
クリックして正解と解説をチェック
正解: a) 引数リストの最後
 - Pythonのコードの実行時間を測定するために使用されるモジュールは何ですか?
- a) profiler
 - b) timeit
 - c) efficiency
 - d) benchmark
 
クリックして正解と解説をチェック
正解: b) timeit>>> import timeit >>> timeit.timeit('"test" in "This is a test."') 0.038593658013269305
 - Pythonのセット(set)はどのような特性を持っていますか?
- a) 順序が保証され、重複を許さない
 - b) 順序が保証され、重複を許す
 - c) 順序が保証されず、重複を許さない
 - d) 順序が保証されず、重複を許す
 
クリックして正解と解説をチェック
正解: c) 順序が保証されず、重複を許さない>>> s1 = {'a', 'b', 'c'} >>> s1.add('z') >>> s1.add('y') >>> s1 {'y', 'c', 'a', 'z', 'b'} >>> s1.add('a') >>> s1.add('b') >>> s1 {'y', 'c', 'a', 'z', 'b'}
 - Pythonのリスト(list)とタプル(tuple)の主な違いは何ですか?
- a) リストはイミュータブルであり、タプルはミュータブルである
 - b) リストは順序が保証され、タプルは順序が保証されない
 - c) リストは角括弧([])で、タプルは丸括弧(())で作成される
 - d) リストはミュータブルであり、タプルはイミュータブルである
 
クリックして正解と解説をチェック
正解: d) リストはミュータブルであり、タプルはイミュータブルである括弧も大きい違いだと言えるかもしれませんが、ミュータブルかイミュータブルかは確かに特徴的です。
listのヘルプ:
Help on class list in module builtins: class list(object) | list(iterable=(), /) | | Built-in mutable sequence.tupleのヘルプ:
Help on class tuple in module builtins: class tuple(object) | tuple(iterable=(), /) | | Built-in immutable sequence.
 - Pythonでオブジェクトのクラスを確認するために使用される関数は何ですか?
- a) class()
 - b) type()
 - c) object_class()
 - d) inspect()
 
クリックして正解と解説をチェック
正解: b) type()>>> type(1) <class 'int'>
 - Pythonで辞書(dictionary)のキーを取得するためのメソッドは何ですか?
- a) keys()
 - b) values()
 - c) items()
 - d) get()
 
クリックして正解と解説をチェック
正解: a) keys()>>> d1 = {'a':1, 'b':2, 'c':3} >>> d1.keys() dict_keys(['a', 'b', 'c'])
 - PythonでJSONデータを読み込むために使用されるモジュールは何ですか?
- a) jsonparser
 - b) jsonencode
 - c) jsonlib
 - d) json
 
クリックして正解と解説をチェック
正解: d) json
 - Pythonのテストフレームワークで、テストケースを定義するための基本的なクラスは何ですか?
- a) TestSuite
 - b) TestCase
 - c) TestFixture
 - d) TestRunner
 
クリックして正解と解説をチェック
正解: b) TestCaseテストケースを作成する場合は次のように
unittest.TestCaseを継承します。import unittest class AddTest(unittest.TestCase):
 

コメントを残す