Data Type And String in Python Report
VerifiedAdded on 2022/09/11
|9
|2149
|24
AI Summary
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
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")
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")
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
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.
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.
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()
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()
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
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
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
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
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
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()
Section B (30 marks)
For the problem described below, write the following:
(a) a defining diagram
(b) pseudo code
(c) a data dictionary
Problem Description
A bank requires a program to calculate fees and interest on their bank accounts. Customers
are charged an annual account fee of $40.00 for having an account. If the annual average
balance of the account is under $1000; the customer isn't paid any interest and they pay fees
of $0.50 per transaction. If the customer has an average balance of $1000 or more in their
account, then they get their first 10 transactions for free, but pay $0.50 per transaction after
this, and receive 3% interest on the average account balances. Validation of input is NOT
required.
Example Run
Run
Average Balance (negative to exit) ($)? 100
Number of transactions? 20
Fees = $50.00
Interest Rate = 0.0%
Interest = $0.00
Average Balance (negative to exit) ($)? 2000
Number of transactions? 5
Fees = $40.00
Interest Rate = 3.0%
Interest = $60.00
Average Balance (negative to exit) ($)? 5000
Number of transactions? 110
Fees = $90.00
Interest Rate = 3.0%
Interest = $150.00
Average Balance (negative to exit) ($)? -1
Solution:
(a) Defining Diagram
Input Processing Output
balance Prompt for balance fees
number of transactions Prompt for number of interest rate
A. infile.read(2)
B. infile.read()
C. nfile.readline()
D. infile.readlines()
Question 25
Answer
C. nfile.readline()
Section B (30 marks)
For the problem described below, write the following:
(a) a defining diagram
(b) pseudo code
(c) a data dictionary
Problem Description
A bank requires a program to calculate fees and interest on their bank accounts. Customers
are charged an annual account fee of $40.00 for having an account. If the annual average
balance of the account is under $1000; the customer isn't paid any interest and they pay fees
of $0.50 per transaction. If the customer has an average balance of $1000 or more in their
account, then they get their first 10 transactions for free, but pay $0.50 per transaction after
this, and receive 3% interest on the average account balances. Validation of input is NOT
required.
Example Run
Run
Average Balance (negative to exit) ($)? 100
Number of transactions? 20
Fees = $50.00
Interest Rate = 0.0%
Interest = $0.00
Average Balance (negative to exit) ($)? 2000
Number of transactions? 5
Fees = $40.00
Interest Rate = 3.0%
Interest = $60.00
Average Balance (negative to exit) ($)? 5000
Number of transactions? 110
Fees = $90.00
Interest Rate = 3.0%
Interest = $150.00
Average Balance (negative to exit) ($)? -1
Solution:
(a) Defining Diagram
Input Processing Output
balance Prompt for balance fees
number of transactions Prompt for number of interest rate
transactions
Calculate fees interest
Calculate interest rate
Calculate interest
Display fees
Display interest rate
Display interest
(b) Pseudo code
Prompt for account balance
Get balance
DOWHILE (balance > 0)
Prompt number of transaction
Get number of transaction
Set fees to 40.0
Set interest rate to 0.0
IF balance < 1000 THEN
fees += number of transaction * 0.50
ELSE
interest rate = 3.00
IF number of transaction > 10 THEN
fees += (number of transaction – 10) * 0.50
END IF
END IF
Interest = balance * (interest rate/100.0)
Display fees
Display interest rate
Display interest
Prompt for account balance
Get balance
ENDDO
(c) Data Dictionary
Field Name Required Type Default Condition Notes
Balance Yes Float 0 >=0 Stores the account balance
number of
transaction
Yes Integer 0 >=0 Stores the number of transaction
performed by the account
Fees Yes Float 0 >=0 Store aum of account fees and fees
incurred for each transactions
interest rate Yes Float 0 >=0 Store the interest rate of the account
which is determined based on account
balance
Interest Yes Float 0 >=0 Stores the interest based on the interest
rate
Section C (20 marks)
This Section contains 5 questions and is worth of 20 Marks. You must attempt all
questions. Answers must be provided only in the answer book. Answers provided
Calculate fees interest
Calculate interest rate
Calculate interest
Display fees
Display interest rate
Display interest
(b) Pseudo code
Prompt for account balance
Get balance
DOWHILE (balance > 0)
Prompt number of transaction
Get number of transaction
Set fees to 40.0
Set interest rate to 0.0
IF balance < 1000 THEN
fees += number of transaction * 0.50
ELSE
interest rate = 3.00
IF number of transaction > 10 THEN
fees += (number of transaction – 10) * 0.50
END IF
END IF
Interest = balance * (interest rate/100.0)
Display fees
Display interest rate
Display interest
Prompt for account balance
Get balance
ENDDO
(c) Data Dictionary
Field Name Required Type Default Condition Notes
Balance Yes Float 0 >=0 Stores the account balance
number of
transaction
Yes Integer 0 >=0 Stores the number of transaction
performed by the account
Fees Yes Float 0 >=0 Store aum of account fees and fees
incurred for each transactions
interest rate Yes Float 0 >=0 Store the interest rate of the account
which is determined based on account
balance
Interest Yes Float 0 >=0 Stores the interest based on the interest
rate
Section C (20 marks)
This Section contains 5 questions and is worth of 20 Marks. You must attempt all
questions. Answers must be provided only in the answer book. Answers provided
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
else where will not be marked.
Q1. Explain the difference between a compiler and an interpreter?
(3 marks)
Solution:
Compiler Interpreter
Compiler is the software program which
converts the program in high level language
to low-level language like (Object code or
machine code)
Interpreter is a software program that
executes each line of program without
converting to object/ machine code.
Ex: C, C++, Java Ex: Python, Perl.
Q2. Can you describe what is wrong with the below code?
def some_function()
msg = "hello, world!"
print(msg)
return msg
(5 marks)
Solution:
When executing the above program in Python, syntax error will be raised in line
def some_function()
because, the function definition does not contain colon at its end
Suppose if we add colon(:) at end of function definition as
def some_function():
indentation error error still arise in following lines
print(msg)
return msg
So the correct function definition after correcting all the errors is given below:
def some_function():
msg = "hello, world!"
print(msg)
return msg
Q3. Explain under what circumstances a break statement used in a for-loop?
(5 marks)
Solution:
Break statement is used inside a for loop when we need to terminate for loop based on
any termination condition such as sum of number is greater than 100.
Example:
sum=0
for i in range(100):
sum += i
Q1. Explain the difference between a compiler and an interpreter?
(3 marks)
Solution:
Compiler Interpreter
Compiler is the software program which
converts the program in high level language
to low-level language like (Object code or
machine code)
Interpreter is a software program that
executes each line of program without
converting to object/ machine code.
Ex: C, C++, Java Ex: Python, Perl.
Q2. Can you describe what is wrong with the below code?
def some_function()
msg = "hello, world!"
print(msg)
return msg
(5 marks)
Solution:
When executing the above program in Python, syntax error will be raised in line
def some_function()
because, the function definition does not contain colon at its end
Suppose if we add colon(:) at end of function definition as
def some_function():
indentation error error still arise in following lines
print(msg)
return msg
So the correct function definition after correcting all the errors is given below:
def some_function():
msg = "hello, world!"
print(msg)
return msg
Q3. Explain under what circumstances a break statement used in a for-loop?
(5 marks)
Solution:
Break statement is used inside a for loop when we need to terminate for loop based on
any termination condition such as sum of number is greater than 100.
Example:
sum=0
for i in range(100):
sum += i
if sum > 100:
break
print(sum)
Q4. What will the following code display?
(5 marks)
mystr = 'abracadabra'
print(mystr[6:9])
Solution:
The above code outputs
dab
Description:
The print statement displays the sliced string from mystr variable. mystr is sliced with
characters starting from index 6 to 8 (9-1).
Q5. What is a Boolean function in Python?
(2 marks)
Solution:
Boolean function, bool() is used to convert the any value passed as its parameter to boolean
as either True or False and return them.
If returns False when following type of value is passed as parameter, False, None, 0, 0.0 and
empty sequence such as [] {} (), and when no parameter is passed bool().
In other cases, it returns True.
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.
break
print(sum)
Q4. What will the following code display?
(5 marks)
mystr = 'abracadabra'
print(mystr[6:9])
Solution:
The above code outputs
dab
Description:
The print statement displays the sliced string from mystr variable. mystr is sliced with
characters starting from index 6 to 8 (9-1).
Q5. What is a Boolean function in Python?
(2 marks)
Solution:
Boolean function, bool() is used to convert the any value passed as its parameter to boolean
as either True or False and return them.
If returns False when following type of value is passed as parameter, False, None, 0, 0.0 and
empty sequence such as [] {} (), and when no parameter is passed bool().
In other cases, it returns True.
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.
1 out of 9
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024 | Zucol Services PVT LTD | All rights reserved.