1. What is the output of the following code?
python
Copy code
print(type([]))
a) <class 'list'>
b) <type 'list'>
c) <list>
d) Error
Answer: a
2. Which of the following is the correct way to declare a variable in
Python?
a) int x = 10
b) x: int = 10
c) x = 10
d) var x = 10
Answer: c
3. What is the result of 10 // 3 in Python?
a) 3.33
b) 3
c) 4
d) 10
Answer: b
4. Which of the following functions converts a string to a float in Python?
a) int()
b) str()
c) float()
d) eval()
Answer: c
5. What is the output of bool([]) in Python?
a) True
b) False
c) None
d) Error
Answer: b
6. How do you access the first element of a list lst in Python?
a) lst[1]
b) lst[0]
c) lst[-1]
d) lst.first()
Answer: b
7. Which of the following is an immutable data type in Python?
a) list
b) set
c) tuple
d) dict
Answer: c
8. What will be the output of the following code?
python
Copy code
a = [1, 2, 3]
b=a
b.append(4)
print(a)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) [4]
d) Error
Answer: b
9. Which method is used to add an element to the end of a list?
a) append()
b) insert()
c) add()
d) extend()
Answer: a
10.How do you create a set in Python?
a) set = {}
b) set = []
c) set = set()
d) set = ()
Answer: c
11.What is the output of the following code?
python
Copy code
def func(x=10):
return x + 5
print(func())
a) 5
b) 15
c) 10
d) Error
Answer: b
12.What is the purpose of the *args parameter in a Python function?
a) Accepts a fixed number of arguments.
b) Accepts any number of positional arguments.
c) Accepts only keyword arguments.
d) Declares the function as static.
Answer: b
13.Which of the following is used to define a lambda function?
a) lambda
b) def
c) func
d) lambda_func
Answer: a
14.What will be the output of this code?
python
Copy code
def add(x, y):
return x + y
print(add(2, 3))
a) 23
b) 5
c) None
d) Error
Answer: b
15.Which keyword is used to return a value from a function?
a) return
b) yield
c) output
d) value
Answer: a
16.How do you define a class in Python?
a) class MyClass:
b) MyClass class:
c) def MyClass:
d) create MyClass:
Answer: a
17.What is the output of the following code?
python
Copy code
class MyClass:
def __init__(self, x):
self.x = x
obj = MyClass(10)
print(obj.x)
a) x
b) 10
c) None
d) Error
Answer: b
18.Which keyword is used to inherit a class in Python?
a) inherit
b) extends
c) super
d) class(ClassName):
Answer: d
19.Which method is called automatically when an object is created in
Python?
a) __create__
b) __init__
c) __start__
d) __new__
Answer: b
20.What is the purpose of self in a class method?
a) Refers to the current object.
b) Declares a static method.
c) Creates a private variable.
d) Allows inheritance.
Answer: a
21.What mode is used to open a file for reading in Python?
a) w
b) r
c) rw
d) a
Answer: b
22.What happens if you try to open a file that doesn’t exist in r mode?
a) Creates a new file.
b) Raises a FileNotFoundError.
c) Opens a blank file.
d) Closes the program.
Answer: b
23.Which method is used to read all lines of a file?
a) read()
b) readlines()
c) readline()
d) readall()
Answer: b
24.How do you write to a file in Python?
a) file.write()
b) file.writelines()
c) Both a and b
d) file.append()
Answer: c
25.What does the with keyword do in file handling?
a) Closes the file automatically after operations.
b) Writes data to the file.
c) Deletes the file after use.
d) Creates a new file.
Answer: a
26.Which keyword is used to handle exceptions in Python?
a) try
b) catch
c) except
d) Both a and c
Answer: d
27.What is the output of the following code?
python
Copy code
try:
x=1/0
except ZeroDivisionError:
print("Error")
a) Error
b) None
c) ZeroDivisionError
d) RuntimeError
Answer: a
28.What is the purpose of the finally block in Python?
a) Handles errors.
b) Executes cleanup code.
c) Stops the program execution.
d) Declares exceptions.
Answer: b
29.Which exception is raised when a variable is not defined?
a) TypeError
b) NameError
c) ValueError
d) SyntaxError
Answer: b
30.What happens if an exception is not handled in Python?
a) The program continues.
b) The program terminates.
c) The program ignores the exception.
d) None of the above.
Answer: b
31.Which module is used to work with regular expressions in Python?
a) regex
b) re
c) regexp
d) expression
Answer: b
32.What does the math.ceil() function do?
a) Rounds a number down.
b) Returns the largest integer greater than or equal to a number.
c) Returns the smallest integer greater than a number.
d) Rounds a number to the nearest integer.
Answer: b
33.How do you install a Python library?
a) pip install <library>
b) import <library>
c) python install <library>
d) library.add()
Answer: a
34.Which library is used for data manipulation in Python?
a) numpy
b) pandas
c) matplotlib
d) scipy
Answer: b
35.Which function is used to generate random numbers in Python?
a) random.random()
b) rand()
c) generate.random()
d) random.rand()
Answer: a
36.What is an iterator in Python?
a) An object that contains values.
b) An object that produces values on demand.
c) A function that returns multiple values.
d) A collection of data.
Answer: b
37.Which of the following is NOT a generator method?
a) yield
b) next()
c) iter()
d) append()
Answer: d
38.What will the following code output?
python
Copy code
def func():
yield 1
yield 2
print(next(func()))
a) 1
b) 2
c) Error
d) None
Answer: c
39.How do you create an iterator object in Python?
a) iter()
b) next()
c) yield
d) for
Answer: a
40.What is the purpose of the yield keyword?
a) Creates a generator.
b) Ends a function.
c) Replaces return.
d) Declares variables.
Answer: a
41.What is the result of the following code?
python
Copy code
x = [1, 2, 3]
print([i * 2 for i in x])
a) [1, 2, 3]
b) [2, 4, 6]
c) [2, 4]
d) Error
Answer: b
42.What does __init__ represent in Python?
a) A special method for initialization.
b) A destructor method.
c) A private method.
d) A method for threading.
Answer: a
43.Which of the following is a dunder method?
a) def func()
b) def __str__()
c) def _name()
d) def myMethod()
Answer: b
44.What does the global keyword do?
a) Declares a variable as global.
b) Imports global functions.
c) Stops recursion.
d) Declares local variables.
Answer: a
45.How do you handle multiple exceptions in Python?
a) Using multiple except blocks.
b) Using a single try block.
c) Using elif.
d) By ignoring errors.
Answer: a
46.Which keyword is used for creating a virtual environment?
a) virtualenv
b) venv
c) env
d) activate
Answer: b
47.What is the default encoding in Python 3?
a) ASCII
b) UTF-8
c) Latin-1
d) ISO-8859-1
Answer: b
48.How do you check the type of a variable?
a) type()
b) isinstance()
c) Both a and b
d) var.type()
Answer: c
49.Which function converts an integer to its binary representation?
a) bin()
b) binary()
c) int.binary()
d) hex()
Answer: a
50.What is the result of 5 in [1, 2, 3, 4, 5]?
a) True
b) False
c) Error
d) None
Answer: a
51.What is the result of 'Python' * 3?
a) 'Python3'
b) PythonPythonPython
c) 'Python'
d) Error
Answer: b
52.Which method is used to convert a string to lowercase?
a) to_lower()
b) lower()
c) downcase()
d) casefold()
Answer: b
53.How do you remove whitespace from both ends of a string?
a) strip()
b) trim()
c) rstrip()
d) lstrip()
Answer: a
54.What is the output of the following code?
python
Copy code
print('Hello'.find('l'))
a) 1
b) 2
c) 3
d) Error
Answer: b
55.What does the split() method do?
a) Joins strings.
b) Divides a string into substrings.
c) Removes characters.
d) Reverses a string.
Answer: b
56.What is the result of [x**2 for x in range(3)]?
a) [0, 1, 4]
b) [1, 4, 9]
c) [0, 1, 2]
d) Error
Answer: a
57.Which of the following creates a list of even numbers from 0 to 10?
a) [x for x in range(10) if x % 2 == 0]
b) [x for x in range(10) where x % 2 == 0]
c) [x if x % 2 == 0 in range(10)]
d) [x for x in range(10).even()]
Answer: a
58.Can list comprehensions include multiple for loops?
a) Yes
b) No
c) Only with if conditions
d) Only for lists of numbers
Answer: a
59.Which keyword is used for filtering in list comprehensions?
a) filter
b) if
c) while
d) for
Answer: b
60.What is the result of [x + y for x in 'AB' for y in '12']?
a) ['A1', 'A2', 'B1', 'B2']
b) ['AB12']
c) ['A1B1', 'A2B2']
d) ['12AB']
Answer: a
61.Which of the following defines a lambda function?
a) lambda x: x * 2
b) def lambda(x): return x * 2
c) func = lambda(x): x * 2
d) lambda x => x * 2
Answer: a
62.What is the output of the following code?
python
Copy code
x = lambda a, b: a + b
print(x(3, 5))
a) 8
b) 35
c) Error
d) None
Answer: a
63.Can a lambda function have multiple lines?
a) Yes
b) No
c) Only with parentheses
d) Only with return
Answer: b
64.What does the following lambda function return?
python
Copy code
y = lambda x: x ** 3
print(y(2))
a) 6
b) 8
c) 4
d) None
Answer: b
65.What is the primary use of lambda functions?
a) Define anonymous, small functions.
b) Define a class method.
c) Create loops.
d) Debug a program.
Answer: a
66.Which method is used to add elements to a set?
a) insert()
b) append()
c) add()
d) extend()
Answer: c
67.What is the result of set([1, 2, 2, 3])?
a) {1, 2, 2, 3}
b) {1, 2, 3}
c) [1, 2, 3]
d) Error
Answer: b
68.Which of the following is a valid dictionary in Python?
a) {1: 'a', 2: 'b'}
b) {'a', 'b'}
c) [1: 'a', 2: 'b']
d) {1, 2, 3}
Answer: a
69.How do you access the value of key x in a dictionary d?
a) d.x
b) d['x']
c) d[x]
d) d.get('x')
Answer: b
70.Which method is used to get all keys of a dictionary?
a) keys()
b) get_keys()
c) all_keys()
d) items()
Answer: a