Python Test Upwork

In Python 3, which of the following is incorrect regarding data structures ?

Lists are ordered collections that can hold arbitrary objects and can expand dynamically as new items are added.
Tuples are ordered collections that can hold arbitrary objects, but are immutable.
A set is an unordered collection of unique values and support standard Set operations such as union, intersection, and difference.
A Dictionary is an unordered set of key-value pairs, but becomes essentially immutable once a key already exists.

What will be the output of the following code snippet?
def f(x): return x % 2 != 0 and x % 3 != 0
result=filter(f, range(1, 20))
print result

[1, 3, 7, 10, 13, 16, 19]
[1, 6, 9, 11, 15, 17, 19]
[1, 5, 7, 11, 13, 17, 19]
[1, 4, 7, 12, 15, 17, 19]

Which function could be used to list every file and folder in the current directory?

os.dirnames('.')
os.listdir('.')
os.listdir('/')
os.ls()

What will be the output of following code?
print "\x48\x49!"

HI!
48      49!
4849!
\x48\x49!
4849

In Python 3, which of the following is correct regarding data types Lists, Tuples, Dictionaries and Sets ?

`add()` from a Dictionary object will add a new key-value pair to the end of the collection.
If `a_set` is a Set instance, then `a_set.difference(a_set)` will always return an empty Set object.
To add a new object to the end of a Tuple, one can use the method `append()`.
List objects do not have a `pop()` method as this is typical of Stack data type.

What would be the output of the following code?
def change_list(list1, list2):
    list1.append('four')
    list2 = ['and', 'we', 'can', 'not', 'lie']
outer_list1 = ['one', 'two', 'three']
outer_list2 = ['we', 'like', 'proper', 'English']
change_list(outer_list1, outer_list2)
print outer_list1
print outer_list2

['one', 'two', 'three', 'four']
['we', 'like', 'proper', 'English']

['one', 'two', 'three', 'four']
['and', 'we', 'can', 'not', 'lie']

['one', 'two', 'three']
['we', 'like', 'proper', 'English']

['one', 'two', 'three']
['and', 'we', 'can', 'not', 'lie']

If you have a User class, how can you make a new User object?

a = new User()
User a = new User()
User.new(Object)
a = User()

Which of the following can be used to create a directory?

os.make_dir()
os.create_dir()
os.mkdir()
All of the above

What will be the output of following code?
var1 = lambda var: var * 2
ret = lambda var: var * 2
result = 3
result = var1(result)
result = ret(result)
result = var1(result)
print(result)

12
6
24
36

Which of the following is a reentrant lock type?

RLock
UnLock
Lock
Condition

In the following code, what will be printed ?
class parent:
    def __init__(self, param):
        self.v1 = param
class child(parent):
    def __init__(self, param):
        self.v2 = param
obj = child(5)
print "%d %d" % (obj.v1, obj.v2)

AttributeError will be raised.
None None
None 5
5 None
5 5

Which of the following will be the output of the below code snippet?
x = []
y = x
y.append(10)
z = 5
w = z
z = z - 1
print(y)
print(x)
print(z)
print(w)

[10]
[]
[4]
[5]

[10]
[10]
[4]
[5]

[10]
[[10]]
[5]
[5]

[10]
[None]
[5]
[4]

Which of the following is a correct way to add a new element to a list?

list.add(1)
list.addValue(1)
list.append(1)
list.addItem(1)

Which of the following environment variables for Python is an alternative module search path?

PYTHONCASEOK
PYTHONHOME
PYTHONSEARCH
PYTHONURL

How to get JSON to load into an OrderedDict in python?

By simply using json.loads(D) where D is Ordered dictionary
By specifying the object_pairs_hook argument to JSONDecoder
Ordered dictionary is not supported yet
By simply using json.dumps(D) where D is Ordered dictionary

What is the result of the following code:
listOne = [1, 3, 5]
listTwo = [2, 4, 6]
listNew = listOne + listTwo
print listNew

[3, 7, 11]
[1, 2, 3, 4, 5, 6]
[1, 3, 5, 2, 4, 6]
[ [1, 3, 5],  [2, 4, 6] ]

What gets printed in the code below ?
def f(x, l=[]):
    for i in range(x):
        l.append(i * i)
    return l
f(2)
f(3, [3, 2, 1])
print f(3)

[0, 1, 4]
[0, 1, 0, 1, 4]
[0, 1, 0]
[3, 2, 1, 0, 1, 4]

Regarding the code below, what is the correct statement ?
class Book:
    def __init__(self, isbn):
        self.isbn = isbn
        isbn = 'test'
book = Book(12345)

book.isbn will contain the value 12345.
book.isbn will contain the value 'test'.
This program won't run the Book class expects a string instead of integer.
None of the above is correct.

How can you convert a json string to a Python object?

simplejson.load()
json.load()
json.dumps()
json.loads()

Which of the following exceptions is raised for an error that doesn’t fall in any of the categories?

SyntaxError
RuntimeError
SystemError
CodeError

What is the method to retrieve the list of all active thread objects?

activethreads()
getThreads()
enumerate()
threads()

Which  following method is used to get the name of a thread?

getName()
getthread()
getThread()
get_id()

What is the type of each element in sys.argv ?

array
string
tuple
list

What will be the output of the following code?
x, y = 15, 45
res = x if x < y else y
print(res)

45
1
Error
15

An object can contain?

variables
methods
functions
variables and methods

What will be the output of the following code?
def val1(param):
    return param
def val2(param):
    return param * 2
def val3(param):
    return param + 5
result = val1(val2(val3(1)))
print(result)

8
12
10
6

Which of the following will be the output of the below code snippet?
def foo(x=[]):
    x.append(1)
    print(x)
foo()
foo()
foo()

[1, 1]
[1, 1]
[1, 1]

[1, 1, 1]
[1, 1, 1]
[1, 1, 1]

[1]
[1]
[1]

[1]
[1, 1]
[1, 1, 1]

Consider the code, and choose the correct statement:
class A:
    def __init__(self, i = 0):
        self.i = i
class B(A):
    def __init__(self, j = 0):
        self.j = j
def main():
    b = B()
    print(b.i)
    print(b.j)
main()

Class B inherits A, but the data field i in A is not inherited.
Class B inherits A and automatically inherits all data fields in A.
When you create an object B, you have to pass an integer such as B(5).
The data field j cannot be accessed by object b.

Which of the following creates an instance of Firm class correctly?

Firm = Firm('client', 2000)
Firm = Firm()
Firm = Firm('server', 2000)
None of the above

What will be the output of the following code?
nums = list()
i = 3
while (i < 9):
    nums.append(i)
    i = i+2
print(nums)

[3, 5, 9]
[3, 2, 7]
[3, 9, 7]
[3, 5, 7]

Which of the following will be the output of the below code snippet?
squares = []
for x in range(5):
    squares.append(lambda: x**2)
x = 8
for i in range(5):
    print(squares[i]())

1
4
9
16
25

64
64
64
64
64

UnboundLocalError: local variable 'x' referenced before assignment

64
4
9
16
25

What will be the output of following code?
def addItem(l):
    l += [0]
mylist = [1, 2, 3, 4, 5]
addItem(mylist)
print len(mylist)

12345
1234
01234
6

What will be the output of the following code?
d = lambda p: p * 2
t = lambda p: p * 3
x = 2
x = d(x)
x = t(x)
x = d(x)
print x

7
12
24
36
48

Which of the following will be the output of the below code snippet?
x = 10
def foo():
    print(x)
    x = x - 1
foo()
print(x)

10
11

10
9

UnboundLocalError: local variable 'x' referenced before assignment

9
9

What will be the output of the following code?
counter = 1
def count():
    global counter
    t = (1, 2, 3)
    for i, v in enumerate(t):
        counter += i + v
count()
print counter

9
10
11
12
13

What is a type of Inf?

Boolean
Integer
Float
Complex

What is the return value of trunc()?

tuple
float
int
bool

What is the type of x?
x = 10.0//5

int
float
long
number

Which of the following data types is not supported in python ?

List
Generics
Numbers
Tuple

Consider the following Python code:
test = 1/2
What is the value of 'test' in Python 2.5, respectively ?

0 and 1
0 and 0.5
0.5 and 0
1 and 0

Python Test Upwork 2019