Python Exam Questions (136 - 200) , PART 3
148. What is the correct way of printing the "T" in the string "PYTHON"?
"PYTHON"[2]
141. What operator is a short-circuit operator?
"and" are short-circuit operators.So it evaluates the second argument if first one is false. "or" is short-circuit operators .So it evaluates the second argument if first one is false.
173. How do you replace all "a" in a string "example" with "b"?
"example".replace("a","b")
163. What symbol can be used to comment out one line of code in Python?
#
147. Which of the following is the modulus operator?
%
191. What is the output of print('23'+'7')
'237'
158. def value(a,b): return a[:b]. What is: print(value("Notebook",5) ?
'Noteb'
149. If my_variable = "food", what is the data type of my_variable?
'str'
146. Which of the following is the exponent operator?
**
161. print(1//3)
0
155. What is an expression after an if statement always followed by?
: (colon)
189. In Python,the expression 4+3^2-6/3 will output
TypeError: unsupported operand type(s) for ^: 'int' and 'float
162. In Python, what is one function to output content to the display?
print()
177. How do you print the second item in the list variable 'example'?
print(example[1])
197. num = set([1,1,2,3,3,3,4])
saves one copy of each number like {1, 2, 3, 4}
198. x = True
stores True in x
187. When input is from the keyboard, what data type does it default to?
str
139. What is 9*2//3 ?
6
157. What is this equal to? min(343, 74, 334, 93)
74
140. What is 43//4
10
178. What would the statement print('%.2f'% 123.444) display?
123.44
159. What will this return? (min(2,13,3,7)*max(2,-34,4))-abs(-3)
5
194. How do you end an infinite loop on windows?
Ctrl-C
137. What does the // operator do in Python?
Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity)
200. print("\x48\x49!")
HI!
188. What role does a semicolon ; perform in Python?
If you want to put several Python statements on the same line, you can use a semi-colon to separate them
156. What are functions?
In programming, a named section of a program that performs a specific task. In this sense, a function is a type of procedure or routine.
136. What does the % operator do in Python?
It takes modulus / remainder of two real values
145. In Python, what is used to structure code?
Modules , Packages and Object Oriented programming by using classes is used to structure code
153. Which of the following is true?
N/A
166. Which of the following is not a valid assignment operator?
N/A
167. Which one of the following is a valid Python if statement?
N/A
170. Whichof the following is a valid loop in Python?
N/A
171. Which of the following is a valid while loop in Python?
N/A
174. Which of the following is a valid list in Python?
N/A
175. Which of the following is a valid tuple in Python?
N/A
176. Which of the following is a valid dictionary in Python?
N/A
183. What is not true a recursive function?
N/A
190. Which of the expressions produces 144?
N/A
192. Which of the following is an acceptable Python variable name?
N/A
152. What is flow control?
When we want the program to take some decisions and do different things depending on different situations, we do it by controlling flow of execution . if , for and while are used as flow control statements;
199. print(r"\nhello")
\nhello
193. What is the order of precedence for logical operators? (and, or, not)
and , or , not
184. Why should you not use == to compare two floating point numbers?
because floating point numbers are stored as binary floating point approximations and thus two numbers seemingly equal might not be equal because of precision changes because of storage
195. A group of consecutive statements with the same indentation is called a _________ .
block
154. In Python, what data type do comparisons generate?
bool
168. How would you add an alternate condition to an if statement?
by using elif : for eg if exp1: elif "alternate condition":
196. The process of finding and getting rid of programming errors
debugging
169. What is a valid way to start a function in Python?
def function_name():
144. How are the items in a dictionary deleted?
del d[key] where d is dictionary
142. How is a dictionary defined?
dictionary = {}
179. What is the keyword used after the try statement to handle exceptions?
except
180. What is the proper way to open a file that you intend to read from?
f=open("filename","rb")
181. What is the proper way to open a file that you intend to write to?
f=open("filename","wb")
151. What correctly imports the datetime library?
import datetime
150. What correctly imports the math library?
import math
143. What is the keyword used to determine if a key is in a dictionary?
in for eg. if key in dictionary
164. How would you cast the variable a that is equal to "2" to an integer 2?
int(a)
186. In Python, the operator != does what ?
it compares if two values are "not equal"
182. What does import locale as lc do?
it imports the library/module which can be accessed now by using "lc" as it lc is like a variable to use the library
185. What does the exit() function from the system module do?
it stops further execution and comes out of the program
160. How would you call the square root from the math module ?
math.sqrt()
165. How would you cast the variable a that is equal to 2 to a string "2"?
str(a)
138. How are empty strings assigned in Python?
string = ""
172. How do you check the type of variable ?
type(variable)