Programming in Java | Week 6

Session: JAN-APR 2024

Course name: Programming In Java

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q1. What is the output of the following program?
a. “Programming In Java”
b. Run time error
c. Compile time error
d. ArithmeticException

Answer: c. Compile time errorc. Compile time error


Q2. What is the output of the following program?
a. 1 3
b. 1 2 3 4
c. Runtime error
d. 1 2

Answer: a. 1 3


Q3. For the program given below, what will be the output after its execution?
a. 0
b. true
c. 1
d. false

Answer: c. 1


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q4. Which of the following is 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: a. Thread(Runnable a, String str);


Q5. What is the output of the following program?
a. Compiler Error
b. “Running”
c. Runtime Exception
d. No output, but no error

Answer: b. “Running”


Q6. How many threads does the following program run on?
a. 0
b. 1
c. 2
d. 3

Answer: c. 2


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q7. In the following java program, what is the NAME of the thread?
a. thread
b. main
c. system
d. None of the above

Answer: b. main


Q8. Which of the following line(s) of code is suitable to START a thread at #1?
a. Thread t = new Thread(Nptel);
b. Thread t = new Thread(Nptel);
t.start();
c. Nptel run = new Nptel();
Thread t = new Thread(run);
t.start();
d. Thread t = new Thread();
Nptel.run();

Answer: c. Nptel run = new Nptel();
Thread t = new Thread(run);
t.start();


Q9. What is the name of the priority of this Thread in this program?
a. 1
b. 4
c. 0
d. 5

Answer: d. 5


Q10. What does I/O stand for in Java?
a. Input/Output
b. Inheritance/Overriding
c. Integer/Object
d. Iteration/Observation

Answer: a. Input/Output


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Programming Assignment

Question 1
Complete the code fragment to read two integer inputs from keyboard and find the quotient and remainder.

Solution:

int quotient = x / y;
int remainder = x % y;
System.out.println("The Quotient is = " + quotient);
System.out.print("The Remainder is = " + remainder);

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Question 2
Complete the code segment to count number of digits in an integer using while loop.

Solution:

int count = 0;
while (num != 0){
  num = num / 10;
  count++;
}
System.out.print(count);

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Question 3
Complete the code segment to display the factors of a number n.

Solution:

for (int i = 1; i <= num; i++){
  if (num % i == 0){
    System.out.print(i + " ");
  }
}

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Question 4
Complete the code segment to find the standard deviation of the 1-D array.
Use the following formula:
σ=√(1/N∑N to i=1(Xi−μ)2)
Here,
σ = Population standard deviation
N = Number of observations in the population
Xi = ith observation in the population
μ = Population mean

Solution:

for (int i = 0; i < arr.length; i++){
  sum += arr[i];
}
mean=sum/arr.length;
for (int i = 0; i < arr.length; i++){
  sq += Math.pow((arr[i] - mean), 2);
}
res = Math.sqrt(sq / arr.length);

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Question 5
Write a program which will print a pattern of “*” ‘s of height “n”.
For example:
Input:
n = 3
Output:
***
**
*
**
***

Solution:

for (int i = n; i >= 1; i--){
  for (int j = 1; j <= i; j++){
    System.out.print("*");
  }
  System.out.println();
}
for (int i = 2; i <= n; i++){
  for (int j = 1; j <= i; j++){
    System.out.print("*");
  }
  System.out.println();
}

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 6 Assignment 6 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Session: JULY-DEC 2023

Course Name: Programming In Java

Course Link: Click Here

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Programming Assignment

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:

// Write the appropriate code to extend the Thread class to complete the class Question61.
public class Question61 extends Thread{
  public void run(){
		System.out.print("Thread is Running.");
  }

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:

// 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 NPTEL Programming In Java Week 6 Assignment 6 Answers


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.

Solution:

// Class MyThread is defined which extends class B
class MyThread extends B {
	// run() is overriden and 'NPTEL Java' is printed.
	public void run() {
 		System.out.print("NPTEL Java week-6 new Assignment Q3");
	}
}

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
————————————————-

// 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++;
    }
}

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

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

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

These are NPTEL Programming In Java Week 6 Assignment 6 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Session: JAN-APR 2023

Course Name: Programming in Java

Course Link: Click Here

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q1. 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.


Q2. Consider the following code.
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, d


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q3. 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, d


Q4. 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: d. Recovers a thread from deadlock and begin execution


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q5. 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, c


Q6. 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.


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q7. The following is a simple program using the concept of thread.
What is the output of the above program?
a. 1
3
b. 2
4
6
8
c. Runtime error
d. 2
4

Answer: b. 2
4
6
8


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

Answer: a. 1


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


Q9. 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()


Q10. 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: a. public static void yield()


Programming Assignment of Programming in Java

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:

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

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 static void main(String[] args)
{
  Question62 obj = new Question62();
  Thread t1 = new Thread(obj);
  t1.setName("Main Thread");
  t1.start();
  System.out.println("Welcome to Java Week 6 New Question.");
  t1.setName("Main Thread");
}

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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.

Solution:

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

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:

public synchronized int sum()
{
  return(a+b);
}
public synchronized void inc()
{
  a++;
  b++;
}
}

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

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

These are NPTEL Programming In Java Week 6 Assignment 6 Answers

More Weeks of Programming In Java: Click Here

More Nptel courses: https://progiez.com/nptel-assignment-answers/


Session: JULY-DEC 2022

Course Name: Programming in Java NPTEL

Link of Course: Click Here

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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 NPTEL Programming In Java Week 6 Assignment 6 Answers


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 NPTEL Programming In Java Week 6 Assignment 6 Answers


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

Answer: d


These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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 NPTEL Programming In Java Week 6 Assignment 6 Answers


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 NPTEL Programming In Java Week 6 Assignment 6 Answers


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 NPTEL Programming In Java Week 6 Assignment 6 Answers


//Code

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

//Code

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

//Code

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

//Code

These are NPTEL Programming In Java Week 6 Assignment 6 Answers


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:

//Code

These are NPTEL Programming In Java Week 6 Assignment 6 Answers

More NPTEL Solutions: https://progiez.com/nptel


These are NPTEL Programming In Java Week 6 Assignment 6 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.