{"id":9683,"date":"2020-01-29T07:29:17","date_gmt":"2020-01-29T07:29:17","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=9683"},"modified":"2021-11-13T06:06:54","modified_gmt":"2021-11-13T06:06:54","slug":"python-interview-questions-and-answer-part-1","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/python-interview-questions-and-answer-part-1\/","title":{"rendered":"Python Interview Questions and Answer Part \u2013 1"},"content":{"rendered":"\n<p><strong>What Python function is used to open files?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>open_file()<\/li><li>file()<\/li><li>new_file()<\/li><li><strong>open() (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>What does the pass keyword do?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Passes control to another function<\/li><li>Continues to the next iteration of a loop<\/li><li><strong>it is a no-op (Ans)<\/strong><\/li><li>Raises an exception<\/li><\/ul>\n\n\n\n<p><strong>What are with-blocks used for?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Writing to files<\/li><li>Defining functions<\/li><li><strong>Resource management (Ans)<\/strong><\/li><li>Looping over sequences<\/li><\/ul>\n\n\n\n<p><strong>Which one of the following is true?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If present, the module docstring must begin on the first non-empty line in the module<\/li><li>The first statement in the module must be an import statement<\/li><li><strong>If present, the shebang comment must be the first non-empty line in the module (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>What does the in operator do for lists?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Inserts an element in to the list<\/li><li>Runs a function for each element in the list<\/li><li>Duplicates the list<\/li><li><strong>Checks for membership of a value (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>What is the standard package that provides support for building and installing additional modules into a Python installation?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>fixture<\/li><li>pkgutils<\/li><li>palindrome<\/li><li><strong>distutils (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>Where must keyword arguments come when calling a function?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Before positional arguments<\/li><li>After the closing parenthesis<\/li><li>They can appear anywhere<\/li><li><strong>After positional arguments (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>Python source files can contain multiple classes.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>False<\/li><li><strong>True (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>The control flow construct for nested conditionals uses which form:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>elif: (Ans)<\/strong><\/li><li>else if:<\/li><li>elsif:<\/li><li>elseif:<\/li><\/ul>\n\n\n\n<p><strong>What does a comprehension&#8217;s filtering predicate do?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Prevents certain values from going into the collection (Ans)<\/strong><\/li><li>Modifies the values going into the collection<\/li><li>Allows sets to contain duplicates<\/li><li>Determines the actual type of collection constructed<\/li><\/ul>\n\n\n\n<p><strong>Can classes have docstrings?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Yes (Ans)<\/strong><\/li><li>No<\/li><\/ul>\n\n\n\n<p><strong>Why is the first command line argument in sys.argv[1]<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Because Python lists use one-based indexing<\/li><li><strong>Because the program filename is in sys.argv [0] (Ans)<\/strong><\/li><li>Because the program module name is in sys.argv[0]<\/li><\/ul>\n\n\n\n<p><strong>What does the LEGB acronym stand for?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Local, Enclosing, Global, Built-in (Ans)<\/strong><\/li><li>Lookup, Enable, Gateway, Backup<\/li><li>Link, Extension, Generic, Binary<\/li><li>Local, Embedded, Global, Built-in<\/li><\/ul>\n\n\n\n<p><strong>The REPL always displays the value of the entered expression.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>False (Ans)<\/strong><\/li><li>True<\/li><\/ul>\n\n\n\n<p><strong>How do you define a test function on a TestCase?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Manually add it to the test_functions list<\/li><li><strong>Start the function name with test_ (Ans)<\/strong><\/li><li>Add the word test-function to the function&#8217;s docstring<\/li><li>End the function name with _test<\/li><\/ul>\n\n\n\n<p><strong>How do you replace an element in a list?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>the square-bracket operator (Ans)<\/strong><\/li><li>the cons() method<\/li><li>the replace() method<\/li><li>you can&#8217;t because lists are immutable<\/li><\/ul>\n\n\n\n<p><strong>What does the global keyword do?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Declares a function as global<\/li><li>Defines a global variable<\/li><li><strong>Allows assignment to globals in a function (Ans)<\/strong><\/li><li>Exports functions from a namespace<\/li><\/ul>\n\n\n\n<p><strong>Copying a list does a deep copy of the list&#8217;s.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>True<\/li><li><strong>False (Ans)<\/strong><\/li><\/ul>\n\n\n\n<p><strong>How is iteration termination signalled in Python?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>With a special <em>end<\/em> token<\/li><li>By raising an IndexError exception<\/li><li><strong>By raising the StopIteration exception (Ans)<\/strong><\/li><li>By calling a user-provided callback function<\/li><\/ul>\n\n\n\n<p><strong>Why should you follow existing patterns when choosing exception types?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To improve program performance<\/li><li><strong>To simplify integration of your code (Ans)<\/strong><\/li><li>To avoids SyntaxErrors<\/li><li>To avoid documenting your APIs<\/li><\/ul>\n\n\n\n<p><strong>When is top level module code executed?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Only the first time a module is imported (Ans)<\/strong><\/li><li>Every time the module is imported<\/li><\/ul>\n\n\n\n<p><strong>What is the Python 3.3 tool for creating virtual environments?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>create_env<\/li><li>virtenv<\/li><li><strong>pyvenv (Ans)<\/strong><\/li><li>makeenv<\/li><\/ul>\n\n\n\n<p><strong>Which pdb function will stop program execution?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>pdb.set_trace() (Ans)<\/strong><\/li><li>pdb.break()<\/li><li>pdb.debug()<\/li><li>pdb.halt()<\/li><\/ul>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_16350\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_36262\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_16350\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What Python function is used to open files? open_file() file() new_file() open() (Ans) What does the pass keyword do? Passes control to another function Continues to the next iteration of a loop it is a no-op (Ans) Raises an exception What are with-blocks used for? Writing to files Defining functions Resource management (Ans) Looping over&#8230;<\/p>\n","protected":false},"author":1,"featured_media":9748,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[499],"tags":[641,2497,176,213],"class_list":["post-9683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-programming-scripting-languages","tag-answers","tag-interview-questions","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=9683"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9683\/revisions"}],"predecessor-version":[{"id":24986,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/9683\/revisions\/24986"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/9748"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=9683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=9683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=9683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}