Back to Must-Know Dashboard

Python

Must-Know Questions & Answers

Introduction to Python#1

Identify which of the following is an interpreted language.

A
Pascal
B
C
C
Python
D
C++

Pascal, C and C++ are all compiled languages. Python is an interpreted language — the official implementation uses byte code interpretation.

Introduction to Python#2

Python syntax is case-sensitive. State true or false.

A
True
B
False

Case sensitivity means that 'x' is different from 'X', and 'John' is different from 'john'. Python syntax is case-sensitive.

Introduction to Python#3

Select the command to be used to run the Python script file named t.py.

A
go python t.py
B
run python t.py
C
execute python t.py
D
python t.py

To run Python files from the command prompt, set the path of Python and run the file using: python filename.py

Introduction to Python#4

Select the method that can be used to create a directory in Python.

A
os.mkdir()
B
os.make_dir()
C
os.create_dir()
D
os.creat_dir()

Python's OS module provides os.mkdir(path) to create a directory. It creates a directory at the specified path.

Introduction to Python#5

Name the creator of Python.

A
Steve Jobs
B
Guido van Rossum
C
James Gosling
D
Bill Gates

Guido van Rossum is the creator of Python. Python was conceived in the late 1980s and its implementation was started in December 1989.

Introduction to Python#6

In Python, who detects the syntax error and when?

A
interpreter/at compile time
B
compiler/at runtime
C
compiler/at compile time
D
interpreter/at runtime

Syntax errors arise when the Python parser is unable to understand a line of code. They are reported by the interpreter at runtime.

Introduction to Python#7

Identify the symbol that a Python single-line comment begins with.

A
#
B
//
C
/*
D
$$

Single-line comments in Python begin with the hash (#) character and are automatically terminated by the end of that line.

Introduction to Python#8

Identify the style that Python paragraph (multi-line) comments use.

A
// comments //
B
''' comments '''
C
/* comments */
D
# comments #

For writing Python paragraph comments, you may use triple-quoted strings (''' or """). Add the triple-quoted string at the beginning and end of the comment block.

Introduction to Python#9

Select the command to be used to start Python from the command prompt.

A
python
B
execute python
C
go python
D
run python

The command 'python' must be used to start the Python interactive interpreter from the command prompt.

Introduction to Python#10

Select the correct code snippet: A: print("Programming is fun") print("Python is fun") [extra indentation on second line] B: print("Programming is fun") print("Python is fun") C: print("Programming is fun) print("Python is fun") D: print("Programming is fun") print("Python is fun)

A
A
B
B
C
C
D
D

Option A has wrong indentation (Python is sensitive to indentation). Options C and D have missing closing quotation marks. Option B is syntactically correct.

Control Structures#11

Define what kind of language Python is.

A
Compiled
B
Outsourced
C
Interpreted
D
Compiled and Interpreted

Interpreted/compiled is a property of the implementation, not the language itself. The official Python implementation uses byte code interpretation, so Python is called an interpreted language.

Control Structures#12

Object Oriented Programming is possible in Python. State whether True or False.

A
True
B
False

OOP is a paradigm where logic is coded in terms of Classes and Objects. Python supports OOP, as well as Procedural and Functional programming styles.

Control Structures#13

Identify the correct operator for exponentiation (x to the power y).

A
x^^y
B
x**y
C
x^y
D
pow(x,y)

The ** operator in Python raises the number on the left to the power of the exponent on the right. E.g., 5**3 = 125. Note: pow(x,y) also works but x**y is the operator form.

Control Structures#14

Identify which one of these is the floor division operator in Python.

A
/*
B
//
C
/
D
%

In Python 3, the / operator returns the quotient with decimals, while the floor division operator // returns the quotient after truncating the decimal portion.

Control Structures#15

Mathematical operations can be performed on a string. State whether True or False.

A
True
B
False

Mathematical operations (such as addition, subtraction, multiplication of values) cannot be performed on strings. Attempting to do so raises a TypeError.

Control Structures#16

Analyse and find which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100: A: for i in range(1, 99): sum += i / (i + 1) B: for i in range(1, 100): sum += i / (i + 1) C: for i in range(1.0, 99.0): sum += i / (i + 1) D: for i in range(1.0, 100.0): sum += i / (i + 1)

A
CDE
B
BCD
C
ABCD
D
B
E
CD

Python range() does not support float type, so C and D are invalid. Option A is wrong because i goes from 1 to 98 (last fraction is 98/99, not 99/100). Only Option B is correct.

Control Structures#17

Which single line of code can replace this block? nums = [1,2,3,4,5,6,7,8,9] x = 0 for n in nums: x = x + n print(x)

A
print(sum(range(1,10)))
B
print(sum(1 to 10))
C
print(sum(range(0,9)))
D
print(sum(range(1,9)))

The code computes the sum of 1 through 9. range(1, 10) generates 1 to 9 inclusive. print(sum(range(1,10))) is the correct one-liner.

Control Structures#18

Select the choice that should NOT appear in the place of EXP in: for k in EXP:

A
a dictionary
B
a range of float
C
a range of int
D
a string
E
a list

Lists, dictionaries, strings, and integer ranges are all valid iterables in a for loop. However, Python's range() does not accept floats, so a range of float is illegal.

Control Structures#19

Which statement best describes the behavior of: x != 0 and y % x != 0

A
It executes successfully for all values of x
B
It causes a run-time error if x is not 0
C
It contains a type error
D
It causes a run-time error if x is 0
E
It contains a syntax error

Due to short-circuit evaluation, if x == 0, the first condition (x != 0) is False and the second part (y % x) is never evaluated, preventing ZeroDivisionError. The expression is valid for all values of x.

Control Structures#20

Analyse and predict the output: isCorrect = False print("Correct" if isCorrect else "Incorrect")

A
Nothing
B
Correct
C
Correct Incorrect
D
Incorrect

The ternary expression prints "Correct" only if isCorrect is True. Since isCorrect is False, the output is "Incorrect".

Key Topics to Study

Based on our question bank analysis, master these concepts to score high in Python.

Preparation Tip

"Focus on understanding the logic behind pseudocode loops and selection statements, as they form the bulk of technical assessments."