Predict the output of the given code:

  1. public class Test {

  2. private static String msg = "HCL ";

  3. static{

  4. Thread t = new Thread(new Runnable(){

  5. public void run(){

  6. msg = "Technologies ";

  7. }

  8. });

  9. t.start();

  10. }

  11. public static void main(String[] args){

  12. System.out.print(msg);

  13. }}

a) Compiles and prints HCL

b) Compiles and prints Technologies

c) Compiles and prints HCL Technologies

d) Output can be HCL or Technologies

Questions by lalithakasiraj

Showing Answers 1 - 21 of 21 Answers


  • Feb 13th, 2013

Compiles and prints HCL


  • Feb 20th, 2013

It compiles and print HCL

  Was this answer useful?  Yes


  • Mar 21st, 2013

option b

  Was this answer useful?  Yes

Answer is a.

Static variables value can be change by only static method . Here, Run is not a static method so the value of variable msg can not change.

  Was this answer useful?  Yes

satish Vishwakarma

  • May 10th, 2013

b) Compiles and prints Technologies

  Was this answer useful?  Yes


  • Aug 5th, 2013

Ans. C
Since there will be 2 threads will be created one is Main and other created in static, it depends how threads executed.

  Was this answer useful?  Yes


  • Aug 7th, 2013


  Was this answer useful?  Yes

santosh kumar

  • Oct 16th, 2013

"technology" will be printed as in static block msg is reinitialized with value "technology" before calling main method.

  Was this answer useful?  Yes


  • Mar 16th, 2014

it is HCL

  Was this answer useful?  Yes


  • May 9th, 2014


  Was this answer useful?  Yes


  • May 20th, 2014

B) compiles and prints Technologies

  Was this answer useful?  Yes

santosh kumar

  • Jun 7th, 2014


  Was this answer useful?  Yes


  • Jun 12th, 2014

Computer checked : a)Compile and prints HCL.

  Was this answer useful?  Yes


  • Sep 1st, 2014

Could be either. The static initializer creates a new thread that changes the value of the msg string. Whichever thread runs first will determine the output.

  Was this answer useful?  Yes


  • Sep 25th, 2014

Hi my question is that
predict the output:

  1. a)print right("lovely day",5)

  2. b)print left ("bye",1)

  3. c)print mid ("pretty flower",5,6)

  4. d)x=-4

  5. y=3

  Was this answer useful?  Yes


  • Sep 26th, 2014

compile and print HCL

  Was this answer useful?  Yes


  • Dec 19th, 2014

Answer is coming as HCL because there are two threads main and t and both are sharing the common resource ( static variable msg ) . Thread t is getting created and brought into runnable state ( by calling start()) but it will be thread of execution( start of run() ) only when thread scheduler will decide to make it.The main thread is getting executed first and it is reading the original msg value as "HCL". The static variable msg is getting changed to "TECHNOLOGY" when thread t gets its turn . If u want to check it. Make current thread to sleep for 100 ms in main method before printing msg value. The t thread will execute in mean time and will change the value of msg to "TECHNOLOGY". same will be printed by main method also once it get back from slEEp.
Happy coding :)

  Was this answer useful?  Yes


  • Jan 29th, 2015


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions