1. Questions on Data Types, Operators and Expressions in Python
The section contains questions on datatypes, operators, numbers and their types, regular expressions and variable names.
Want to Become Linux Developer?
|
Python Questions and Answers – Core Datatypes
This set of Python Questions & Answers focuses on “Core Data Types”.
1. Which of these in not a core datatype?
a) Lists
b) Dictionary
c) Tuples
d) Class
View Answer
a) Lists
b) Dictionary
c) Tuples
d) Class
View Answer
Answer:d
Explanation:Class is a user defined datatype.
Explanation:Class is a user defined datatype.
2. Given a function that does not return any value, What value is thrown by itby default when executed in shell.
a) int
b) bool
c) void
d) None
View Answer
a) int
b) bool
c) void
d) None
View Answer
Answer:d
Explanation:Python shell throws a NoneType object back.
Explanation:Python shell throws a NoneType object back.
3. Following set of commands are executed in shell, what will be the output?
>>>str="hello"
>>>str[:2]
>>>
a) he
b) lo
c) olleh
d) hello
View Answer
b) lo
c) olleh
d) hello
View Answer
Answer:a
Explanation:We are printing only the 1st two bytes of string and hence the answer is “he”.
Explanation:We are printing only the 1st two bytes of string and hence the answer is “he”.
4. Which of the following will run without errors(multiple answers possible) ?
a) round(45.8)
b) round(6352.898,2)
c) round()
d) round(7463.123,2,1)
View Answer
a) round(45.8)
b) round(6352.898,2)
c) round()
d) round(7463.123,2,1)
View Answer
Answer:a,b
Explanation:Execute help(round) in the shell to get details of the parameters that are passed into the round function.
Explanation:Execute help(round) in the shell to get details of the parameters that are passed into the round function.
5. What is the return type of function id ?
a) int
b) float
c) bool
d) dict
View Answer
a) int
b) float
c) bool
d) dict
View Answer
Answer:a
Explanation:Execute help(id) to find out details in python shell.id returns a integer value that is unique.
Explanation:Execute help(id) to find out details in python shell.id returns a integer value that is unique.
6. In python we do not specify types,it is directly interpreted by the compiler, so consider the following operation to be performed.
>>>x = 13 ? 2
objective is to make sure x has a integer value, select all that apply (python 3.xx)a) x = 13 // 2 b) x = int(13 / 2) c) x = 13 / 2 d) x = 13 % 2 View AnswerAnswer:a,b Explanation:// is integer operation in python 3.0 and int(..) is a type cast operator.7. What error occurs when you execute? apple = mango a) SyntaxError b) NameError c) ValueError d) TypeError View AnswerAnswer:b Explanation:Banana is not defined hence name error.8. Carefully observe the code and give the answer.def example(a):
a = a + '2'
a = a*2
return a>>>example("hello")
a) indentation Error b) cannot perform mathematical operation on strings c) hello2 d) hello2hello2 View AnswerAnswer:a Explanation:Python codes have to be indented properly.9. What dataype is the object below ? L = [1, 23, ‘hello’, 1] a) List b) dictionary c) array d) tuple View AnswerAnswer:a Explanation:List datatype can store any values within it.10. In order to store values in terms of key and value we use what core datatype. a) List b) tuple c) class d) dictionary View AnswerAnswer:d Explanation:Dictionary stores values in terms of keys and values.11. Which of the following results in a SyntaxError(Multiple answers possible) ? a) ‘”Once upon a time…”, she said.’ b) “He said, “Yes!”” c) ‘3\’ d) ”’That’s okay”’ View AnswerAnswer:b,c Explanation:Carefully look at the colons.12. The following is displayed by a print function call:tom
dick
harry
Select all of the function calls that result in this output a) print(”’tom \ndick \nharry”’) b) print(”’tom dick harry”’) c) print(‘tom\ndick\nharry’) d) print(‘tom dick harry’) View Answer
Answer:b,c Explanation:The \n adds a new line. 13. What is the average value of the code that is executed below ?>>>grade1 = 80
>>>grade2 = 90
>>>average = (grade1 + grade2) / 2
a) 85 b) 85.0 c) 95 d) 95.0 View AnswerAnswer:b Explanation:Cause a decimal value to appear as output.14. Select all options that print hello-how-are-you a) print(‘hello’, ‘how’, ‘are’, ‘you’) b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4) c) print(‘hello-‘ + ‘how-are-you’) d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘-‘ + ‘you’) View AnswerAnswer:c,d Explanation:Execute in the shell.15. What is the return value of trunc() ? a) int b) bool c) float d) None View AnswerAnswer:a Explanation:Executle help(math.trunc) to get details.
No comments:
Post a Comment