Output of Python program | Set 16 (Threads)
Last Updated :
28 Jun, 2017
1) What is the output of the following program?
Python
import threading
barrier = threading.Barrier(4)
class thread(threading.Thread):
def __init__(self, thread_ID, thread_name):
threading.Thread.__init__(self)
self.thread_ID = thread_ID
self.thread_name = thread_name
def run(self):
print("ThreadID = " + str(self.thread_ID) + ", ThreadName = " +
self.thread_name + "\n")
try:
barrier = threading.Barrier(4)
barrier.wait()
except:
print("barrier broken")
thread1 = thread(100, "GFG")
thread2 = thread(101, "Geeks")
thread3 = thread(102, "GeeksforGeeks")
thread1.start()
thread2.start()
thread3.start()
barrier.wait()
print("Exit")
a) ThreadID = 100, ThreadName = GFG
ThreadID = 101, ThreadName = Geeks
ThreadID = 102, ThreadName = GeeksforGeeks
b) ThreadID = 100, ThreadName = GFG
ThreadID = 101, ThreadName = Geeks
ThreadID = 102, ThreadName = GeeksforGeeks
Exit
c) Compilation error
d) Runtime error
Ans. (a)
Explanation: This is an example of deadlock. Each thread creates it's own barrier and calls .wait() function on that barrier.
2) Which among the following is NOT the output of the following program?
Python
import threading
class thread(threading.Thread):
def __init__(self, thread_ID, thread_name):
threading.Thread.__init__(self)
self.thread_ID = thread_ID
self.thread_name = thread_name
def run(self):
print(self.thread_name)
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Geeks ")
thread3 = thread(102, "GeeksforGeeks ")
thread1.start()
thread2.start()
thread3.start()
print("Exit")
a) GFG Geeks GeeksforGeeks Exit
b) Exit Geeks GeeksforGeeks GFG
c) GFG Exit GeeksforGeeks Geeks
d) None of the above
Ans. (d)
Explanation: Calling start() method on a thread moves the thread to ready state. It's the responsibility of the thread scheduler to schedule the thread. So, a particular thread can be scheduled at any instant.
3) What is the output of the following program?
Python
import threading
class thread(threading.Thread):
def __init__(self, thread_ID, thread_name):
threading.Thread.__init__(self)
self.thread_ID = thread_ID
self.thread_name = thread_name
def run(self):
print(self.thread_name)
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Geeks ")
thread3 = thread(102, "GeeksforGeeks ")
thread = []
thread.append(thread1)
thread.append(thread2)
thread.append(thread3)
thread1.start()
thread2.start()
for thread in thread:
thread.join()
thread3.start()
print("Exit")
a) GFG Geeks GeeksforGeeks Exit
b) Compilation error
c) Program will halt in between due to Runtime error
d) None of these
Ans. (c)
Explanation: join() method cannot be called on a thread that hasn't yet started it's execution.
4) What is the output of the following program?
Python
import threading
i = 5
class thread(threading.Thread):
def __init__(self, thread_ID, thread_name):
threading.Thread.__init__(self)
self.thread_ID = thread_ID
self.thread_name = thread_name
def run(self):
i = i + 1
print(i)
thread1 = thread(100, "GFG ")
thread2 = thread(101, "Geeks")
thread1.start()
thread2.start()
a) 66
b) 67
c) Compilation error
d) Runtime error
Ans. (d)
Explanation: Each thread has it's own space reserved in memory. So, for each threads, thread1 and thread2, the variable temp is not declared as temp is not defined within the thread's run method.
5) What is the output of the following program?
Python
import threading
class thread(threading.Thread):
def __init__(self, thread_ID):
self.thread_ID = thread_ID
def run(self):
print(self.thread_ID)
thread1 = thread(100)
thread1.start()
a) 100
b) Compilation error
c) Runtime error
d) None of these
Ans. (c)
Explanation: thread.__init__() has to be called explicitly by each of the threads being created within the __init__ function.
Similar Reads
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Output of Python programs | Set 9 (Dictionary) Prerequisite: Dictionary 1) What is the output of the following program? Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.items(): print(key) dictionary.clear(); for key, values in diction
3 min read
Output of Python programs | Set 10 (Exception Handling) Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program?Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try:
3 min read
Output of python program | Set 11(Lists) Pre-requisite: List in python 1) What is the output of the following program? Python data = [2, 3, 9] temp = [[x for x in[data]] for x in range(3)] print (temp) a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]] b) [[2, 3, 9], [2, 3, 9], [2, 3, 9]] c) [[[2, 3, 9]], [[2, 3, 9]]] d) None of these Ans. (a) Exp
3 min read
Output of python program | Set 12(Lists and Tuples) Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? PYTHON L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11 d) 3
3 min read
Output of python program | Set 13(Lists and Tuples) Prerequisite: Lists and Tuples1) What is the output of the following program? PYTHON List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List)) a) [True, 50, 10, 5] Sum is: 66 b) [True, 50, 5, 10] Sum is: 65 c) TypeError: unsupported operand type(s) for +: 'int' and 'str' d) [True, 5
3 min read
Output of python program | Set 14 (Dictionary) Prerequisite: Dictionary Note: Output of all these programs is tested on Python31) What is the output of the following program? PYTHON3 D = dict() for x in enumerate(range(2)): D[x[0]] = x[1] D[x[1]+7] = x[0] print(D) a) KeyError b) {0: 1, 7: 0, 1: 1, 8: 0} c) {0: 0, 7: 0, 1: 1, 8: 1} d) {1: 1, 7: 2
3 min read
Output of python program | Set 15 (Modules) Prerequisite: Regular Expressions Note: Output of all these programs is tested on Python3 1) Which of the options below could possibly be the output of the following program? PYTHON from random import randrange L = list() for x in range(5): L.append(randrange(0, 100, 2)-10) # Choose which of outputs
3 min read
Output of Python program | Set 15 (Loops) Prerequisite - Loops in Python Predict the output of the following Python programs. 1) What is the output of the following program? Python x = ['ab', 'cd'] for i in x: i.upper() print(x) Output:['ab', 'cd']Explanation: The function upper() does not modify a string in place, but it returns a new stri
2 min read
Output of Python program | Set 16 (Threads) 1) What is the output of the following program? Python import threading barrier = threading.Barrier(4) class thread(threading.Thread): def __init__(self, thread_ID, thread_name): threading.Thread.__init__(self) self.thread_ID = thread_ID self.thread_name = thread_name def run(self): print("Thre
3 min read