Programming in Java NPTEL Week 6 Assignment

                     

Course Name: Programming in Java NPTEL

Link of Course: https://onlinecourses.nptel.ac.in/noc22_cs47/preview

These are answers for Programming In Java NPTEL Week 6 Assignment


Q1. Which of the following is NOT a method of the Thread class in Java?
a. public void run()
b. public void exit()
c. public void start()
d. public final int getPriority()

Answer: b


Q2. Which of the following statement is true in case of starting a thread with “run)” and *start0′ method?
a. There is no difference between starting a thread with ‘run()’ and ‘start()’ method.
b. When you call start() method. main thread intenally calls run() method to start newly Created Thread
c. When you call run() method directly no new Thread is created and code inside run() will execute on curent Thread.
d. None

Answer: b


These are answers for Programming In Java NPTEL Week 6 Assignment


Q3. Which of the following can be used to create an instance of Thread?
a. By implementing the Runnable interface.
b. By extending the Thread class.
c. By creating a new class named Thread and calling method run().
d. By importing the Thread class from package.

Answer: a, b


Q4. What is the output of the following program?
a. Hello World
b. a Arithmetic Exception
c. ArithmeticException Exception Hello World
d. ArithmeticException Hello World
e. none

Answer: d


These are answers for Programming In Java NPTEL Week 6 Assignment


Q5. Which method restarts a dead thread
a. start()
b. restart()
c. restart Thread)
d. none

Answer: d


These are answers for Programming In Java NPTEL Week 6 Assignment


Q6. Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?
a. After thread A is notified, or after two seconds
b. Two seconds after thread A is notified.
c. After the lock on B is released, or after two seconds.
d. Two seconds after lock B is released.

Answer: a


Q7. The following is a simple program using the concept of thread.
What is the output of the above program?

a. 1 3
b. 1 2 3 4
c. Runtime error
d. 2 4

Answer: d


These are answers for Programming In Java NPTEL Week 6 Assignment


Q8. For the program given below, what will be the output after its execution?
a. 01
b. False True
c. True True
d. 11

Answer: d


Q9. Which of the following is/are not a correct constructor for a thread object?
a. Thread(Runnable a, String str):
b Thread(Runnable a, int priority):
c. Thread(Runnable a, ThreadGroup t):
d. Thread(int priority):

Answer: b, c, d


These are answers for Programming In Java NPTEL Week 6 Assignment


Q10. Which exception is thrown when an array element is accessed beyond the array size?
a. ArayElementOut OfBounds
b. Arraylndex OutOiBounds Exception
c. AmaylndexOutOfBounds
d. None of these

Answer: b


Programming Assignment Solutions

Question 1
Complete the code segment to print the following using the concept of extending the Thread class in Java:
OUTPUT
Thread is Running.

Solution:


These are answers for Programming In Java NPTEL Week 6 Assignment


public class Question61 extends Thread
{
	public void run()
    {
		System.out.print("Thread is Running.");
	}
	public static void main(String args[]){

		// Creating object of thread class
		Question61 thread=new Question61();

        // Start the thread
		thread.start();
	}
}

These are answers for Programming In Java NPTEL Week 6 Assignment


Question 2
In the following program, a thread class Question62 is created using the Runnable interface Complete the main() to create a thread object of the class Question62 and run the thread. It should print the output as given below.
OUTPUT
Welcome to Java Week 6 New Question.
Main Thread has ended.

Solution:

public class Question62 implements Runnable {

    @Override
    public void run() {
        System.out.print(Thread.currentThread().getName()+" has ended.");
    }
    // Create main() method and appropriate statements in it
    public static void main(String[] args)
    {
            Question62 ex = new Question62();
            Thread t1= new Thread(ex);
            t1.setName("Main Thread");
            t1.start();
            System.out.println("Welcome to Java Week 6 New Question.");
            t1.setName("Main Thread");
    }
}

These are answers for Programming In Java NPTEL Week 6 Assignment


Question 3
A part of the Java program is given, which can be completed in many ways, for example using the concept of thread, etc.  Follow the given code and complete the program so that your program prints the message “NPTEL Java week-6 new Assignment Q3“. Your program should utilize the given interface/ class.Invalid HTML tag: tag name o:p is not allowed

Solution:

// Interface A is defined with an abstract method run()
interface A {
	public abstract void run();
}

// Class B is defined which implements A and an empty implementation of run()
class B implements A {
	public void run() {}
}
// Create a class named MyThread and extend/implement the required class/interface
class MyThread extends B
{
  // Define a method in MyThread class to print the output
  public void run()
  {
		System.out.print("NPTEL Java week-6 new Assignment Q3");
  }
}
// Main class Question is defined
public class Question63 {
     public static void main(String[] args) {
		 // An object of MyThread class is created
         MyThread t = new MyThread();
		 // run() of class MyThread is called
         t.run();
     }
}

These are answers for Programming In Java NPTEL Week 6 Assignment


Question 4
Execution of two or more threads occurs in a random order. The keyword ‘synchronized’ in Java is used to control the execution of thread in a strict sequence. In the following, the program is expected to print the output as given below. Do the necessary use of ‘synchronized’ keyword, so that, the program prints the Final sum as given below: 
OUTPUT
Final sum:6000

Solution:

class Pair {
    private int a, b;
    public Pair() {
        a = 0;
        b = 0;
    }

    // Returns the sum of a and b. (reader)
    // Should always return an even number.
    public synchronized int sum()
    {
      return(a+b);
    }

    // Increments both a and b. (writer)
    public synchronized void inc()
    {
        a++;
        b++;
    }
}

public class PairWorker extends Thread {
    public final int COUNT = 1000;
    private Pair pair;
    // Ctor takes a pointer to the pair we use
    public PairWorker(Pair pair) {
        this.pair = pair;
    }
    // Send many inc() messages to our pair
    public void run() {
        for(int i=0; i< COUNT; i++) {
            pair.inc();
        }
    }

    public static void main(String args[]) {
        Pair pair = new Pair();
        PairWorker w1 = new PairWorker(pair);
        PairWorker w2 = new PairWorker(pair);
        PairWorker w3 = new PairWorker(pair);
        w1.start();
        w2.start();
        w3.start();
        // the 3 workers are running
        // all sending messages to the same object
        // we block until the workers complete
        try{
            w1.join();
            w2.join();
            w3.join();
        }
        catch(InterruptedException ignored) {}
        System.out.println("Final sum:" + pair.sum());
    }
}

These are answers for Programming In Java NPTEL Week 6 Assignment


Question 5
Given a snippet of code, add necessary codes to print the following:
OUTPUT
Name of thread ‘t1’:Thread-0
Name of thread ‘t2’:Thread-1
New name of thread ‘t1’:Week 6 Assignment Q5
New name of thread ‘t2’:Week 6 Assignment Q5 New

Solution:

public class Question65 extends Thread{
  public void run(){

  }
 public static void main(String args[]){
    Question65 t1=new Question65();
    System.out.println("Name of thread 't1':"+ t1.getName());

    Question65 t2=new Question65();
    System.out.println("Name of thread 't2':"+ t2.getName());
    // start the thread-1
    t1.start();
    // set the name of thread-1
    t1.setName("Week 6 Assignment Q5");

    // start the thread-2
    t2.start();
    // set the name of thread-2
    t2.setName("Week 6 Assignment Q5 New");
    System.out.println("New name of thread 't1':"+ t1.getName());
    System.out.println("New name of thread 't2':"+ t2.getName());
 }
}

These are answers for Programming In Java NPTEL Week 6 Assignment

All Weeks of Programming In Java: https://progies.in/answers/nptel/programming-in-java

More NPTEL Solutions: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Programming In Java NPTEL Week 6 Assignment