Upwork Test Answers: Get all the correct answers of most recent and possible Upwork Tests A to Z (Updated on Jan, 2016)
Cover Letter Templates: These cover letter samples are not only for Upwork job, but also you will have some idea about your real life job
 
Freelance Profile Overviews: Different Profile samples and overviews of experts, advanced and intermediate level freelancers
For Newbie of Upwork: Upwork Help - How to apply for a job in Upwork with 10 most important articles about Upwork

A to Z View - All Upwork Test Answers

Python Test Answers (updated)

Here you will find all possible answers of Upwork Python Test of General Programming Category for Qualification Tests for Freelancers & Programmers - Certifications for Outsourcing - Upwork.

Please press Ctrl + F to find your desired answers of the Upwork Python Test questions

1. Which of the following will disable output buffering in Python?
 Answers: • Using the -u command line switch or • class Unbuffered: def __init__(self, stream): self.stream = stream def write(self, data): self.stream.write(data) self.stream.flush() def __getattr__(self, attr): return getattr(self.stream, attr) import sys sys.stdout=Unbuffered(sys.stdout) or • Setting the PYTHONUNBUFFERED environment variable

 2. Which of the following members of the object class compare two parameters?
 Answers: • object.__eq__(self, other) or • object.__ne__(self, other)

 3. Object is the base class of new-style datatypes. Which of the following functions is not a member of the object class?
 Answers: • object.__nz__(self)

 4. In Python, what is the default maximum level of recursion?
 Answers: • 1000

 5. Various email and news clients store messages in a variety of formats, many providing hierarchical and structured folders. Which of the following provides a uniform API for reading the messages stored in all the most popular folder formats?
 Answers: • mailbox

 6. Which of the following are the main features of Python?
 Answers: • Cross-platform

 7. Which of the following variables store parameters passed from outside?
 Answers: • sys.argv

 8. Which of the following functions can change the maximum level of recursion?
 Answers: • setrecursionlimit function in the sys module

 9. Read the following statements:

 >>> import array
 >>> a = array.array('c','spam and eggs')
 >>> a[0] = 'S'
 >>> a[-4:] = array.array('c','toast')
 >>> print ''.join(a)

 Which of the following will be the output of the above code snippet?
 Answers: • Spam and toast

 10. How can a numeric String (eg. "545.2222") be converted to Float or Integer?
 Answers: • a = "545.2222" float(a) int(float(a))
 

 11. Which of the following is the correct prototype of the string.find() function?
 Answers: • string.find(s, sub [,start [,end]])

 12. It is possible to use encoding other than ASCII in Python source files. The best way to do it is to put one more special comment line right after the #! line to define the source file encoding. Which of the following is the correct statement to define encoding?
 Answers: • # -*- coding: iso-8859-15 -*-

 13. How can a null object be declared in Python?
 Answers: • None

 14. Which of the following modules lets you check whether two files are identical, and whether two directories contain some identical files?
 Answers: • filecmp

 15. Which function could be used to list every file and folder in the current directory?
 Answers: • os.listdir('.')

 16. The core text processing tasks in working with email are parsing, modifying, and creating the actual messages. Which of the following modules deal with parsing and processing email messages?
 Answers: • Both MimeWriter and mimify

 17. Which of the following is the correct prototype for the 'open' function of the file class in python 2.2+?
 Answers: • open(fname [,mode [,buffering]])

 18. Which user input method will act like a file-object on which read and readline functions can be called?
 Answers: • sys.stdin

 19. Which of the following protocol libraries can be used for an email implementation in a Python application?
 Answers: • smtplib

 20. Which of the following statements imports every name in a module namespace into the current namespace?
 Answers: • from modname import *

 21. Which is the correct way to remove an installed Python package?
 Answers: • pip uninstall <package>

 22. One common way to test a capability in Python is to try to do something, and catch any exceptions that occur.
 Which of the following is the correct mechanism of trapping an error?
 Answers: • try: code to be verified will come here except <exception>:

 23. Which of the following is the correct way to get the size of a list in Python?
 Answers: • len(list)

 24. What will be the output of the following statements:

 >>> import string
 >>> string.ljust(width=30,s="Mary had a little lamb")
 Answers: • 'Mary had a little lamb '

 25. The most important element of the email package is the message. The email class provides the classes for messages in Python. Which of the following classes is used for the message?
 Answers: • email.message.Message

 26. Consider the function:

 def hello():
 return "Hello, world!"

 Which of the following is the correct way to make a decorator that could be used to make hello() return "#Hello, world!#"?
 Answers: • def hashes(fn): def wrapped(): return "#" + fn() + "#" return wrapped @hashes def hello(): return "Hello, world!"

 27. Object is the base class of new-style datatypes. Which of the following functions is not a member of the object class?
 Answers: • object.__notequal__(self, other)

 28. Which of the following modules keep prior directory listings in the memory to avoid the need for a new call to the file system?
 Answers: • dircache

 29. Which of the following modules is used internally to determine whether a path matches?
 Answers: • fnmatch

 30. Examine the following prototype for the 'open' function of the file class in Python 2.2+:

 open(fname [,mode [,buffering]])

 Which of the following is correct for the 'buffering' argument?
 Answers: • All of these

 31. Which of the following statements copy the contents of a list and not just a reference to the list?
 Answers: • newlist = oldlist[:]

 32. Which of the following functions is used to send audio data via the Internet?
 Answers: • email.MIMEAudio.MIMEAudio(audiodata [,subtype [,encoder [,**params]]])

 33. Read the following statements:

 Statement 1: A simple assignment statement binds a name into the current namespace, unless that name has been declared as global.
 Statement 2: A name declared as global is bound to the global (module-level) namespace.

 Which of the following is correct?
 Answers: • Both statements are true.

 34. Which of the following commands would produce the following result:
 result: 'line 1\nline 2'
 Answers: • result = '\nline 1\nline 2\n'.strip()

 35. Which is not used to install Python packages?
 Answers: • installtools

 36. Which of the following exceptions occurs while importing a module?
 Answers: • ImportError

 37. Which of the following statements can be used to remove an item from a list by giving the index?
 Answers: • del listname[index]

 38. Read the following statements:

 >>> word = 'Help' + 'A'
 >>> '<' + word*5 + '>'

 Which of the following will be the output of the above code snippet?
 Answers: • '<HelpAHelpAHelpAHelpAHelpA>'

 39. The least sophisticated form of text output in Python is writing to open files. In particular, which of the following streams can be used?
 Answers: • STDOUT

 40. Which of the following options are true regarding these two code samples?

 Code sample 1:
 def main():
 for i in xrange(10**8):
 pass
 main()

 Code sample 2:
 for i in xrange(10**8):
 pass
 Answers: • Code sample 2 would take a longer time to complete than code sample 1. This is because in code sample 1, 'i' is local, whereas in code sample 2 it is global.

 41. How many arguments can a print statement handle?
 Answers: • Any number of string or numeric arguments

 42. When is the "yield" keyword used in Python?
 Answers: • When a function needs to return a list that only needs to be read through once

 43. What is the best way to check if the object 'myobject' is iterable in Python?
 Answers: • try: [ e for e in myobject] except TypeError: print myobject, 'is not iterable'

 44. In Python, built-in exceptions can be inherited from. Which of the following is the base exception class?
 Answers: • Exception

 45. Given a program that saved a text file in the directory "/temp_files", which of the following will make sure that "/temp_files" exists before writing the file?
 Answers: • if not os.path.exists("/temp_files"): os.makedirs(d)

 46. Read the following statements:

 >>> lst = ['spam','and','eggs']
 >>> lst[2] = 'toast'
 >>> print ''.join(lst)
 >>> print ' '.join(lst)

 Which of the following is the output of the second print statement in the above code snippet?
 Answers: • spam and toast

 47. Read the following statements:

 >>> import string
 >>> s = 'mary\011had a little lamb'
 >>> print s

 Which of the following will be the output of the above code snippet?
 Answers: • mary had a little lamb

 48. Read the following statements:

 Statement 1: Many string module functions are now also available as string object methods.
 Statement 2: To use string object methods, there is no need to import the string module.

 Which of the following is correct?
 Answers: • Both statements are true.

 49. What would the 'sorted_tel' be in the following code:
 tel = {'jack': 4098, 'sape': 5139, 'bill': 3678, 'mike': 2122}
 sorted_tel = sorted(tel.items(), key=lambda x: x[1])
 Answers: • A list of Tuples sorted by the second element.

 50. Inheriting from a base class enables a custom class to use a few new capabilities, such as slots and properties. Which of the following is the base class of new-style datatypes?
 Answers: • object

 51. How can a list be split into equal sized chunks?
 Answers: • def chunks(l, n): for i in xrange(0, len(l), n): yield l[i:i+n]

 52. Writing to STDOUT and STDERR is fairly inflexible, and most of the time the print statement accomplishes the same purpose more flexibly. How many arguments can a print statement handle?
 Answers: • Any number

 53. Which of the following is the best way to reverse the string 'Test String' in Python?
 Answers: • 'Test String'[::-1]

 54. What is the result of the following code:
 >>> import itertools
 >>> x = itertools.count(0)
 >>> x.__class__.__name__
 Answers: • 'count'

 55. What is the most flexible way to call the external command "ls -l" in Python?
 Answers: • from subprocess import call call(["ls", "-l"])

 56. Which of the following is the correct method for changing a global variable inside a function?
 Answers: • def change_globvar(): global globvar globvar = 1

 57. Which list flattening method will have the shortest running time?
 Answers: • l = [ [1, 2, 3], [4, 5, 6], [7], [8, 9] ] * 99 import itertools list( itertools.chain.from_iterable( l ) )

 58. Which of the following is the correct way to flush output of Python print?
 Answers: • import sys sys.stdout.flush()

 59. Which of the following is the best method to find the indices of all occurances of a word in a string?
 line = 'mary had a little lamb, little lamb, little lamb'
 word = 'lamb'
 Answers: • import re indices = [occurance.start() for occurance in re.finditer(word, line)]

 60. In Python 2.x, which of the following is the way to check to make sure that the variable 'x' is not a string?
 Answers: • assert not isinstance(x, basestring)

 61. Which of the following will throw an exception in Python?
 Answers: • raise Exception("Here is an Exception!")

 62. Which of the following functions modifies the list in place to indicate which items are directories, and which are plain files?
 Answers: • dircache.annotate(path, lst)

 63. Which Python module can be used for copying files?
 Answers: • shutil

 64. Which of the following statements are true?

 A. ._variable is semi-private and meant just for convention.
 B. .__variable is considered superprivate and gets name mangled to prevent accidental access.
 C. .__variable__ is typically reserved for built-in methods or variables.
 Answers: • A, B, and C

 65. How can an element be removed from a list using its list index?
 Answers: • del myList[index]

 66. Which of the following is the correct way to check to see if the variable theVar is an integer?
 Answers: • isinstance(theVar, int)

 67. Which of the following is the base class for new-style file objects?
 Answers: • file

 68. Which of the following methods returns the ASCII value of a character in Python?
 Answers: • ord

 69. What is the output of the following code:

 name = 'Jon'
 name.rjust(4, 'A')
 Answers: • 'AJon'

 70. Which of the following is a way to find a local computer's IP address with Python?
 Answers: • import socket socket.gethostbyname(socket.gethostname())

 71. What is a metaclass in Python?
 Answers: • Something that can be attached to any class, that gives it a constant set of attributes

 72. Which of the following is the correct way to call the private method, myPrivateMethod(), in class MyClass, using dir(obj)?

 class MyClass:
 def __myPrivateMethod(self):
 print "Private Method"

 obj = MyClass()
 print dir(obj)
 Answers: • _MyClass__myPrivateMethod

 73. What is the output of the following code?

 def foo(param1, *param2):
 print param1
 print param2

 def bar(param1, **param2):
 print param1
 print param2

 foo(1,2,3,4,5)
 bar(1,a=2,b=3)
 Answers: • 1 (2, 3, 4, 5) 1 {'a': 2, 'b': 3}

 74. Which of the following is the correct way to execute a program from inside Python without having to consider how the arguments/quotes are formatted?
 Answers: • import subprocess subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])

 75. What is the correct way to delete a directory that is not empty using Python?
 Answers: • import shutil shutil.rmtree('/directory')

 76. Which of the following is the correct way to write a generator which will output the numbers between 1 and 100 (inclusive)?
 Answers: • def onehundred(start, end): current = start while current <= end: yield current current += 1 for x in onehundred(1, 100): print x

 77. Which of the following code snippets concatenates the list a_list = [1, 2, 3] with the tuple a_tuple = (4, 5), so the result would be [1, 2, 3, 4, 5]?
 Answers: • a_list.extend(a_tuple)

 78. Which of the following will determine the number of CPUs available in the operating environment?
 Answers: • import multiprocessing multiprocessing.cpu_count()

No comments:

Post a Comment