Programming in Java Nptel Week 6 Assignment Answers
Are you looking for Programming in Java Nptel Week 6 Assignment Answers? You’ve come to the right place! Access the latest and most accurate solutions for your Week 6 assignment in the Programming in Java course
Course Link: Click Here
Table of Contents

Programming in Java Nptel Week 6 Assignment Answers (Jan-Apr 2025)
Que. 1 Consider the following code snippet.
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("Running thread: " + i);
}
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.run();
System.out.println("Main method complete.");
}
}
What will be the output of the program?
a) Running thread: 0
Running thread: 1
Running thread: 2
Main method complete.
b) Running thread: 0
Main method complete.
Running thread: 1
Running thread: 2
c) Main method complete.
d) Error: The thread was not started using start().
Que. 2 Which of the following best describes the concept of multithreading in Java?
a) Multiple threads execute concurrently, sharing the same memory space.
b) Only one thread executes at a time, ensuring sequential execution.
c) Threads in Java cannot communicate with each other.
d) Threads require separate memory allocation for each thread to run.
Que. 3 What will happen when the following code is executed?
class ExampleThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class Main {
public static void main(String[] args) {
ExampleThread thread = new ExampleThread();
thread.start();
thread.start();
}
}
a) The program will execute successfully, printing "Thread is running." twice.
b) The program will throw an error when attempting to start the thread a second time.
c) The program will terminate without any output.
d) The thread will run only once, and the second start() call will be ignored.
Que. 4 Find the error in the following program:
class RunnableExample implements Runnable {
public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println("Runnable thread: " + i);
}
}
}
public class Main {
public static void main(String[] args) {
RunnableExample runnable = new RunnableExample();
runnable.run();
System.out.println("Main method ends.");
}
}
a) The program will throw an error because Runnable cannot be executed directly.
b) The program will run successfully but will not create a new thread.
c) The program will create a new thread and execute concurrently.
d) The program will throw a runtime error because Thread is not used.
Que. 5 What will happen when the following code is executed?
class RunnableExample implements Runnable {
public void run() {
for (int i = 1; i <= 3; i++) {
System.out.println("Thread running: " + i);
}
}
}
public class Main {
public static void main(String[] args) {
RunnableExample task = new RunnableExample();
Thread thread = new Thread(task);
thread.start();
}
}
a) The program will throw a compile-time error because Runnable is not a thread.
b) The program will execute successfully, and the run() method will run in a new thread.
c) The program will execute the run() method directly on the main thread.
d) The program will throw a runtime error because Runnable is not properly implemented.
Que. 6 Which of the following states can a thread enter during its lifecycle in Java?
a) New, Runnable, Running, Blocked
b) New, Runnable, Waiting, Blocked, Terminated
c) New, Runnable, Running, Sleeping, Dead
d) New, Active, Waiting, Suspended, Terminated
Que. 7 What does the thread scheduler use to decide which thread to run when multiple threads are in the runnable state?
a) Thread priority
b) Thread’s execution time
c) Thread name
d) Thread creation order
Que. 8 Consider the following program:
class PriorityExample extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName() +
" with priority " +
Thread.currentThread().getPriority());
}
}
public class Main {
public static void main(String[] args) {
PriorityExample t1 = new PriorityExample();
PriorityExample t2 = new PriorityExample();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
Which of the following is true about the output?
a) The thread with the higher priority is guaranteed to execute first.
b) The thread with the lower priority will never execute.
c) The order of execution depends on the JVM and OS scheduling policies.
d) The program will throw an error due to invalid priority values.
Que. 9 What is the primary purpose of thread synchronization in Java?
a) To allow multiple threads to execute a method at the same time
b) To ensure thread execution follows a specific order
c) To prevent race conditions and ensure data consistency
d) To allow threads to communicate with each other
Que. 10 What is the primary difference between Byte Streams and Character Streams in Java?
a) Byte Streams handle characters, while Character Streams handle bytes.
b) Byte Streams are used for binary data, while Character Streams are used for text data.
c) Character Streams are faster than Byte Streams in all cases.
d) Character Streams cannot handle international characters like Unicode.
Programming in Java Nptel Week 6 Assignment Answers (July-Dec 2024)
1. What is the output of the following program?
a. “Hello World”
b. “Cannot divide by 0”
c. Compilation Error
d. Runtime Error (the code compiles successfully)
Answer: c. Compilation Error
2. What will be the output of the following program?
a. 20
b. 21
c. 19
d. 22
Answer: b. 21
For answers or latest updates join our telegram channel: Click here to join
3. What is the output of the following program?
a. 1
b. 2
c. 0
d. Compilation Error
Answer : c. 0
4. Which exception is thrown when an array element is accessed beyond the array size?
a. ArrayElementOutOfBounds
b. ArrayIndexOutOfBoundsException
c. ArrayIndexOutOfBounds
d. None of these
Answer: b. ArrayIndexOutOfBoundsException
For answers or latest updates join our telegram channel: Click here to join
These are Programming in Java Nptel Week 5 Assignment Answers
5.What is the output of the following program?
a. java
b. java course
c. nptel course
d. Compilation Error
Answer: d. Compilation Error
6. Fill in the blank in the program so that the output is “Java”.
a. public void
b. void
c. private void
d. static void
Answer: a. public void
For answers or latest updates join our telegram channel: Click here to join
7. How many times will “Java” be printed if the following code is executed?
a. 0
b. 1
c. 2
d. 3
Answer: c. 2
8. The following is a simple program using the concept of thread. What is the output of the following program?
a. 1 3 5 7
b. 2 4 6 8
c. 1 3 5 7 9
d. 2 4 6
Answer: b. 2 4 6 8
For answers or latest updates join our telegram channel: Click here to join
9. For the program given below, what will be the output after its execution?
a. 1
b. 2
c. 0
d. 01
Answer: a. 1
10.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();
For answers or latest updates join our telegram channel: Click here to join
All Weeks of Programming In Java: Click Here
For answers to additional Nptel courses, please refer to this link: NPTEL Assignment Answers
Programming in Java Nptel Week 6 Assignment Answers (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 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 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 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 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 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 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 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 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 Answers
More Weeks of Programming In Java: Click here
More Nptel Courses: https://progiez.com/nptel-assignment-answers
Programming in Java Nptel Week 6 Assignment Answers (JULY-DEC 2023)
Course Name: Programming In Java
Course Link: Click Here
These are Programming In Java 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 Programming In Java 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 Programming In Java 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 Programming In Java Assignment 6 Answers
More Weeks of Programming In Java: Click here
More Nptel Courses: Click here