0
1
2
N
Classes
Functions
Objects only
Namespaces
i > j
i < j
i ≤ j
i = j
Uses minimum symbols
Has only one loop
Produces correct output for all valid inputs
Uses standard variable names
What does the following code do?
from datetime import datetime dt = datetime.strptime("2024-11-22", "%Y-%m-%d") print(dt)
Returns the current date
Converts the string "2024-11-22" to a datetime object
Returns the string "2024-11-22"
Raises a ValueError
What is the output of following?
def add(a, b=5): return a + b print(add(3))
3
5
8
Error
print(input() + input())
print(int(input()) + int(input()))
print(input + input)
print(eval(input))
Declared outside all functions
Declared in loop
Declared using global
Declared inside a function
Debugging is the process of locating and fixing errors
Debugging removes syntax errors only
Debugging is done before testing
Debugging increases algorithm complexity
What will be printed?
x = 10 def f(): x = 20 print(x) f() print(x)
10 10
20 20
20 10
Input/Output operation
Processing step
Decision making
Loop termination
Testing and debugging are identical processes
Testing guarantees error-free programs
Debugging must be completed before testing
Testing reveals defects, while debugging locates and removes them
import math
from math import pi
include math
using math
f = open("test.txt", "r") f.seek(0) print(f.tell())
File size
Bytecode execution
Syntax analysis
Output generation
Runtime evaluation
for i in range(3): pass print(i)
What is printed?
i = 1 while i < 4: print(i, end=" ") i += 1 else: print("Done")
1 2 3
Infinite loop
Done
1 2 3 Done
import math as m
from math import sqrt
load math
a > b
b == 0
while (b != 0)
a == b
Indexing
Slicing
Concatenation
Element assignment
List
Dictionary
Set
Tuple
Tuple of array dimensions
Total elements
Data type
Memory size
What does the following slice return?
s = [0,1,2,3,4,5] print(s[1:5:2])
[1, 3]
[2, 4]
[1, 2, 3, 4]
[0, 2, 4]
int
float
Boolean
String
What will be the output of the following code?
for i in range(1,5): if i == 3: break print(i, end=" ")
1 2 3 4
1 2 4
1 2
i = 0
while i < 3: i += 1 if i == 2: continue print(i, end=" ") else: print("END")
1 2 3 END
1 3
1 3 END
Finiteness
Effectiveness
Definiteness
Generality
from math import * print(cos(0))
Prints 1.0
Raises a NameError
Prints cos(0)
Global
Enclosing
Built-in
Local
def test(): return print(test())
None
Blank
strip()
lstrip()
rstrip()
trim()
<class 'int'>
<class 'float'>
<class 'complex'>
TypeError
argv[]
sys.argv
input()
args[]
At function definition
At runtime always
At function call
Never
Looping
Function calling another function
Function calling itself
Infinite execution
for i in range(5): if i == 3: continue print(i, end=" ")
0 1 2 3 4
0 1 2 4
0 1 2
File name
Number of lines
Current file pointer position
A function
A package
A class
A file containing Python code
10
True
11
Error is raised
File is appended
File is read-only
File is overwritten
def f(a, b): print(a, b) f(b=2, a=1)
2 1
f = open("data.txt", "r") print(f.readline())
One line from the file
Entire file
List of lines
{1, 2, 3}
[1, 2, 3]
{}
(1, 2, 3)
Compilation/interpretation phase
Logical analysis
Program execution only
Documentation phase
Data is written successfully
File content is overwritten
Raises an exception
Nothing happens
Correctness and finiteness only
Finiteness and efficiency only
Correctness, finiteness, and clarity
Correctness and hardware dependency
fun
u
Either f, u, or n
Row 1
Column 2
Element at row 1, column 2
Entire second row
file()
open()
fopen()
load()
print(round(4.567, 2))
4.56
4.57
4.5
writelines()
write()
readlines()
append()
np.create()
np.array()
np.ndarray()
np.makearray()
Compiled, low-level language
Purely interpreted, machine-dependent language
High-level, interpreted language with dynamic typing
Assembly-level procedural language
extern
static
global
nonlocal
Use of multiple terminators
Absence of comments
Use of rectangles instead of parallelograms
A decision block with paths that do not reconverge logically
x = 5 def f(): global x x = 10 f() print(x)
Converts string to int
Evaluates Python expression
Prints expression
Converts to float
==
is
has
in
Dictionary keys must be mutable
Dictionary values must be immutable
Dictionary reserves insertion order only in Python 2
Dictionary keys must be unique
3.333
4
3.0
w+
a
x
r+
2 rows, 3 columns of ones
3 rows, 2 columns of ones
6×6 matrix
1D array
Adjacent indices
First and last, second and second-last, etc.
Random indices
Even indices only
itemsize
nbytes
memory
bytesize
Output of the code:
t = (10, 20, 30) t += (40,) print(t)
(10, 20, 30)
(10, 20, 30, 40)
(40,)
What will be the output?
print(type(()))
<class 'set'>
<class 'dict'>
<class 'tuple'>
Decision-based processing
Sequential processing
Iterative processing with variable update
Randomized processing
What is the output?
s = "Python" print(s[-1:-4])
hon
hon in reverse
IndexError
Empty string
Index of 'a'
Frequency of 'a'
.py
.mod
.exe
.txt
lst = [1, 2, 3, 4] print(lst[::-1])
[4, 3, 2, 1]
Ignored
Compiled only
Executed once
Executed every time
while
do-while
for
if
Syntax error
Logical error
Compilation error
Lifetime of an object
Region of code where a name is accessible
Size of a variable
Memory address
list
tuple
dictionary
ndarray
x = 5 y = 10 print(x > 3 or y < 5 and x == 5)
False
1 to n
2 to n-1
1 to √n
2 to √n
string
depends on user input
What will be the output of the code given below?
import math as m print(m.pi)
3.141592654
math.pi
Python does not allow runtime errors
Python checks all errors at compile time
Python executes code line by line and reports errors immediately
Python programs are always shorter
What is the output of following code?
x = 1 y = 2 z = 3 print(x < y < z == True)
a = [1, 2, 3] b = a b.append(4) print(a)
[4]
pass
continue
break
assert
[1,3,5,7,9]
[1,2,3,4,5]
[2,4,6,8]
Enclosing function scope
Built-in scope
Global scope
Local scope
Reads one line
Reads all lines into a list
Reads entire file as a string
Reads one character
Counter
Factorial value
Input number
Loop limit
Iteration and sequential arithmetic
Recursion
Decision-only processing
Sorting
{1:'a', 2:'b'}
{2:'b'}
{1:'c', 2:'b'}
astype()
convert()
change()
typecast()
Sorts array
Reverses array
Deletes elements
Returns copy only
A is preferable because it terminates
B is preferable because correctness is fundamental
Both satisfy algorithm correctness
Both violate essential properties of algorithms
s[1:4]
s[1:3]
s[2:5]
s[1:5:2]
float64
int32
int64
object
Terminates loop
Skips iteration
Prints message
Raises error if condition is False
Even numbers
Odd numbers from 1 to 9
Odd numbers from 1 to 10