Programming in Java NPTEL Assignment Answers of Week 6 (2023)

In this article, you will get NPTEL Assignment Answers of Week 6 (2023) of the course Programming in Java

1. Which of the following is NOT TRUE in Java?
a. Every thread has a priority.
b. JVM allows multiple threads of execution running concurrently.
c. Threads with higher priority are executed first.
d. You cannot set a maximum priority value that a thread can have.

Answer : d. You cannot set a maximum priority value that a thread can have.


2. Consider the following code.

class PrimeRun implements Runnable {
    long minprime;
    primeRun (long minPrime) {
        this .minPrime = minPrime;
    }

    public void run ( ) {
    // compute primes larger than minPrime
      . . .
    }
  }

Which of the following is TRUE regarding the above code?
a. Creating a thread in Java using Runnable interface
b. Thread creation by declaring a class to be a subclass of Thread
c. Overriding the run method of class Thread
d. The class implements the run method.

Answer : a. Creating a thread in Java using Runnable interface, d. The class implements the run method.


3. Which of the following cannot 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 the related package.

Answer : c. By creating a new class named Thread and calling method run().
. d. By importing the Thread class from the related package.


4. Which method start () will do which of the following?
a. Causes this thread to begin execution.
b. Either start the execution for new thread or pause an ongoing thread.
c. The JVM calls the run method of this thread.
d. Recovers a thread from deadlock and begin execution

Answer : a. Causes this thread to begin execution.


5. Which method start () will do which of the following?
a. Causes this thread to begin execution.
b. Either start the execution for new thread or pause an ongoing thread.
c. The JVM calls the run method of this thread.
d. Recovers a thread from deadlock and begin execution

Answer : a. Causes this thread to begin execution.
. c. The JVM calls the run method of this thread.


6. Which of the following is not platform independent in Java?
a) Everything in Java thread is platform dependent.
b) The thread constructor with the stackSize parameter is platform dependent.
c) The inheritance in java is platform dependent as multiple classes are involved.
d) There is no platform dependency in Java.

Answer : b) The thread constructor with the stackSize parameter is platform dependent.


7. The following is a simple program using the concept of thread.

public class Question extends Thread(
public void run ( ) {
for (int i=l; i<5; i++)  {
System.out.println(++i);
}
}
public static void main (String args [] {
Question tl=new Question ( ) ;
tl.run();
}
}

What is the output of the above program?
a. 1
ㅤ3
b. 2
ㅤ4
ㅤ6
ㅤ8
c. Runtime error
d. 2
ㅤ4

Answer : d. 2
. 4


8. For the program given below, what will be the output after its execution?

public class Main {
public static void main (String []args) {
Thread thread=Thread.currentThread();
thread.run();
System.out.print(thread.activeCount());
}
}

a. 1
b. 10
c. 01
d. 11

Answer : d. 11


9. Which of the following method returns a reference to the currently executing thread object?
a. public static boolean interrupted()
b. public static Thread currentThread()
c. public final boolean isAlive()
d. public final void suspend()

Answer : b. public static Thread currentThread()


10. Which of the following methods can be used to reduce over-utilization of CPU?
a. public static void yield()
b. public static void main(String args[])
c. public static void sleep(long millis)
d. public void start()

Answer : c. public static void sleep(long millis)


Programming Assignment Answers


Week 6 : Programming Assignment 1

Complete the code segment to print the following using the concept of extending the Thread class in Java:
—————–OUTPUT——————-

Thread is Running.

————————————————-

Answer:

public class Question61 extends Thread

 { 

       public void run()
    {    
		System.out.print("Thread is Running."); 	}
  

Week 6 : Programming Assignment 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.

————————————————-

Answer:

public static void main(String[] args) 
   {  
        Question62 ex = new Question62();  
        Thread t0= new Thread(ex);  
        t0.setName("Main Thread");
        t0.start();  
        System.out.println("Welcome to Java Week 6 New Question.");  
        t0.setName("Main Thread");
    }

Week 6 : Programming Assignment 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.

Answer:

 class MyThread extends B
 
 {	
	  public void run() 
    {System.out.print("NPTEL Java week-6 new Assignment Q3");}  }

Week 6 : Programming Assignment 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

————————————————-

Answer:






Week 6 : Programming Assignment 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

————————————————-

Answer:

  t1.start();  
  t1.setName("Week 6 Assignment Q5");  


  t2.start(); 


  t2.setName("Week 6 Assignment Q5 New");  

Leave a Reply

Your email address will not be published. Required fields are marked *