Question

ist all possible results b. tions and options for controlig till 44. What is the output of each of the following code sequenc
d. Count counter c new Counter (). nnable r new Increase (c. 3): Thread t- new Thread (r): +.start(; c.incremento: System.out
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.  Answer: 2

Explanation:

Counter c = new Counter();   // create an object of Counter class with default constructor that sets the counter to zero
       c.increment(); c.increment(); // two times call the increment() method, now final value of counter is 2.
       System.out.println(c); // toString() method is called while print the object and it should print 2.

b. Answer: 0

Explanation:

Counter c = new Counter(); // create an object of Counter class with default constructor that sets the counter to zero
       Runnable r = new Increase(c, 3); // reference r of Runnable interface refers to the object of Increase class. In the constructor of Increase class accepts two arguments: Counter class reference c and a integer value
       Thread t = new Thread(r); // create an object of Thread class that refers to the object of Increase class
       t.start(); // start() method is called, and hence run() method invoked automatically
       System.out.println(c); // toString() method is called while print the object and but it may print 0, since main() thread may ends before thread t. Note that sometimes it may print 3 also.

c. Answer: 1

Explanation:

Counter c = new Counter();
       Runnable r = new Increase(c, 3);
       Thread t = new Thread(r);
       c.increment(); t.start();   // The the increment() method is called once. Then start() method is called, and hence run() method invoked automatically. In the run() method the value of count increase by 3.
       System.out.println(c);  // toString() method is called while print the object and but it may print 1, since main() thread may ends before thread t. Note that sometimes it may print 4 also.

d. Answer 1

Explanation:

Counter c = new Counter();
       Runnable r = new Increase(c, 3);
       Thread t = new Thread(r);
       t.start(); c.increment();  // start() method is called, and hence run() method invoked automatically. Then the increment() method is called once.
       System.out.println(c);  // toString() method is called while print the object and but it may print 1, since main() thread may ends before thread t. Note that sometimes it may print 4 also.

e. Annwer 4

Explanation:

Counter c = new Counter();
       Runnable r = new Increase(c, 3);
       Thread t = new Thread(r);
       t.start(); t.join(); //here join method is called to ensure that thread t ends before of main thread
       c.increment();
       System.out.println(c);  // toString() method is called while print the object and it print 4, since t thread ends before main thread.

f. Answer 4

Explanation:

Counter c = new Counter();
       Runnable r = new Increase(c, 3);
       Thread t = new Thread(r);
       t.start(); c.increment();
       t.join();  //here join method is called (after the calling of increment method) to ensure that thread t ends before of main thread
       System.out.println(c);  // toString() method is called while print the object and it print 4, since t thread ends before main thread.

Add a comment
Know the answer?
Add Answer to:
ist all possible results b. tions and options for controlig till 44. What is the output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord...

    According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. public class CandidateGenerator { private final LongFifo outputFifo; public CandidateGenerator(LongFifo outputFifo) { this.outputFifo = outputFifo; Runnable r = new Runnable() { @Override public void run() { runWork();}}; Thread t = new Thread(r, "CandidateGenerator"); t.start() } private void runWork() { try { outputFifo.add(2); long number = 3; while ( true ) { outputFifo.add(number); number += 2;} }...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; impo...

    JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; import javafx.animation.AnimationTimer; import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Game extends Pane implements Runnable {       // instance variables    private ArrayList<MOB> mobs;    private ArrayList<Rectangle> hitBoxes;    private ArrayList<Treasure> treasure;    private Player player;    private final ImageView background = new ImageView();    private Level level;    private SimpleIntegerProperty score;    private...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • 1) A: List all possible outcomes (the sample space) for the tree diagram below B: Calculate...

    1) A: List all possible outcomes (the sample space) for the tree diagram below B: Calculate the number of all possible outcomes: bb 2) Based on the tree diagram below, how many ways can a coin be tossed four times and get exactly 3 tails? Hнн HHT HHHH HHHT HHTH H HTT HTHH HTHT HTTH HTTT THH T THT TTHS THHH THHT THTH THTT TTHH ΤΤΗΤ ΤΤΤΗ ΤΤΤΤ ΤΤΤ 3) How many 12-letter "words" (real or made-up) can be made...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT