Python MCQ Assignment: Data Types, Functions, and File Handling

Verified

Added on  2022/09/11

|6
|1191
|28
Homework Assignment
AI Summary
This document presents a solved multiple-choice question (MCQ) assignment focused on core Python programming concepts. The assignment consists of 25 questions, each worth 4 marks, covering fundamental topics such as data types (including strings), functions (defining, parameters, return values, built-in functions), control flow (if/else statements, loops), file handling (reading, writing), and data structures (sets). The solution provides the correct answer for each question. This assignment is designed to test understanding of fundamental Python syntax, semantics, and common programming tasks. The questions assess the ability to predict code output, identify correct function definitions, and understand the behavior of different Python operations and methods. The assignment is suitable for students learning introductory Python programming. This assignment is available on Desklib, a platform providing AI-based study tools, offering students access to past papers and solved assignments to aid in their learning and exam preparation.
Document Page
Section A (100 marks)
This section contains Multiple-choice questions. There are 25 questions and each question
carries 4 marks. Please provide the answer ONLY in the space provided below every
question.
Answers provided elsewhere will NOT be marked.
1. What is the name of data type for character in Python?
A. Chr
B. Char
C. Character
D. Python do not have any data type for characters they are treated as string.
Question 1 Answer D. Python do not have any data type for characters they are treated as
string.
2. What does the following code do?
def a (b, c, d): pass
A. Defines a list and initialise it
B. Defines a function, which does nothing
C. Defines a function, which passes its parameters through
D. defines an empty class
Question 2 Answer B. Defines a function, which does nothing
Q3. What will be printed if we execute the following statement?
nums = set ([1,1,2,3,3,3,4])
print(len(nums))
A. 1
B. 2
C. 4
D. 5
Question 3 Answer C. 4
Q4. What is the output of the following program?
temp = ['Geeks', 'for', 'Geeks']
arr = [i [0]. upper () for i in temp]
print(arr)
A. [‘G’, ‘F’, ‘G’]
B. [‘GEEKS’]
C. [‘GEEKS’, ‘FOR’, ‘GEEKS’]
D. Compilation error
Question 4 Answer A. [‘G’, ‘F’, ‘G’]
Q5. What is the output of the code?
if (7 < 0) and (0 < -7):
print("abhi")
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
elif (7 > 0) or False:
print("love")
else:
print("geeksforgeeks")
A. geeksforgeeks
B. love
C. abhi
D. Error
Question 5 Answer B. love
6. What is the output of the following?
True = False
while True:
print (True)
break
A. False.
B. True.
C. Error.
D. None of these
Question 6 Answer C. Error
7. What is the output of the following?
i = 1
while True:
if i % 3 == 0:
break
print(i)
i + = 1
A. 1 2 3
B. 1 2
C. Syntax Error
D. None of these
Question 7 Answer B. 1 2
8. A ----------------is a variable that is created inside a function.
A. Global variable
B. Local variable
C. Hidden variable
D. None of the above; you cannot create a variable inside a function
Question 8 Answer B. Local variable
9. This statement causes a function to end and sends a value back to the part of the
program that called the function.
Document Page
A. End
B. Send
C. Exit
D. Return
Question 9 Answer D. Return
10. A file that data is read from is known as a(n)--------------.
A. Input file
B. Output file
C. Sequential access file
D. Binary file
Question 10
Answer
A. Input file
11. The contents of this type of file can be viewed in an editor such as Notepad.
A. Text file
B. Binary
C. English file
D. Human-readable file
Question 11
Answer
A. Text file
12. You write this statement to responds to exceptions.
A. Run/handle
B. Try/except
C. Try/handle
D. Attempt/ except
Question 12
Answer
B. Try/except
13. This list method adds an item to the end of an existing list.
A. Add
B. Add to
C. Increase
D. Append
Question 13
Answer
D. Append
14. Which built-in function can be used to read input that has been typed on the
keyboard?
A. input ()
B. get_input()
C. read_input()
D. keyboard ()
Question 14
Answer
A. input()
Document Page
15. A (n) __________ is a special variable that receives a piece of data when a function is
called.
A. argument
B. parameter
C. Header
D. packet
Question 15
Answer
B. parameter
16. This set method removes an element, but does not raise an exception if the element is
not found.
A. remove
B. discard
C. delete
D. erase
Question 16
Answer
B. discard
17. What are the two main types of functions?
A. Custom function & System function
B. Built-in function & User defined function
C. User function & Build-in function
D. System function & User defined function
Question 17 Answer B. Built-in function & User defined function
18. Which are the advantages of functions in python?
A. Reducing duplication of code
B. Decomposing complex problems into simpler pieces
C. Improving clarity of the code
D. All the mentioned
Question 18 Answer D. All the mentioned
19. Which of these about a set is not true?
A. Mutable data type
B. Allows duplicate values
C. Data type with unordered values
D. Immutable data type
Question 19 Answer B. Allows duplicate values
20. If a function does not have a return statement, which of the following does the
function return?
A. int
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
B. null
C. None
D. An exception is thrown without the return statement
Question 20 Answer C. None
21. This function returns the length of a string.
A. Length
B. Size
C. Len
D. Lengthof
Question 21 Answer C. Len
22. What is the output of the following code?
x = 'abcd'
for i in range(x):
print(i)
A. b c d
B. 0 1 2 3
C. Error
D. none of the mentioned
Question 22
Answer
C. Error
23. Before reading or writing to a file, a file object must be created via
A. Open
B. Create
C. File
D. Folder
Question 23
Answer
A. Open
24. Which of the following statements are true?
A. When you open a file for reading, if the file does not exist, an error occurs
B. When you open a file for writing, if the file does not exist, a new file is created
C. When you open a file for writing, if the file exists, the existing file is overwritten with
the new file
D. All the mentioned
Question 24 Answer D. All the mentioned
Document Page
25. To read the next line of the file from a file object infile, we use
A. infile.read(2)
B. infile.read()
C. nfile.readline()
D. infile.readlines()
Question 25
Answer
C. nfile.readline()
Reference:
Barry, P. (2010). Head First Python. North Sebastopol, CA: O'Reilly Publisher.
Phillips, D. (2010). Python 3 Object Oriented Programming. Birmingham, UK: Packt
Publishing
Ramalho, L. (2015). Fluent Python: Clear, Concise, and Effective Programming. North
Sebastopol, CA: O'Reilly Publisher.
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]