SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Threads
Duration : 30 mins
1. What will be the result of attempting to compile and run the following program?

public class MyThread implements Runnable
{
  String msg = "default";
  public MyThread(String s)
  {
    msg = s;
  }
  public void run( )
  {
    System.out.println(msg);
  }
  public static void main(String args[])
  {
    new Thread(new MyThread("String1")).run();
    new Thread(new MyThread("String2")).run();
    System.out.println("end");
  }
}

Select 1 correct option.
a The program will compile and print only 'end'.
b It will always print 'String1' 'String2' and 'end', in that order.
c It will always print 'String1' 'String2' in random order followed by 'end'.
d It will always print 'end' first.
e No order is guaranteed.
2. The following program will always terminate.

class Base extends Thread
{
               static int k = 10;
}
class Incrementor extends Base
{
               public void run()
               {
                                    for(; k>0; k++)
                                    {
                                                      System.out.println("IRunning...");
                                    }
              }
}
class Decrementor extends Base
{
              public void run()
              {
                                for(; k>0; k--)
                                {
                                                      System.out.println("DRunning...");
                                    }
                }
}
public class TestClass
{
                public static void main(String args[]) throws Exception
                {
                                  Incrementor i = new Incrementor();
                                  Decrementor d = new Decrementor();
                                  i.start();
                                  d.start();
                }
}

Select 1 correct option.
a True


b False
3. What will happen if you run the following program...

public class TestClass extends Thread
{
  public void run()
  {
    for(;;);
  }
  public static void main(String args[])
  {
    System.out.println("Starting main");
    new TestClass().start();
    System.out.println("Main returns");
  }
}

Select 3 correct options
a It will print "Starting Main"
b It will print "Main returns"
c It will not print "Main returns"
d The program will never exit.
e main() method will never return
4. What will be the result of attempting to compile and run the following program?

public class Test extends Thread
{
  String msg = "default" ;
  public Test(String s)
  {
    msg = s;
  }
  public void run()
  {
    System.out.println(msg);
  }
  public static void main(String args[])
  {
    new Test("String1").start();
    new Test("String2").start();
    System.out.println("end");
  }
}

Select 1 correct option.
a The program will fail to compile.
b It will always print 'String1' 'String2' and 'end', in that order.
c It will always print 'String1' 'String2' in random order followed by 'end'.
d It will always print 'end' first.
e No order is guaranteed.
5. Which of these statements are true?

Select 2 correct options
a Calling the method sleep( ) does not kill a thread.

b A thread dies when the start( ) method ends.

c A thread dies when the thread's constructor ends.

d A thread dies when the run( ) method ends.

e Calling the method kill( ) stops and kills a thread.
6. What will be the output when the following code is run?

class MyRunnable implements Runnable
{
            MyRunnable(String name)
            {
                         new Thread(this, name).start();
            }
            public void run()
            {
                         System.out.println(Thread.currentThread().getName());
            }
}
public class TestClass
{
            public static void main(String[] args)
            {
                         Thread.currentThread().setName("MainThread");
                         MyRunnable mr = new MyRunnable("MyRunnable");
                         mr.run();
            }
}

Select 1 correct option.
a MainThread
b MyRunnable
c "MainThread" will be printed twice.
d "MyRunnable" will be printed twice.
e It will print "MainThread" as well as "MyRunnable"
7. What do you need to do to define and start a thread?

Select 1 correct option.
a Extend from class Thread, override method run() and call method start();

b Extend from class Runnable, override method run() and call method
start();

c Extend from class Thread, override method start() and call method run();

d Implement interface Runnable and call start()

e Extend from class Thread, override method start() and call method start();
8. Consider the following code:

class MyClass implements Runnable
{
            int n = 0;
            public MyClass(int n){ this.n = n; }
            public static void main(String[] args)
            {
                       new MyClass(2).run();
                       new MyClass(1).run();
            }
            public void run()
            {
                       for(int i=0; i<n; i++)
                       {
                                   System.out.println("Hello World");
                       }
            }
}
What is true about above program?
Select 1 correct option.
a It'll print "Hello World" twice.
b It'll keep printing "Hello World".
c 2 new threads are created by the program.
d 1 new thread is created by the program.
e None of these.
9. Consider the following code:

class MyRunnable implements Runnable
{
            public static void main(String[] args)
            {
                      new Thread( new MyRunnable(2) ).start();
            }
            public void run(int n)
            {
                      for(int i=0; i<n; i++)
                      {
                                 System.out.println("Hello World");
                      }
            }
}
What will be the output when this program is compiled and run from the command line?
Select 1 correct option.
a It'll print "Hello World" once.
b It'll print "Hello World" twice.
c This program will not even compile.
d This will compile but will throw an exception at runtime.
e It will compile and run but it will not print anything.
10. Consider the following program...
public class TestClass implements Runnable
{
  int x = 5;
  public void run()
  {
    this.x = 10;
  }
  public static void main(String[] args)
  {
     TestClass tc = new TestClass();
     new Thread(tc).start(); // 1
     System.out.println(tc.x);
  }
}
What will it print when run?


Select 1 correct option.
a 5
b 10
c It will not compile.
d Exception at Runtime.
e The output cannot be determined.
11 Which of the following statements about this program are correct?

class CoolThread extends Thread
{
          String id = "";
          public CoolThread(String s){ this.id = s; }
          public void run()
          {
                     System.out.println(id+"End");
          }
          public static void main(String args [])
          {
                     Thread t1 = new CoolThread("AAA");
                     t1.setPriority(Thread.MIN_PRIORITY);
                     Thread t2 = new CoolThread("BBB");
                     t2.setPriority(Thread.MAX_PRIORITY);
                     t1.start();
          }
}
Select 1 correct option.
a "AAA End" will always be printed before "BBB End".
b "BBB End" will always be printed before "AAA End".
c The program will not compile.
d THe program will throw an exception at runtime.
e None of the above.
12. What will be the output when the following code is run?

class MyRunnable implements Runnable
{
             MyRunnable(String name)
             {
                          new Thread(this, name).start();
             }
             public void run()
             {
                          System.out.println(Thread.currentThread().getName());
             }
}
public class TestClass
{
             public static void main(String[] args)
             {
                          Thread.currentThread().setName("First");
                          MyRunnable mr = new MyRunnable("MyRunnable");
                          mr.run();
                          Thread.currentThread().setName("Second");
                          mr.run();
             }
}
Select 1 correct option.
a It will always print: First, MyRunnable, Second.
b It will always print: MyRunnable, First, Second.
c It will always print: First, Second, MyRunnable.
d It may print First, Second and MyRunnable in any order.
e Second will always be printed after First.
13. Consider the following class:

public class MySecureClass
{
           public synchronized void doALotOfStuff(){
                     try {
                     LINE1: Thread.sleep(10000);
                     }catch(Exception e){ }
           }
           public synchronized void doSmallStuff(){
                     System.out.println("done");
           }
}

Assume that there are two threads. Thread one is executing the doALotOfStuff() method and
has just

executed LINE 1. Now, Thread two decides to call doSmallStff() method on the same object.
What will happen?
Select 1 correct option.
a done will be printed immediately.
b done will not be printed untill about 10 seconds.
c done will never be printed.
d An IllegalMonitorStateException will be thrown.
e An IllegalThreadStateException will be thrown.

Weitere ähnliche Inhalte

Was ist angesagt?

Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189Mahmoud Samir Fayed
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handlingIntro C# Book
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
Import java
Import javaImport java
Import javaheni2121
 
The Ring programming language version 1.10 book - Part 17 of 212
The Ring programming language version 1.10 book - Part 17 of 212The Ring programming language version 1.10 book - Part 17 of 212
The Ring programming language version 1.10 book - Part 17 of 212Mahmoud Samir Fayed
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)mehul patel
 
Слава Бобик «NancyFx для самых маленьких»
Слава Бобик «NancyFx для самых маленьких»Слава Бобик «NancyFx для самых маленьких»
Слава Бобик «NancyFx для самых маленьких»SpbDotNet Community
 

Was ist angesagt? (20)

Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88The Ring programming language version 1.3 book - Part 7 of 88
The Ring programming language version 1.3 book - Part 7 of 88
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
Java programs
Java programsJava programs
Java programs
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
04 threads
04 threads04 threads
04 threads
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Import java
Import javaImport java
Import java
 
The Ring programming language version 1.10 book - Part 17 of 212
The Ring programming language version 1.10 book - Part 17 of 212The Ring programming language version 1.10 book - Part 17 of 212
The Ring programming language version 1.10 book - Part 17 of 212
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
Java practical(baca sem v)
Java practical(baca sem v)Java practical(baca sem v)
Java practical(baca sem v)
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
 
Loop
LoopLoop
Loop
 
Слава Бобик «NancyFx для самых маленьких»
Слава Бобик «NancyFx для самых маленьких»Слава Бобик «NancyFx для самых маленьких»
Слава Бобик «NancyFx для самых маленьких»
 
Clang tidy
Clang tidyClang tidy
Clang tidy
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Class method
Class methodClass method
Class method
 

Ähnlich wie Java Threads

Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptTabassumMaktum
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptTabassumMaktum
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programmingSonam Sharma
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3Dillon Lee
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 

Ähnlich wie Java Threads (20)

Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Java practical
Java practicalJava practical
Java practical
 
作業系統
作業系統作業系統
作業系統
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
14 thread
14 thread14 thread
14 thread
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Java practical
Java practicalJava practical
Java practical
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
7
77
7
 
Java interface
Java interfaceJava interface
Java interface
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programming
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
STS4022 Exceptional_Handling
STS4022  Exceptional_HandlingSTS4022  Exceptional_Handling
STS4022 Exceptional_Handling
 

Kürzlich hochgeladen

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Kürzlich hochgeladen (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Java Threads

  • 2. 1. What will be the result of attempting to compile and run the following program? public class MyThread implements Runnable { String msg = "default"; public MyThread(String s) { msg = s; } public void run( ) { System.out.println(msg); } public static void main(String args[]) { new Thread(new MyThread("String1")).run(); new Thread(new MyThread("String2")).run(); System.out.println("end"); } } Select 1 correct option. a The program will compile and print only 'end'. b It will always print 'String1' 'String2' and 'end', in that order. c It will always print 'String1' 'String2' in random order followed by 'end'. d It will always print 'end' first. e No order is guaranteed.
  • 3. 2. The following program will always terminate. class Base extends Thread { static int k = 10; } class Incrementor extends Base { public void run() { for(; k>0; k++) { System.out.println("IRunning..."); } } } class Decrementor extends Base { public void run() { for(; k>0; k--) { System.out.println("DRunning..."); } } } public class TestClass { public static void main(String args[]) throws Exception { Incrementor i = new Incrementor(); Decrementor d = new Decrementor(); i.start(); d.start(); } } Select 1 correct option. a True b False
  • 4. 3. What will happen if you run the following program... public class TestClass extends Thread { public void run() { for(;;); } public static void main(String args[]) { System.out.println("Starting main"); new TestClass().start(); System.out.println("Main returns"); } } Select 3 correct options a It will print "Starting Main" b It will print "Main returns" c It will not print "Main returns" d The program will never exit. e main() method will never return
  • 5. 4. What will be the result of attempting to compile and run the following program? public class Test extends Thread { String msg = "default" ; public Test(String s) { msg = s; } public void run() { System.out.println(msg); } public static void main(String args[]) { new Test("String1").start(); new Test("String2").start(); System.out.println("end"); } } Select 1 correct option. a The program will fail to compile. b It will always print 'String1' 'String2' and 'end', in that order. c It will always print 'String1' 'String2' in random order followed by 'end'. d It will always print 'end' first. e No order is guaranteed.
  • 6. 5. Which of these statements are true? Select 2 correct options a Calling the method sleep( ) does not kill a thread. b A thread dies when the start( ) method ends. c A thread dies when the thread's constructor ends. d A thread dies when the run( ) method ends. e Calling the method kill( ) stops and kills a thread.
  • 7. 6. What will be the output when the following code is run? class MyRunnable implements Runnable { MyRunnable(String name) { new Thread(this, name).start(); } public void run() { System.out.println(Thread.currentThread().getName()); } } public class TestClass { public static void main(String[] args) { Thread.currentThread().setName("MainThread"); MyRunnable mr = new MyRunnable("MyRunnable"); mr.run(); } } Select 1 correct option. a MainThread b MyRunnable c "MainThread" will be printed twice. d "MyRunnable" will be printed twice. e It will print "MainThread" as well as "MyRunnable"
  • 8. 7. What do you need to do to define and start a thread? Select 1 correct option. a Extend from class Thread, override method run() and call method start(); b Extend from class Runnable, override method run() and call method start(); c Extend from class Thread, override method start() and call method run(); d Implement interface Runnable and call start() e Extend from class Thread, override method start() and call method start();
  • 9. 8. Consider the following code: class MyClass implements Runnable { int n = 0; public MyClass(int n){ this.n = n; } public static void main(String[] args) { new MyClass(2).run(); new MyClass(1).run(); } public void run() { for(int i=0; i<n; i++) { System.out.println("Hello World"); } } } What is true about above program? Select 1 correct option. a It'll print "Hello World" twice. b It'll keep printing "Hello World". c 2 new threads are created by the program. d 1 new thread is created by the program. e None of these.
  • 10. 9. Consider the following code: class MyRunnable implements Runnable { public static void main(String[] args) { new Thread( new MyRunnable(2) ).start(); } public void run(int n) { for(int i=0; i<n; i++) { System.out.println("Hello World"); } } } What will be the output when this program is compiled and run from the command line? Select 1 correct option. a It'll print "Hello World" once. b It'll print "Hello World" twice. c This program will not even compile. d This will compile but will throw an exception at runtime. e It will compile and run but it will not print anything.
  • 11. 10. Consider the following program... public class TestClass implements Runnable { int x = 5; public void run() { this.x = 10; } public static void main(String[] args) { TestClass tc = new TestClass(); new Thread(tc).start(); // 1 System.out.println(tc.x); } } What will it print when run? Select 1 correct option. a 5 b 10 c It will not compile. d Exception at Runtime. e The output cannot be determined.
  • 12. 11 Which of the following statements about this program are correct? class CoolThread extends Thread { String id = ""; public CoolThread(String s){ this.id = s; } public void run() { System.out.println(id+"End"); } public static void main(String args []) { Thread t1 = new CoolThread("AAA"); t1.setPriority(Thread.MIN_PRIORITY); Thread t2 = new CoolThread("BBB"); t2.setPriority(Thread.MAX_PRIORITY); t1.start(); } } Select 1 correct option. a "AAA End" will always be printed before "BBB End". b "BBB End" will always be printed before "AAA End". c The program will not compile. d THe program will throw an exception at runtime. e None of the above.
  • 13. 12. What will be the output when the following code is run? class MyRunnable implements Runnable { MyRunnable(String name) { new Thread(this, name).start(); } public void run() { System.out.println(Thread.currentThread().getName()); } } public class TestClass { public static void main(String[] args) { Thread.currentThread().setName("First"); MyRunnable mr = new MyRunnable("MyRunnable"); mr.run(); Thread.currentThread().setName("Second"); mr.run(); } } Select 1 correct option. a It will always print: First, MyRunnable, Second. b It will always print: MyRunnable, First, Second. c It will always print: First, Second, MyRunnable. d It may print First, Second and MyRunnable in any order. e Second will always be printed after First.
  • 14. 13. Consider the following class: public class MySecureClass { public synchronized void doALotOfStuff(){ try { LINE1: Thread.sleep(10000); }catch(Exception e){ } } public synchronized void doSmallStuff(){ System.out.println("done"); } } Assume that there are two threads. Thread one is executing the doALotOfStuff() method and has just executed LINE 1. Now, Thread two decides to call doSmallStff() method on the same object. What will happen? Select 1 correct option. a done will be printed immediately. b done will not be printed untill about 10 seconds. c done will never be printed. d An IllegalMonitorStateException will be thrown. e An IllegalThreadStateException will be thrown.