Open In App
Related Articles

Output of Python program | Set 16 (Threads)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
1) What is the output of the following program?
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?
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?
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?
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?
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.

Last Updated : 28 Jun, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads