Predict the output of the given code:

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. }}
Copyright GeekInterview.com

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

Pakkiyaraj

  • Feb 13th, 2013
 

Compiles and prints HCL

Ravi

  • Feb 20th, 2013
 

It compiles and print HCL

  Was this answer useful?  Yes

raksha

  • 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

Anil

  • Aug 5th, 2013
 

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

  Was this answer useful?  Yes

raghu

  • Aug 7th, 2013
 

HCL

  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

Sri

  • Mar 16th, 2014
 

it is HCL

  Was this answer useful?  Yes

Sunny

  • May 9th, 2014
 

D

  Was this answer useful?  Yes

christopher

  • May 20th, 2014
 

B) compiles and prints Technologies

  Was this answer useful?  Yes

santosh kumar

  • Jun 7th, 2014
 

a

  Was this answer useful?  Yes

Arpit

  • Jun 12th, 2014
 

Computer checked : a)Compile and prints HCL.

  Was this answer useful?  Yes

Ciaran

  • 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

kirti

  • Sep 25th, 2014
 

Hi my question is that
predict the output:

Code
  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

prabhash

  • Sep 26th, 2014
 

compile and print HCL

  Was this answer useful?  Yes

ankit

  • 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

srk

  • Jan 29th, 2015
 

A

  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