Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Programming In Java Programming Assignment July-2026 Week 8 to 12 - Swayam

 

Programming In Java - July 2026 - W8 to W12

NPTEL

 Please scroll down for latest Programs   ðŸ‘‡ 


Code compiled and tested successfully!



Programming Assignments 1

Due date on 2026-09-17, 23:59 IST

Your last recorded submission was on 2026-09-06, 13:04 IST.

Q.

Creating a Thread using Thread Class


Problem Statement

In Java, you can run multiple tasks at the same time using Multithreading.
The simplest way to create a thread is by extending the built-in Thread class.

What is a Thread?

  • A thread is a small unit of a program that runs independently

  • Multiple threads can run in parallel, improving efficiency

Programming Assignment:

  • Create a class called MyThread that extends Thread

  • In its run() method, print "Thread is running"

  • In the main method, create an object of MyThread and start the thread

This helps you understand the basic way to create and start a thread in Java.

Complete Program

java
public class W08_P1 {
    // Create a class that extends Thread
    static class MyThread extends Thread {
        @Override
        public void run() {
            // --- START OF SOLUTION CODE ---
            System.out.print("Thread is running");
            // --- END OF SOLUTION CODE ---
        }
    }

    public static void main(String[] args) {
        // Create object of MyThread
        MyThread t = new MyThread();
        // Start the thread
        t.start();
    }
}

Code Snippet to Paste in the Editor

java
// --- START OF SOLUTION CODE ---
System.out.print("Thread is running");
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

Thread is running
Thread is running
-
Test Case 2

-

Thread is running
Thread is running
-


Programming Assignments 2

Due date on 2026-09-17, 23:59 IST

Your last recorded submission was on 2026-09-06, 13:38 IST.

Q.

Problem Statement

In Java, another common way to create threads is by implementing the Runnable interface.

What is Runnable?

  • Runnable is an interface with a single method called run()

  • You can pass a Runnable object to a Thread and start the thread

Why use Runnable?

  • It allows your class to extend another class, as Java supports single inheritance

  • It provides flexibility in thread creation

Programming Assignment:

  • Create a class called MyRunnable that implements Runnable

  • In its run() method, print "Runnable thread is running"

  • In the main method, create a Thread object using MyRunnable and start the thread

This demonstrates thread creation using the Runnable interface.

Complete Program

java
public class W08_P2 {
    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            // --- START OF SOLUTION CODE ---
            System.out.print("Runnable thread is running");
            // --- END OF SOLUTION CODE ---
        }
    }

    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();
    }
}

Code Snippet to Paste in the Editor

java
// --- START OF SOLUTION CODE ---
System.out.print("Runnable thread is running");
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

Runnable thread is running
Runnable thread is running
-


Programming Assignments 3

Due date on 2026-09-17, 23:59 IST

Your last recorded submission was on 2026-09-06, 13:21 IST.

Q.

Programming Assignment: Understanding Basic Thread States in Java


Problem Statement

When a thread runs in Java, it moves through different stages called states.

What are Thread States?
Think of a thread like a person:

  • It starts in one state

  • Moves to another as work happens

  • Finally, it finishes

For beginners, focus on these three simple states:

  1. New – The thread is created but not started yet

  2. Running – The thread is doing its work

  3. Terminated – The thread has finished its work

Programming Assignment:

  • Create a class called MyThread that extends Thread

  • Inside its run() method, print "Thread is running"

  • In the main method:

    • Create a MyThread object

    • Print "Thread state before start"

    • Start the thread

    • Print "Thread state after start"

    • Wait for thread to finish using join()

    • Print "Thread state after completion"

This shows how thread state changes as the thread runs.

Complete Program

java
public class W08_P3 {
    // Create a class that extends Thread
    static class MyThread extends Thread {
        @Override
        public void run() {
            // --- START OF SOLUTION CODE ---
            System.out.println("Thread is running");
            // --- END OF SOLUTION CODE ---
        }
    }

    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");
    }
}

Code Snippet to Paste in the Editor

java
// --- START OF SOLUTION CODE ---
System.out.println("Thread is running");
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

Thread state before start\n
Thread state after start\n
Thread is running\n
Thread state after completion
Thread state before start\n
Thread state after start\n
Thread is running\n
Thread state after completion\n
Presentation Error


Programming Assignments 4

Due date on 2026-09-17, 23:59 IST

Your last recorded submission was on 2026-09-06, 13:24 IST.

Q.

Understanding Thread Priority in Java


Problem Statement

In Java, each thread has a priority, a number from 1 (lowest) to 10 (highest).
Priority suggests how important a thread is, though actual scheduling depends on the system.

Programming Assignment:

  • Create a class MyThread that extends Thread

  • In the main method:

    • Create a MyThread object

    • Set its priority to 8

    • Start the thread

    • Print the thread's priority after setting

No output should come from the thread's run() method to avoid output mismatch.

Complete Program

java
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();

        // --- START OF SOLUTION CODE ---
        System.out.print("Thread priority is: " + t.getPriority());
        // --- END OF SOLUTION CODE ---
    }
}

Code Snippet to Paste in the Editor

java
// --- START OF SOLUTION CODE ---
System.out.print("Thread priority is: " + t.getPriority());
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

Thread priority is: 8
Thread priority is: 8
-


Programming Assignments 5

Due date on 2026-09-17, 23:59 IST

Your last recorded submission was on 2026-09-06, 13:25 IST.

Q.

Programming Assignment: Introduction to Thread Synchronization


Problem Statement

What is a Thread?
Imagine your computer doing many tasks at once — for example:

  • Playing music

  • Downloading files

  • Browsing the internet

In Java, each small task that runs independently is called a Thread.
Threads help programs run faster by working at the same time.

Why Synchronization?
When multiple threads work on the same thing together, they may interfere with each other.
For example:

  • Two threads try to update the same number at the same time

  • The final result may be wrong

What is Synchronization?

  • It is like putting a lock

  • Only one thread can work on the shared thing at a time

  • This prevents problems caused by threads disturbing each other


Programming Assignment:

  • Create a class Counter with a number count starting from 0

  • Write a method increment() to increase the number by 1, using synchronized keyword

  • Create a thread class called MyThread that runs the increment() method 1000 times

  • In main, run two threads to increase the number

  • After both threads finish, print the final count

This shows how to use synchronization to avoid problems when multiple threads share data.

Complete Program

java
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() {
            // --- START OF SOLUTION CODE ---
            for (int i = 0; i < 1000; i++) {
                c.increment();
            }
            // --- END OF SOLUTION CODE ---
        }
    }
    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);
    }
}

Code Snippet to Paste in the Editor

java
// --- START OF SOLUTION CODE ---
for (int i = 0; i < 1000; i++) {
    c.increment();
}
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

Final count is: 2000
Final count is: 2000\n
Presentation Error


W09 Programming Assignments 1

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-14, 13:43 IST.

Q.

Write suitable code to develop a 2D Flip-Flop Array with dimension 5 × 5, which replaces all input elements with values 0 by 1 and 1 by 0. An example is shown below:

INPUT:
00001
00001
00001
00001
00001

OUTPUT:
11110
11110
11110
11110
11110

Note the following points carefully:
  • Here, the input must contain only 0 and 1.
  • The input and output array size must be of dimension 5 × 5.
  • Flip-Flop: If 0 then 1 and vice-versa.

Java
import java.util.Scanner;

public class W09_P1 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        // Declare the 5X5 2D array to store the input
        char original[][] = new char[5][5];

        // Input 2D Array using Scanner Class and check data validity
        for (int line = 0; line < 5; line++) {
            String input = sc.nextLine();
            char seq[] = input.toCharArray();
            if (seq.length == 5) {
                for (int i = 0; i < 5; i++) {
                    if (seq[i] == '0' || seq[i] == '1') {
                        original[line][i] = seq[i];
                        if (line == 4 && i == 4)
                            flipflop(original);
                    } else {
                        System.out.print("Only 0 and 1 supported.");
                        break;
                    }
                }
            } else {
                System.out.print("Invalid length");
                break;
            }
        }
    } // The main() ends here

    static void flipflop(char[][] flip) {
        // Flip-Flop Operation
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (flip[i][j] == '1')
                    flip[i][j] = '0';
                else
                    flip[i][j] = '1';
            }
        }

        // Output the 2D FlipFlopped Array without a trailing newline after the 5th line
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(flip[i][j]);
            }
            if (i < 4) {
                System.out.println();
            }
        }
    }
} // The main class ends here




You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

00001 00001 00001 00001 00001

11110\n
11110\n
11110\n
11110\n
11110
11110\n
11110\n
11110\n
11110\n
11110\n
Presentation Error


W09 Programming Assignments 2

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-14, 13:44 IST.

Q.

Complete the code to develop a BASIC CALCULATOR that can perform operations like Addition, Subtraction, Multiplication and Division.

Note the following points carefully:
  • Use only double datatype to store calculated numeric values.
  • Assume input to be of integer datatype.
  • The output should be rounded using Math.round() method.
  • Take care of the spaces during formatting output (e.g., single space each before and after =).
  • The calculator should be able to perform required operations on a minimum of two operands as shown in the below example:
Input:
5+6

Output:
5+6 = 11
Java
import java.util.Scanner;

public class W09_P2 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine(); // Read as string, e.g., 5+6

        // Declare and initialize the required variable(s)
        int i = 0;
        int j = 0;
        double output = 0;
        // Split the input string into character array
        char seq[] = input.toCharArray();
        /*
        Use some method to separate the two operands
        and then perform the required operation.
        */
        for (int a = 0; a < seq.length; a++) {
            if (seq[a] == '+') {
                i = Integer.parseInt(input.substring(0, a));
                j = Integer.parseInt(input.substring(a + 1, seq.length));
                output = (double) i + j;
            } else if (seq[a] == '-') {
                i = Integer.parseInt(input.substring(0, a));
                j = Integer.parseInt(input.substring(a + 1, seq.length));
                output = (double) i - j;
            } else if (seq[a] == '/') {
                i = Integer.parseInt(input.substring(0, a));
                j = Integer.parseInt(input.substring(a + 1, seq.length));
                output = (double) i / j;
            } else if (seq[a] == '*') {
                i = Integer.parseInt(input.substring(0, a));
                j = Integer.parseInt(input.substring(a + 1, seq.length));
                output = (double) i * j;
            }
        }
        System.out.print(input + " = " + Math.round(output));
    } // The main() method ends here
} // The main class ends here
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5+6

5+6 = 11
5+6 = 11
-


W09 Programming Assignments 3

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-14, 13:46 IST.

Q.

Write a Java program that utilizes multithreading to calculate and print the squares of numbers from a specified begin to a specified end.
The main method is already created.
You need to design a SquareThread class that has two members,
  • int begin;
  • int end;
Each thread should sequentially print the squares of numbers from begin to end (both inclusive).
The same code will be used to create another thread that prints the sqaure of numbers from end to begin in reverse order.
(if begin is greater than end, print the square of each number in reverse order first)
The main method will first call SquareThread with begin and end and then in reverse order.
The class you create should be able to handle such case and print as required in the correct order.
HINT: use the keyword `synchronized` in the run method.
Java
import java.util.Scanner;

class SquareThread extends Thread {
    private int begin;
    private int end;

    public SquareThread(int begin, int end) {
        this.begin = begin;
        this.end = end;
    }

    public synchronized void run() {
        if (begin > end) {
            for (int i = begin; i >= end; i--) {
                System.out.println(i * i);
            }
        } else {
            for (int i = begin; i <= end; i++) {
                System.out.println(i * i);
            }
        }
    }
}

public class W09_P3 {
    public static void main(String args[]) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
        int begin = scanner.nextInt();
        int end = scanner.nextInt();
        scanner.close();
        
        SquareThread thread1 = new SquareThread(begin, end);
        SquareThread thread2 = new SquareThread(end, begin);
        thread1.start();
        thread1.join();
        thread2.start();
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

1 5

1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1
1\n
4\n
9\n
16\n
25\n
25\n
16\n
9\n
4\n
1\n
Presentation Error
Test Case 2

9 6

81\n
64\n
49\n
36\n
36\n
49\n
64\n
81
81\n
64\n
49\n
36\n
36\n
49\n
64\n
81\n
Presentation Error


W09 Programming Assignments 4

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-14, 13:47 IST.

Q.

Complete the code segment to catch the exception in the following, if any.
On the occurrence of such an exception, your program should print
"Please enter valid data".
If there is no such exception, it will print the square of the number entered.
Java
import java.io.*;

class W09_P4 {
    public static void main(String args[]) {
        try {
            java.util.Scanner r = new java.util.Scanner(System.in);
            String number = r.nextLine();
            int x = Integer.parseInt(number);
            System.out.print(x * x);
        } catch (Exception e) {
            System.out.print("Please enter valid data");
        }
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

2

4
4
-
Test Case 2

p

Please enter valid data
Please enter valid data
-


W09 Programming Assignments 5

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-14, 13:48 IST.

Q.

Define a class Point with members
  • private double x;
  • private double y;
and methods:
  • public Point(double x, double y){} // Constructor to create a new point
  • public double distance(Point p2){} // Function to return the distance of this Point from another Point
Java
import java.util.Scanner;

public class W09_P5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        System.out.print(p1.distance(p2));
    }
}

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double distance(Point p2) {
        double d;
        d = Math.sqrt((p2.x - x) * (p2.x - x) + (p2.y - y) * (p2.y - y));
        return d;
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

0 0 1 1

1.4142135623730951
1.4142135623730951
-
Test Case 2

0 0 0 5

5.0
5.0
-



Week 10 Programming Assignments 1

Due date on 2026-10-01, 23:59 IST

Your last recorded submission was on 2026-09-22, 05:17 IST.

Q.

Introduction to JDBC and Required Imports


Problem Statement

What is JDBC?
JDBC means Java Database Connectivity, which allows Java programs to work with databases.

Before writing database programs, you must import the correct packages. Without proper imports, the code will not compile.

Programming Assignment:

  • Import the necessary JDBC packages so the program can compile

  • You only need to complete the import section

  • The output "import successful" is printed automatically

This helps you practice the correct way to prepare Java programs for database work.

Complete Program

Java
public class W10_P2 {
    public static void main(String[] args) {
        // --- START OF SOLUTION CODE ---
        String url = "jdbc:sqlite:test.db";
        // --- END OF SOLUTION CODE ---

        // Portal test output
        if (url.equals("jdbc:sqlite:test.db")) {
            System.out.println("connection string ready");
        } else {
            System.out.println("incorrect connection string");
        }
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
String url = "jdbc:sqlite:test.db";
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

import successful
import successful\n
Presentation Error


Week 10 Programming Assignments 2

Due date on 2026-10-01, 23:59 IST

Q.

 Writing a JDBC Connection String


Problem Statement

What is a JDBC Connection String?
To connect a Java program to a database, we use a special sentence called a connection string.
It tells Java:

  • Which database to use

  • Where the database is located

In this assignment, you will practice writing the correct JDBC connection string for SQLite database.

Programming Assignment:

  • Write the correct connection string for an SQLite database called test.db

  • The rest of the program prints "connection string ready" if your string is correct

This helps beginners practice writing JDBC connection strings safely, without actual database access.

Complete Program

Java
public class W10_P2 {
    public static void main(String[] args) {
        // --- START OF SOLUTION CODE ---
        String url = "jdbc:sqlite:test.db";
        // --- END OF SOLUTION CODE ---

        // Portal test output
        if (url.equals("jdbc:sqlite:test.db")) {
            System.out.println("connection string ready");
        } else {
            System.out.println("incorrect connection string");
        }
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
String url = "jdbc:sqlite:test.db";
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

connection string ready
connection string ready\n
Presentation Error


Week 10 Programming Assignments 3

Due date on 2026-10-01, 23:59 IST

Your last recorded submission was on 2026-09-22, 05:24 IST.

Q.

Writing a Simple SQL Insert Statement


Problem Statement

What is an SQL Insert Statement?
When working with databases, we use the INSERT command to add new records (data) into a table.

In this assignment:

  • Imagine there is a table called students with two columns: id and name

  • You will practice writing the correct SQL insert statement as a text string

  • No actual database operation is performed, only the string is checked

Programming Assignment:

  • Complete the SQL insert statement to add a student with id 1 and name 'Alice'

  • The rest of the code checks your string and prints "insert statement ready" if correct

This task helps beginners practice writing SQL commands safely.

Complete Program

Java
public class W10_P3 {
    public static void main(String[] args) {
        // --- START OF SOLUTION CODE ---
        String sql = "INSERT INTO students (id, name) VALUES (1, 'Alice');";
        // --- END OF SOLUTION CODE ---

        // Portal test output
        if (sql.equals("INSERT INTO students (id, name) VALUES (1, 'Alice');")) {
            System.out.println("insert statement ready");
        } else {
            System.out.println("incorrect statement");
        }
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
String sql = "INSERT INTO students (id, name) VALUES (1, 'Alice');";
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Private Test Cases

Private Test cases used for Evaluation
Status
Test Case 1
Passed



Week 10 Programming Assignments 4

Due date on 2026-10-01, 23:59 IST

Q.


Writing a Simple SQL SELECT Statement 


Problem Statement

What is a SQL SELECT Statement?
The SELECT statement is used to get data from a database table.
You can choose to fetch all columns or specific columns.

In this assignment:

  • Imagine a table called students with two columns: id and name

  • You will practice writing a SQL statement to fetch all columns for all students

Programming Assignment:

  • Write a SQL SELECT statement as a text string to get all data from students table

  • The rest of the program checks your string and prints "select statement ready" if correct

This task helps beginners practice safe SQL reading commands.

Complete Program

Java
public class W10_P4 {
    public static void main(String[] args) {
        // --- START OF SOLUTION CODE ---
        String sql = "SELECT * FROM students;";
        // --- END OF SOLUTION CODE ---

        if (sql.equals("SELECT * FROM students;")) {
            System.out.println("select statement ready");
        } else {
            System.out.println("incorrect statement");
        }
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
String sql = "SELECT * FROM students;";
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

select statement ready
select statement ready\n
Presentation Error


Week 10 Programming Assignments 5

Due date on 2026-10-01, 23:59 IST

Q.

Writing a Simple SQL UPDATE Statement 

Problem Statement

What is an SQL UPDATE Statement?
The UPDATE command is used to change existing data in a database table.

In this assignment:

  • Imagine a table called students with two columns: id and name

  • You will write a SQL statement to update the name of the student with id 1 to 'Bob'

Programming Assignment:

  • Write the correct SQL UPDATE statement as a text string

  • The rest of the code checks your string and prints "update statement ready" if correct

This helps beginners practice safe SQL update syntax.

Complete Program

Java
public class W10_P5 {
    public static void main(String[] args) {
        // --- START OF SOLUTION CODE ---
        String sql = "UPDATE students SET name = 'Bob' WHERE id = 1;";
        // --- END OF SOLUTION CODE ---

        // Portal test output
        if (sql.equals("UPDATE students SET name = 'Bob' WHERE id = 1;")) {
            System.out.println("update statement ready");
        } else {
            System.out.println("incorrect statement");
        }
    }
}

Code Snippet to Paste in the Editor

Java
// --- START OF SOLUTION CODE ---
String sql = "UPDATE students SET name = 'Bob' WHERE id = 1;";
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

-

update statement ready
update statement ready\n
Presentation Error



W11 Programming Assignments 1

Due date on 2026-10-08, 23:59 IST

Your last recorded submission was on 2026-09-26, 16:08 IST.

Q.

The following code needs some package to work properly.
Write appropriate code to
  • import the required package(s) in order to make the program compile and execute successfully.
(Ignore the hidden code.)

Complete Program

Java
//Import required packages
// Hint: USE static import
import java.sql.*;
import java.lang.*;
import static java.sql.DriverManager.*;

public class W11_P1 {
    public static void main(String args[]) {
        try {
            Connection conn = null;
            Statement stmt = null;
            String DB_URL = "jdbc:sqlite:/tempfs/db";
            System.setProperty("org.sqlite.tmpdir", "/tempfs");
            
            // Connection using static import method from DriverManager
            conn = getConnection(DB_URL);
            System.out.println(conn.isValid(1));
            conn.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

0

true
true
-


W11 Programming Assignments 2

Due date on 2026-10-08, 23:59 IST

Your last recorded submission was on 2026-09-26, 16:11 IST.

Q.

Write the JDBC codes needed to create a Connection interface using the DriverManager class and the variable DB_URL. Check whether the connection is successful using 'isValid(timeout)'method to generate the output, which is either 'true' or 'false'.

Note the following points carefully:
  • Name the connection object as conn only.
  • Use timeout value as 1.
  • Ignore the hidden code.

Complete Program

Java
import java.sql.*;
import java.util.Scanner;

public class W11_P2 {
    public static void main(String args[]) {
        try {
            Connection conn = null;
            Statement stmt = null;
            String DB_URL = "jdbc:sqlite:/tempfs/db";
            System.setProperty("org.sqlite.tmpdir", "/tempfs");

            // Open a connection
            conn = DriverManager.getConnection(DB_URL);
            System.out.print(conn.isValid(1));

            conn.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

0

true
true
-


W11 Programming Assignments 3

Due date on 2026-10-08, 23:59 IST

Your last recorded submission was on 2026-09-26, 16:12 IST.

Q.

Due to some mistakes in the below code, the code is not compiled/executable.
Modify and debug the JDBC code to make it execute successfully.

Complete Program

Java
// Fix bugs in the code, DO NOT ADD or DELETE ANY LINE
import java.sql.*;  // All sql classes are imported
import java.lang.*; // Semicolon is added
import java.util.Scanner;
public class W11_P3 {
    public static void main(String args[]) {
        try {
            Connection conn = null;
            Statement stmt = null;
            String DB_URL = "jdbc:sqlite:/tempfs/db";
            System.setProperty("org.sqlite.tmpdir", "/tempfs");
            // Connection object is created
            conn = DriverManager.getConnection(DB_URL);     // Add this line
            conn.close();                                   // correction here
            System.out.print(conn.isClosed());
            
            // Hidden code completes try-catch block
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

0

true
true
-


W11 Programming Assignments 4

Due date on 2026-10-08, 23:59 IST

Your last recorded submission was on 2026-09-26, 16:14 IST.

Q.

Complete the code segment to create a new table named 'STUDENTS' in SQL database using the following information.

ColumnUIDNameRollAge
TypeIntegerVarchar (45)Varchar (12)Integer

Complete Program

Java
import java.sql.*;
import java.lang.*;

public class W11_P4 {
    public static void main(String args[]) {
        try {
            Connection conn = null;
            Statement stmt = null;
            String DB_URL = "jdbc:sqlite:/tempfs/db";
            System.setProperty("org.sqlite.tmpdir", "/tempfs");
            // Open a connection
            conn = DriverManager.getConnection(DB_URL);
            stmt = conn.createStatement();

            String CREATE_TABLE_SQL="CREATE TABLE STUDENTS (UID INT, Name VARCHAR(45), Roll VARCHAR(12), Age INT);";
            // Execute the statement containing SQL command
            stmt.executeUpdate(CREATE_TABLE_SQL);

        }
        catch(Exception e){ System.out.println(e);}
    }
}

Code Snippet to Paste in the Editor

Java
String CREATE_TABLE_SQL="CREATE TABLE STUDENTS (UID INT, Name VARCHAR(45), Roll VARCHAR(12), Age INT);";
// Execute the statement containing SQL command
stmt.executeUpdate(CREATE_TABLE_SQL);
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

1

No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Roll\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT
No. of columns : 4\n
Column 1 Name: UID\n
Column 1 Type : INT\n
Column 2 Name: Name\n
Column 2 Type : VARCHAR\n
Column 3 Name: Roll\n
Column 3 Type : VARCHAR\n
Column 4 Name: Age\n
Column 5 Type : INT
-


W11 Programming Assignments 5

Due date on 2026-10-08, 23:59 IST

Your last recorded submission was on 2026-09-26, 16:16 IST.

Q.

Complete the code segment to rename an already created table named 'STUDENTS' into 'GRADUATES'.

Complete Program

Java
import java.sql.*;
import java.lang.*;

public class W11_P5 {
    public static void main(String args[]) {
        try {
            Connection conn = null;
            Statement stmt = null;
            String DB_URL = "jdbc:sqlite:/tempfs/db";
            System.setProperty("org.sqlite.tmpdir", "/tempfs");
            // Open a connection
            conn = DriverManager.getConnection(DB_URL);
            stmt = conn.createStatement();

            // Write the SQL command to rename a table
            String alter="ALTER TABLE STUDENTS RENAME TO GRADUATES;";
            // Execute the SQL command
            stmt.executeUpdate(alter);

        }    catch(Exception e){ System.out.println(e);}
    }
}

Code Snippet to Paste in the Editor

Java
// Write the SQL command to rename a table
String alter="ALTER TABLE STUDENTS RENAME TO GRADUATES;";
// Execute the SQL command
stmt.executeUpdate(alter);
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 1/1 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

1

TABLE NAME = GRADUATES
TABLE NAME = GRADUATES
-


























































































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.