Python McQs

Q.1 What is the purpose of an if statement in Python?

A. To loop through a sequence
B. To execute a block conditionally
C. To define a function
D. To handle exceptions

Shw me The Right Answer

Answer . B 

Q.2 Find the mistake:

user_input = input(“Enter a number: “);
print(“You entered: “, int(user_input))

A. Syntax error
B. No error
C. int() should be str()
D. input() function used incorrectly

Shw me The Right Answer

Answer . B 

Q.3 Identify the error:

print(“Hello World!”))

A. Missing quotation marks
B. Extra parenthesis
C. Missing parenthesis
D. No error

Shw me The Right Answer

Answer . B 

Q.4 Initialize a variable with “Python”, print first and last character

A. Prints “Pn”
B. Prints “Py”
C. Prints “Python”
D. Error

Shw me The Right Answer

Answer . A 

Pseudocode:

Q.5 Print each element in list [1, 2, 3] with a space in between

A. 123
B. 37623
C. [1, 2, 3]
D. 37623

Shw me The Right Answer

Answer . A 

Pseudocode:

Q.6 Ask user for number, multiply by 2, print result

A. Reads number, prints double
B. Reads string, prints same
C. Error in input
D. No output

Shw me The Right Answer

Answer . A 

Q.7 What is the output of

print(“Hello”, end=’@’);
print(“World”)

A. HelloWorld
B. Hello@World
C. Hello World
D. Hello@ World

Shw me The Right Answer

Answer . B 

Q.8 What will be the output of

print(“Python”, “Programming”, sep=”-“)

A. Python-Programming
B. Python Programming
C. PythonProgramming
D. Python,Programming

Shw me The Right Answer

Answer . A 

Q.9 What does the sep parameter do in the print() function?

A. Separates lines
B. Specifies separator between values
C. Separates syntax errors
D. None of these

Shw me The Right Answer

Answer . B 

Q.10 What is the purpose of the end parameter in the print() function?

A. To add a space at the end
B. To end the script
C. To specify the string appended after the last value
D. To break the line

Shw me The Right Answer

Answer . C 

Q.11 Which function in Python is used to display data as output?

A. display()
B. print()
C. show()
D. output()

Shw me The Right Answer

Answer . B 

Q.12 What is the default type of data returned by the input() function in Python 3.x?

A. int
B. string
C. boolean
D. list

Shw me The Right Answer

Answer . B 

Q.13 Which function is used to read input from the console in Python?

A. input()
B. read()
C. scan()
D. getInput()

Shw me The Right Answer

Answer . A 

Q.14 Find the mistake:

x = (1, 2, 3);
x[1] = 4;
print(x)

A. Tuples are immutable
B. x[1] should be x[2]
C. Syntax error
D. No error

Shw me The Right Answer

Answer . A 

Q.15 Identify the error in this code:

x = [1, 2, 3];
print(x)

A. Syntax error in variable x
B. Missing parenthesis in print
C. Missing bracket in x
D. No error

Shw me The Right Answer

Answer . D 

Q.16 Evaluate this pseudocode:

Set x = [1, 2, 3];
If x is a list, print length of x, else print “Not a list”

A. 3
B. Not a list
C. Error
D. 0

Shw me The Right Answer

Answer . A 

Q.17 Pseudocode:

Variable
x = “Python”;
Check if x is a string,
if yes
print “String”,
otherwise
“Not a String”

A. String
B. Not a String
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.18 What will be the output of the following pseudocode?

Initialize x as 10;
If x is of type int,
print “Integer”
else
print “Not Integer”

A. Integer
B. Not Integer
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.19 What does the following code output:

x = [1, 2, 3];
print(type(x) == list)

A. TRUE
B. FALSE
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.20 What is the output of the following code snippet:

print(type(“Hello, World!”))

A. <class ‘str’>
B. <class ‘int’>
C. <class ‘list’>
D. <class ‘list’>

Shw me The Right Answer

Answer . A 

Q.21 Which of the following is an immutable data type?

A. Lists
B. Dictionaries
C. Tuples
D. Sets

Shw me The Right Answer

Answer . C 

Q.22 What will be the data type of the variable x after this assignment:

x = 3.5?

A. int
B. float
C. str
D. complex

Shw me The Right Answer

Answer . B 

Q.23 Which of the following is not a Python built-in data type?

A. dict
B. array
C. set
D. frozenset

Shw me The Right Answer

Answer . B 

Q.24 What data type would you use to store a whole number in Python?

A. int
B. float
C. str
D. bool

Shw me The Right Answer

Answer . B 

Q.25 Which of the following is a mutable data type in Python?

A. String
B. Tuple
C. List
D. Integer

Shw me The Right Answer

Answer . C 

Q.26 Which of these data types does Python not natively support?

A. Lists
B. Tuples
C. Arrays
D. Dictionaries

Shw me The Right Answer

Answer . C 

Q.27 Python is a:

A. Low-level language
B. High-level language
C. Middle-level language
D. Machine-level language

Shw me The Right Answer

Answer . B 

Q.28 Which of the following is a valid Python comment?

A. <!−− −−>
B. /* */
C. #
D. //

Shw me The Right Answer

Answer . C 

Q.29 Which version of Python removed the print statement?

A. Python 1.x
B. Python 2.x
C. Python 3.x
D. Python 4.x

Shw me The Right Answer

Answer . C 

Q.30 Python is known as:

A. A compiled language
B. An interpreted language
C. A machine language
D. An assembly language

Shw me The Right Answer

Answer . B 

Q.31 What will be the output of the following code?

myTuple = (1, 2, 3);
print(myTuple[1])

A. 1
B. 2
C. 3
D. Error

Shw me The Right Answer

Answer . B 

Q.32 How is memory allocation for lists and tuples different in Python?

A. Lists use more memory than tuples
B. Tuples use more memory than lists
C. Both use the same amount of memory
D. Memory usage is random and unpredictable

Shw me The Right Answer

Answer . A 

Q.33 What does myList[::-1] do?

A. Reverses myList
B. Copies myList
C. Removes the last element from myList
D. Sorts myList in descending order

Shw me The Right Answer

Answer . A 

Q.34 In Python, how can you combine two lists, list1 and list2?

A. list1 + list2
B. list1.append(list2)
C. list1.combine(list2)
D. list1.extend(list2)

Shw me The Right Answer

Answer . A 

Q.35 How do you access the last element of a list named myList?

A. myList[0]
B. myList[-1]
C. myList[len(myList)]
D. myList[-2]

Shw me The Right Answer

Answer . B 

Q.36 What is the main difference between a list and a tuple in Python?

A. Syntax
B. Data type
C. Mutability
D. Method of declaration

Shw me The Right Answer

Answer . C 

Q.37 Find the mistake:

while True:
print(“Looping”);
break

A. Infinite loop
B. No error
C. The loop will never break
D. Incorrect use of print statement

Shw me The Right Answer

Answer . B 

Q.38 Identify the error:
for i in range(5)
print(i)

A. Missing colon after range(5)
B. Missing parentheses around print
C. Syntax error in range
D. No error

Shw me The Right Answer

Answer . A 

Q.39 Pseudocode:

For i = 0 to 4,
print “Python”
if i is even

A. Prints “Python” twice
B. Prints “Python” three times
C. Prints “Python” four times
D. No output

Shw me The Right Answer

Answer . B 

Q.40 Pseudocode:

For numbers 1 to 5,
print number
if it’s odd

A. Prints 1 3 5
B. Prints 2 4
C. Prints all numbers 1 to 5
D. No output

Shw me The Right Answer

Answer . A 

Q.41 Pseudocode:

For each element in list [1, 2, 3],
print element

A. Prints 1 2 3
B. Prints 3 2 1
C. Prints [1, 2, 3]
D. No output

Shw me The Right Answer

Answer . A 

Q.42 Pseudocode:

Set x = 10;
While x > 0,
decrement x by 1,
print x

A. Prints numbers from 10 to 1
B. Prints numbers from 1 to 10
C. Prints numbers from 9 to 0
D. Infinite loop

Shw me The Right Answer

Answer . C 

Q.43 What will the following code print?

i = 5;
while i > 0:
print(i);
i -= 1

A. 5 4 3 2 1
B. 5 4 3 2
C. 1 2 3 4 5
D. Infinite loop

Shw me The Right Answer

Answer . A 

Q.44 What is the output of the following code?

for i in range(3):
print(i)

A. “0 1 2”
B. “1 2 3”
C. “0 1 2 3”
D. “Error”

Shw me The Right Answer

Answer . A 

Q.45 What is the difference between a for loop and a while loop in Python?

A. for is used for fixed iterations
B. while is faster than for
C. for can’t use conditional statements
D. There is no difference

Shw me The Right Answer

Answer . A 

Q.46 In a while loop, what happens if the condition never becomes False?

A. The loop runs forever
B. The loop stops after 10 iterations
C. Syntax error
D. The loop doesn’t start

Shw me The Right Answer

Answer . A 

Q.47 What does the continue statement do inside a loop?

A. Pauses the loop
B. Stops the loop
C. Skips the current iteration
D. Exits the program

Shw me The Right Answer

Answer . C 

Q.48 Which statement immediately terminates a loop in Python?

A. break
B. continue
C. exit
D. stop

Shw me The Right Answer

Answer . A 

Q.49 What is the use of a for loop in Python?

A. To repeat a block a fixed number of times
B. To check a condition
C. To declare variables
D. To handle exceptions

Shw me The Right Answer

Answer . A 

Q.50 Find the mistake:

if x > 5:
print(“Yes”)
elif x < 10:
print(“No”)

A. Syntax error in the if block
B. Missing colon after elif
C. Missing else between blocks
D. No error

Shw me The Right Answer

Answer . D 

Q.51 Identify the error:

if x > 10
print(“Greater”)

A. Missing parentheses around condition
B. No error
C. Missing colon after condition
D. Incorrect comparison operator

Shw me The Right Answer

Answer . C 

Q.52 Pseudocode: What is printed when the temperature is 25 degrees?

If temperature > 30,
print “Hot”,
elif temperature > 20,
print “Warm”,
else
“Cold”

A. Prints “Hot”
B. Prints “Warm”
C. Prints “Cold”
D. No output

Shw me The Right Answer

Answer . B 

Q.53 Pseudocode:

Check if a number is divisible by 2,
if yes
print “Even”,
otherwise
“Odd”

A. Prints “Even”
B. Prints “Odd”
C. Error
D. No output

Shw me The Right Answer

Answer . A 

Q.54 Pseudocode:

If age > 18,
print “Adult”,
else
print “Minor”

A. Prints “Adult”
B. Prints “Minor”
C. Prints “Age”
D. No output

Shw me The Right Answer

Answer . A 

Q.55 What will the following code print?

x = 10;
if x < 10:
print(“Less”);
else:
print(“Not Less”)

A. Less
B. Not Less
C. Error
D. Nothing

Shw me The Right Answer

Answer . B 

Q.56 What is the output of the following code?

x = 10;
if x > 5:
print(“Greater”)

A. Error
B. Greater
C. Nothing
D. Less

Shw me The Right Answer

Answer . B 

Q.57 What is the outcome if the elif and else blocks are missing in an if statement?

A. Syntax error
B. The program will stop
C. Nothing, it’s optional
D. Runtime error

Shw me The Right Answer

Answer . C 

Q.58 Which of the following is a valid conditional statement in Python?

A. if a = 5:
B. if a == 5:
C. if a <> 5:
D. if (a = 5):

Shw me The Right Answer

Answer . B 

Q.59 What will happen if the condition in an if statement in Python evaluates to False?

A. The program will stop
B. It will execute the else block
C. It will raise an error
D. It will skip to the next if

Shw me The Right Answer

Answer . B 

Q.60 In Python, which keyword is used to check additional conditions if the previous conditions fail?

A. elif
B. else if
C. then
D. switch

Shw me The Right Answer

Answer . A 

Q.61 What does the following code do?

with open(‘file.txt’, ‘w’) as file:
file.write(“Hello World”)

A. Reads from file.txt
B. Writes “Hello World” to file.txt
C. Deletes file.txt
D. Creates a new file

Shw me The Right Answer

Answer . B 

Q.62 When using with open(file) as f, what does with do?

A. Ensures file is saved
B. Automatically closes the file
C. Opens file in write mode
D. Reads entire file

Shw me The Right Answer

Answer . B 

Q.63 Which file mode in Python allows you to read and write to a file?

A. r+
B. w+
C. a+
D. rb+

Shw me The Right Answer

Answer . A 

Q.64 What is the purpose of the open() function in Python?

A. To read data from URL
B. To open a new Python file
C. To open a file
D. To create a Python module

Shw me The Right Answer

Answer . C 

Q.65 Find the mistake:

import math;
print(math.sqr(4))

A. Incorrect module
B. Incorrect function name
C. Wrong number of arguments
D. No error

Shw me The Right Answer

Answer . B 

Q.66 dentify the error:

def my_func(x, y):
return x + y;
my_func(1)

A. Missing argument
B. Syntax error
C. Logic error
D. No error

Shw me The Right Answer

Answer . A 

Q.67 Pseudocode:

Function that checks if a number is positive, negative, or zero

A. Prints type of number
B. Returns type of number
C. No output
D. Error

Shw me The Right Answer

Answer . A 

Q.68 Define a function that multiplies two numbers and returns the result

A. Returns sum
B. Returns product
C. No return
D. Syntax error

Shw me The Right Answer

Answer . B 

Q.69 What is the output of this code:

def add(x, y):
return x + y;
print(add(3, 4))

A. 3
B. 4
C. 7
D. Error

Shw me The Right Answer

Answer . C 

Q.70 What will the following function return:

def example():
return 5+5

A. 5+5
B. 10
C. 0
D. None

Shw me The Right Answer

Answer . B 

Q.71 What is the difference between arguments and parameters in Python functions?

A. No difference
B. Parameters define, arguments call
C. Arguments define, parameters call
D. Based on data type

Shw me The Right Answer

Answer . B 

Q.72 How do you import a module in Python?

A. using the import keyword
B. using the include keyword
C. using the require keyword
D. using the module keyword

Shw me The Right Answer

Answer . A 

Q.73 In Python, what is a module?

A. A data type
B. A built-in function
C. A file with definitions
D. A code block

Shw me The Right Answer

Answer . C 

Q.74 What is a function in Python?

A. A variable
B. A module
C. A block of code
D. An operator

Shw me The Right Answer

Answer . C 

Q.75 Find the mistake:

d = {‘a’: 1, ‘b’: 2};
print(d.get(‘c’, ‘Not Found’))

A. Syntax error in the dictionary
B. get() method used incorrectly
C. Key ‘c’ doesn’t exist
D. No error

Shw me The Right Answer

Answer . D 

Q.76 Identify the error:
d = {[‘first’]: 1, [‘second’]: 2}

A. Syntax error with the dictionary
B. Lists can’t be dictionary keys
C. No error
D. Dictionary keys must be strings

Shw me The Right Answer

Answer . B 

Q.77 Pseudocode:

Given a dictionary,
print each key-value pair

A. Prints keys only
B. Prints values only
C. Prints key-value pairs
D. Error

Shw me The Right Answer

Answer . C 

Q.78 Pseudocode:

Initialize dictionary with {‘a’: 1, ‘b’: 2};
Update ‘b’ value to 3;
Print dictionary

A. Prints {‘a’: 1, ‘b’: 2}
B. Prints {‘a’: 1, ‘b’: 3}
C. Error
D. No output

Shw me The Right Answer

Answer . B 

Q.79 What will be the result of the following code?

d = {‘a’: 1, ‘b’: 2};
d[‘c’] = 3;
print(len(d))

A. 2
B. 3
C. Error
D. 1

Shw me The Right Answer

Answer . B 

Q.80 What is the output of the following code?

d = {‘a’: 1, ‘b’: 2};
print(d[‘b’])

A. 1
B. 2
C. b
D. KeyError

Shw me The Right Answer

Answer . B 

Q.81 In Python 3.7 and later, how are elements in a dictionary ordered?

A. By the order they were added
B. In ascending order of keys
C. In descending order of keys
D. Randomly

Shw me The Right Answer

Answer . A 

Q.82 How can you remove a key-value pair from a dictionary?

A. Using the del statement
B. Using the remove() method
C. Using the pop() method
D. Both A and C

Shw me The Right Answer

Answer . D 

Q.83 What happens if you try to access a non-existent key ‘k’ in a dictionary ‘d’ using d[k]?

A. Returns None
B. Returns an empty string
C. Raises a KeyError
D. Creates a new key ‘k’ with value None

Shw me The Right Answer

Answer . C 

Q.84 How do you access the value associated with a key ‘k’ in a dictionary ‘d’?

A. d(‘k’)
B. d[k]
C. d.get(‘k’)
D. All of the above

Shw me The Right Answer

Answer . B 

Q.85 What is a dictionary in Python?

A. An ordered collection of elements
B. A mutable collection of key-value pairs
C. An immutable collection of items
D. A collection of unique elements

Shw me The Right Answer

Answer . B 

Q.86 Find the mistake:

myTuple = (1, 2, 3);
myTuple[1] = 4

A. Tuples are immutable
B. Wrong index used
C. Syntax error in tuple declaration
D. myTuple should be a list

Shw me The Right Answer

Answer . A 

Q.87 Identify the error:

myList = [1, 2, 3);
myList.append(4)

A. Syntax error with the list declaration
B. append() method is used incorrectly
C. No error
D. Lists cannot contain integers

Shw me The Right Answer

Answer . A 

Q.88 Pseudocode:

Given a tuple (1,2,3),
print each element multiplied by 2

A. Prints 2, 4, 6
B. Prints 1, 2, 3
C. Error
D. No output

Shw me The Right Answer

Answer . A 

Q.89 Initialize list with values [1,2,3];
Add 4 to the end;
Print list

A. Prints [1, 2, 3]
B. Prints [1, 2, 3, 4]
C. Error
D. Prints [4, 1, 2, 3]

Shw me The Right Answer

Answer . B 

Q.90 What is the result of the following code?
myList = [1, 2, 3];
myList[1] = 4;
print(myList)

A. [1, 2, 3]
B. [1, 4, 3]
C. Error
D. [4, 2, 3]

Shw me The Right Answer

Answer . B 

Q.91 What is inheritance in object-oriented programming?

A. Sharing data between classes
B. Copying methods from one class to another
C. Deriving new classes from existing classes
D. Protecting data within a class

Shw me The Right Answer

Answer . C 

Q.92 Identify the error in this code:
class Animal:
def init(self, name):
name = name

A. Incorrect use of the constructor
B. name should be ‘self.name’
C. The class should have more methods
D. No error

Shw me The Right Answer

Answer . B 

Q.93 Pseudocode:
Define a class ‘Car’
with a method ‘drive’;
Create an instance of ‘Car’;
Call ‘drive’ method

A. Error as ‘drive’ method is undefined
B. The ‘drive’ method of the ‘Car’ instance is executed
C. Nothing as ‘Car’ has no attributes
D. A new ‘Car’ object is created

Shw me The Right Answer

Answer . B 

Q.94 Given the class definition:

class Dog:
def init(self, name):
self.name = name,
what does Dog(‘Buddy’).name return?

A. Dog
B. name
C. Buddy
D. An error occurs

Shw me The Right Answer

Answer . C 

Q.95 What is polymorphism in the context of object-oriented programming?

A. Changing the functionality of methods in subclasses
B. Creating multiple classes that inherit from a single class
C. The ability of different object types to be accessed through the same interface
D. Splitting a class into multiple smaller classes

Shw me The Right Answer

Answer . C 

Q.96 How is a class attribute different from an instance attribute in Python?

A. Class attributes are shared by all instances
B. Instance attributes can’t be changed
C. Class attributes can’t be accessed by instances
D. No difference

Shw me The Right Answer

Answer . A 

Q.97 In Python, what is ‘self’ in a class method?

A. A variable that holds the class name
B. A reference to the class itself
C. A reference to the instance that calls the method
D. A placeholder for future arguments

Shw me The Right Answer

Answer . C 

Q.98 What is a class in Python?

A. A blueprint for creating objects
B. A function inside an object
C. A variable inside an object
D. A specific object instance

Shw me The Right Answer

Answer . A 

Q.99 Identify the error in this lambda expression:
lambda x, y:
if x > y
return x
else
return y

A. Incorrect syntax for if-else
B. Lambda function cannot use conditional
C. Lambda cannot return values
D. No error

Shw me The Right Answer

Answer . A 

Q.100 Pseudocode:
Define a lambda to square a number, use it to get the square of 5

A. Returns 25
B. Returns 10
C. Syntax error
D. No output

Shw me The Right Answer

Answer . A 

Q.101 What is the output of the following lambda expression?
(lambda x, y: x * y)(3, 4)

A. 7
B. 12
C. 0
D. SyntaxError

Shw me The Right Answer

Answer . B 

Q.102 How is a lambda function different from a regular function defined using def in Python?

A. Lambda can only have one expression
B. Lambda can return multiple values
C. Lambda functions are faster
D. Lambda functions can’t have arguments

Shw me The Right Answer

Answer . A 

Q.103 What is a lambda expression in Python?

A. A one-time anonymous function
B. A named, reusable function
C. A loop structure
D. A data type

Shw me The Right Answer

Answer . A 

Q.104 Identify the error in this code:
[x for x in range(10)
if x%2 = 0]

A. Syntax error in range function
B. Incorrect use of assignment operator
C. List comprehension is incorrectly formed
D. No error

Shw me The Right Answer

Answer . B 

Q.105 Pseudocode:

Create a list of lowercase letters from a list of mixed case letters, ‘A’, ‘b’, ‘C’, ‘d’, ‘E’

A. [‘A’, ‘b’, ‘C’, ‘d’, ‘E’]
B. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
C. [‘A’, ‘C’, ‘E’]
D. [‘b’, ‘d’]

Shw me The Right Answer

Answer . B 

Q.106 What does the following dictionary comprehension do?
{x: x*x for x in range(5)
if x%2 == 0}

A. Squares of all numbers from 0 to 4
B. Squares of odd numbers from 0 to 4
C. Squares of even numbers from 0 to 4
D. Cubes of even numbers from 0 to 4

Shw me The Right Answer

Answer . C 

Q.107 What will this list comprehension produce:

[x**2 for x in range(5)]

A. [0, 1, 2, 3, 4]
B. [1, 4, 9, 16, 25]
C. [0, 1, 4, 9, 16]
D. [1, 2, 3, 4, 5]

Shw me The Right Answer

Answer . C 

Q.108 What is the key difference between list comprehensions and generator expressions?

A. The syntax used
B. The type of brackets used
C. List comprehensions produce lists, while generator expressions produce generators
D. Execution speed

Shw me The Right Answer

Answer . C 

Q.109 What is a list comprehension in Python?

A. A concise way to create lists
B. A method to iterate through lists
C. A type of Python comprehension
D. A special function for lists

Shw me The Right Answer

Answer . A 

Q.110 Find the mistake: try:
x = 1 / “0”
except ValueError:
print(“Type error occurred”)

A. Wrong exception type is caught
B. Should be ZeroDivisionError
C. It will raise a TypeError
D. No error

Shw me The Right Answer

Answer . C 

Q.111 Identify the error in this code:
try:
x = 1 / 0
except:
print(“An error occurred”)

A. Division by zero is not handled properly
B. Generic exception should not be used
C. Syntax error in try statement
D. No error

Shw me The Right Answer

Answer . B 

Q.112 Pseudocode:
Try to open a file, read its contents;
if file not found,
print error message

A. File contents are displayed
B. Error message is printed
C. No output
D. File is created and then read

Shw me The Right Answer

Answer . B 

Q.113 What will be the output of the following code?
try: x = 1 / 0 except ZeroDivisionError:
print(“Error”)

A. 0
B. 1
C. Error
D. No output

Shw me The Right Answer

Answer . C 

Q.114 When is the else clause in a try-except block executed?

A. Always after the try block
B. When no exceptions are raised
C. When an exception is handled
D. It is never executed

Shw me The Right Answer

Answer . B 

Q.115 Which exception is raised by Python when a file you try to open does not exist?

A. FileNotFoundError
B. IOError
C. OSError
D. ValueError

Shw me The Right Answer

Answer . A 

Q.116 In Python, what is the purpose of the try statement?

A. To test a block of code for errors
B. To execute code regardless of errors
C. To generate an error
D. To declare variables

Shw me The Right Answer

Answer . A 

Q.117 Find the mistake:

with open(‘data.txt’, ‘r’) as file:
data = file.readlines();
print(data[2])

A. Wrong function used
B. Error in print statement
C. Index error
D. No error

Shw me The Right Answer

Answer . C 

Q.118 Identify the error:

file = open(‘data.txt’, ‘r’);
data = file.read();
file.close()

A. File not closed properly
B. Incorrect mode for reading
C. No error
D. Error in file.read()

Shw me The Right Answer

Answer . C 

Q.119 Pseudocode:

Open ‘data.txt’, read each line, print each line

A. Prints name of the file
B. Prints contents of data.txt
C. Error
D. No output

Shw me The Right Answer

Answer . B 

Q.120 What is the result of this code?

f = open(‘file.txt’, ‘r’);
print(f.readline())

A. Prints entire file
B. Prints the first line of file.txt
C. Error
D. Prints ‘file.txt’

Shw me The Right Answer

Answer . B 

Q.121 Why is PEP 8 important in Python development?

A. It provides guidelines for error handling
B. It outlines the standard coding conventions
C. It defines the core functionality of Python
D. It lists Python’s reserved words

Shw me The Right Answer

Answer . B 

Q.122 What is PEP in the context of Python?

A. A Python enhancement proposal
B. A Python error protocol
C. A Python environment path
D. A Python executable program

Shw me The Right Answer

Answer . A 

Q.123 Identify the error in this code:
import os;
os.rmdir(‘mydir’) assuming ‘mydir’ contains files and subfolders.

A. Incorrect module imported
B. os.rmdir() cannot delete non-empty directories
C. Wrong syntax for removing directories
D. No error

Shw me The Right Answer

Answer . B 

Q.124 What does os.listdir(‘.’) do?

A. Lists all Python files in the current directory
B. Creates a new directory
C. Lists all files and directories in the current directory
D. Deletes the current directory

Shw me The Right Answer

Answer . C 

Q.125 How does the os.path.join() function benefit file path construction?

A. Increases file access speed
B. Ensures correct path format for the operating system
C. Encrypts the file path
D. Splits the file path into components

Shw me The Right Answer

Answer . B 

Q.126 Which function in the OS module is commonly used to change the current working directory?

A. os.change_directory()
B. os.chdir()
C. os.setcwd()
D. os.update_directory()

Shw me The Right Answer

Answer . B 

Q.127  What is the primary use of the OS module in Python?

A. Networking
B. Web development
C. Interacting with the operating system
D. Data analysis

Shw me The Right Answer

Answer . C 

Q.128 What is the key benefit of using requirements.txt in a Python project?

A. For documenting the code
B. To list all the packages and their versions used in the project
C. To improve the performance of the project
D. For version control

Shw me The Right Answer

Answer . B 

Q.129 Which tool is used to install Python packages?

A. git
B. virtualenv
C. pip
D. setuptools

Shw me The Right Answer

Answer . C 

Q.130 What is the purpose of a virtual environment in Python programming?

A. Version control
B. To isolate project-specific dependencies
C. Web development
D. Data analysis

Shw me The Right Answer

Answer . B 

Q.131 Identify the error in this code for reading a CSV file:
import csv;
reader = csv.reader(open(‘file.csv’, ‘wb’))

A. The file mode should be ‘rb’, not ‘wb’
B. csv.reader does not take a file object
C. The file ‘file.csv’ does not exist
D. Incorrect use of the csv module

Shw me The Right Answer

Answer . A 

Q.132 What does the following code do?
import json; json.loads(‘{“name”: “John”, “age”: 30}’)

A.Creates a JSON file
B. Parses a JSON string into a Python dictionary
C. Serializes a Python dictionary to JSON
D. Throws an error

Shw me The Right Answer

Answer . B 

Q.133 When working with Excel files in Python, which library is commonly used to read and write .xlsx files?

A. Pandas
B. Numpy
C. OpenPyXL
D. CSV

Shw me The Right Answer

Answer . C 

Q.134 In Python, which module is used to work with CSV files?

A. csv
B. json
C. excel
D. fileio

Shw me The Right Answer

Answer . A 

Q.135 What is JSON primarily used for in Python?

A. Web development
B. Data serialization and communication between different systems
C. Numeric computations
D. File compression

Shw me The Right Answer

Answer . B 

Q.136 Identify the issue in this regex:
re.findall(r”[A-Z+]”, “Python+C+Java”)

A. Incorrect use of escape character
B. Wrong pattern for matching uppercase letters
C. ‘+’ should not be escaped
D. No issue

Shw me The Right Answer

Answer . C 

Q.137 What will the following Python code return?
re.search(r”\d{3}”, “The code 123 is valid”)

A. ‘123’
B. <re.Match object; span=(9, 12), match=’123′>
C. None
D. ‘The code 123 is valid’

Shw me The Right Answer

Answer . B 

Q.138 What does the regular expression ^a…s$ match?

A. Any seven-letter string starting with ‘a’ and ending with ‘s’
B. Any string starting with ‘a’ and ending with ‘s’
C. Any five-letter string starting with ‘a’ and ending with ‘s’
D. Any string containing ‘a’ and ‘s’

Shw me The Right Answer

Answer . A 

Q.139 Which module in Python provides support for regular expressions?

A. sys
B. re
C. os
D. json

Shw me The Right Answer

Answer . B 

Q.140 What is a regular expression used for in Python?

A. Error handling
B. Data serialization
C. Pattern matching and text manipulation
D. Memory management

Shw me The Right Answer

Answer . C 

Q.141 When should you choose multiprocessing over threading in Python?

A. For I/O-bound tasks
B. For quick tasks
C. For CPU-bound tasks
D. When memory usage is a concern

Shw me The Right Answer

Answer . C 

Q.142 What is the main difference between threading and multiprocessing in Python?

A. Threading is faster
B. Multiprocessing uses more memory
C. Threading involves parallel execution
D. Multiprocessing involves separate memory spaces for each process

Shw me The Right Answer

Answer . D 

Q.143 How do generators differ from standard functions?

A. Generators can’t take arguments
B. Generators don’t return values
C. Generators yield values, maintaining state between each yield
D. Generators execute faster

Shw me The Right Answer

Answer . C 

Q.144 What is a generator in Python?

A. A type of collection
B. A tool for creating iterators
C. A function that returns a single value
D. A module

Shw me The Right Answer

Answer . B 

Q.145 How do decorators contribute to cleaner code in Python?

A. By reducing the need for global variables
B. By providing a syntax for error handling
C. By allowing code reuse and adding functionality to existing code
D. By optimizing memory usage

Shw me The Right Answer

Answer . C 

Q.146 What is a decorator in Python?

A. A function that modifies the functionality of another function
B. A function that creates new functions
C. A syntax for defining anonymous functions
D. A tool for debugging functions

Shw me The Right Answer

Answer . A 

Q.147 Identify the issue in this inheritance scenario:
class Bird(Animal):
def fly(self):
pass, class Penguin(Bird):
def fly(self):
print(“Can’t fly”)

A. Birds don’t have a ‘fly’ method by default
B. Penguins shouldn’t inherit from ‘Bird’
C. fly’ method in ‘Penguin’ contradicts ‘Bird’
D. No issue present

Shw me The Right Answer

Answer . C 

Q.148 Pseudocode:
Create a base class ‘Shape’, derived classes ‘Circle’, ‘Square’; Implement method ‘area’ in both

A. Error, as ‘area’ cannot be implemented in ‘Shape’
B. Circle’ and ‘Square’ can’t inherit from ‘Shape’
C. area’ method behaves differently in ‘Circle’ and ‘Square’
D. ‘Shape’ should not have an ‘area’ method

Shw me The Right Answer

Answer . C 

Q.149 Given a class Animal and a subclass Dog, which method demonstrates polymorphism?

A. Animal.speak() and Dog.speak() have different implementations
B. Dog has the same attributes as Animal
C. Dog uses a method from Animal without changing it
D. Animal has a method not present in Dog

Shw me The Right Answer

Answer . A 

Q.150 How does polymorphism enhance code reusability?

A. By allowing methods to perform different tasks based on the object
B. By using the same method in multiple classes
C. By changing the functionality of inherited methods
D. By encapsulating code in modules

Shw me The Right Answer

Answer . A 

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments