PYTHON PROGRAMMING MCQ (III B.SC (CS) )
PYTHON PROGRAMMING MCQ (III B.SC (CS) )
(AUTONOMOUS)
DEPARTMENT OF COMPUTER SCIENCE AND COMPUTER APPLICATIONS
UNIT 1
Python
1. Who created Python?
a) Guido van Rossum b) Larry Wall
c) Linus Torvalds d) Bill Gates
2. In what year was Python first released?
a) 1991 b) 1995 c) 2000 d) 2005
3. What is the purpose of the "=" operator in Python?
a) Comparison b) Assignment c) Arithmetic d) Logical
UNIT - 2
1) Which of the following data types is used for double-precision floating-point numbers in
Python?
a) int b) float c) double d) complex
2) What is the maximum value of an integer (int) in Python?
a) 2147483647 b) 9223372036854775807
c) Infinity d) There is no maximum integer value
3) Which of the following is a valid way to represent a double precision floating-point number in
Python?
a) 11 b) 10 c) 3.14 d) 678
4)Which of the following methods can be used to convert a floating-point number to an integer?
a) int() b) float() c) round() d) convert()
5)What will be the result of the following code?
x=7
y=2
print(x ** y)
a) 49
b) 14
c) 5.5
d) 3.5
6) What is the output of the following code?
x = 10.5
y=4
print(x / y)
a) 2.625 b) 2.6 c) 2 d) 4
7) What is the result of the following code?
x = 3.14159
print(int(x))
a) 3.14 b) 3 c) 4 d)3.1415
8) What is the correct way to define a complex number in Python?
a) x = 3 + 4i b) x = 3 + 4j c) x = 3 + 4I d) x = (3, 4)
9) What is the output of the following code?
x = 3 + 4j
print(x.real)
a) 3 b) 4 c) 3 + 4j d) 4j
10) How do you compute the conjugate of a complex number in Python?
a) x.conjgate() b) x.conjugate() c) conjugate(x) d) y.conjugate()
11) Which of the following is not a complex number?
a) k = 2 + 3j b) k = complex(2, 3) c) k = 2 + 3l d) k = 2 + 3J
12) What does ~4 evaluate to?
a) -5 b) -4 c) -3 d) +3
13) Which of the following is not a valid operation for complex numbers in Python?
a) Addition b) Subtraction c) Division d) Square root
14) How do you get the absolute value (magnitude) of a complex number in Python?
a) abs(x) b) x.abs() c) cmath.abs(x) d) x.magnitude()
15) What is the output of the following code?
x = 1 + 2j
print(abs(x))
a) 2 b) 2.23606797749979 c) 1 d) 1.5
16) How can you check if a number is complex in Python?
a) isinstance(x, complex) b) type(x),complex c) is_complex(x) d) type(x)
17) What is the result of the following operation?
x = 3 + 4j
print(x ** 2)
a) -7 + 24j b) -7 - 24j c) 7 + 24j d) 7 - 24j
18) How do you extract the real part of a complex number?
a) x.real() b) x.real c) real(x) d) get_real(x)
19) What does the cmath.phase() function return?
a) The real part of a complex number. b) The imaginary part of a complex number.
c) The magnitude of a complex number. d) The phase (angle) of a complex number.
20) What does the // operator do in Python?
a) Division that returns a float b) Division that returns an integer
c) Remainder of division d) Exponentiation
21) What is the result of the following code?
x = 10
y=3
print(x // y)
a) 3.33 b) 4 c) 2.0 d) 3
22) What will the following expression return?
x=5
y=5
print(x is y)
a) 11 b) 0 c) 1 d) 00
23) Which operator is used to assign a value to a variable?
a) = b) == c) += d) &
24) What is the result of the following code?
x = 10
x %= 3
print(x)
a) 0 b) 3 c) 1 d) 2
25) Which of the following operators is used to concatenate two strings?
a) & b) + c) * d) ==
26) Which of the following operators is a bitwise XOR operator?
a) ^ b) & c) | d) ~
27) What is the result of the following operation?
x=4
y=2
print(x << y)
a) 4 b) 8 c) 16 d) 2
28) Which operator is used for bitwise AND in Python?
a) & b) | c) ^ d) ~
29) Which of the following is the result of the operation 5 / 2 in Python?
a) 2 b) 2.5 c) 3 d) 3.0
30) What does the isnumeric() method do in Python?
a) Checks if a string is a valid floating-point number.
b) Checks if a string contains only alphabetic characters.
c) Checks if a string contains only numeric characters (0-9).
d) Checks if a string contains any digits.
31) Which of the following strings will return True when passed to the isnumeric() method?
a) "123abc" b) "456" c) "12.34" d) " " (empty string)
32) Does isnumeric() consider negative numbers (e.g., -5) as numeric?
a) Yes
b) No
c) Only if the negative sign is at the end
d) Only if the negative sign is in the middle
33) Which of the following is not considered a numeric string by the isnumeric() method?
a) "123" b) "Ⅹ" (Roman numeral for 10)
c) "٣" (Arabic digit) d) "12.34"
34) Which of the following is not a sequence type in Python?
a) List b) Tuple c) Dictionary d) String
35) What is the output of the following code?
s = "Hello"
print(s[1])
a) H b) e c) l d) o
36) Which of the following is immutable in Python?
a) List b) Set c) Dictionary d) Tuple
37) What does the len() function do when applied to a sequence in Python?
a) Returns the last element of the sequence
b) Returns the number of elements in the sequence
c) Returns the first element of the sequence
d) Returns a copy of the sequence
38) What will the following code output?
s = "Python"
print(s[1:4])
a) yth b) pyth c) tho d) Py
39) Which of the following string methods can be used to convert a string to uppercase in
Python?
a) lower() b) capitalize() c) swapcase() d) upper()
40) What does the len() function return when applied to a string?
a) The first character of the string b) The last character of the string
c) The number of characters in the string d) The string itself
41) What will the following code output?
s = "python programming"
print(s[:6])
a) python b) python c) program d) pytho
42) What does the find() method do in Python?
a) Finds the longest word in a string
b) Finds the index of the last occurrence of a substring
c)Finds the first occurrence of a substring
d) Finds the number of occurrences of a substring
43) What is the result of the following code?
s = "hello"
print(s[::-1])
a) hello b) olleh c) Error d) None
44) Which of the following methods can be used to check if a string contains only alphabetic
characters?
a) isalpha() b) isdigit() c) isalnum() d) isalpha()
45) Which of the following methods is used to replace a substring in a string with another
substring?
a) change() b) substitute() c) replace() d) transform()
46) What will be the result of the following code?
s = "Python"
print(s * 3)
a) PythonPythonPython b) Python3Python3 c) PYTHON d) PYT
47) Which method can be used to convert a string to a list of characters?
a) charlist() b) split() c) join() d) List()
48) What does the startswith() method do in Python?
a) Checks if the string starts with a specific substring
b) Checks if the string ends with a specific substring
c) Checks if the string contains a specific substring
d) Checks if the string is empty
49) What will the following code output?
s = "HELLO"
print(s.lower())
a) Hello b) HELLO c) hello d) Error
50) What does the capitalize() method do in Python?
a) Converts the first character to uppercase and the rest to lowercase
b) Converts the entire string to uppercase
c) Converts the entire string to lowercase
d) Capitalizes every word in the string
51) What is the result of the following code?
s = "apple"
print(s[1:4])
a) pple b) appl c) ppl d) ple
52) How can you check if a string contains only numeric characters?
a) isnum() b) isdigit() c) isnumeric() d) isalpha()
53) What does the join() method do in Python?
a) Joins a sequence of characters into a single string
b) Splits a string into a list of words
c) Combines a list of words into a sentence
d) Concatenates two strings
54) Which of the following is a valid way to create a list in Python?
a) list = (1, 2, 3) b) list = [1, 2, 3]
c) list = {1, 2, 3} d) list = 1, 2, 3
55) What will be the output of the following code?
lst = [1, 2, 3, 4]
print(lst[2])
a) 1 b) 2 c) 3 d) 4
56) Which method is used to add an element to the end of a list?
a) append() b) insert() c) add() d) extend()
57) What is the output of the following code?
lst = [1, 2, 3, 4]
lst[1:3] = [5, 6]
print(lst)
a) [1, 5, 6, 4, 6] b) [1, 5, 6, 4]
c) [5, 6, 3, 4] d) [1, 2, 3, 4, 5, 6]
58) What is the result of the following code?
lst = [1, 2, 3, 4]
lst.pop()
print(lst)
a) [1, 2, 3] b) [1, 2, 3, 4]
c) [2, 3, 4] d) [1, 2, 3, 4, None]
59) What is the correct syntax to access the last element of a list in Python?
a) list[0] b) list[-1]
c) list[len(list)] d) list[len(list) - 1]
60) Which method is used to sort a list in ascending order in Python?
a) order() b) sorted() c) sort() d) ascending()
38. Which of the following is the correct way to iterate over all key-value pairs in a dictionary d?
a) for key, value in d: b) for key in d:
c) for key, value in d.items(): d) for key, value in d.keys():
39. The dict.fromkeys() method is used to:
a) Create a dictionary from a list of keys
b) Create a dictionary with default values for all keys
c) Remove keys from a dictionary
d) Update the values of keys in a dictionar
40. What is the return type of dict.items()?
a) List of tuples b) List of dictionaries
c) View object d) Set of key-value pairs
41. What will d = {1: 'a', 2: 'b', 3: 'c'} and d.pop(2) return?
a) b b) {2: 'b'}
c) 2 d) KeyError
42. Which of the following methods removes a specific key and returns its value from a
dictionary?
a) dict.pop(key) b) dict.remove(key)
c) dict.delete(key) d) dict.erase(key)
43. What is the time complexity of looking up a key in a dictionary in Python?
a) O(1) b) O(n)
c) O(log n) d) O(n^2)
44. Which of the following functions can be used to create a dictionary from two lists, one of
keys and the other of values?
a) dict(zip(keys, values)) b) dict(keys, values)
c) dict.create(keys, values) d) dict(pair)
45. What is the purpose of the dict.update() method in Python?
a) Adds new keys to the dictionary
b) Replaces the entire dictionary with another dictionary
c) Merges another dictionary or iterable of key-value pairs into the current dictionary
d) Removes specific keys from the dictionary
46. How would you create a dictionary with keys 'a', 'b', and 'c' all initialized to 0 in Python?
a) dict('a'=0, 'b'=0, 'c'=0) b) dict.fromkeys(['a', 'b', 'c'], 0)
c) dict(['a', 'b', 'c'], 0) d) dict({'a': 0, 'b': 0, 'c': 0})
47. Which of the following is used to create a new dictionary from an iterable of key-value pairs?
a) dict.update() b) dict()
c) dict.fromkeys() d) dict.items()
48. What is the output of dict([('a', 1), ('b', 2)])?
a) {'a': 1, 'b': 2} b) [['a', 1], ['b', 2]]
c) ('a', 1), ('b', 2) d) {('a', 1), ('b', 2)}
49. Which of the following methods removes all items from a dictionary?
a) dict.clear() b) dict.delete()
c) dict.popall() d) dict.remove())
50. What is the behavior of dict.setdefault(key, default) in Python if the key already exists in the
dictionary?
a) It replaces the key’s value with default. b) It throws an exception.
c) It returns the current value of the key. d) It does nothing
51. Which built-in function can be used to find the largest item in an iterable?
a) max() b) min()
c) sum() d) abs()
52. What does the len() function return?
a) lower() b) upper()
c) casefold() d) capitalize()
54.What is the purpose of the round() function?
a) Converts a string to an integer b) Rounds a number to the nearest integer
c) Converts an integer to a float d) Returns the absolute value
55. In Python, what keyword is used to execute a block of code when a condition is true?
a) if b) else
c) elif d) switch
2. In pure function always produce the _____ output for the ______ arguments irrespective of
anything else.
a)Same, Different b)Different, Same c)Same, Same d)Different, Different
6.The first-class variables can be passed to functions as a ________, can be returned from
functions, or stored in data structures.
a)class b)inheritance c)parameter d)arguments
7. What is the purpose of using *args in a function definition?
a) To pass a fixed number of arguments.
b) To pass a variable number of arguments to a function.
c) To specify default values for arguments.
d) To pass keyword arguments.
8. When calling a function, the order of arguments matters unless you're using keyword
Arguments is_________
a) Positional arguments b)Named arguments
c)Missing arguments d) Default and Positional Arguments
9. If a required argument is missing, Python raises a TypeError is_________
a) funcational call b) named arguments c)missing arguments d)positional arguments
10. The *args allows you to pass a variable number of arguments to the function is _____.
a) Positional arguments b)Named arguments c)Missing arguments d)variable arguments
11. What is the correct syntax to define a function in Python?
a) function myFunc(): b) def myFunc(): c) create myFunc(): d) def function myFunc():
12. Which of the following statements is true about the return value of a function in Python?
a) A function without a return statement will always return None.
b) A function with a return statement can only return integers.
c) A function can return only one value, but that value can be a list.
d) A function must return a value; otherwise, it will result in an error.
13. What will happen if a function is called with fewer arguments than it has parameters?
a) Python will throw a SyntaxError.
b) Python will use default values for missing arguments, if specified.
c) The function will not execute.
d) Python will ignore the extra parameters and proceed.
14. Which of the following is the correct way to define a function that accepts variable numbers
of arguments?
a) def myFunc(*args): b) def myFunc(**kwargs):
c) def myFunc(*args,**kwargs): d) def myFunc(args):
18. Which of the following allows you to pass an arbitrary number of keyword arguments to a
Python function?
a) *args b) * c) **kwargs d) **args
19. Which of the following is the correct way to call a function with named arguments in
Python?
a) myFunc(name="Alice", age=25) b) myFunc("Alice", age=25)
c) myFunc(name="Alice", 25) d) myFunc(name=Alice, age=25)
21. Which of the following is the correct way to pass a function as an argument to another
function in Python?
a) function1(function2) b) function1(&function2)
c) function1(function2()) d) function1(function2)
23. Which of the following is the correct way to pass a lambda function to another function in
Python?
a) function1(lambda x: x+1) b) function1(lambda x = 1: x+1)
c) function1(x: lambda x: x+1 d) function1(lambda)
24. Which of the following Python code snippets correctly uses the map() function with a
function passed as an argument?
a) map(add, [1, 2, 3]) b) map([1, 2, 3], add)
c) map(lambda x: x + 1, [1, 2, 3]) d) map(add, [1, 2, 3], [4, 5, 6])
25.Which of the following statements correctly uses the filter() function with a function passed
as an argument?
a) filter(lambda x: x > 5, [1, 3, 6, 8]) b) filter([1, 3, 6, 8], lambda x: x > 5)
c) filter(lambda x > 5, [1, 3, 6, 8]) d) filter(x > 5, [1, 3, 6, 8])
26. Which of the following is the correct way to find the length of a list in Python?
a) length(list) b) list.len() c) len(list) d) length_of(list)
29. Which of the following functions is used to convert a string into an integer?
a) strtoint() b) int() c) string_to_int() d) convert_to_int()
30. Which of the following functions can be used to round a floating point number to the nearest
integer?
a) round() b) floor() c) ceil() d) int()
33. Which function is used to get the maximum value from a list?
a) max_value() b) maximum() c) max() d) highest()
34. Which of the following functions is used to return the unique elements of a sequence in
Python?
a) unique() b) distinct() c) set() d) remove_duplicates()
36. Which of the following functions is used to join elements of an iterable into a string?
a) join() b) concatenate() c) merge() d) combine()
38. Which function can be used to check whether an object is callable in Python?
a) callable() b) is_call() c) can_call() d) check_call()
41. Which built-in function is used to remove leading whitespaces from a string?
a) trim() b) strip() c) lstrip() d) rstrip()
46. Which of the following functions is used to apply a function along an axis of a
DataFrame in Pandas?
a) map() b) apply() c) reduce() d) filter()
49. Which function can be used to combine elements of an iterable using a binary function
(e.g., addition, multiplication)?
a) map() b) reduce() c) filter() d) apply()
50. In the filter() function, the function applied to the iterable must return:
a) True or False b) An iterable of the same length
c) The transformed elements d) A boolean list
54. Which function is used to apply a function element-wise to a sequence, like a list, and return
the results as a new iterable?
a) apply() b) filter() c) reduce() d) map()
55. How many arguments does the reduce() function take in Python?
a) One b) Two c) Three d) Four
56. Which of the following is the correct way to use reduce() in Python?
a) reduc() b) reduce(x, y: x + y, [1, 2, 3, 4], 0)
c) reduce(lambda x, y: x * y, [1, 2, 3, 4]) d) (x, y: x + y, [1, 2, 3, 4], 0)
57. Which of the following will return the square of each number from a list using map()?
a) map(lambda x: x**2, [1, 2, 3, 4]) b) map(lambda x: x > 2, [1, 2, 3, 4])
c) map(lambda x: x + 1, [1, 2, 3, 4]) d) map(lambda x: x - 1, [1, 2, 3, 4])
58. Which of the following functions in Python is often used in functional programming and
takes two arguments to cumulatively reduce a sequence?
a) map() b) reduce() c) apply() d) filter()
60. Which of the following statements about the apply() function in Python's Pandas is correct?
a) apply() can be used only on lists.
b) apply() can apply a function along either axis of a DataFrame (rows or columns).
c) apply() returns a function object.
d) apply() can only be used for mathematical functions.
63. Which of the following commands imports the entire math module?
a) from math import * b) import math as m c) import math d) include math
65. Which of the following functions is used to get the path of the current module in Python?
a) os.path.current() b) os.getcwd() c) sys.argv() d) sys.path
67. How can you check whether a module is already imported in Python?
a) module.is_loaded() b) import sys; sys.modules
c) module.is_imported() d) import module; module.exists()
69. Which of the following is the correct way to reload a module in Python?
a) reload(module_name) b) importlib.reload(module_name)
c) module_name.reload() d) import reload(module_name)
72. In Python, which of the following is the correct way to import a module from a subpackage?
a) from subpackage import module_name
b) import subpackage.module_name
c) import subpackage as module_name
d) import subpackage.module_name as module_name
73. What will the following statement do in Python? import os; os.system('echo Hello')
a) It prints "Hello" to the console
b) It imports the os module
c) It executes the command echo Hello in the system's shell
d) It returns "Hello" as a string
74. Which module is commonly used for handling file and directory operations in Python?
a) os b) sys c) time d) random
75. Which module would you import if you want to work with JSON data in Python?
a) json b) csv c) xml d) yaml
78. Which of the following modules would you import for working with dates and times in
Python?
a) datetime b) calculator c) software d) datetime,time,calendar
79. How do you access the list of modules currently imported in a Python program?
a) sys.modules b) os.modules c) imported_modules() d) list(modules)
80. Which of the following is NOT a built-in module in Python?
a) math b) datetime c) re d) numpy
81. What is the correct way to import the math module in Python?
a) import math() b) import math[]
c) import math d) from math import *
82. Which of the following functions is used to load a module in Python?
a) load() b) import() c) require() d) import
83. How do you access the pi constant from the math module?
a) math.pi() b) math(pi) c) math.pi d) pi(math)
84. What is the purpose of the __name__ variable in Python modules?
a) It is used to define the module name for logging.
b) It is used to test whether the module is being run as a standalone script or imported into
another script.
c) It is used to name functions within a module.
d) It is a built-in variable that holds the module's author name.
85. Which of the following is the correct syntax to write to a file in Python?
a) file.write("Hello World") b) write.file("Hello World")
c) write("file", "Hello World") d) file("Hello World").write()
86. Which method is used to open a file in write mode in Python?
a) open("filename", "r") b) open("filename", "w")
c) open("filename", "a") d) open("filename", "rw")
87. What is the default mode when opening a file in Python?
a) r (read) b) w (write) c) a (append) d) rb (read binary)
88. Which of the following methods is used to read the contents of a file in Python?
a) file.read() b) file.get() c) file.load() d) file.text()
89. What will happen if you try to open a file that does not exist in Python with the mode w
(write)?
a) Python will raise a FileNotFoundError.
b) Python will create a new empty file.
c) Python will print a warning but continue running.
d) Python will return a None value.
90. Which of the following is the correct way to import a specific function sqrt from the math
module in Python?
a) import sqrt from math b) from math import sqrt
c) import math.sqrt d) from sqrt import math
91. Which of the following is the correct way to append text to a file in Python?
a) open("file.txt", "w").write("Hello World")
b) open("file.txt", "a").write("Hello World")
c) open("file.txt", "r").append("Hello World")
d) open("file.txt", "a").append("Hello World")
92. Which Python function can be used to list all the modules that are currently imported?
a) list_modules() b) get_imports() c) dir() d) imported()
93. Which method is used to close a file after opening it in Python?
a) file.close() b) file.end() c) file.stop() d) file.quit()
94. Which of the following Python commands will delete a module from memory?
a) del module b) remove(module) c) unload(module) d) delattr(module)
95. Which of the following is the correct way to read a file line by line in Python?
a) for line in file: b) file.readlines() c) file.read() d) file.each_line()
96. How do you open a file for reading and writing in Python?
a) open("file.txt", "rw") b) open("file.txt", "r+")
c) open("file.txt", "w+") d) open("file.txt", "rw+")
97. What is the function __init__() used for in Python modules?
a) It initializes the module variables. b) It initializes the Python interpreter.
c) It defines the main function in a script. d) It initializes class instances.
98. What is the output of the following code snippet?
import os
print(os.getcwd())
a) Prints the current working directory. b) Prints the os module’s location.
c) Prints the home directory. d) Prints the last directory used.
99. What does the import statement do in Python?
a) It imports functions and variables from external modules into the current script.
b) It loads external files into the Python interpreter.
c) It imports data from external sources like databases.
d) It executes a file as a standalone script.
100. Which of the following file modes is used for reading binary files in Python?
a) rb b) wb c) r d) a
101. Which of the following functions is used to import a specific function from a module in
Python?
a) import module.function b) from module import function
c) import function d) from function import module
102. Which Python module provides the function sqrt() to calculate the square root of a number?
a) math b) cmath c) statistics d) random
103. What will the following code output?
import random
print(random.randint(1, 10))
a) A random float between 1 and 10 b) A random integer between 1 and 10
c) A random string of length 10 d) A random number between 1 and 10, inclusive
104. Which function is used to get the current date and time in Python?
a) datetime.now() b) time.get_time() c) datetime.today() d) date.current()
105. Which function in Python is used to read all lines from a file and return them as a list?
a) readlines() b) read() c) getlines() d) filelines()
106. What will be the result of the following Python code?
import math
print(math.ceil(3.14))
a) 3 b) 3.0 c) 4 d) 2
107. Which module is used to generate pseudo-random numbers in Python?
a) random b) math c) randomize d) numpy
108. Which of the following Python functions returns the absolute value of a number?
a) abs() b) absval() c) value() d) absolute()
109. What does the dir() function do in Python?
a) Returns the directory of a module
b) Lists all the attributes and methods of an object
c) Returns the current working directory
d) Returns the name of the directory in which a module is located
110. Which of the following Python built-in functions can be used to return the length of an
object (string, list, tuple, etc.)?
a) len() b) length() c) size() d) count()
111. Which Python function is used to convert a string to lowercase?
a) lower() b) tolower() c) convert() d) casefold()
112. What does the help() function do in Python?
a) Returns a list of all available modules
b) Provides documentation about a function, class, or module
c) Prints the error traceback
d) Offers suggestions for troubleshooting
113. Which of the following modules in Python is used to work with regular expressions?
a) regex b) re c) regexp d) pattern
114. What will be the result of the following code?
import sys
print(sys.version)
a) Python's current version number b) Python's installation location
c) The version of a specific library d) The operating system version
115. Which of the following built-in functions is used to round a floating point number to the
nearest integer in Python?
a) round() b) ceil() c) floor() d) int()
UNIT 5
1. Why is database programming important in Python?
a) To perform mathematical calculations
b) To store and manage large amounts of data efficiently
c) To create graphical user interfaces
d) To write system-level code
2. Which Python library is commonly used for database connectivity?
a) NumPy b) Matplotlib c) SQLite3 d) TensorFlow
3. What is the primary benefit of using an ORM (Object-Relational Mapping) tool in
Python?
a) It speeds up the execution of Python scripts.
b) It allows interaction with databases using Python objects instead of SQL
cueries.
c) It eliminates the need for any database.
d) It improves the performance of graphical applications.
4. Which of the following is NOT a type of database you can work with in Python?
a) Relational databases b) NoSQL databases
c) In-memory databases d) Word processing databases
5. In Python, which statement is true regarding SQL injection?
a) It is a technique to improve database performance.
b) It is a type of security vulnerability that allows attackers to execute
arbitrary SQL code.
c) It is a method for creating database tables.
d) It is a way to optimize database queries.
6. What is the role of a database connection in Python programming?
a) To manage user sessions in web applications
b) To facilitate communication between the Python application and the
database
c) To generate HTML content for web pages
d) To perform data visualization
7. Which of the following SQL commands is used to retrieve data from a database in
Python?
a) INSERT b) DELETE c) UPDATE d) SELECT
8. Why might a developer choose to use a NoSQL database with Python?
a) To enforce strict schema requirements
b) To handle large volumes of unstructured data
c) To perform complex SQL queries
d) To utilize traditional relational database features
9. What is a common use case for database programming in Python applications?
a) Writing mathematical algorithms
b) Storing user information and application data
c) Rendering graphics
d) Generating random numbers
10. Which of the following statements is true about transactions in a database context?
a) Transactions are not important for data integrity.
b) A transaction can contain multiple SQL statements and can be committed
or rolled back.
c) Transactions only apply to NoSQL databases.
d) Transactions are automatically managed by Python.
11. What is the purpose of SQL (Structured Query Language) in Python programming?
a) To format strings and manipulate text
b) To create and manage relational databases
c) To generate graphical user interfaces
d) To handle web requests
12. Which of the following SQL commands is used to insert new records into a table?
a) UPDATE b) INSERT c) SELECT d) DELETE
13. In Python, which library is commonly used for executing SQL commands with SQLite
databases?
a) NumPy b) Matplotlib c) SQLite3 d) Pandas
14. What is the correct SQL command to retrieve all columns from a table named
"customers"?
a) SELECT * FROM customers; b) GET ALL FROM customers;
c) SHOW customers; d) FETCH ALL FROM customers;
14. Which statement best describes the role of a primary key in a database table?
a) It is used to define the data type of each column.
b) It uniquely identifies each record in the table.
c) It is an optional field that can be null.
d) It creates a relationship between two tables.
15. How do you ensure that changes made in a transaction are saved to the database in
Python?
a) By closing the connection b) By committing the transaction
c) By rolling back the transaction d) By using the SELECT command
16. What is the purpose of the cursor object in Python's database programming?
a) To connect to the database
b) To execute SQL commands and fetch results
c) To close the database connection
d) To create new database tables
17. Which of the following SQL commands is used to update existing records in a table?
a) UPDATE b) MODIFY c) CHANGE d) ALTER
18. What SQL clause is used to filter records in a query?
a) WHERE b) HAVING c) GROUP BY d) ORDER BY
19. Which of the following statements correctly demonstrates how to connect to an SQLite
database in Python?
a) db = sqlite3.connect('database.db') b) db = sqlite3.open('database.db')
c) db = sqlite3.create('database.db') d) db = sqlite3.access('database.db')
20. When executing a SQL command, which method is used to fetch the results from a
cursor object?
a) cursor.get() b) cursor.fetch() c) cursor.fetchall() d) cursor.retrieve()
21. What does the DROP TABLE command do in SQL?
a) Deletes all records from the table but keeps the structure
b) Deletes the entire table along with all its data and structure
c) Modifies the structure of the table
d) Creates a new table
22. Which of the following SQL commands is used to delete records from a table?
a) REMOVE b) DELETE c) DROP d) TRUNCATE
23. Which Python method is used to close a database connection?
a) db.end() b) db.close() c) db.quit() d) db.terminate()
24. In SQL, what is the purpose of the ORDER BY clause?
a) To sort the results of a query
b) To filter records based on a condition
c) To join multiple tables
d) To group records based on a specific column
a) ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
b) ^\w+@\w+\.\w+$
c) ^[a-zA-Z0-9]+@[a-z]+\.[a-z]{2,3}$
d) ^.+@.+\..+$
50. How would you check if a string starts with the word "Hello"?
a) re.match("Hello", string) b) re.search("Hello", string)
c) re.findall("Hello", string) d) re.begin("Hello", string)
51. What does the \s pattern match in a regular expression?
a) A single whitespace character (space, tab, newline)
b) A non-whitespace character
c) A digit character
d) A letter character
52. Which of the following statements about the re.split() function is true?
a) It splits a string based on the matches of the pattern and returns a list.
b) It replaces occurrences of a pattern in a string with a specified replacement.
c) It searches for a pattern and returns the first match.
d) It compiles a regular expression pattern for future use.
53. How can you make a regular expression case-insensitive in Python?
a) By using the flag
b) By using the re.CASEINSENSITIVE flag
c) By using re.compile('pattern', re.I)
d) By using Pattern
54. Which of the following is NOT a valid use of regular expressions in Python?
a) Validating user input (e.g., email, phone number)
b) Searching for specific patterns in strings
c) Performing complex mathematical computations
d) Replacing substrings based on patterns
55. What does the # symbol represent in Python?
a) The start of a variable name b) A comment
c) An operator d) A string delimiter
56. Which of the following is the escape character in Python?
a) \ b) / c) # d) @
57. What is the purpose of the \n character in a string?
a) To represent a tab b) To represent a new line
c) To escape the next character d) To terminate the string
58. Which symbol is used to denote a string in Python?
a) " b) & c) # d) $
59. What does the \t character represent in a string?
a) A new line b) A tab character c) A backslash d) A space character
60. What is the function of the __ (double underscore) prefix in a Python class attribute?
a) It makes the attribute public.
b) It indicates a private variable with name mangling.
c) It makes the attribute static.
d) It indicates a global variable.
61. Which symbol is used for list comprehensions in Python?
a) [] b) {} c) () d) //
62. What does the * symbol do when used in a function definition?
a) It denotes multiplication.
b) It indicates variable-length arguments (arbitrary argument list).
c) It creates a list.
d) It signifies a generator.
63. What is the purpose of the @ symbol in Python?
a) To denote a variable b) To create decorators
c) To define lists d) To concatenate strings
64. What does the ** symbol indicate when used in function arguments?
a) It multiplies the arguments.
b) It allows for variable-length keyword arguments.
c) It indicates a type hint.
d) It denotes an unpacking of a list.