0% found this document useful (0 votes)
10 views

Python Mid Ans

Uploaded by

Neelam Raju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Mid Ans

Uploaded by

Neelam Raju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MCQs:

1. Which of the following commands will create a list?


o a) list1 = list()
o b) list1 = []
o c) list1 = list ([1 2 3])
o d) all of the mentioned (Ans.)
2. Suppose list1 is [2 33 222 14 25] What is list1[-1]?
o a) Error
o b) None
o c) 25 (Ans.)
o d) 2
3. What will be the output of the following Python code?

python
Copy code
names1 = ['Srikanth' 'Divya' 'SriCharan']
if 'Srikanth' in names1:
print(1)
else:
print(2)

o a) None
o b) 1 (Ans.)
o c) 2
o d) Error
4. Which of the following is a Python tuple?
o a) [1 2 3]
o b) (1 2 3) (Ans.)
o c) {1 2 3}
o d) {}
5. What will be the output of the Code

python
Copy code
>>> my_tuple = (1 2 3 4)
>>> my_tuple.append((5 6 7))
>>> print len(my_tuple)

o a) 1
o b) 2
o c) 5
o d) Error (Ans.)
6. What type of data is: a=[(11)(24)(39)]?
o a) Array of tuples
o b) List of tuples
o c) Tuples of lists
o d) Invalid type (Ans.)
7. Which keyword is used for function?
o a) Fun
o b) Define
o c) def (Ans.)
o d) Function
8. Which of the following is the use of id() function in python?
o a) Id returns the identity of the object (Ans.)
o b) Every object doesn’t have a unique id
o c) All of the mentioned
o d) None of the mentioned
9. What will be the output of the following Python code?

python
Copy code
def cube(x):
return x * x * x
x = cube(3)
print x

o a) 9
o b) 0
o c) 27 (Ans.)
o d) 30
10. Which of the following statements create a dictionary?
o a) d = {}
o b) d = {"Ranjith":40 "Vishnu":45}
o c) d = {40:" Ranjith" 45:" vishnu"}
o d) All of the mentioned (Ans.)
11. Suppose listExample is ['h' 'e' 'l' 'l' 'o'] what is len(listExample)?
o a) 5 (Ans.)
o b) 4
o c) None
o d) Error
12. To which of the following the “in” operator can be used to check if an item is in
it?
o a) Lists
o b) Dictionary
o c) Set
o d) All of the mentioned (Ans.)
13. Suppose list1 is [3 5 25 1 3] what is min(list1)?
o a) 3
o b) 5
o c) 25
o d) 1 (Ans.)
14. >>t=(1243) >>>t[1:3]
o a) (1 2)
o b) (1 2 4)
o c) (2 4)
o d) (2 4 3) (Ans.)
15. What will be the output of the following Python code?

python
Copy code
>>> t = (1 2)
>>> 2 * t
o a) (1 2 1 2) (Ans.)
o b) [1 2 1 2]
o c) (1 1 2 2)
o d) [1 1 2 2]
16. What will be the output of the following Python code?

python
Copy code
>>> a=(12)
>>> b=(34)
>>> c=a+b
>>> c

o a) (46)
o b) (1234)
o c) Error as tuples are immutable (Ans.)
o d) None
17. What will be the output of the following Python code?

python
Copy code
def printMax(a b):
if a > b:
print(a 'is maximum')
elif a == b:
print(a 'is equal to' b)
else:
print(b 'is maximum')
printMax(3 4)

o a) 3
o b) 4
o c) 4 is maximum (Ans.)
o d) None of the mentioned
18. Which of the following refers to mathematical function?
o a) sqrt (Ans.)
o b) rhombus
o c) add
o d) summation
19. >> d = {"Ranjith":40 "Vishnu":45} >>> "Ranjith" in d
o a) True (Ans.)
o b) False
o c) None
o d) Error
20. What will be the output of the following Python code snippet?

python
Copy code
d = {"vishnu":40 "peter":45}
d["vishnu"]

o a) 40 (Ans.)
o b) 45
o c) “vishnu”
o d) “peter”
21. Suppose list1 is [1 5 9] what is sum(list1)?
o a) 1
o b) 9
o c) 15 (Ans.)
o d) Error
22. What will be the output of the following Python code?

python
Copy code
numbers = [1 2 3 4]
numbers.append([5678])
print(len(numbers))

o a) 4
o b) 5 (Ans.)
o c) 8
o d) 12
23. What will be the output of the following Python code?

python
Copy code
list1 = [1 2 3 4]
list2 = [5 6 7 8]
print(len(list1 + list2))

o a) 2
o b) 4
o c) 5
o d) 8 (Ans.)
24. What will be the output of the following Python code?

python
Copy code
>>> a=("Check")*3
>>> a

o a) (‘Check’’Check’’Check’)
o b) * Operator not valid for tuples
o c) (‘CheckCheckCheck’) (Ans.)
o d) Syntax error
25. What will be the output of the following Python code?

python
Copy code
>>> a=(1234)
>>> del(a[2])

o a) Now a=(124)
o b) Now a=(134)
o c) Now a=(34)
o d) Error as tuple is immutable (Ans.)
26. Which of the following is a feature of DocString?
o a) Provide a convenient way of associating documentation with Python
modules functions classes and methods
o b) All functions should have a docstring
o c) Docstrings can be accessed by the doc attribute on objects
o d) All of the mentioned (Ans.)
27. What are the two main types of functions?
o a) Custom function
o b) Built-in function & User defined function (Ans.)
o c) User function
o d) System function
28. Where is function defined?
o a) Module
o b) Class
o c) Another function
o d) All of the mentioned (Ans.)
29. What will be the output of the following Python code snippet?

python
Copy code
d = {"john":40 "peter":45}
print(list(d.keys()))

o a) [“Ranjith” “Vishnu”]
o b) [“Ranjith”:40 “Vishnu”:45]
o c) (“Ranjith” “Vishnu”)
o d) (“Ranjith”:40 “Vishnu”:45) (Ans.)
30. >>> a=dict() >>> a[1]
o a) An empty dictionary
o b) Key error (Ans.)
o c) None
o d) Error
31. Array will store --------------- type of elements.
o a) homogeneous (Ans.)
32. List will store ----------------type of elements.
o a) heterogeneous (Ans.)
33. The Key-value pair contains------------datatype.
o a) Dict (Ans.)
34. Tuple is mutable data type object(Yes/no)
o a) No (Ans.)
35. A try block contains multiple exceptions True or False
o a) True (Ans.)
36. Single commenting is done by ------------Symbol
o a) # (Ans.)
37. To create a function the keyword is………….
o a) def (Ans.)
38. Input() is a built in function--------------(true/False)
o a) True (Ans.)
39. Pop() will delete the -------------element.
o a) Last (Ans.)
40. Dividing the elements into smaller parts are called in Python is………….
o a) Decomposition (Ans.)
41. List is ---------------------Object.
o a) Mutable (Ans.)
42. The elements can be added with --------------method.
o a) append() (Ans.)
43. Function can be defined with ----------------Keyword.
o a) def (Ans.)
44. print () is built in method(yes/no)
o a) Yes (Ans.)
45. Dictionary is a pair of __________ and --------------------
o a) Key and value (Ans.)
46. d = {“Vishnu”:40 “Ranjith”:45}. To obtain the number of entries in dictionary
which command do we use?
o a) len(d) (Ans.)
47. d = {“Vishnu”:40 “Ranjith”:45}. then print(d.keys()) will give………
o a) ['Vishnu', 'Ranjith'] (Ans.)
48. Exception is------------------termination.
o a) Abnormal (Ans.)
49. try will identify the----------------in code.
o a) Exceptions (Ans.)
50. Except will throw suitable----------------- to the user for the error.
o a) Messages (Ans.)
51. When the exception is raised or not raised ---------------executed.
o a) Finally (Ans.)
52. What happens when ‘1’ == 1 is executed? Result is (True/False)
o a) False (Ans.)
53. How many except statements can a try-except block have?
o a) Multiple (Ans.)
54. when there is no exception------------block will executed.
o a) Else (Ans.)
55. >>t=(1243) >>>t[1:3] output is----------
o a) (2 4) (Ans.)
56. d = {"john":40 "peter":45} print(d["john"]) is---------
o a) 40 (Ans.)
57. Tuple is immutable datatype------------(yes/no)
o a) Yes (Ans.)
58. To add the elements in list the method is…………
o a) append() (Ans.)
59. to add the elements in dictionary method is……….
o a) update() (Ans.)
60. The len() function meant for………………..
o a) Finding the length (Ans.)

Fill in the blanks:

1. Array will store --------------- type of elements.


o Ans. homogeneous
2. List will store ----------------type of elements.
o Ans. heterogeneous
3. The Key-value pair contains------------datatype.
o Ans. Dict
4. Tuple is mutable data type object(Yes/no)
o Ans. No
5. A try block contains multiple exceptions True or False
o Ans. True
6. Single commenting is done by ------------Symbol
o Ans. #
7. To create a function the keyword is………….
o Ans. def
8. Input() is a built in function--------------(true/False)
o Ans. True
9. Pop() will delete the -------------element.
o Ans. Last
10. Dividing the elements into smaller parts are called in Python is………….
o Ans. Decomposition
11. List is ---------------------Object.
o Ans. Mutable
12. The elements can be added with --------------method.
o Ans. append()
13. Function can be defined with ----------------Keyword.
o Ans. def
14. print () is built in method(yes/no)
o Ans. Yes
15. Dictionary is a pair of __________ and --------------------
o Ans. Key and value
16. d = {“Vishnu”:40 “Ranjith”:45}. To obtain the number of entries in dictionary
which command do we use?
o Ans. len(d)
17. d = {“Vishnu”:40 “Ranjith”:45}. then print(d.keys()) will give………
o Ans. ['Vishnu', 'Ranjith']
18. Exception is------------------termination.
o Ans. Abnormal
19. try will identify the----------------in code.
o Ans. Exceptions
20. Except will throw suitable----------------- to the user for the error.
o Ans. Messages
21. When the exception is raised or not raised ---------------executed.
o Ans. Finally

1. What is a function, what is the keyword to define the function with Syntax. Write a
program to reverse a given number.

A function in Python is a block of reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of code
reusing. The keyword to define a function is def.
python
Copy code
# Program to reverse a given number
num = int(input("Enter a number: "))
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: ", reversed_num)

2. Write the Properties of list, create a list, add the elements in the list, print the list, how
many times an element is occurred in the list.

Properties of list: Lists are ordered, mutable, and allow duplicate elements.

python
Copy code
# Creating a list and adding elements
my_list = []
my_list.append(1)
my_list.append(2)
my_list.append(2)
my_list.append(3)

# Printing the list


print("List: ", my_list)

# Counting occurrences of an element


element = 2
print(f"Occurrence of {element}: ", my_list.count(element))

3. Explain Tuple data type, create a tuple, and add the elements in the tuple, and print
the tuple.

A tuple is an immutable sequence of Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists, and tuples
use parentheses, whereas lists use square brackets.

python
Copy code
# Creating a tuple
my_tuple = (1, 2, 3)

# Tuples are immutable, so we can't add elements to them directly

# Printing the tuple


print("Tuple: ", my_tuple)

4. Explain arrays in Python and explain with syntax. Explain module and create an
integer, float, Unicode character array, and display them.
Arrays in Python are collections of items stored at contiguous memory locations. The array
can hold only a single data type elements. The module array allows us to create arrays.

python
Copy code
import array as arr

# Integer array
int_arr = arr.array('i', [1, 2, 3])
print("Integer array: ", int_arr)

# Float array
float_arr = arr.array('f', [1.5, 2.5, 3.5])
print("Float array: ", float_arr)

# Unicode character array


unicode_arr = arr.array('u', 'hello')
print("Unicode array: ", unicode_arr)

5. Write a program to check the biggest number in given three given numbers using
functions.

python
Copy code
# Program to check the biggest number among three given numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

if num1 > num2 and num1 > num3:


print(f"The biggest number is: {num1}")
elif num2 > num3:
print(f"The biggest number is: {num2}")
else:
print(f"The biggest number is: {num3}")

6. Write a program to read Student Name, Hall ticket number along with subject1,
subject2, subject3 marks and find the average if average is greater than 75 display
distinction. If average is between 65 to 75 display first class if average is less than 60
display pass otherwise display failed.

python
Copy code
# Program to read student details and calculate average
student_name = input("Enter student name: ")
hall_ticket = input("Enter hall ticket number: ")
subject1 = int(input("Enter marks for subject 1: "))
subject2 = int(input("Enter marks for subject 2: "))
subject3 = int(input("Enter marks for subject 3: "))

average = (subject1 + subject2 + subject3) / 3

print(f"Average marks: {average}")


if average > 75:
print("Distinction")
elif 65 <= average <= 75:
print("First Class")
elif 60 <= average < 65:
print("Pass")
else:
print("Failed")

7. Explain While loop, write a python program to read a number from the keyboard and
check the number is palindrome number or not.

A while loop in Python repeatedly executes a target statement as long as a given condition is
true.

python
Copy code
# Program to check if a number is a palindrome
num = int(input("Enter a number: "))
original_num = num
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

if original_num == reversed_num:
print(f"{original_num} is a palindrome number")
else:
print(f"{original_num} is not a palindrome number")

8. Write a program to sum the given digits of the number.

python
Copy code
# Program to sum the digits of a number
num = int(input("Enter a number: "))
sum_of_digits = 0

while num != 0:
digit = num % 10
sum_of_digits += digit
num //= 10

print(f"Sum of digits: {sum_of_digits}")

9. Write a program to check whether a given number is Armstrong number or not.

An Armstrong number is a number that is equal to the sum of cubes of its digits.

python
Copy code
# Program to check if a number is an Armstrong number
num = int(input("Enter a number: "))
sum_of_cubes = 0
temp = num

while temp != 0:
digit = temp % 10
sum_of_cubes += digit ** 3
temp //= 10

if num == sum_of_cubes:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")

10. Write a program to check whether the given input is digit or lowercase character or
uppercase character or a special character (use 'if-else-if' ladder).

python
Copy code
# Program to check the type of character
char = input("Enter a character: ")

if char.isdigit():
print(f"{char} is a digit")
elif char.islower():
print(f"{char} is a lowercase character")
elif char.isupper():
print(f"{char} is an uppercase character")
else:
print(f"{char} is a special character")

11. Explain functions in python with Syntax and Write a program to create a list and add
the elements using functions and display the list.

Functions in Python are defined using the def keyword. They allow for code reusability and
better organization. The syntax for defining a function is as follows:

python
Copy code
# Function definition
def function_name(parameters):
# Function body
pass

Program to create a list, add elements, and display the list using functions:

python
Copy code
# Creating a list and adding elements using functions
def create_list():
return []
def add_element(lst, element):
lst.append(element)

def display_list(lst):
print("List: ", lst)

# Main program
my_list = create_list()
add_element(my_list, 1)
add_element(my_list, 2)
add_element(my_list, 3)
display_list(my_list)

12. Write a program to check whether given letter is alphabet or not if so check it is
vowel or not.

python
Copy code
# Program to check if a letter is an alphabet and if it is a vowel
char = input("Enter a character: ")

if char.isalpha():
if char.lower() in 'aeiou':
print(f"{char} is a vowel")
else:
print(f"{char} is a consonant")
else:
print(f"{char} is not an alphabet")

13. What is mean by exception? Explain Exception handling in python. Write a program
to demonstrate try.... Except block with a suitable program.

An exception is an error that occurs during the execution of a program. Exception handling in
Python uses the try, except, else, and finally blocks to handle exceptions gracefully.

python
Copy code
# Program to demonstrate try...except block
try:
num1 = int(input("Enter the numerator: "))
num2 = int(input("Enter the denominator: "))
result = num1 / num2
print(f"Result: {result}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed")
except ValueError:
print("Error: Invalid input, please enter numeric values")

14. Explain Tuple data type, create a tuple, and add the elements in the tuple, and print
the tuple.
A tuple is an immutable sequence of Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists, and tuples
use parentheses, whereas lists use square brackets.

python
Copy code
# Creating a tuple
my_tuple = (1, 2, 3)

# Tuples are immutable, so we can't add elements to them directly

# Printing the tuple


print("Tuple: ", my_tuple)

15. Explain arrays in Python and explain with syntax. Explain module and create an
integer, float, Unicode character array, and display them.

Arrays in Python are collections of items stored at contiguous memory locations. The array
can hold only a single data type elements. The module array allows us to create arrays.

python
Copy code
import array as arr

# Integer array
int_arr = arr.array('i', [1, 2, 3])
print("Integer array: ", int_arr)

# Float array
float_arr = arr.array('f', [1.5, 2.5, 3.5])
print("Float array: ", float_arr)

# Unicode character array


unicode_arr = arr.array('u', 'hello')
print("Unicode array: ", unicode_arr)

You might also like