MCQ-2
MCQ-2
Question 1: The _____ function is used to generate a random integer between 1 and 100
(inclusive) in Python.
Options:
a) rand()
b) randint(1, 100)
c) randomInt(1, 100)
d) random.randint(1, 100)
Question 2: The function used to calculate the square root of a number in Python is _______.
Options:
a) sqrt()
b) squareRoot()
c) root()
d) math.sqrt()
Question 3: The _____ function is used to get the current date in Python.
Options:
a) currentDate()
b) today()
c) getCurrentDate()
d) date.today()
Question 4: The _____ function is used to create an array filled with zeros in numpy.
Options:
a) zerosArray()
b) createZeros()
c) np.zeros()
d) zeros()
Options:
a) True
b) False
Question 2: The math.ceil() function in Python returns the smallest integer greater than or equal to
a given number.
Options:
a) True
b) False
Options:
a) True
b) False
Options:
a) True
b) False
import random
print(random.randint(1, 10))
Options:
b) 1
c) 10
d) 11
import random
print(random.choice(["apple", "banana", "cherry", "date"]))
Options:
a) A random fruit from the list
b) "apple"
c) "banana"
d) "cherry"
import math
print(math.pi)
Options:
a) 3.14
b) 3.141592653589793
c) 22/7
d) 3
import random
print(random.choice([True, False]))
Options:
a) True
b) False
c) Either True or False
d) None
import math
print(math.factorial(5))
Options:
a) 5
b) 15
c) 120
d) 25
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
Options:
a) (3, 2)
b) (2, 3)
c) [3, 2]
d) [2, 3]
import numpy as np
arr = np.arange(10)
print(arr[2:5])
Options:
a) [2, 3, 4]
b) [3, 4, 5]
c) [0, 1, 2]
d) [7, 8, 9]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr[5] = 6
print(arr)
Options:
a) The list index is out of range.
b) There is no error.
c) The assignment operator is missing.
d) The list cannot contain integers.
Question 2: Identify the error in the following code:
import random
print(random.choice([1, 2, 3, 4]))
Options:
b) There is no error.
import math
print(math.sqrt(-1))
Options:
a) The code will raise an error.
b) There is no error.
c) The sqrt function is not imported correctly.
d) Negative numbers cannot be square rooted.
Type: Theory
Question 1: Which module in Python is used for handling dates and times?
Options:
a) time
b) date
c) datetime
d) calendar
Question 2: Which module in Python provides functions for generating random numbers?
Options:
a) randomize
b) randint
c) random
d) randomize
Question 6: What is the significance of the math.floor() function in the math module?
Options:
Options:
open()
openfile()
display()
show()
Options:
Read mode
Append mode
Write mode
Options:
List of integers
List of lines
single line
Question 4 When you open a file in a+ mode file pointer will be at………
Options:
a)Beginning of file
b)End of file
Options:
a) True
b) False
a) False
b) True
b) True
Question: 4 There are three ways to read data from a text file.
a) False
b) True
Options:
a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
Options:
a) remove(file_name)
b) remove(() , file_name))
Options:
a)rename(current_file_name, new_file_name)
b)rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d)none of the mentioned
Question 4.: Choose the right code to open the file “hello.txt” in read-only mode.
Options:
open('hello.txt', 'r')
open('hello.txt', 'ro')
open('hello.txt', 'read_only')
open('w', 'hello.txt')
Question 5 : Choose the right code to open the file “animals.txt” in write-only mode.
Options:
a) open('animals.txt', 'r')
b) open('animals.txt', 'write_only')
c)open('animals.txt', 'w+')
d)open('animals.txt', 'w')
Question 6: Choose the right code to open the file “fruit.txt” in append mode.
Options:
a) open('fruit.txt', 'r+')
b) open('fruit.txt', 'w+')
c) open('fruit.txt', 'a')
d) open('fruit.txt', 'a+')
10 20 30 40 50
You want to read the contents of this file, calculate the sum of all numbers, and then write this
sum to another file named "output.txt". Which of the following Python code snippets correctly
accomplishes this task?
numbers = file.read().split()
total = sum(numbers)
output_file.write(total)
numbers = file.read().split()
output_file.write(str(total))
numbers = file.readlines()
total = sum(map(int, numbers))
output_file.write(total)
numbers = file.readlines()
output_file.write(str(total))
f = None
if i > 2:
break
print(f.closed)
a) True
b) False
c) None
d) Error
Question 3 What will be the output of the following Python code?
a)
b)
c)
fo = open("foo.txt", "wb")
fo.flush()
fo.close()
a) Compilation Error
b) Runtime Error
c) No Output
a)truncate()
b)size()
c)setsize()
d) both a and b
a)No difference
b)In case of r+ the pointer is initially placed at the beginning of the file and the pointer is at the end
for w+
c) In case of w+ the pointer is initially placed at the beginning of the file and the pointer is at the
end for r+
write()
writelines()
writeall()
None of above
Question 4 From the following which file mode is used to read from a binary file?
R+
Rb
Question 5:What function is used to open a file in Python for reading?
a) open_file()
b) read_file()
c) open()
d) read()
Question 6 What function is used to read the entire contents of a file as a string in Python?
a) read_file()
b) read_string()
c) readlines()
d) read()
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
Question:
1. The ______ block is used to wrap around the code that may raise an exception.
a) try
b) catch
c) except
d) raise
In Python, the _____ keyword is used to define code that executes after the 'try' and 'except' blocks,
regardless of whether an exception occurs or not.
a) final
b) else
c) catch
d) finally
4. The _____ keyword is used to handle exceptions when no specific exception type is specified.
a) any
b) catch
c) except
d) all
a) True
b) False
2. The else block in Python's exception handling is executed if no exceptions are raised in the try
block.
a) True
b) False
a) True
b) False
a) True
b) False
try:
result = 10 / 0
except Exception as e:
d) No error
2. What is the correct syntax for handling an exception in Python?
a)
try:
catch Exception as e:
b)
try:
except Exception as e:
c)
try:
finally:
d)
try:
else:
try:
print("Hello")
except:
else:
c) There is no mistake
5. Identify the correct way to raise a custom exception named 'MyCustomError' in Python.
class MyCustomError(Exception):
pass
try:
throw MyCustomError
except MyCustomError:
print("Custom Error")
a) raise CustomError()
b) throw MyCustomError()
except ______:
print("Invalid input.")
a) IntException
b) ValueError
c) SyntaxError
d) IOError
7. Correct the following code snippet to handle both ZeroDivisionError and ValueError:
try:
result = 10 / x
except ZeroDivisionError:
except ValueError:
try:
print(10 / 0)
except ZeroDivisionError:
b) 0
d) None
try:
raise ValueError("Error")
except ValueError as e:
print(e)
finally:
a) Error
c) Error
Error
x = "5"
y = "hello"
try:
z=x+y
print(z)
except TypeError:
a) hello
b) 5
c) 5hello
try:
print("Hello" + 10)
except TypeError:
print("Error: Type mismatch")
b) Hello
c) Hello10
a = [1, 2, 3]
try:
except:
a) An error occurred
Second element = 2
b) Second element = 2
c) An error occurred
d) Second element = 2
An error occurred
b) To ignore errors
a) It catches exceptions
a) A syntax error
c) A logical error
d) A warning message
a) Custom exceptions must always inherit from the built-in Exception class.
d) Custom exceptions provide a way to create and raise user-defined errors in Python programs.