
☞ Programming in Java
NPTEL Week 08 : Programming Assignments Answers- 2025
public class W08_P1 {
// Create a class that extends Thread
static class MyThread extends Thread {
@Override
public void run() {
// Answer
System.out.print("Thread is running");
//End
}
}
public static void main(String[] args) {
// Create object of MyThread
MyThread t = new MyThread();
// Start the thread
t.start();
}
}
public class W08_P2 {
// Create a class that implements Runnable interface
static class MyRunnable implements Runnable {
@Override
public void run() {
// Answer
System.out.print("Runnable thread is running");
//End
}
}
public static void main(String[] args) {
// Create object of MyRunnable
MyRunnable r = new MyRunnable();
// Create Thread using Runnable object
Thread t = new Thread(r);
// Start the thread
t.start();
}
}
public class W08_P3 {
// Create a class that extends Thread
static class MyThread extends Thread {
@Override
public void run() {
// Answer
System.out.println("Thread is running");
// End
}
}
public static void main(String[] args) {
// Create thread object
MyThread t = new MyThread();
// Thread is created but not started yet
System.out.println("Thread state before start");
// Start thread
t.start();
// Thread has started running
System.out.println("Thread state after start");
try {
// Wait for thread to finish
t.join();
} catch (InterruptedException e) {
// Not needed for beginners, but required to handle possible interruptions
}
// Thread has finished
System.out.println("Thread state after completion");
}
}
public class W08_P4 {
// Thread class
static class MyThread extends Thread {
@Override
public void run() {
// No output here to keep portal testing consistent
}
}
public static void main(String[] args) {
MyThread t = new MyThread();
// Set thread priority
t.setPriority(8);
// Start thread
t.start();
// ANSWER
System.out.print("Thread priority is: " + t.getPriority());
// END
}
}
public class W08_P5 {
// Shared class with a number
static class Counter {
int count = 0;
// Synchronized method to safely increase number
public synchronized void increment() {
count++;
}
}
// Thread class to run increment
static class MyThread extends Thread {
Counter c;
MyThread(Counter c) {
this.c = c;
}
@Override
public void run() {
//ANSWER
for(int i =1 ; i<=1000 ; i++)
{
c.increment();
}
//END
}
}
public static void main(String[] args) {
Counter c = new Counter();
// Create two threads
MyThread t1 = new MyThread(c);
MyThread t2 = new MyThread(c);
// Start both threads
t1.start();
t2.start();
try {
// Wait for both threads to finish
t1.join();
t2.join();
} catch (InterruptedException e) {
}
// Print final count
System.out.println("Final count is: " + c.count);
}
}
☞ NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025
☞ NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025
☞ NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025
☞ NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025
☞ NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025
☞ NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025
☞ NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025
☞ NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025
☞ NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025
☞ NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025
☞ NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025
☞ NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025
☞ NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025