Document Details

Uploaded by Deleted User

Tags

python programming programming quiz coding computer science

Summary

This document is a Python quiz covering various topics including programming concepts, functions, data structures, identifiers, and namespaces. Questions range from basic syntax to more complex operations using Python code snippets, which are intended for practice.

Full Transcript

Round 1 1. Who developed Python Programming Language? a) Wick van Rossum b) Rasmus Lerdorf c) Guido van Rossum d) Niene Stom Answer: c Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the Netherlands. 2....

Round 1 1. Who developed Python Programming Language? a) Wick van Rossum b) Rasmus Lerdorf c) Guido van Rossum d) Niene Stom Answer: c Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the Netherlands. 2. Which of the following is the correct extension of the Python file? a).python b).pl c).py d).p Answer: c Explanation: ‘.py’ is the correct extension of the Python file. Python programs can be written in any text editor. To save these programs we need to save in files with file extension ‘.py’. 3. Which of the following functions can help us to find the version of python that we are currently working on? a) sys.version(1) b) sys.version(0) c) sys.version() d) sys.version Answer: d Explanation: The function sys.version can help us to find the version of python that we are currently working on. It also contains information on the build number and compiler used. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the version. 4. What does pip stand for python? a) Pip Installs Python b) Pip Installs Packages c) Preferred Installer Program d) All of the mentioned Answer: c Explanation: pip is a package manager for python. Which is also called Preferred Installer Program. 5. Which of the following character is used to give single-line comments in Python? a) // b) # c) ! d) /* Answer: b Explanation: To write single-line comments in Python use the Hash character (#) at the beginning of the line. It is also called number sign or pound sign. To write multi-line comments, close the text between triple quotes. 6. Which of the following is not a core data type in Python programming? a) Tuples b) Lists c) Class d) Dictionary Answer: c Explanation: Class is a user-defined data type. 7. What is the order of namespaces in which Python looks for an identifier? a) Python first searches the built-in namespace, then the global namespace and finally the local namespace b) Python first searches the built-in namespace, then the local namespace and finally the global namespace c) Python first searches the local namespace, then the global namespace and finally the built-in namespace d) Python first searches the global namespace, then the local namespace and finally the built-in namespace Answer: c Explanation: Python first searches for the local, then the global and finally the built-in namespace. 8. What will be the output of the following Python expression if x=56.236? print("%.2f"%x) a) 56.236 b) 56.23 c) 56.0000 d) 56.24 Answer: d Explanation: The expression shown above rounds off the given number to the number of decimal places specified. Since the expression given specifies rounding off to two decimal places, the output of this expression will be 56.24. Had the value been x=56.234 (last digit being any number less than 5), the output would have been 56.23. 9. What will be the output of the following Python function? len(["hello",2, 4, 6]) a) Error b) 6 c) 4 d) 3 Answer: c Explanation: The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4. 10. What will be the output of the following Python code? x = 'abcd' for i in x: print(i.upper()) a) a B C D b) a b c d c) error d) A B C D Answer: d Explanation: The instance of the string returned by upper() is being printed. Round 2 1. What are the two main types of functions in Python? a) System function b) Custom function c) Built-in function & User defined function d) User function Answer: c Explanation: Built-in functions and user defined ones. The built-in functions are part of the Python language. Examples are: dir(), len() or abs(). The user defined functions are functions created with the def keyword. 2. What is the maximum possible length of an identifier in Python? a) 79 characters b) 31 characters c) 63 characters d) none of the mentioned Answer: d Explanation: Identifiers can be of any length. 3. To add a new element to a list we use which Python command? a) list1.addEnd(5) b) list1.addLast(5) c) list1.append(5) d) list1.add(5) Answer: c Explanation: We use the function append to add an element to the list. 4. What is output of print(math.pow(3, 2))? a) 9.0 b) None c) 9 d) None of the mentioned Answer: a Explanation: math.pow() returns a floating point number. 5. What will be the output of the following Python code? i = 1 while True: if i%3 == 0: break print(i) i+=1 a) 1 2 3 b) error c) 1 2 d) none of the mentioned Answer: c 6. What will be the output of the following Python code? l=[1, 0, 2, 0, 'hello', '', []] list(filter(bool, l)) a) [1, 0, 2, ‘hello’, ”, []] b) Error c) [1, 2, ‘hello’] d) [1, 0, 2, 0, ‘hello’, ”, []] Answer: c Explanation: The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, ‘hello’]. 7. What will be the value of the following Python expression? 4 + 3 % 5 a) 7 b) 2 c) 4 d) 1 Answer: a Explanation: The order of precedence is: %, +. Hence the expression above, on simplification results in 4 + 3 = 7. Hence the result is 7. 8. What will be the output of the following Python code snippet? for i in [1, 2, 3, 4][::-1]: print(i, end=' ') a) 4 3 2 1 b) error c) 1 2 3 4 d) none of the mentioned Answer: a Explanation: [::-1] reverses the list. 9. Which function is called when the following Python program is executed? f = foo() format(f) a) str() b) format() c) __str__() d) __format__() Answer: c Explanation: Both str(f) and format(f) call f.__str__(). 10. What will be the output of the following Python code snippet? z=set('abc$de') 'a' in z a) Error b) True c) False d) No output Answer: b Explanation: The code shown above is used to check whether a particular item is a part of a given set or not. Since ‘a’ is a part of the set z, the output is true. Round 3 1. What will be the output of the following Python expression? round(4.576) a) 4 b) 4.6 c) 5 d) 4.5 Answer: c Explanation: This is a built-in function which rounds a number to give precision in decimal digits. In the above case, since the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Hence the output will be 5. 2. What will be the output of the following Python code snippet if x=1? a) 4 b) 2 c) 1 d) 8 Answer: a Explanation: The binary form of 1 is 0001. The expression x>temp = tester(12) 7. >>>print(temp.id) a) 12 b) 224 c) None d) Error Answer: a Explanation: Id in this case will be the attribute of the instance. 7. What will be the output of the following Python program? 1. def addItem(listParam): 2. listParam += 3. mylist = [1, 2, 3, 4] 4. addItem(mylist) 5. print(len(mylist)) a) 5 b) 8 c) 2 d) 1 Answer: a Explanation: + will append the element to the list. 8. What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'])) print(z) a) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’} b) {‘abc’, ‘p’, ‘q’, ‘san’} c) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’} d) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san} Answer: c Explanation: The code shown first adds the element ‘san’ to the set z. The set z is then updated and two more elements, namely, ‘p’ and ‘q’ are added to it. Hence the output is: {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’} 9. What will be the output of the following Python code? x = [, ] print((' '.join(list(map(str, x))),)) a) 01 b) c) (’01’) d) (‘ ’,) Answer: d Explanation: (element,) is not the same as element. It is a tuple with one item. 10. What will be the output of the following Python function? min(max(False,-3,-4), 2,7) a) -4 b) -3 c) 2 d) False Answer: d Explanation: The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0 (false).

Use Quizgecko on...
Browser
Browser