Output of Python program | Set 16 (Threads)

1) What is the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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")

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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")

chevron_right


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?



filter_none

edit
close

play_arrow

link
brightness_4
code

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")

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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()

chevron_right


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.

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.