Skip to main content

Book review: 390+ Python Interview Questions and Answers

I downloaded a preview portion of 390+ Python MCQs from Anazon, thinking reading through it would help me advance my Python skills beyond what I have learned from Harvard’s online CS50P (Python) course. I’m an experienced program looking to add a new skill to my repertoire, and while the course covered many significant aspects of Python programming, there are many other details to perfect, such as best practices, developing packages, and so on.


The book is written by Manish Dnyandeo Salunke, who claims 15 years experience in IT,  but it is not clear who published it. It is obvious no one edited it, or verified the correctness of the questions, answers and explanations.


Amazon allowed me to download a sample of (I think) 57 questions. Roughly half of these were wrong, and some of the others struck me as irrelevant. The maximum allowed length for an identifier, apparently, is 79 characters. Anything over 20 characters should be considered unusual, so sufficient to say the limit is several times any useful size.


Here are my complaints … I wonder how many of the remaining 330+ questions are also wrong?


Question: What is the main difference between a .py file and a .pyc file?

  1. .py files are source code files

  2. .py files are bytecode files

  3. .py files are compiled Python files

  4. Py files are compressed archive files


(in)Correct Response: 2 SHOULD BE ‘1’

Explanation: .py files contain Python source code, while .pyc files contain bytecode


Explanation is correct but response doesn’t match the options.


Question: The process of converting source code into bytecode, which is then executed by the Python interpreter, is called ……

  1. Compilation

  2. Interpretation

  3. Compilation

  4. Execution

(in)Correct Response: 2 SHOULD BE ‘1’ (or ‘3’, since the answer is shown twice)

(their)Explanation: Python uses interpretation, not compilation, to execute code.


Actually, executing the code is achieved by interpreting the bytecode. The bytecode is obtained by compiling the source code. Ask google about Java bytecode, Python bytecode.


Question: A colleague is trying to run a Python 2 script in a Python 3 environment, and the script fails due to a syntax error in the print statement. What would be the likely cause?

  1. Python 2 uses print as a statement

  2. Python 2 uses print as a function

  3. Python 2 has a different file encoding

  4. Python 2 requires a ‘__future__’ import

(in)Correct Response: 2 SHOULD BE ‘1’

(their)Explanation: …Python 2 uses print as a statement, while Python 3 uses it as a function …


The explanation is correct but the response doesn’t match the options.


Question: What will be the result of the comparison 5!=6 in Python?

  1. TRUE

  2. FALSE

  3. 1

  4. Error

Correct Response: 1

Explanation: … the result is ‘True’

Complaint: Some languages use TRUE and FALSE - ALL CAPS - Python uses a capitalised first letter only. The right version is used in the explanation, but not shown in the options.


Silly questions about the logical operators to return True only if both values are True / if one value is True, and similar questions about comparison operators. Anyone who does not know such things needs to be looking for a book/course to train them in beginner Python, rather than in preparing them for interviews.


Question: Consider a scenario where you need to check if neither ‘a’ nor ‘b’ are true in Python. Which operator would you use?

  1. not a and not b

  2. A and b

  3. A or b

  4. not(a or b)

(in)Correct Response: 1

While 1 is a correct answer, so is 4, by deMorgan’s theorem. Having two correct answers is a minor error; listing only one of them as valid is a sin against the trusting consumer.


Question: What is the result of True or False and True?

  1. TRUE

  2. FALSE

  3. True and False is not allowed

  4. Error

(in)Correct Response: 2

(their nonsense) Explanation: The operation ‘True or False and True’ will result in an error because ‘and’ has a higher precedence than ‘or’, and ‘and’ is trying to combine a Boolean ‘False’ with a non-Boolean ‘True’

If their explanation were correct, the answer should be ‘4’. ‘2’ is also wrong. The correct answer is ‘1’. ‘And’ does have  higher precedence, so False and True evaluate to False. Then True or False evaluate to True.The


Question: In the context of logical operators in Python, the not operator _______ the result.

  1. Inverts

  2. Negates

  3. Reverses

  4. Complements

(their) Correct Response: 1

But ‘negates’ and ‘complements’ are also correct answers.


Question: The ______ operator is used to determine if two values are not equal in Python.

  1. Unequal

  2. Not equal

  3. Inequality

  4. Difference

(Their) Correct response: 2

Answers ‘1’, ‘2’ and ‘3’ are essentially the same thing, but none of them is really the correct answer, which should be ‘!=’.


Question: A developer wants to compare two numbers and check if they are approximately equal ( with a small threshold…)

  1. a ==b

  2. abs(a-b) < epsilon

  3. a is b

  4. a !=b

(their) Response: 2

While correct, epsilon is unavailable without preparation. It requires importing the sys module.


Question: Given a scenario where a system should notify a user if the storage space goes below  5% or above 95% ….

  1. If storage < 5 or storage > 95:

  2. If storage <= 5 or storage >= 95:

  3. If storage > 5 and storage < 95:

  4. If 5 <= storage <= 95:

(their) Response: 2

The answer should be ‘1’. The requirements accept 5% and 95% as safe conditions, only outside those values is a problem.


Question: What keyword is used in Python to make a decision?

  1. if

  2. else

  3. decide

  4. choice

This question is apparently used to determine whether the applicant attended Python classes after the first day.


Question: For loops in Python use the _____ method internally to iterate over items.

  1. Loop

  2. Enumerate

  3. Range

  4. Iterate

(their) Response: 2

(their) Explanation: For loops in pYthon use the enumerate method internally to iterate over items. Enumerate returns both the index and the value of each item in the sequence, making it useful for tracking positions in the loop.

The answer should be ‘4’, or even better, ‘__iter__’. The question is about “internally”, while ‘enumerate’, while convenient, is used sometimes but not always at the user code level.


Question: You have a list of numbers and you want to compute the sum of all positive numbers only. Which control structure can be most beneficial to achieve this?

  1. For loop

  2. While loop

  3. Conditional statement

  4. List comprehension

(their) Response: 3

(their) Explanation: …a conditional statement can be most beneficial. You can iterate  through the list…

You might as easily say that a loop ( for OR while ) is most beneficial. Within the loop check for positive numbers …. Actually, my choice would have been the list comprehension, since it combines everything into a compact assembly. Of cour there needs to be an ‘if’ clause … Essentially, the  question is meaningless.


Comments

Popular posts from this blog

Perl5, Moxie and Enumurated Data Types

Moxie - a new object system for Perl5 Stevan Little created the Moose multiverse to upgrade the Perl 5 programming language's object-oriented system more in line with the wonderfull world of Perl 6. Unfortunately, it's grown into a bloated giant, which has inspired light-weight alternatives Moos, Moo, Mo, and others. Now he's trying to create a modern, efficient OO system that can become built into the language. I've seen a few of his presentations at YAPC (Yet Another Perl Conference, now known as TPC, The Perl Conference), among them ‎p5 mop final final v5 this is the last one i promise tar gz While the package provides some POD documentation about the main module, Moxie, it doesn't actually explain the enum package, Moxie::Enum. But delving into the tests directory reveals its secrets. Creating an Enum package Ranks { use Moxie::Enum; enum by_ARRAY => qw( unused 2 3 4 5 6 7 8 9 10 J Q K A ); enum by_HASH => { 2 => 2, 3 =...

BASH Matrix Multiplication

tl;dr Bash is not the language for math-intensive operations. REPS=$1; FILE_1=$2; FILE_2=$3 OUTFILENAME=$4; readonly COLS=`head -1 $FILE_1 | wc -w`; readonly ROWS=`cat $FILE_1 | wc -l`; # echo "rows is $ROWS; cols is $COLS" if [[ $ROWS != $COLS ]]; then echo "Expecting square matrices, " \ "but rows = $ROWS, cols = $COLS\n"; exit 1; fi # -------------------------------------------------- # SUBROUTINES # function outputMatrix() { local matrixname=$1; local matrix; local elem; echo "matrix is '$matrixname'."; eval matrix=\( \${${matrixname}[@]} \); local i=0; for elem in "${matrix[@]}"; do echo -n "$elem "; if (( ++i == $COLS )); then echo ''; i=0; fi done } function multiply() { declare -a product; local M=$1 N=$2; local i j k idx1 idx2 idx3; for ((i=0; i < $ROWS; i++ )); do for ((j=0; j<$COLS; j++)); do ...