0% found this document useful (0 votes)
20 views64 pages

Python Interview Companion

The document is a Python interview companion containing 190 questions and answers covering various aspects of the Python programming language. It includes topics such as Python features, environment variables, data types, string and list operations, tuples, dictionaries, and type conversions. Each question is accompanied by the correct response and an explanation.

Uploaded by

usman4220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views64 pages

Python Interview Companion

The document is a Python interview companion containing 190 questions and answers covering various aspects of the Python programming language. It includes topics such as Python features, environment variables, data types, string and list operations, tuples, dictionaries, and type conversions. Each question is accompanied by the correct response and an explanation.

Uploaded by

usman4220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Python Interview Companion: 190 Inquiries and

Corresponding Answers
20/2/2024

WHAT IS PYTHON?
A. A high-level programming language
B. A low-level programming language
C. A database management system
D. A data analysis tool

Correct Response: 1

Explanation: Python is a high-level, interpreted, and general purpose dynamic programming language.

NAME SOME OF THE FEATURES OF PYTHON.


A. Object-Oriented
B. High-Level
C. Interpreted
D. Dynamic

Correct Response: 1, 2, 3

Explanation: Python is an object-oriented, high-level, interpreted, and dynamic programming language.

WHAT IS THE PURPOSE OF PYTHONPATH ENVIRONMENT VARIABLE?


A. To set the location of the Python interpreter
B. To set the location of the Python libraries
C. To set the location of the Python modules
D. To set the location of the Python scripts

Correct Response: 2

Explanation: The PYTHONPATH environment variable is used to set the location of the Python libraries.

WHAT IS THE PURPOSE OF PYTHONSTARTUP ENVIRONMENT


VARIABLE?
A. To set the location of the Python interpreter
B. To set the location of the Python libraries
C. To set the location of the Python scripts that will run automatically when the Python interpreter starts
D. To set the location of the Python modules

Correct Response: 3

Explanation: The PYTHONSTARTUP environment variable is used to set the location of the Python scripts that will run
automatically when the Python interpreter starts.

WWW.CDPNETWORKS.COM | [email protected] PAGE 1 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PURPOSE OF PYTHONCASEOK ENVIRONMENT VARIABLE?


A. To ignore the case sensitivity of the Python keywords
B. To ignore the case sensitivity of the Python variables
C. To ignore the case sensitivity of the Python functions
D. To ignore the case sensitivity of the Python modules

Correct Response: 1

Explanation: The PYTHONCASEOK environment variable is used to ignore the case sensitivity of the Python keywords.

WHAT ARE THE SUPPORTED DATA TYPES IN PYTHON?


A. Integer
B. Float
C. String
D. All of the above

Correct Response: 4

Explanation: Python supports several data types, including integers, floating-point numbers, and strings.

WHAT IS THE OUTPUT OF PRINT STR IF STR = 'HELLO WORLD!'?


A. Error
B. None
C. 'Hello World!'
D. 'Hello world!'

Correct Response: 3

Explanation: The output of the print statement will be 'Hello World!', as the value of the variable 'str' is assigned the string
'Hello World!'.

WHAT IS THE OUTPUT OF PRINT STR[0] IF STR = 'HELLO WORLD!'?


A. Error
B. None
C. 'H'
D. 'h'

Correct Response: 3

Explanation: The output of the print statement will be 'H', as the first character of the string 'Hello World!' is 'H'.

WWW.CDPNETWORKS.COM | [email protected] PAGE 2 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE OUTPUT OF PRINT STR[2:5] IF STR = 'HELLO WORLD!'?


A. 'llo'
B. 'llo World'
C. 'Hello World'
D. 'llo Wo'

Correct Response: 1

Explanation: The slice of the string starts at index 2 and goes up to but not including index 5, so the result is 'llo'

WHAT IS THE OUTPUT OF PRINT STR[2:] IF STR = 'HELLO WORLD!'?


A. 'llo'
B. 'Hello World'
C. 'llo World!'
D. 'llo Wo'

Correct Response: 3

Explanation: The slice of the string starts at index 2 and goes up to the end of the string, so the result is 'llo World!'

WHAT IS THE OUTPUT OF PRINT STR * 2 IF STR = 'HELLO WORLD!'?


A. 'Hello World!Hello World!'
B. 'HelloWorld!HelloWorld!'
C. 'Hello World'
D. 'Hello Wo'

Correct Response: 1

Explanation: The string is repeated twice, so the result is 'Hello World!Hello World!'

WHAT IS THE OUTPUT OF PRINT STR + "TEST" IF STR = 'HELLO WORLD!'?


A. 'Hello World!TEST'
B. 'Hello WorldTEST'
C. 'Hello World!'
D. 'Hello Wo'

Correct Response: 1

Explanation: The "+" operator concatenates the two strings, so the result is 'Hello World!TEST'

WWW.CDPNETWORKS.COM | [email protected] PAGE 3 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE OUTPUT OF PRINT LIST IF LIST = [ 'ABCD', 786 , 2.23, 'JOHN', 70.2 ]?
A. [ 'abcd', 786 , 2.23, 'john', 70.2 ]
B. 'abcd, 786, 2.23, john, 70.2'
C. 'abcd 786 2.23 john 70.2'
D. 'abcd786john70.2'

Correct Response: 1

Explanation: The "print" function outputs the entire list, so the result is [ 'abcd', 786 , 2.23, 'john', 70.2 ]

WHAT IS THE OUTPUT OF PRINT LIST[0] IF LIST = [ 'ABCD', 786 , 2.23, 'JOHN', 70.2 ]?
A. 'abcd'
B. 786
C. 2.23
D. 'john'

Correct Response: 1

Explanation: The first element of the list is 'abcd'

WHAT IS THE OUTPUT OF PRINT LIST[1:3] IF LIST = [ 'ABCD', 786 , 2.23, 'JOHN', 70.2 ]?
A. [786, 2.23]
B. [786, 'john']
C. [2.23, 'john']
D. [786, 2.23, 'john']

Correct Response: 1

Explanation: The slice of the list from index 1 to 3 is [786, 2.23]

WHAT IS THE OUTPUT OF PRINT LIST[2:] IF LIST = [ 'ABCD', 786 , 2.23, 'JOHN', 70.2 ]?
A. [2.23, 'john', 70.2]
B. [2.23, 'john']
C. [70.2]
D. [786, 2.23, 'john']

Correct Response: 1

Explanation: The slice of the list starting from index 2 is [2.23, 'john', 70.2]

WWW.CDPNETWORKS.COM | [email protected] PAGE 4 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE OUTPUT OF PRINT TINYLIST * 2 IF TINYLIST = [123, 'JOHN']?


A. [123, 'john', 123, 'john']
B. [246, 'johnjohn']
C. [123, 123, 'john', 'john']
D. [246, 'john']

Correct Response: 1

Explanation: The list is repeated twice to give [123, 'john', 123, 'john']

WHAT IS THE OUTPUT OF PRINT LIST1 + LIST2, IF LIST1 = [ 'ABCD', 786 , 2.23, 'JOHN',
70.2 ] AND IST2 = [123, 'JOHN']?
A. [ 'abcd', 786 , 2.23, 'john', 70.2, 123, 'john']
B. [123, 'johnabcd', 786 , 2.23, 'john', 70.2]
C. [ 'abcd', 786 , 2.23, 'john', 70.2, 123]
D. [ 'abcd', 786 , 2.23, 'john', 70.2]

Correct Response: 1

Explanation: The two lists are concatenated to give [ 'abcd', 786 , 2.23, 'john', 70.2, 123, 'john']

WHAT ARE TUPLES IN PYTHON?


A. A type of list
B. A type of dictionary
C. An ordered and unchangeable collection of elements
D. An unordered and changeable collection of elements

Correct Response: 3

Explanation: Tuples are an ordered and unchangeable collection of elements.

WHAT IS THE DIFFERENCE BETWEEN TUPLES AND LISTS IN


A. Python? Tuples are ordered and lists are unordered
B. Tuples are changeable and lists are unchangeable
C. Tuples are unchangeable and lists are changeable
D. Tuples are unordered and lists are ordered

Correct Response: 3

Explanation: Tuples are unchangeable and lists are changeable.

WWW.CDPNETWORKS.COM | [email protected] PAGE 5 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE OUTPUT OF PRINT TUPLE IF TUPLE = ( 'ABCD', 786 , 2.23, 'JOHN', 70.2 )?
A. Error
B. ('abcd', 786 , 2.23, 'john', 70.2)
C. abcd
D. 786

Correct Response: 2

Explanation: The output would be: ('abcd', 786 , 2.23, 'john', 70.2)

WHAT IS THE OUTPUT OF PRINT TUPLE[0] IF TUPLE = ( 'ABCD', 786 , 2.23, 'JOHN', 70.2 )?
A. Error
B. abcd
C. 786
D. 2.23

Correct Response: 2

Explanation: The output would be: abcd

WHAT IS THE OUTPUT OF PRINT TUPLE[1:3] IF TUPLE = ( 'ABCD', 786 , 2.23, 'JOHN', 70.2
)?
A. Error
B. (786, 2.23)
C. (786, 2.23, 'john')
D. ('abcd', 786)

Correct Response: 1

Explanation: The output would be: (786, 2.23)

WWW.CDPNETWORKS.COM | [email protected] PAGE 6 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE OUTPUT OF PRINT TUPLE[2:] IF TUPLE = ( 'ABCD', 786 , 2.23, 'JOHN', 70.2
)?
A. (786, 2.23, 'john', 70.2)
B. (2.23, 'john', 70.2)
C. (abcd, 786, 2.23)
D. Error

Correct Response: 1

Explanation: The tuple index starts from 0. So, the output of print tuple[2:] will be (786, 2.23, 'john', 70.2).

WHAT ARE PYTHON'S DICTIONARIES?


A. Unordered collection of items
B. Ordered collection of items
C. Collection of lists
D. Collection of tuples

Correct Response: 1

Explanation: Python's dictionaries are unordered collection of items.

HOW WILL YOU CREATE A DICTIONARY IN PYTHON?


A. dict = {'Name': 'John', 'Age': 31, 'Country': 'USA'}
B. dict = {'John', 31, 'USA'} dict = ('John', 31, 'USA')
C. dict = ['John', 31, 'USA']

Correct Response: 1

Explanation: You can create a dictionary in python by using the following syntax: dict = {'Key1': 'Value1', 'Key2': 'Value2',
...}.

HOW WILL YOU GET ALL THE KEYS FROM THE DICTIONARY?
A. dict.keys()
B. dict.values()
C. dict.items()
D. dict.get()

Correct Response: 1

Explanation: Use dict.keys() method to get all the keys from the dictionary

WWW.CDPNETWORKS.COM | [email protected] PAGE 7 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET ALL THE VALUES FROM THE DICTIONARY?
A. dict.keys()
B. dict.values()
C. dict.items()
D. dict.get()

Correct Response: 2

Explanation: Use dict.values() method to get all the values from the dictionary

HOW WILL YOU CONVERT A STRING TO AN INT IN PYTHON?


A. int("string")
B. float("string")
C. long("string")
D. str("string")

Correct Response: 1

Explanation: Use int("string") to convert a string to an integer

HOW WILL YOU CONVERT A STRING TO A LONG IN PYTHON?


A. int("string")
B. float("string")
C. long("string")
D. str("string")

Correct Response: 3

Explanation: Use long("string") to convert a string to a long integer

HOW WILL YOU CONVERT A STRING TO A FLOAT IN PYTHON?


A. int("string")
B. float("string")
C. long("string")
D. str("string")

Correct Response: 2

Explanation: Use float("string") to convert a string to a float

WWW.CDPNETWORKS.COM | [email protected] PAGE 8 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CONVERT A OBJECT TO A STRING IN PYTHON?


A. str(obj)
B. obj.toString()
C. string(obj)
D. obj.str()

Correct Response: 1

Explanation: The function str() is used to convert an object to a string in

HOW WILL YOU CONVERT A OBJECT TO A REGULAR EXPRESSION IN PYTHON?


A. re.compile(str(obj))
B. obj.re()
C. obj.toRegex()
D. regex(obj)

Correct Response: 1

Explanation: The function re.compile() is used to convert an object to a regular expression in python

HOW WILL YOU CONVERT A STRING TO AN OBJECT IN PYTHON?


A. eval(string)
B. string.toObject()
C. object(string)
D. string.obj()

Correct Response: 1

Explanation: The function eval() is used to convert a string to an object in

HOW WILL YOU CONVERT A STRING TO A TUPLE IN PYTHON?


A. tuple(string)
B. string.toTuple()
C. string.tuple()
D. tuple(string.split(","))

Correct Response: 1

Explanation: The function tuple() is used to convert a string to a tuple in

WWW.CDPNETWORKS.COM | [email protected] PAGE 9 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CONVERT A STRING TO A LIST IN PYTHON?


A. list(string)
B. string.toList()
C. string.list()
D. list(string.split(","))

Correct Response: 1

Explanation: The function list() is used to convert a string to a list in

HOW CAN YOU PICK A RANDOM ITEM FROM A LIST OR TUPLE?


A. Use the random module's random.choice() function
B. Use the random module's random.randint() function
C. Use the random module's random.sample() function
D. Manually loop through the list/tuple and pick a random index

Correct Response: 1

Explanation: The random.choice() function can be used to pick a random item from a list or tuple in Python.

HOW CAN YOU PICK A RANDOM ITEM FROM A RANGE?


A. Use the random module's random.choice() function
B. Use the random module's random.randint() function
C. Use the random module's random.sample() function
D. Manually loop through the range and pick a random index

Correct Response: 2

Explanation: The random.randint() function can be used to pick a random item from a range in Python.

HOW CAN YOU GET A RANDOM NUMBER IN PYTHON?


A. Use the random module's random.choice() function
B. Use the random module's random.randint() function
C. Use the random module's random.sample() function
D. Manually loop through a range and pick a random index

Correct Response: 2

Explanation: The random.randint() function can be used to generate a random number in Python.

WWW.CDPNETWORKS.COM | [email protected] PAGE 10 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU SET THE STARTING VALUE IN GENERATING RANDOM NUMBERS?
A. By using the random.seed() function
B. By using the random.randint() function
C. By using the random.choice() function
D. By manually setting a starting value

Correct Response: 1

Explanation: The random.seed() function can be used to set the starting value for generating random numbers in Python.

HOW WILL YOU RANDOMIZE THE ITEMS OF A LIST IN PLACE?


A. By using the random.shuffle() function
B. By using the random.sample() function
C. By manually swapping elements
D. By using the sorted() function with the reverse argument set to True

Correct Response: 1

Explanation: The random.shuffle() function can be used to randomly shuffle the items of a list in place in Python.

HOW WILL YOU CAPITALIZES FIRST LETTER OF STRING?


A. str.capitalize()
B. str.title()
C. str.upper()
D. str.lower()

Correct Response: 1

Explanation: The method str.capitalize() returns a copy of the string with only its first character capitalized.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE ALPHANUMERIC?
A. str.isalnum()
B. str.isdigit()
C. str.isalpha()
D. str.islower()

Correct Response: 1

Explanation: The method str.isalnum() returns True if all characters in the string are alphanumeric (letters and numbers
only) and False otherwise.

WWW.CDPNETWORKS.COM | [email protected] PAGE 11 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE DIGITS?
A. str.isdigit()
B. str.isalnum()
C. str.isalpha()
D. str.islower()

Correct Response: 1

Explanation: The method str.isdigit() returns True if all characters in the string are digits and False otherwise.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE IN LOWERCASE?
A. str.islower()
B. str.isdigit()
C. str.isalpha()
D. str.isalnum()

Correct Response: 1

Explanation: The method str.islower() returns True if all cased characters in the string are in lowercase and False otherwise.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE NUMERICS?
A. str.isdigit()
B. str.isalnum()
C. str.isalpha()
D. str.islower()

Correct Response: 1

Explanation: The method str.isdigit() returns True if all characters in the string are digits and False otherwise.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE WHITESPACES?
A. string.isspace()
B. string.isalpha()
C. string.isdigit()
D. string.islower()

Correct Response: 1

Explanation: The isspace() method returns True if all the characters in a string are whitespaces, otherwise it returns False.

WWW.CDPNETWORKS.COM | [email protected] PAGE 12 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CHECK IN A STRING THAT IT IS PROPERLY TITLECASED?


A. string.istitle()
B. string.isalpha()
C. string.isdigit()
D. string.islower()

Correct Response: 1

Explanation: The istitle() method returns True if the first character of each word in the string is in uppercase and the rest of
the characters are in lowercase, otherwise it returns False.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE IN UPPERCASE?
A. string.isupper()
B. string.isalpha()
C. string.isdigit()
D. string.islower()

Correct Response: 1

Explanation: The isupper() method returns True if all the characters in the string are in uppercase, otherwise it returns
False.

HOW WILL YOU MERGE ELEMENTS IN A SEQUENCE?


A. "".join(sequence)
B. "".merge(sequence)
C. "".concat(sequence)
D. "".append(sequence)

Correct Response: 1

Explanation: The join() method is used to merge all elements in a sequence, such as a list or tuple, into a string, using a
specified separator.

HOW WILL YOU GET THE LENGTH OF THE STRING?


A. len(string)
B. length(string)
C. size(string)
D. count(string)

Correct Response: 1

Explanation: The len() function is used to get the length of a string, which is the number of characters in the string.

WWW.CDPNETWORKS.COM | [email protected] PAGE 13 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET A SPACE-PADDED STRING WITH THE ORIGINAL STRING LEFT -
JUSTIFIED TO A TOTAL OF WIDTH COLUMNS?
A. Use the ljust() method
B. Use the rjust() method
C. Use the center() method
D. Use the zfill() method

Correct Response: 1

Explanation: The ljust() method returns a left-justified string of a given width by padding with spaces

HOW WILL YOU CONVERT A STRING TO ALL LOWERCASE?


A. Use the lower() method
B. Use the upper() method
C. Use the capitalize() method
D. Use the title() method

Correct Response: 1

Explanation: The lower() method returns the string in lowercase

HOW WILL YOU REMOVE ALL LEADING WHITESPACE IN STRING?


A. Use the lstrip() method
B. Use the rstrip() method
C. Use the strip() method
D. Use the replace() method

Correct Response: 1

Explanation: The lstrip() method removes all leading whitespaces from the string

HOW WILL YOU GET THE MAX ALPHABETICAL CHARACTER FROM THE STRING?
A. Use the max() function
B. Use the min() function
C. Use the sorted() function
D. Use the reverse() function

Correct Response: 1

Explanation: The max() function returns the maximum alphabetical character from the string

WWW.CDPNETWORKS.COM | [email protected] PAGE 14 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET THE MIN ALPHABETICAL CHARACTER FROM THE STRING?
A. Use the min() function
B. Use the max() function
C. Use the sorted() function
D. Use the reverse() function

Correct Response: 1

Explanation: The min() function returns the minimum alphabetical character from the string

HOW WILL YOU REPLACES ALL OCCURRENCES OF OLD SUBSTRING IN STRING WITH
NEW STRING?
A. str.replace() method
B. str.sub() method
C. str.translate() method
D. str.format() method

Correct Response: 1

Explanation: The str.replace() method replaces all occurrences of the old substring with the new string.

HOW WILL YOU REMOVE ALL LEADING AND TRAILING WHITESPACE IN STRING?
A. str.strip() method
B. str.lstrip() method
C. str.rstrip() method
D. All of the above

Correct Response: 1

Explanation: The str.strip() method removes all leading and trailing whitespace in the string.

HOW WILL YOU CHECK IN A STRING THAT ALL CHARACTERS ARE DECIMAL?
A. string.isdecimal()
B. string.isdigit()
C. string.isnumeric()
D. string.isalnum()

Correct Response: 1

Explanation: The method string.isdecimal() returns True if all characters in the string are decimal characters and False
otherwise.

WWW.CDPNETWORKS.COM | [email protected] PAGE 15 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN DEL() AND REMOVE() METHODS OF LIST?


A. del() method deletes an element from the list, remove() method removes the first occurrence of the element with
the specified value.
B. del() method removes the first occurrence of the element with the specified value, remove() method deletes an
element from the list.
C. Both methods are same and deletes an element from the list.
D. Both methods are same and removes the first occurrence of the element with the specified value.

Correct Response: 1

Explanation: The del method deletes an element from the list using an index, while the remove method removes the first
occurrence of the element with the specified value.

WHAT IS THE OUTPUT OF [1, 2, 3] + [4, 5, 6]?


A. [1, 2, 3, 4, 5, 6]
B. [7, 8, 9]
C. [4, 5, 6]
D. Error

Correct Response: 1

Explanation: The + operator concatenates two lists, in this case [1, 2, 3] and [4, 5, 6] resulting in a new list [1, 2, 3, 4, 5, 6].

HOW WILL YOU INSERT AN OBJECT AT GIVEN INDEX IN A LIST?


A. list.insert(index, object)
B. insert(index, object, list)
C. list.add(index, object)
D. add(index, object, list)

Correct Response: 1

Explanation: list.insert(index, object) inserts the object at the given index in the list.

HOW WILL YOU REMOVE LAST OBJECT FROM A LIST?


A. list.pop()
B. pop(list)
C. list.remove()
D. remove(list)

Correct Response: 1

Explanation: list.pop() removes and returns the last object from the list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 16 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU REMOVE AN OBJECT FROM A LIST?


A. list.remove(object)
B. remove(object, list)
C. list.delete(object)
D. delete(object, list)

Correct Response: 1

Explanation: list.remove(object) removes the first occurrence of the object from the list.

HOW WILL YOU REVERSE A LIST?


A. list.reverse()
B. reverse(list)
C. list.invert()
D. invert(list)

Correct Response: 1

Explanation: list.reverse() reverses the elements in the list.

HOW WILL YOU SORT A LIST?


A. list.sort()
B. sort(list)
C. list.arrange()
D. arrange(list)

Correct Response: 1

Explanation: list.sort() sorts the elements in the list in ascending order by default.

WHAT IS LAMBDA FUNCTION IN PYTHON?


A. A function that returns a value
B. A function that returns a function
C. A function without a name
D. A function with one line

Correct Response: 1

Explanation: A function without a name that can have any number of arguments but only one expression, which is
evaluated and returned

WWW.CDPNETWORKS.COM | [email protected] PAGE 17 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT WE CALL A FUNCTION WHICH IS INCOMPLETE VERSION OF A FUNCTION?


A. Anonymous function
B. Incomplete function
C. Half function
D. Not a function

Correct Response: 1

Explanation: A function that does not have a name and defined using the lambda keyword

WHEN A FUNCTION IS DEFINED THEN THE SYSTEM STORES PARAMETERS AND


LOCAL VARIABLES IN AN AREA OF MEMORY. WHAT THIS MEMORY IS KNOWN AS?
A. Stack Memory
B. Heap Memory
C. Hard Disk
D. RAM

Correct Response: 1

Explanation: The memory that is used to store function call stack is known as stack memory

WHAT ARE THE APPLICATIONS OF PYTHON?


A. Web Development
B. Machine Learning
C. Data Science
D. Game Development

Correct Response: 1, 2, 3

Explanation: Python is widely used in web development, machine learning, data science, and game development.

WHAT IS THE BASIC DIFFERENCE BETWEEN PYTHON VERSION 2 AND PYTHON


VERSION 3?
A. Syntax
B. Performance
C. Library Support
D. All of the above

Correct Response: 4

Explanation: Python 3 is the latest version and has improved performance, enhanced library support, and different syntax
compared to Python 2.

WWW.CDPNETWORKS.COM | [email protected] PAGE 18 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHICH PROGRAMMING LANGUAGE IS AN IMPLEMENTATION OF PYTHON


PROGRAMMING LANGUAGE DESIGNED TO run on Java Platform?
A. Jython
B. IronPython
C. Pyjamas
D. Cython

Correct Response: 1

Explanation: Jython is an implementation of Python that runs on the Java platform.

WHICH MODULE OF PYTHON IS USED TO APPLY THE METHODS RELATED TO OS.?


A. os
B. sys
C. math
D. random

Correct Response: 1

Explanation: The os module in Python provides a way of using operating system dependent functionality like reading or
writing to the file system.

WHEN DOES A NEW BLOCK BEGIN IN PYTHON?


A. When a new line is encountered
B. When a new indented line is encountered
C. When a new function is declared
D. When a class is declared

Correct Response: 2

Explanation: In Python, a new block of code begins when a line is indented.

NAME THE PYTHON LIBRARY USED FOR MACHINE LEARNING.


A. NumPy
B. Pandas
C. Matplotlib
D. scikit-learn

Correct Response: 4

Explanation: scikit-learn is a popular Python library for machine learning that provides simple and efficient tools for data
mining and data analysis.

WWW.CDPNETWORKS.COM | [email protected] PAGE 19 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT DOES PASS OPERATION DO?


A. Compiles the code
B. Generates an error
C. Does nothing
D. Terminates the code

Correct Response: 3

Explanation: pass is a placeholder statement in Python. It is used when a statement is required syntactically but you do not
want any command or code to execute.

WHAT ARE THE BUILT-IN TYPES AVAILABLE IN PYTHON?


A. List
B. Tuple
C. Dictionary
D. Set

Correct Response: 1, 2, 3, 4

Explanation: Python has several built-in data types including List, Tuple, Dictionary, and Set.

NAME SOME CHARACTERISTICS OF PYTHON?


A. Object-Oriented
B. High-Level
C. Interpreted
D. Portable

Correct Response: 1, 2, 3, 4

Explanation: Python is an Object-Oriented, High-Level, Interpreted, and Portable programming language.

HOW DO I MODIFY A STRING?


A. By using the reassign operation
B. By using the modify operation
C. By creating a new string
D. Strings are immutable in Python, so they cannot be modified

Correct Response: 3

Explanation: Strings are immutable in Python, so to modify a string you have to create a new string.

WWW.CDPNETWORKS.COM | [email protected] PAGE 20 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

NAME SOME BENEFITS OF PYTHON


A. Improved Productivity
B. Ease of Learning
C. High Performance
D. Portable

Correct Response: 1

Explanation: Python has a very simple syntax, which makes it easier to learn and use. It also has a vast library that makes it
easier to perform complex tasks.

WHAT IS LAMBDA FUNCTIONS IN PYTHON?


A. A way to write functions in one line
B. A way to write functions that return None
C. A way to write functions with multiple arguments
D. A way to write functions that return a value

Correct Response: 1

Explanation: Lambda functions are anonymous functions in Python, which are small and do not have a name. They can be
used wherever a function is required.

WHEN TO USE A TUPLE VS LIST VS DICTIONARY IN PYTHON?


A. When the data is ordered and unchangeable, use a tuple. When the data is ordered and changeable, use a list.
When the data is unordered and changeable, use a dictionary.
B. When the data is ordered and changeable, use a tuple. When the data is ordered and unchangeable, use a list.
When the data is unordered and unchangeable, use a dictionary.
C. When the data is unordered and unchangeable, use a tuple. When the data is ordered and changeable, use a list.
When the data is unordered and changeable, use a dictionary.
D. When the data is ordered and unchangeable, use a dictionary. When the data is ordered and changeable, use a list.
When the data is unordered and changeable, use a tuple.

Correct Response: 1

Explanation: The choice of data structure depends on the specific requirements of the task at hand. For example, a tuple is
useful when the data is ordered and unchangeable, while a list is useful when the data is ordered and changeable. A
dictionary is useful when the data is unordered and changeable.

WWW.CDPNETWORKS.COM | [email protected] PAGE 21 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT ARE THE RULES FOR LOCAL AND GLOBAL VARIABLES IN PYTHON?
A. A local variable can be accessed only within the function in which it is declared, while a global variable can be
accessed from any function.
B. A local variable can be accessed from any function, while a global variable can be accessed only within the function
in which it is declared.
C. There are no rules for local and global variables in Python.
D. A local variable can be accessed from any function, and a global variable can be accessed only within the same
module.

Correct Response: 1

Explanation: In Python, a local variable can be accessed only within the function in which it is declared, while a global
variable can be accessed from any function. To make a local variable accessible from outside the function, it must be
declared as global.

WHAT ARE LOCAL VARIABLES AND GLOBAL VARIABLES IN PYTHON?


A. Variables declared within a function
B. Variables declared outside a function
C. Variables that can only be accessed within a function
D. Variables that can be accessed anywhere in the code

Correct Response: 1

Explanation: Local variables are those declared within a function and are only accessible within that function. Global
variables, on the other hand, are declared outside a function and can be accessed from anywhere in the code.

WHAT IS BINARY SEARCH


A. A method for finding an item from a sorted list by repeatedly dividing the search interval in half
B. A method for finding an item from an unsorted list by repeatedly dividing the search interval in half
C. A method for finding the maximum value from a list
D. A method for finding the minimum value from a list

Correct Response: 1

Explanation: Binary search is an efficient algorithm for finding an item from a sorted list by repeatedly dividing the search
interval in half. It is faster than linear search.

WWW.CDPNETWORKS.COM | [email protected] PAGE 22 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS LINEAR (SEQUENTIAL) SEARCH


A. A method for finding an item from a sorted list by repeatedly dividing the search interval in half
B. A method for finding an item from an unsorted list by checking each item one by one until the desired item is found
C. A method for finding the maximum value from a list
D. A method for finding the minimum value from a list

Correct Response: 2

Explanation: Linear search is a simple method for finding an item from a list by checking each item one by one until the
desired item is found. It is the simplest search algorithm, but it is slower than binary search.

DIFFERENCE BETWEEN LISTS AND TUPLES


A. Lists are mutable while tuples are immutable Tuples are mutable while lists are immutable
B. Lists have a fixed length while tuples have a variable length Tuples have a fixed length while lists have a variable
length

Correct Response: 1

Explanation: Lists are mutable, meaning their elements can be changed, while tuples are immutable, meaning their
elements cannot be changed.

IS IT POSSIBLE TO HAVE STATIC METHODS IN PYTHON


A. No, Python does not have static methods
B. Yes, Python has static methods
C. Yes, but static methods can only be defined in class methods
D. No, but static methods can only be defined in class methods

Correct Response: 2

Explanation: Yes, Python has the ability to define static methods in a class using the @staticmethod decorator.

HOW DOES PYTHON MEMORY MANAGEMENT WORK


A. Python memory management is based on garbage collection
B. Python memory management is based on manual memory allocation and deallocation
C. Python memory management is based on dynamic memory allocation
D. Python memory management is based on static memory allocation

Correct Response: 1

Explanation: Python memory management is based on garbage collection, which automatically frees up memory that is no
longer being used by the program.

WWW.CDPNETWORKS.COM | [email protected] PAGE 23 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT'S THE DIFFERENCE BETWEEN THE LIST METHODS APPEND() AND EXTEND()?
A. append() adds an element to the end of a list.
B. extend() concatenates two lists.
C. append() creates a new list.
D. extend() appends an element to the end of a list.

Correct Response: 1

Explanation: The method append() adds an element to the end of a list, whereas extend() concatenates two lists.

HOW CAN I CREATE A COPY OF AN OBJECT IN PYTHON?


A. Using the "copy" module
B. Using the "deepcopy" method
C. By assigning the object to a new variable
D. By using the "clone" method

Correct Response: 2

Explanation: The "deepcopy" method creates a completely separate copy of an object, including any objects it contains,
while a shallow copy only copies the reference to the object.

WHAT ARE THE KEY DIFFERENCES BETWEEN PYTHON 2 AND 3?


A. Syntax differences
B. Improved memory management
C. Improved support for functional programming
D. All of the above

Correct Response: 4

Explanation: Python 3 has a number of improvements over Python 2, including syntax changes, better memory
management, and improved support for functional programming.

WWW.CDPNETWORKS.COM | [email protected] PAGE 24 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS A CALLABLE?
An object that can be invoked with a call operator (())

An object that can be used as a function

An object that can be used as a class

An object that can be used as a module

Correct Response: 2

Explanation: A callable is an object that can be invoked as a function and can return a value.

WHAT IS THE DIFFERENCE BETWEEN RANGE AND XRANGE? HOW HAS THIS
CHANGED OVER TIME?
range and xrange are the same in Python

range is a built-in function while xrange is a sequence type

range is used in Python 2 while xrange is used in Python 3

xrange is used in Python 2 while range is used in Python 3

Correct Response: 4

Explanation: In Python 2, range returns a list while xrange returns an object that generates the numbers in the desired
range on the fly, which is more memory-efficient for large ranges. In Python 3, range is equivalent to xrange in Python 2.

WHAT IS INTROSPECTION/REFLECTION AND DOES PYTHON SUPPORT IT?


A. Introspection is the process of reflecting on the properties and values of an object, and Python does not support it.
B. Introspection is the process of reflecting on the properties and values of an object, and Python supports it.
C. Reflection is the process of modifying an object's properties and values at runtime, and Python does not support it.
D. Reflection is the process of modifying an object's properties and values at runtime, and Python supports it.

Correct Response: 2

Explanation: Python supports introspection, allowing you to inspect the properties and values of objects at runtime.

WWW.CDPNETWORKS.COM | [email protected] PAGE 25 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PYTHON WITH STATEMENT DESIGNED FOR?


A. The with statement is used to automatically close file objects.
B. The with statement is used to automatically close database connections.
C. The with statement is used to automatically close network sockets.
D. The with statement is used for all of the above.

Correct Response: 1

Explanation: The with statement in Python is used to wrap the execution of a block of code with methods defined by a
context manager, which is typically used to manage resources such as file objects.

WHAT DOES THE PYTHON NONLOCAL STATEMENT DO (IN PYTHON 3.0 AND LATER)?
A. The nonlocal statement is used to declare a variable as non-local (global)
B. The nonlocal statement is used to declare a variable as local to the current function
C. The nonlocal statement is used to declare a variable as being a module-level variable
D. The nonlocal statement is used to declare a variable as being a class-level variable

Correct Response: 2

Explanation: The nonlocal statement in Python is used to indicate that a variable is going to be assigned to in the nearest
enclosing scope that defines the variable.

EXPLAIN HOW TO USE SLICING IN PYTHON?


A. Slicing is a way to extract a portion of a list or a string
B. Slicing can be performed on tuples as well
C. To slice an object in Python, use the syntax object[start:end]
D. All of the above

Correct Response: 4

Explanation: Slicing in Python is a way to extract a portion of a sequence (such as a string, list, or tuple) by specifying a start
and end index. Slicing can be performed on any sequence object, including strings, lists, and tuples. The syntax for slicing is
object[start:end], where start is the index of the first element to include in the slice and end is the index of the first element
to exclude from the slice.

WWW.CDPNETWORKS.COM | [email protected] PAGE 26 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

EXPLAIN WHAT IS INTERPOLATION SEARCH


A. A search algorithm that finds the position of a target value within a sorted array by linear approximation
B. A technique to extrapolate values between two known values
C. A method to calculate the natural logarithm of a number
D. A method to evaluate mathematical expressions

Correct Response: 1

Explanation: Interpolation Search is a search algorithm that finds the position of a target value within a sorted array by
linear approximation.

WHAT IS A JUMP (OR BLOCK) SEARCH


A. A search algorithm that divides the list into equal blocks and performs linear search on each block
B. A sorting algorithm that rearranges elements in a particular order
C. A technique to calculate the inverse of a matrix
D. A method to find the square root of a number

Correct Response: 1

Explanation: Jump (or Block) Search is a search algorithm that divides the list into equal blocks and performs linear search
on each block.

HOW TO DETERMINE K USING THE ELBOW METHOD


A. By finding the value of k where the rate of decrease in the sum of squared distances is the highest
B. By finding the value of k where the sum of squared distances is the lowest
C. By finding the value of k where the sum of squared distances is equal to zero
D. By finding the value of k where the rate of increase in the sum of squared distances is the highest

Correct Response: 1

Explanation: To determine k using the Elbow Method, we find the value of k where the rate of decrease in the sum of
squared distances is the highest.

WHAT IS THE PURPOSE OF MESHGRID IN PYTHON/NUMPY


A. To create a rectangular grid out of two one-dimensional arrays To evaluate a mathematical expression
B. To calculate the inverse of a matrix
C. To find the eigenvalues and eigenvectors of a matrix

Correct Response: 1

Explanation: The purpose of meshgrid in Python/NumPy is to create a rectangular grid out of two one-dimensional arrays.

WWW.CDPNETWORKS.COM | [email protected] PAGE 27 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

IN WHICH CASES WOULD YOU USE PYTHON OVER R AND VICE VERSA
A. Python is better suited for data analysis, while R is better suited for statistical modeling
B. R is better suited for data analysis, while Python is better suited for web development
C. Python is better suited for machine learning, while R is better suited for deep learning
D. R is better suited for machine learning, while Python is better suited for natural language processing

Correct Response: 1

Explanation: In general, Python is better suited for data analysis, while R is better suited for statistical modeling.

WHAT ARE THE DISADVANTAGES OF R IN COMPARISON WITH PYTHON?


A. R lacks support for web development and GUI
B. R is slow compared to Python
C. R is difficult to learn compared to Python
D. R is not as widely used as Python

Correct Response: 1

Explanation: R lacks support for web development and GUI compared to Python.

WHAT ARE THE ADVANTAGES OF R IN COMPARISON WITH PYTHON?


A. R has a large community of data scientists and statisticians
B. R has better visualization capabilities compared to Python
C. R is easy to learn compared to Python
D. R has a large number of libraries for data analysis

Correct Response: 2

Explanation: R has better visualization capabilities compared to Python.

HOW WOULD YOU CREATE A RECOMMENDER SYSTEM FOR TEXT


INPUTS?
A. Using Cosine Similarity
B. Using Euclidean Distance
C. Using Pearson Correlation
D. Using Jaccard Similarity

Correct Response: 1

Explanation: A Recommender System for text inputs can be created using Cosine Similarity.

WWW.CDPNETWORKS.COM | [email protected] PAGE 28 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN @STATICMETHOD AND @CLASSMETHOD?


A. @staticmethod does not have access to the class, while @classmethod does
B. @staticmethod has access to the class, while @classmethod does not
C. @staticmethod and @classmethod are the same
D. @staticmethod and @classmethod have different uses

Correct Response: 1

Explanation: @staticmethod does not have access to the class, while @classmethod does.

WHAT ARE METACLASSES IN PYTHON?


A. Metaclasses are classes that define the behavior of other classes
B. Metaclasses are classes that define the behavior of objects
C. Metaclasses are classes that define the behavior of functions
D. Metaclasses are classes that define the behavior of modules

Correct Response: 1

Explanation: Metaclasses are classes that define the behavior of other classes.

WHAT'S THE DIFFERENCE BETWEEN A PYTHON MODULE AND A PYTHON PACKAGE?


A. A module is a single file with Python code. A package is a collection of modules in directories that give a package
hierarchy.
B. A module is a single file with Python code. A package is a folder with Python code.
C. A module is a single file or a folder with Python code. A package is a collection of modules in directories that give a
package hierarchy.
D. A module is a single file or a folder with Python code. A package is a folder with Python code.

Correct Response: 1

Explanation: A module is a single file with Python code, while a package is a collection of modules in directories that give a
package hierarchy.

WWW.CDPNETWORKS.COM | [email protected] PAGE 29 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHY ARE DEFAULT VALUES SHARED BETWEEN OBJECTS?


A. Because default values are static and shared between all objects.
B. Because default values are assigned to the class, not to individual instances.
C. Because default values are assigned to the object, not to the class.
D. Because default values are dynamic and assigned to individual instances.

Correct Response: 2

Explanation: Default values are assigned to the class, not to individual instances, which is why they are shared between all
objects.

WHAT IS GIL?
A. Global Interpreter Lock
B. Global Index Lock
C. Global Interpreter Loop
D. Global Index Loop

Correct Response: 1

Explanation: The Global Interpreter Lock (GIL) is a mechanism in CPython that ensures that only one thread is executed at a
time, even on multi-core systems.

IS IT A GOOD IDEA TO USE MULTI-THREAD TO SPEED YOUR PYTHON CODE?


A. Yes, it is always a good idea to use multi-threading to speed up Python code.
B. No, it is never a good idea to use multi-threading to speed up Python code.
C. It depends on the task and the implementation.
D. It depends on the task only.

Correct Response: 3

Explanation: It depends on the task and the implementation. In some cases, multi-threading can significantly improve the
performance of Python code, while in other cases it can lead to performance degradation.

WWW.CDPNETWORKS.COM | [email protected] PAGE 30 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHY PYTHON (CPYTHON AND OTHERS) USES THE GIL?


A. To prevent data races and other concurrency-related issues.
B. To allow for easy parallelization of CPU-bound tasks.
C. To ensure that only one thread is executed at a time, even on multi-core systems.
D. To simplify the implementation of the Python interpreter.

Correct Response: 4

Explanation: The GIL was implemented to simplify the implementation of the Python interpreter, however it also has the
side effect of limiting the parallelization of CPU-bound tasks.

WHAT IS THE DIFFERENCE BETWEEN OLD STYLE AND NEW STYLE CLASSES IN
PYTHON?
A. Old style classes do not support inheritance
B. New style classes support multiple inheritance
C. Old style classes do not support method resolution order
D. New style classes support method resolution order

Correct Response: 4

Explanation: New style classes, introduced in Python 2.2, support a consistent method resolution order (MRO) through the
use of C3 linearization. Old style classes do not have a consistent MRO.

WHY USE ELSE IN TRY/EXCEPT CONSTRUCT IN PYTHON?


A. To handle unexpected exceptions
B. To execute code when no exceptions were raised
C. To handle exceptions other than specified in except
D. To execute code when exceptions were raised

Correct Response: 2

Explanation: The else clause is executed if no exceptions were raised in the try block

WWW.CDPNETWORKS.COM | [email protected] PAGE 31 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS A GLOBAL INTERPRETER LOCK (GIL) AND WHY IS IT AN ISSUE?


A. A lock that allows only one thread to execute at a time in CPython, leading to performance issues
B. A lock that allows multiple threads to execute at the same time in CPython, leading to performance issues
C. A lock that allows only one thread to execute at a time in all implementations of Python
D. A lock that allows multiple threads to execute at the same time in all implementations of Python

Correct Response: 1

Explanation: GIL is a lock in CPython that allows only one thread to execute at a time, leading to performance issues when
multiple threads are used

IS THERE ANY DOWNSIDE TO THE -O FLAG APART FROM MISSING ON


THE BUILT-IN DEBUGGING INFORMATION?
A. Yes, it disables the interactive mode
B. Yes, it disables the garbage collector
C. No, there are no downsides to using -O flag
D. No, it only disables built-in debugging information

Correct Response: 2

Explanation: The -O flag disables the garbage collector in Python

WHAT DOES PYTHON OPTIMISATION (-O OR PYTHONOPTIMIZE) DO?


A. Enables debugging information
B. Disables debugging information and enables optimised bytecode
C. Enables debugging information and optimised bytecode
D. Disables optimised bytecode

Correct Response: 2

Explanation: The -O or PYTHONOPTIMIZE flag disables debugging information and enables optimised bytecode in Python

WWW.CDPNETWORKS.COM | [email protected] PAGE 32 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHY ISN'T ALL MEMORY FREED WHEN PYTHON EXITS?


A. Memory is freed when Python exits
B. Python does not provide any memory management
C. Memory is freed when Python's Garbage Collection mechanism is run
D. Memory is freed when the program is closed

Correct Response: 1

Explanation: Memory is freed when Python exits, but some memory may not be freed if there are still references to it.
Python's Garbage Collection mechanism frees up memory that is no longer in use, but it may not catch everything.

HOW TO TRAIN A NETWORK ONLY ON ONE OUTPUT WHEN THERE ARE


MULTIPLE OUTPUTS IN KERAS?
A. Train on all outputs
B. Train on a single output using the "compile" function
C. Train on a single output using the "fit" function
D. Train on a single output by creating a new model

Correct Response: 3

Explanation: Train on a single output using the "fit" function by specifying the target(s) to be trained on.

WHAT IS THE DIFFERENCE BETWEEN RANGE AND XRANGE IN PYTHON 2?


A. range returns a list, xrange returns a generator
B. xrange is faster than range
C. range is faster than xrange
D. xrange is only available in Python 3

Correct Response: 1

Explanation: range returns a list, xrange returns a generator in Python 2. xrange is similar to range in Python 3.

WWW.CDPNETWORKS.COM | [email protected] PAGE 33 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN DEEP AND SHALLOW COPY IN PYTHON?


A. Shallow copy is faster than deep copy
B. Deep copy creates a new copy of the object, shallow copy only creates a reference
C. Shallow copy creates a new copy of the object, deep copy only creates a reference
D. Deep copy is only available in Python 3

Correct Response: 2

Explanation: Deep copy creates a new copy of the object, while shallow copy only creates a reference to the original object.

CAN YOU EXPLAIN THE "YIELD" STATEMENT IN PYTHON?


A. The "yield" statement returns a value and stops execution
B. The "yield" statement returns a value and continues execution
C. The "yield" statement only stops execution
D. The "yield" statement only continues execution

Correct Response: 2

Explanation: The "yield" statement returns a value and continues execution. It is used in Python to create a generator.

WHAT IS THE DIFFERENCE BETWEEN STR AND REPR IN PYTHON?


A. str returns a string that's meant to be human-readable.
B. repr returns a string that's meant to be a machine-readable representation of an object.
C. str returns a string that's meant to be a machine-readable representation of an object.
D. repr returns a string that's meant to be human-readable.

Correct Response: 2

Explanation: str is used for creating output for end user while repr is mainly used for debugging and development.

WHAT IS THE "WITH" STATEMENT USED FOR IN PYTHON?


A. It is used to manage the execution of a block of code that requires specific setup and cleanup actions.
B. It is used to create a dictionary.
C. It is used to open and read a file.
D. It is used to manage the execution of a loop.

Correct Response: 1

Explanation: The "with" statement is used to wrap the execution of a block of code with methods defined by a context
manager.

WWW.CDPNETWORKS.COM | [email protected] PAGE 34 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PURPOSE OF INIT METHOD IN PYTHON?


A. It is used to initialize class attributes.
B. It is used to create an object.
C. It is used to delete an object.
D. It is used to define class methods.

Correct Response: 1

Explanation: The __init__ method is called automatically when an object of the class is created, and it allows you to set up
the attributes for the object.

WHAT IS THE DIFFERENCE BETWEEN A TUPLE AND A LIST IN PYTHON?


A. Lists are mutable while tuples are immutable.
B. Tuples are mutable while lists are immutable.
C. Lists are ordered while tuples are not.
D. Tuples are ordered while lists are not.

Correct Response: 1

Explanation: The main difference between tuples and lists is that tuples are immutable, meaning you can't change their
contents after they're created. Lists are mutable, so you can add, remove, or modify elements as needed.

WHAT IS A PYTHON DECORATOR?


A. A special type of function that can be used to modify the behavior of another function.
B. A special type of class that can be used to modify the behavior of another class.
C. A special type of module that can be used to modify the behavior of another module.
D. A special type of data structure that can be used to store data.

Correct Response: 1

Explanation: Decorators are a way to modify the behavior of a function or class without changing the source code of the
function or class itself. They are applied to a function or class using the "@" symbol.

WWW.CDPNETWORKS.COM | [email protected] PAGE 35 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN A MODULE AND A PACKAGE IN PYTHON?


A. A module is a collection of functions, while a package is a collection of modules.
B. A module is a single file, while a package is a directory with multiple files.
C. A package can only be imported, while a module can be executed.
D. A package can be executed, while a module can only be imported.

Correct Response: 2

Explanation: A package is a collection of modules, and it is stored in a directory that contains multiple files, while a module
is a single file that can contain functions, classes, or variables.

WHAT IS THE PURPOSE OF THE "NAME" VARIABLE IN PYTHON?


A. To store the name of the current module or script.
B. To store the name of the current class.
C. To store the name of the current function.
D. To store the name of the current variable.

Correct Response: 1

Explanation: The "name" variable in Python is used to store the name of the current module or script. It is automatically set
by Python and can be used to access the name of the current module or script.

HOW DO YOU CREATE A PYTHON CLASS?


A. By using the "class" keyword and defining the class name and its properties and methods.
B. By using the "def" keyword and defining the class name and its properties and methods.
C. By using the "struct" keyword and defining the class name and its properties and methods.
D. By using the "type" keyword and defining the class name and its properties and methods.

Correct Response: 1

Explanation: In Python, you create a class by using the "class" keyword and defining the class name, its properties, and
methods. The properties and methods are defined inside the class using the "def" keyword.

WWW.CDPNETWORKS.COM | [email protected] PAGE 36 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN A CLASS METHOD AND A STATIC METHOD IN


PYTHON?
A. A class method is a method that is bound to the class and can access class-level data, while a static method is a
method that is bound to the instance and can access instance-level data.
B. A class method is a method that is bound to the instance and can access instance-level data, while a static method
is a method that is bound to the class and can access class-level data.
C. A class method is a method that can only be called on the class and not on an instance, while a static method can
be called on either the class or an instance.
D. A class method is a method that is not bound to either the class or the instance and cannot access any class-level or
instance-level data, while a static method is a method that can access both class-level and instance-level data.

Correct Response: 2

Explanation: A class method is a method that is bound to the class and can access class-level data, while a static method is
a method that is not bound to either the class or the instance and does not have access to any class-level or instance-level
data.

WHAT IS THE DIFFERENCE BETWEEN A SHALLOW COPY AND A DEEP COPY IN


PYTHON?
A. A shallow copy only copies the reference to the object, not the object itself.
B. A deep copy creates a new copy of the object and all its references.
C. A shallow copy only copies the first level of the object, not any nested objects.
D. A deep copy only copies the reference to the object, not the object itself.

Correct Response: 2

Explanation: A shallow copy creates a new reference to the same object, while a deep copy creates a new object with its
own references to the data.

CAN YOU EXPLAIN THE "TRY-EXCEPT" BLOCK IN PYTHON?


A. The try-except block is used to handle exceptions in Python.
B. The try-except block is used to ignore exceptions in Python.
C. The try-except block is used to raise exceptions in Python.
D. The try-except block is not used in Python.

Correct Response: 1

Explanation: The try block contains code that might raise an exception, and the except block contains code that will be
executed if an exception is raised. This allows you to handle errors gracefully in your code.

WWW.CDPNETWORKS.COM | [email protected] PAGE 37 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW DO YOU HANDLE ERRORS IN PYTHON?


A. By using the try-except block.
B. By using the if-else block.
C. By using the while loop.
D. By using the for loop.

Correct Response: 1

Explanation: The try-except block is the recommended way to handle exceptions and errors in Python.

WHAT IS THE DIFFERENCE BETWEEN A DICTIONARY AND A SET IN PYTHON?


A. A dictionary is an ordered collection of key-value pairs, while a set is an unordered collection of unique elements.
B. A dictionary is an unordered collection of key-value pairs, while a set is an ordered collection of unique elements.
C. A dictionary is a collection of unique elements, while a set is a collection of key-value pairs.
D. There is no difference between a dictionary and a set in Python.

Correct Response: 1

Explanation: Dictionaries are mutable, unordered collections of key-value pairs, while sets are mutable, unordered
collections of unique elements.

CAN YOU EXPLAIN THE "ASSERT" STATEMENT IN PYTHON?


A. The assert statement is used to test a condition in your code and raise an exception if the condition is not met.
B. The assert statement is used to ignore a condition in your code.
C. The assert statement is used to handle exceptions in your code.
D. The assert statement is not used in Python.

Correct Response: 1

Explanation: The assert statement is used as a debugging aid to check for conditions that should never occur in your code.
If the condition is not met, an AssertionError is raised.

WWW.CDPNETWORKS.COM | [email protected] PAGE 38 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN A PYTHON GENERATOR AND A LIST?


A. A generator is a collection of values returned one at a time, whereas a list is a collection of values stored in
memory.
B. A generator is a collection of values returned all at once, whereas a list is a collection of values stored in memory.
C. A generator is a collection of values stored in memory, whereas a list is a collection of values returned one at a
time.
D. A generator is a collection of values stored in memory, whereas a list is a collection of values returned all at once.

Correct Response: 1

Explanation: In Python, generators are iterators that generate values one at a time and do not store the values in memory.
On the other hand, lists are collections of values that are stored in memory and can be accessed all at once.

WHAT IS THE PURPOSE OF THE "CALL" METHOD IN PYTHON?


A. The "call" method is used to call a function in Python.
B. The "call" method is used to call a method of an object in Python.
C. The "call" method is used to call a class in Python.
D. The "call" method is used to call a module in Python.

Correct Response: 2

Explanation: The "call" method is used to call a method of an object in Python, and it is typically used to invoke methods of
objects that have been defined using classes.

WHAT IS THE DIFFERENCE BETWEEN "APPEND" AND "EXTEND" METHODS IN


PYTHON LISTS?
A. The "append" method is used to add a single element to the end of a list, whereas the "extend" method is used to
add multiple elements to the end of a list.
B. The "append" method is used to add multiple elements to the end of a list, whereas the "extend" method is used to
add a single element to the end of a list.
C. The "append" method is used to add elements to the beginning of a list, whereas the "extend" method is used to
add elements to the end of a list.
D. The "append" method is used to add elements to the middle of a list, whereas the "extend" method is used to add
elements to the end of a list.

Correct Response: 1

Explanation: In Python, the "append" method is used to add a single element to the end of a list, while the "extend"
method is used to add multiple elements to the end of a list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 39 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PURPOSE OF THE "SUPER" FUNCTION IN PYTHON?


A. The "super" function is used to call the parent class's method in Python.
B. The "super" function is used to call the child class's method in Python.
C. The "super" function is used to call the grandparent class's method in Python.
D. The "super" function is used to call the sibling class's method in Python.

Correct Response: 1

Explanation: The "super" function in Python is used to call the parent class's method, and it is typically used in situations
where a child class wants to inherit the behavior of its parent class but also wants to modify or extend that behavior in
some way.

WHAT IS THE DIFFERENCE BETWEEN "IS" AND "==" IN PYTHON?


A. "is" checks if two variables refer to the same object in memory, while "==" checks if the objects they refer to have
the same value.
B. "is" checks if the objects they refer to have the same value, while "==" checks if two variables refer to the same
object in memory. "is" and "==" are interchangeable in Python and have the same result.

Correct Response: 1

Explanation: "is" checks if two variables refer to the same object in memory, while "==" checks if the objects they refer to
have the same value.

WHAT IS THE DIFFERENCE BETWEEN A TUPLE AND A LIST IN PYTHON?


A. Lists are mutable and tuples are immutable.
B. Tuples are mutable and lists are immutable.
C. Lists are ordered and tuples are unordered.

Correct Response: 1

Explanation: Lists are mutable and tuples are immutable.

CAN YOU EXPLAIN THE "GLOBAL" KEYWORD IN PYTHON?


A. The "global" keyword is used to indicate that a variable is a global variable and can be accessed from anywhere in
the code.
B. The "global" keyword is used to indicate that a variable is a local variable and cannot be accessed from outside the
function.
C. The "global" keyword has no special meaning in Python.

Correct Response: 1

Explanation: The "global" keyword is used to indicate that a variable is a global variable and can be accessed from
anywhere in the code.

WWW.CDPNETWORKS.COM | [email protected] PAGE 40 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PURPOSE OF THE "LAMBDA" FUNCTION IN PYTHON?


A. The "lambda" function is used to create small, throwaway functions that can be used in place of a full-fledged
function.
B. The "lambda" function is used to create large, permanent functions that are used throughout the code.
C. The "lambda" function has no special meaning in Python.

Correct Response: 1

Explanation: The "lambda" function is used to create small, throwaway functions that can be used in place of a full-fledged
function.

WHAT IS THE DIFFERENCE BETWEEN THE "IN" AND "NOT IN" OPERATORS IN
PYTHON?
A. "in" operator returns True if a specified value is present in a sequence.
B. "not in" operator returns True if a specified value is not present in a sequence.
C. "in" operator is used to check if a value exists in a list.
D. "not in" operator is used to check if a value does not exist in a list.

Correct Response: 2

Explanation: The "in" operator returns True if a specified value is present in a sequence, while the "not in" operator returns
True if a specified value is not present in a sequence.

WHAT IS THE DIFFERENCE BETWEEN THE "BREAK" AND "CONTINUE" STATEMENTS


IN PYTHON?
A. "break" statement is used to exit a loop.
B. "continue" statement is used to skip the current iteration and continue with the next iteration.
C. "break" statement is used to skip all remaining iterations.
D. "continue" statement is used to exit a loop.

Correct Response: 2

Explanation: The "break" statement is used to exit a loop, while the "continue" statement is used to skip the current
iteration and continue with the next iteration.

WWW.CDPNETWORKS.COM | [email protected] PAGE 41 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

CAN YOU EXPLAIN THE "FINALLY" BLOCK IN PYTHON?


A. The "finally" block is executed after the "try" block is executed, regardless of whether an exception was raised or
not.
B. The "finally" block is executed only if an exception was raised.
C. The "finally" block is executed before the "try" block is executed.
D. The "finally" block is not executed in Python.

Correct Response: 1

Explanation: The "finally" block is executed after the "try" block is executed, regardless of whether an exception was raised
or not. It is used to clean up any resources that were acquired in the "try" block.

WHAT IS THE DIFFERENCE BETWEEN A LIST AND A SET IN PYTHON?


A. A list is an ordered collection of elements, while a set is an unordered collection of unique elements.
B. A list is an unordered collection of elements, while a set is an ordered collection of unique elements.
C. A list is a collection of unique elements, while a set is a collection of ordered elements.
D. A list is a collection of elements, while a set is a collection of unique ordered elements.

Correct Response: 1

Explanation: A list is an ordered collection of elements, while a set is an unordered collection of unique elements.

WHAT IS THE DIFFERENCE BETWEEN A LIST COMPREHENSION AND A GENERATOR


EXPRESSION IN PYTHON?
A. A list comprehension creates a new list in memory, while a generator expression generates values one at a time as
they are needed.
B. A list comprehension generates values one at a time as they are needed, while a new list is created in memory.
C. A list comprehension creates a new list in memory, while a generator expression creates a new set in memory.
D. A list comprehension creates a new set in memory, while a generator expression generates values one at a time as
they are needed.

Correct Response: 1

Explanation: A list comprehension creates a new list in memory, while a generator expression generates values one at a
time as they are needed.

WWW.CDPNETWORKS.COM | [email protected] PAGE 42 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN "STR" AND "REPR" IN PYTHON?


A. "str" represents a human-readable version of an object, while "repr" represents a unambiguous version of an
object.
B. "str" and "repr" are interchangeable in Python.
C. "str" represents a machine-readable version of an object, while "repr" represents a human-readable version of an
object.
D. "repr" is used for debugging purposes, while "str" is used for display purposes.

Correct Response: 1

Explanation: "str" represents a human-readable version of an object, while "repr" represents a unambiguous version of an
object, that can be used to recreate the object.

CAN YOU EXPLAIN THE "MAP" AND "FILTER" FUNCTIONS IN PYTHON?


A. "map" applies a function to all elements in an iterable and returns a map object, while "filter" filters elements from
an iterable based on a function.
B. "map" and "filter" are interchangeable in Python.
C. "map" modifies elements in an iterable, while "filter" returns a filtered list of elements.
D. "filter" applies a function to all elements in an iterable and returns a filter object, while "map" filters elements from
an iterable based on a function.

Correct Response: 1

Explanation: "map" applies a function to all elements in an iterable and returns a map object, while "filter" filters elements
from an iterable based on a function.

WHAT IS THE DIFFERENCE BETWEEN "SORT" AND "SORTED" IN PYTHON?


A. "sort" modifies the list in-place, while "sorted" returns a new sorted list.
B. "sort" and "sorted" are interchangeable in Python.
C. "sort" sorts elements in ascending order, while "sorted" sorts elements in descending order.
D. "sorted" modifies the list in-place, while "sort" returns a new sorted list.

Correct Response: 1

Explanation: "sort" modifies the list in-place, while "sorted" returns a new sorted list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 43 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN "DEL" AND "REMOVE" IN PYTHON LISTS?


A. "del" deletes elements from a list by index, while "remove" deletes elements from a list by value.
B. "del" and "remove" are interchangeable in Python.
C. "del" deletes elements from a list by value, while "remove" deletes elements from a list by index.
D. "remove" modifies the list in-place, while "del" returns a new list without the deleted elements.

Correct Response: 1

Explanation: "del" deletes elements from a list by index, while "remove" deletes elements from a list by value.

HOW WILL YOU CONVERT A STRING TO A SET IN PYTHON?


A. str = "hello"; set(str)
B. str = "hello"; tuple(str)
C. str = "hello"; list(str)
D. str = "hello"; dict(str)

Correct Response: 1

Explanation: The built-in set() function can be used to convert a string to a set in Python.

HOW WILL YOU CREATE A DICTIONARY USING TUPLES IN PYTHON?


A. dict(("a", 1),("b", 2))
B. dict(("a", 1), 2)
C. dict((1, "a"),(2, "b"))
D. dict(("a", 1),("b", 2),(3, "c"))

Correct Response: 1

Explanation: The built-in dict() function can take a list of tuples as an argument, where each tuple contains a key-value pair,
to create a dictionary.

HOW WILL YOU CONVERT A STRING TO A FROZEN SET IN PYTHON?


A. str = "hello"; frozenset(str)
B. str = "hello"; set(str)
C. str = "hello"; list(str)
D. str = "hello"; dict(str)

Correct Response: 1

Explanation: The built-in frozenset() function can be used to convert a string to a frozen set in Python.

WWW.CDPNETWORKS.COM | [email protected] PAGE 44 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CONVERT AN INTEGER TO A CHARACTER IN PYTHON?


A. chr(97)
B. ord(97)
C. str(97)
D. hex(97)

Correct Response: 1

Explanation: The built-in chr() function can be used to convert an integer to its corresponding Unicode character in Python.

HOW WILL YOU CONVERT AN INTEGER TO AN UNICODE CHARACTER IN


A. unichr(97)
B. chr(97)
C. ord(97)
D. hex(97)

Correct Response: 1

Explanation: The built-in unichr() function can be used to convert an integer to its corresponding Unicode character in
Python (Python 2.x). In Python 3.x, the chr() function can be used instead.

HOW WILL YOU CONVERT A SINGLE CHARACTER TO ITS INTEGER VALUE IN


A. ord(char)
B. int(char)
C. hex(char)
D. oct(char)

Correct Response: 1

Explanation: The ord() function returns an integer representing the Unicode character.

HOW WILL YOU CONVERT AN INTEGER TO HEXADECIMAL STRING IN


A. hex(int)
B. int(hex)
C. oct(int)
D. str(int)

Correct Response: 1

Explanation: The hex() function returns a hexadecimal string representation of an integer.

WWW.CDPNETWORKS.COM | [email protected] PAGE 45 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CONVERT AN INTEGER TO OCTAL STRING IN PYTHON?


A. oct(int)
B. int(oct)
C. hex(int)
D. str(int)

Correct Response: 1

Explanation: The oct() function returns an octal string representation of an integer.

WHAT IS THE PURPOSE OF ** OPERATOR?


A. Exponentiation
B. Division
C. Addition
D. Subtraction

Correct Response: 1

Explanation: The ** operator returns the result of raising the first operand to the power of the second operand.

WHAT IS THE PURPOSE OF // OPERATOR?


A. Floor Division
B. Division
C. Addition
D. Subtraction

Correct Response: 1

Explanation: The // operator returns the result of floor division of two operands, i.e., it returns the largest integer that is
smaller than or equal to the result of division.

HOW WILL YOU COMPARE TWO LISTS?


A. Using the == operator
B. Using the is operator
C. Using the != operator
D. Using the "in" operator

Correct Response: 1

Explanation: The == operator can be used to compare two lists and return True if both lists have the same elements in the
same order, and False otherwise.

WWW.CDPNETWORKS.COM | [email protected] PAGE 46 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET THE LENGTH OF A LIST?


A. Using the len function
B. Using the size method
C. Using the count method
D. Using the length property

Correct Response: 1

Explanation: The len function can be used to get the number of elements in a list.

HOW WILL YOU GET THE MAX VALUED ITEM OF A LIST?


A. Using the max function
B. Using the min function
C. Using the sort method
D. Using the reverse method

Correct Response: 1

Explanation: The max function can be used to get the maximum valued item in a list.

HOW WILL YOU GET THE MIN VALUED ITEM OF A LIST?


A. Using the min function
B. Using the max function
C. Using the sort method
D. Using the reverse method

Correct Response: 1

Explanation: The min function can be used to get the minimum valued item in a list.

HOW WILL YOU GET THE INDEX OF AN OBJECT IN A LIST?


A. Using the index method
B. Using the count method
C. Using the find method
D. Using the search method

Correct Response: 1

Explanation: The index method can be used to get the index of an object in a list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 47 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU COMPARE TWO LISTS?


A. == operator
B. is operator
C. != operator
D. not operator

Correct Response: 1

Explanation: == operator can be used to compare two lists, as it returns

True if both lists have same elements in same order, otherwise returns False.

HOW WILL YOU GET THE LENGTH OF A LIST?


A. len(list)
B. list.length()
C. list.len()
D. list.size()

Correct Response: 1

Explanation: len(list) returns the number of elements in the list.

HOW WILL YOU GET THE MAX VALUED ITEM OF A LIST?


A. max(list)
B. list.max()
C. list.maximum()
D. list.top()

Correct Response: 1

Explanation: max(list) returns the maximum valued item in the list.

HOW WILL YOU GET THE MIN VALUED ITEM OF A LIST?


A. min(list)
B. list.min()
C. list.minimum()
D. list.bottom()

Correct Response: 1

Explanation: min(list) returns the minimum valued item in the list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 48 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET THE INDEX OF AN OBJECT IN A LIST?


A. list.index(object)
B. index(object, list)
C. list.find(object)
D. find(object, list)

Correct Response: 1

Explanation: list.index(object) returns the index of the first occurrence of the object in the list.

WHY WOULD YOU USE THE PASS STATEMENT?


A. As a placeholder for code that has not been written yet.
B. To skip a block of code.
C. To create an empty function.
D. To create an infinite loop.

Correct Response: 1

Explanation: The pass statement is used as a placeholder for code that has not been written yet, and it does nothing.

WHAT ARE DECORATORS IN PYTHON?


A. A way to modify or extend the behavior of a function or class without changing its source code.
B. A way to define functions.
C. A way to define classes.
D. A way to define variables.

Correct Response: 1

Explanation: Decorators are a way to modify or extend the behavior of a function or class without changing its source code.

IS THERE A TOOL TO HELP FIND BUGS OR PERFORM STATIC ANALYSIS?


A. Yes, there are tools such as PyLint, Flake8, and mypy.
B. No, there is no such tool available.
C. Yes, there is a tool called PyChecker.
D. No, only manual code review is possible.

Correct Response: 1

Explanation: Yes, there are tools such as PyLint, Flake8, and mypy that can help find bugs or perform static analysis in
Python.

WWW.CDPNETWORKS.COM | [email protected] PAGE 49 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS MONKEY PATCHING AND IS IT EVER A GOOD IDEA?


A. Monkey patching is a way to change the behavior of a class or module at runtime, and it is a bad idea because it can
lead to unexpected results.
B. Monkey patching is a way to change the behavior of a class or module at runtime, and it can be a good idea in
certain situations.
C. Monkey patching is a way to change the behavior of a function or class at compile time, and it is a good idea.
D. Monkey patching is a way to change the behavior of a function or class at compile time, and it is a bad idea.

Correct Response: 2

Explanation: Monkey patching is a way to change the behavior of a class or module at runtime, and it can be a good idea in
certain situations, but it should be used with caution because it can lead to unexpected results.

EXPLAIN THE UNBOUNDLOCALERROR EXCEPTION AND HOW TO AVOID IT?


A. Occurs when a local variable is referenced before it is assigned a value.
B. Raised when a global variable is referenced before it is assigned a value.
C. Raised when a function does not return a value.
D. Raised when a value is not found in a list or dictionary.

Correct Response: 1

Explanation: UnboundLocalError is raised when a local variable is referenced before it has been assigned a value. This can
be avoided by either initializing the variable with a value before it is used or by using the global keyword to specify that the
variable is a global variable.

WHAT ARE IMMUTABLE OBJECTS IN PYTHON?


A. Objects that can be changed once created.
B. Objects that cannot be changed once created.
C. Objects that can be changed and then set back to their original value.
D. Objects that can be changed by reference.

Correct Response: 2

Explanation: Immutable objects in Python are objects that cannot be changed once they have been created. Examples
include numbers, strings, and tuples.

WWW.CDPNETWORKS.COM | [email protected] PAGE 50 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN RANGE AND XRANGE FUNCTIONS IN PYTHON?


A. range returns a list, while xrange returns an object.
B. range is faster than xrange.
C. xrange is only available in Python 2, while range is available in both Python 2 and Python 3.
D. There is no difference between the two functions.

Correct Response: 3

Explanation: The range function returns a list, while the xrange function returns an object. In Python 2, xrange was used to
save memory, as it generates the numbers in the specified range on the fly, rather than creating a whole list of numbers
upfront. In Python 3, range and xrange have been combined into a single range function, which behaves like the xrange
function in Python 2.

WHAT IS A NONE VALUE?


A. A value that represents the absence of a value.
B. A value that represents the presence of a value.
C. A value that represents the value 0.
D. A value that represents the value 1.

Correct Response: 1

Explanation: The None value in Python is a value that represents the absence of a value. It is equivalent to NULL in other
programming languages.

WHAT IS PICKLING AND UNPICKLING?


A. The process of converting an object hierarchy into a byte stream.
B. The process of converting a byte stream into an object hierarchy.
C. The process of converting a string into an object.
D. The process of converting an object into a string.

Correct Response: 1

Explanation: Pickling is the process of converting an object hierarchy (such as a Python data structure) into a byte stream.
Unpickling is the process of converting the byte stream back into an object hierarchy. The pickle module in Python provides
functions for performing these operations.

WWW.CDPNETWORKS.COM | [email protected] PAGE 51 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT ARE THE ADVANTAGES OF NUMPY OVER REGULAR PYTHON LISTS?


A. NumPy arrays are more memory efficient than Python lists.
B. NumPy arrays are faster to process than Python lists.
C. NumPy arrays can store elements of different data types, while Python lists can only store elements of the same
data type.
D. All of the above

Correct Response: 4

Explanation: NumPy arrays are more memory efficient, faster to process, and can store elements of different data types
compared to Python lists.

WHAT IS THE DIFFERENCE BETWEEN A FUNCTION DECORATED WITH


@STATICMETHOD AND ONE DECORATED WITH @classmethod?
A. A staticmethod is a method that belongs to the class and not the instance of the class.
B. A classmethod is a method that belongs to the class and not the instance of the class.
C. A classmethod is a method that belongs to the instance of the class and not the class.
D. A staticmethod is a method that belongs to the instance of the class and not the class.

Correct Response: 2

Explanation: A staticmethod is a method that belongs to the class and not the instance of the class, whereas a classmethod
is a method that belongs to the class and takes the class itself as the first argument instead of an instance.

HOW IS DIFFERENTIAL EVOLUTION DIFFERENT FROM GENETIC ALGORITHMS?


A. Differential Evolution is a population-based optimization algorithm, whereas Genetic Algorithms are individual-
based optimization algorithms.
B. Differential Evolution is an individual-based optimization algorithm, whereas Genetic Algorithms are population-
based optimization algorithms.
C. Differential Evolution is a deterministic optimization algorithm, whereas Genetic Algorithms are probabilistic
optimization algorithms.
D. Differential Evolution is a probabilistic optimization algorithm, whereas Genetic Algorithms are deterministic
optimization algorithms.

Correct Response: 1

Explanation: Differential Evolution is a population-based optimization algorithm, whereas Genetic Algorithms are
individual-based optimization algorithms.

WWW.CDPNETWORKS.COM | [email protected] PAGE 52 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW TO DETERMINE K USING THE SILHOUETTE METHOD?


A. The optimal value of k is the one that maximizes the average silhouette score of all samples.
B. The optimal value of k is the one that minimizes the average silhouette score of all samples.
C. The optimal value of k is the one that minimizes the sum of all silhouette scores of all samples.
D. The optimal value of k is the one that maximizes the sum of all silhouette scores of all samples.

Correct Response: 1

Explanation: The optimal value of k is the one that maximizes the average silhouette score of all samples. The silhouette
score measures how similar an object is to its own cluster compared to other clusters.

WHY WOULD YOU USE METACLASSES?


A. To modify the behavior of a class at runtime.
B. To modify the behavior of an object at runtime.
C. To modify the behavior of a module at runtime.
D. To modify the behavior of a function at runtime.

Correct Response: 1

Explanation: Metaclasses are classes that define the behavior of other classes. They allow you to modify the behavior of a
class by defining its attributes, methods, and inheritance.

DESCRIBE PYTHON'S GARBAGE COLLECTION MECHANISM IN BRIEF


A. Python uses a reference counting system to determine when to collect an object's memory.
B. Python uses a mark-and-sweep algorithm to determine when to collect an object's memory.
C. Python uses a region-based memory management system to determine when to collect an object's memory.
D. Python uses a heap-based memory management system to determine when to collect an object's memory.

Correct Response: 1

Explanation: Python uses a reference counting system to determine when to collect an object's memory. When the
reference count of an object reaches zero, it is eligible for garbage collection. The garbage collector frees the memory
occupied by the object.

WWW.CDPNETWORKS.COM | [email protected] PAGE 53 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

IS THERE A SIMPLE, ELEGANT WAY TO DEFINE SINGLETONS?


A. Yes, using class methods
B. Yes, using decorators
C. No, it is not possible in Python
D. No, it is possible but not simple and elegant

Correct Response: 1

Explanation: Singletons can be defined using class methods or decorators in Python

WHAT IS THE DIFFERENCE BETWEEN "SORT" AND "SORTED" IN PYTHON?


A. "sort" modifies the list in-place, while "sorted" returns a new sorted list.
B. "sort" and "sorted" are interchangeable in Python.
C. "sort" sorts elements in ascending order, while "sorted" sorts elements in descending order.
D. "sorted" modifies the list in-place, while "sort" returns a new sorted list.

Correct Response: 1

Explanation: "sort" modifies the list in-place, while "sorted" returns a new sorted list.

WHAT IS THE DIFFERENCE BETWEEN "DEL" AND "REMOVE" IN PYTHON LISTS?


A. "del" deletes elements from a list by index, while "remove" deletes elements from a list by value.
B. "del" and "remove" are interchangeable in Python.
C. "del" deletes elements from a list by value, while "remove" deletes elements from a list by index.
D. "remove" modifies the list in-place, while "del" returns a new list without the deleted elements.

Correct Response: 1

Explanation: "del" deletes elements from a list by index, while "remove" deletes elements from a list by value.

HOW WILL YOU CONVERT A STRING TO A SET IN PYTHON?


A. str = "hello"; set(str)
B. str = "hello"; tuple(str)
C. str = "hello"; list(str)
D. str = "hello"; dict(str)

Correct Response: 1

Explanation: The built-in set() function can be used to convert a string to a set in Python.

WWW.CDPNETWORKS.COM | [email protected] PAGE 54 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CREATE A DICTIONARY USING TUPLES IN PYTHON?


A. dict(("a", 1),("b", 2))
B. dict(("a", 1), 2)
C. dict((1, "a"),(2, "b"))
D. dict(("a", 1),("b", 2),(3, "c"))

Correct Response: 1

Explanation: The built-in dict() function can take a list of tuples as an argument, where each tuple contains a key-value pair,
to create a dictionary.

HOW WILL YOU CONVERT A STRING TO A FROZEN SET IN PYTHON?


A. str = "hello"; frozenset(str)
B. str = "hello"; set(str)
C. str = "hello"; list(str)
D. str = "hello"; dict(str)

Correct Response: 1

Explanation: The built-in frozenset() function can be used to convert a string to a frozen set in Python.

HOW WILL YOU CONVERT AN INTEGER TO A CHARACTER IN PYTHON?


A. chr(97)
B. ord(97)
C. str(97)
D. hex(97)

Correct Response: 1

Explanation: The built-in chr() function can be used to convert an integer to its corresponding Unicode character in Python.

HOW WILL YOU CONVERT AN INTEGER TO AN UNICODE CHARACTER IN


A. unichr(97)
B. chr(97)
C. ord(97)
D. hex(97)

Correct Response: 1

Explanation: The built-in unichr() function can be used to convert an integer to its corresponding Unicode character in
Python (Python 2.x). In Python 3.x, the chr() function can be used instead.

WWW.CDPNETWORKS.COM | [email protected] PAGE 55 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU CONVERT A SINGLE CHARACTER TO ITS INTEGER VALUE IN


A. ord(char)
B. int(char)
C. hex(char)
D. oct(char)

Correct Response: 1

Explanation: The ord() function returns an integer representing the Unicode character.

HOW WILL YOU CONVERT AN INTEGER TO HEXADECIMAL STRING IN


A. hex(int)
B. int(hex)
C. oct(int)
D. str(int)

Correct Response: 1

Explanation: The hex() function returns a hexadecimal string representation of an integer.

HOW WILL YOU CONVERT AN INTEGER TO OCTAL STRING IN PYTHON?


A. oct(int)
B. int(oct)
C. hex(int)
D. str(int)

Correct Response: 1

Explanation: The oct() function returns an octal string representation of an integer.

WHAT IS THE PURPOSE OF ** OPERATOR?


A. Exponentiation
B. Division
C. Addition
D. Subtraction

Correct Response: 1

Explanation: The ** operator returns the result of raising the first operand to the power of the second operand.

WWW.CDPNETWORKS.COM | [email protected] PAGE 56 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE PURPOSE OF // OPERATOR?


A. Floor Division
B. Division
C. Addition
D. Subtraction

Correct Response: 1

Explanation: The // operator returns the result of floor division of two operands, i.e., it returns the largest integer that is
smaller than or equal to the result of division.

HOW WILL YOU COMPARE TWO LISTS?


A. Using the == operator
B. Using the is operator
C. Using the != operator
D. Using the "in" operator

Correct Response: 1

Explanation: The == operator can be used to compare two lists and return True if both lists have the same elements in the
same order, and False otherwise.

HOW WILL YOU GET THE LENGTH OF A LIST?


A. Using the len function
B. Using the size method
C. Using the count method
D. Using the length property

Correct Response: 1

Explanation: The len function can be used to get the number of elements in a list.

HOW WILL YOU GET THE MAX VALUED ITEM OF A LIST?


A. Using the max function
B. Using the min function
C. Using the sort method
D. Using the reverse method

Correct Response: 1

Explanation: The max function can be used to get the maximum valued item in a list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 57 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET THE MIN VALUED ITEM OF A LIST?


A. Using the min function
B. Using the max function
C. Using the sort method
D. Using the reverse method

Correct Response: 1

Explanation: The min function can be used to get the minimum valued item in a list.

HOW WILL YOU GET THE INDEX OF AN OBJECT IN A LIST?


A. Using the index method
B. Using the count method
C. Using the find method
D. Using the search method

Correct Response: 1

Explanation: The index method can be used to get the index of an object in a list.

HOW WILL YOU COMPARE TWO LISTS?


A. == operator
B. is operator
C. != operator
D. not operator

Correct Response: 1

Explanation: == operator can be used to compare two lists, as it returns True if both lists have same elements in same
order, otherwise returns False.

HOW WILL YOU GET THE LENGTH OF A LIST?


A. len(list)
B. list.length()
C. list.len()
D. list.size()

Correct Response: 1

Explanation: len(list) returns the number of elements in the list.

WWW.CDPNETWORKS.COM | [email protected] PAGE 58 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

HOW WILL YOU GET THE MAX VALUED ITEM OF A LIST?


A. max(list)
B. list.max()
C. list.maximum()
D. list.top()

Correct Response: 1

Explanation: max(list) returns the maximum valued item in the list.

HOW WILL YOU GET THE MIN VALUED ITEM OF A LIST?


A. min(list)
B. list.min()
C. list.minimum()
D. list.bottom()

Correct Response: 1

Explanation: min(list) returns the minimum valued item in the list.

HOW WILL YOU GET THE INDEX OF AN OBJECT IN A LIST?


A. list.index(object)
B. index(object, list)
C. list.find(object)
D. find(object, list)

Correct Response: 1

Explanation: list.index(object) returns the index of the first occurrence of the object in the list.

WHY WOULD YOU USE THE PASS STATEMENT?


A. As a placeholder for code that has not been written yet.
B. To skip a block of code.
C. To create an empty function.
D. To create an infinite loop.

Correct Response: 1

Explanation: The pass statement is used as a placeholder for code that has not been written yet, and it does nothing.

WWW.CDPNETWORKS.COM | [email protected] PAGE 59 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT ARE DECORATORS IN PYTHON?


A. A way to modify or extend the behavior of a function or class without changing its source code.
B. A way to define functions.
C. A way to define classes.
D. A way to define variables.

Correct Response: 1

Explanation: Decorators are a way to modify or extend the behavior of a function or class without changing its source code.

IS THERE A TOOL TO HELP FIND BUGS OR PERFORM STATIC ANALYSIS?


A. Yes, there are tools such as PyLint, Flake8, and mypy.
B. No, there is no such tool available.
C. Yes, there is a tool called PyChecker.
D. No, only manual code review is possible.

Correct Response: 1

Explanation: Yes, there are tools such as PyLint, Flake8, and mypy that can help find bugs or perform static analysis in
Python.

WHAT IS MONKEY PATCHING AND IS IT EVER A GOOD IDEA?


A. Monkey patching is a way to change the behavior of a class or module at runtime, and it is a bad idea because it can
lead to unexpected results.
B. Monkey patching is a way to change the behavior of a class or module at runtime, and it can be a good idea in
certain situations.
C. Monkey patching is a way to change the behavior of a function or class at compile time, and it is a good idea.
D. Monkey patching is a way to change the behavior of a function or class at compile time, and it is a bad idea.

Correct Response: 2

Explanation: Monkey patching is a way to change the behavior of a class or module at runtime, and it can be a good idea in
certain situations, but it should be used with caution because it can lead to unexpected results.

WWW.CDPNETWORKS.COM | [email protected] PAGE 60 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

EXPLAIN THE UNBOUNDLOCALERROR EXCEPTION AND HOW TO AVOID IT?


A. Occurs when a local variable is referenced before it is assigned a value.
B. Raised when a global variable is referenced before it is assigned a value.
C. Raised when a function does not return a value.
D. Raised when a value is not found in a list or dictionary.

Correct Response: 1

Explanation: UnboundLocalError is raised when a local variable is referenced before it has been assigned a value. This can
be avoided by either initializing the variable with a value before it is used or by using the global keyword to specify that the
variable is a global variable.

WHAT ARE IMMUTABLE OBJECTS IN PYTHON?


A. Objects that can be changed once created.
B. Objects that cannot be changed once created.
C. Objects that can be changed and then set back to their original value.
D. Objects that can be changed by reference.

Correct Response: 2

Explanation: Immutable objects in Python are objects that cannot be changed once they have been created. Examples
include numbers, strings, and tuples.

WHAT IS THE DIFFERENCE BETWEEN RANGE AND XRANGE FUNCTIONS IN PYTHON?


A. range returns a list, while xrange returns an object.
B. range is faster than xrange.
C. xrange is only available in Python 2, while range is available in both Python 2 and Python 3.
D. There is no difference between the two functions.

Correct Response: 3

Explanation: The range function returns a list, while the xrange function returns an object. In Python 2, xrange was used to
save memory, as it generates the numbers in the specified range on the fly, rather than creating a whole list of numbers
upfront. In Python 3, range and xrange have been combined into a single range function, which behaves like the xrange
function in Python 2.

WWW.CDPNETWORKS.COM | [email protected] PAGE 61 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS A NONE VALUE?


A. A value that represents the absence of a value.
B. A value that represents the presence of a value.
C. A value that represents the value 0.
D. A value that represents the value 1.

Correct Response: 1

Explanation: The None value in Python is a value that represents the absence of a value. It is equivalent to NULL in other
programming languages.

WHAT IS PICKLING AND UNPICKLING?


A. The process of converting an object hierarchy into a byte stream.
B. The process of converting a byte stream into an object hierarchy.
C. The process of converting a string into an object.
D. The process of converting an object into a string.

Correct Response: 1

Explanation: Pickling is the process of converting an object hierarchy (such as a Python data structure) into a byte stream.
Unpickling is the process of converting the byte stream back into an object hierarchy. The pickle module in Python provides
functions for performing these operations.

WHAT ARE THE ADVANTAGES OF NUMPY OVER REGULAR PYTHON LISTS?


A. NumPy arrays are more memory efficient than Python lists.
B. NumPy arrays are faster to process than Python lists.
C. NumPy arrays can store elements of different data types, while Python lists can only store elements of the same
data type.
D. All of the above

Correct Response: 4

Explanation: NumPy arrays are more memory efficient, faster to process, and can store elements of different data types
compared to Python lists.

WWW.CDPNETWORKS.COM | [email protected] PAGE 62 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHAT IS THE DIFFERENCE BETWEEN A FUNCTION DECORATED WITH


@STATICMETHOD AND ONE DECORATED WITH @classmethod?
A. A staticmethod is a method that belongs to the class and not the instance of the class.
B. A classmethod is a method that belongs to the class and not the instance of the class.
C. A classmethod is a method that belongs to the instance of the class and not the class.
D. A staticmethod is a method that belongs to the instance of the class and not the class.

Correct Response: 2

Explanation: A staticmethod is a method that belongs to the class and not the instance of the class, whereas a classmethod
is a method that belongs to the class and takes the class itself as the first argument instead of an instance.

HOW IS DIFFERENTIAL EVOLUTION DIFFERENT FROM GENETIC ALGORITHMS?


A. Differential Evolution is a population-based optimization algorithm, whereas Genetic Algorithms are individual-
based optimization algorithms.
B. Differential Evolution is an individual-based optimization algorithm, whereas Genetic Algorithms are population-
based optimization algorithms.
C. Differential Evolution is a deterministic optimization algorithm, whereas Genetic Algorithms are probabilistic
optimization algorithms.
D. Differential Evolution is a probabilistic optimization algorithm, whereas Genetic Algorithms are deterministic
optimization algorithms.

Correct Response: 1

Explanation: Differential Evolution is a population-based optimization algorithm, whereas Genetic Algorithms are
individual-based optimization algorithms.

HOW TO DETERMINE K USING THE SILHOUETTE METHOD?


A. The optimal value of k is the one that maximizes the average silhouette score of all samples.
B. The optimal value of k is the one that minimizes the average silhouette score of all samples.
C. The optimal value of k is the one that minimizes the sum of all silhouette scores of all samples.
D. The optimal value of k is the one that maximizes the sum of all silhouette scores of all samples.

Correct Response: 1

Explanation: The optimal value of k is the one that maximizes the average silhouette score of all samples. The silhouette
score measures how similar an object is to its own cluster compared to other clusters.

WWW.CDPNETWORKS.COM | [email protected] PAGE 63 OF 64


Python Interview Companion: 190 Inquiries and
Corresponding Answers
20/2/2024

WHY WOULD YOU USE METACLASSES?


A. To modify the behavior of a class at runtime.
B. To modify the behavior of an object at runtime.
C. To modify the behavior of a module at runtime.
D. To modify the behavior of a function at runtime.

Correct Response: 1

Explanation: Metaclasses are classes that define the behavior of other classes. They allow you to modify the behavior of a
class by defining its attributes, methods, and inheritance.

DESCRIBE PYTHON'S GARBAGE COLLECTION MECHANISM IN BRIEF


A. Python uses a reference counting system to determine when to collect an object's memory.
B. Python uses a mark-and-sweep algorithm to determine when to collect an object's memory.
C. Python uses a region-based memory management system to determine when to collect an object's memory.
D. Python uses a heap-based memory management system to determine when to collect an object's memory.

Correct Response: 1

Explanation: Python uses a reference counting system to determine when to collect an object's memory. When the
reference count of an object reaches zero, it is eligible for garbage collection. The garbage collector frees the memory
occupied by the object.

IS THERE A SIMPLE, ELEGANT WAY TO DEFINE SINGLETONS?


A. Yes, using class methods
B. Yes, using decorators
C. No, it is not possible in Python
D. No, it is possible but not simple and elegant

Correct Response: 1

Explanation: Singletons can be defined using class methods or decorators in Python

WWW.CDPNETWORKS.COM | [email protected] PAGE 64 OF 64

You might also like