[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

JAVA Programming :: Threads - Discussion

Home > JAVA Programming > Threads > MCQs Questions Discussion

7 / 6

What will happen when you try to compile and run this code?

public class TGo implements Runnable{
 public static void main(String argv[]){
         TGo tg = new TGo();
         Thread t = new Thread(tg);
         t.start();
        }
        public void run() {
         while(true) {
   Thread.currentThread().sleep(1000);
                        System.out.println("looping while");
                }
        }
}

ACompilation and no output

BCompilation and repeated output of "looping while"

CCompilation and single output of "looping while"

DCompile time error

Answer: Option (Login/Signup)

Show Explanation

When the run() method executes,

Our thread first waits for a time unit of 1000 ms after which it prints looping while, and this goes on repeatedly.

Hence, the code compiles fine and we get repeated output of "looping while". 

Asked In ::

Post Your Answer Here:     

Reply    
Rate This: +0 -0
    Report


Report Error

Please Login First Click Here