Programming in Java | Week 10

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 10 Assignment 10 Answers


Q1. Which class provides methods to work with URLs?
a. URLConnection
b. HttpURL
c. NetURLg
d. URL

Answer: d. URL

Q2. What does the following code do?
a. Creates a JFrame with a JButton labeled “Programming in Java”
b. Compiles with errors
c. Displays a message dialog
d. Creates a JPanel with a JButton labeled “Programming in Java”

Answer: a. Creates a JFrame with a JButton labeled "Programming in Java"

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

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q3. What happens when the button in this Java code snippet is clicked?
a. The program exits
b. A message dialog with the text “Welcome to the course” is displayed
c. The button label changes to “Welcome to the course”
d. Nothing happens

Answer: b. A message dialog with the text "Welcome to the course" is displayed

Q4. What GUI component is used to input the password in this Java code snippet?
a. JTextField
b. JButton
c. JtextArea
d. JPasswordField

Answer: d. JPasswordField

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

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q5. What does this Java code snippet create?
a. A frame with a list of colors displayed as buttons
b. A frame with a combo box containing options “Red”, “Green”, and “Blue”
c. A frame with radio buttons for selecting colors
d. A frame with checkboxes for selecting colors

Answer: b. A frame with a combo box containing options "Red", "Green", and "Blue"

Q6. What does this Java code snippet do?
a. Just prints the IP address of the local machine
b. Prints the IP address and host name of the local machine
c. Prints the IP address and host name of “nptel.ac.in”
d. Just prints the IP address of “nptel.ac.in”

Answer: c. Prints the IP address and host name of "nptel.ac.in"

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

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q7. What does this Java code snippet do?
a. Connects to a MySQL database, retrieves data from the “employees” table, and prints it
b. Inserts data into the “employees” table of a MySQL database
c. Deletes data from the “employees” table of a MySQL database
d. Updates data in the “employees” table of a MySQL database

Answer: a. Connects to a MySQL database, retrieves data from the "employees" table, and prints it

Q8. What does this Java code snippet do?
a. Retrieves data from the “employees” table
b. Inserts a new employee record into the “employees” table
c. Updates employee records in the “employees” table
d. Deletes employee records from the “employees” table

Answer: b. Inserts a new employee record into the "employees" table

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

See also  Programming in Java | Week 8

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q9. What additional code is needed at #1 to complete this Java program to send a message?
a. socket.recieve(packet);
b. Socket.Send(packet);
c. Socket.send(packet);
d. socket.send(packet);

Answer: d. socket.send(packet);

Q10. What is missing in this Java program?
a. Retrieving the number of employees
b. Deleting employee records
c. Updating employee records
d. Importing necessary JDBC libraries

Answer: d. Importing necessary JDBC libraries

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

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Programming Assignment

Question 1
Program to sort the elements of an array in ascending order. Follow the naming convention as given in the main method of the suffix code.

Solution:

public static void sortArray(int[] array)
{
  for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length-i-1; j++) {
      if(array[j]>array[j+1])
      {
        int tmp = array[j];
        array[j] = array[j+1];
        array[j+1] = tmp;
      }
    }
  }
}

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 2
Print a given matrix in spiral form. Follow the naming convention as given in the main method of the suffix code.

Solution:

public static void spiral_method(int [][] a, int dimension1, int dimension2)
{
  int k = 0;
  int l = 0;
  
  while(k<dimension1 && l<dimension2){
    
    for (int i = l; i < dimension2 ; i++)
      System.out.print(a[k][i] + " ");
    
    k++;
    
    for (int i = k; i < dimension1 ; i++)
      System.out.print(a[i][dimension2-1] + " ");
    
    dimension2--;
    
    if(k<dimension1){
      for (int i = dimension2-1; i > l-1 ; i--)
      System.out.print(a[dimension1-1][i] + " ");
    
    dimension1--;
    }
    
    if(l<dimension2){
      for (int i = dimension1-1; i > k-1 ; i--)
      System.out.print(a[i][l] + " ");
    
    l++;
    }
  }
}

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 3
Code to create two threads, one printing even numbers and the other printing odd numbers.

Solution:

int start;
int end;
 
PrintNumbers(int start, int end){
  this.start = start;
  this.end = end;
}
 
@Override
public void run() {
  for (int i = start; i <= end; i= i+2) {
    System.out.println(Thread.currentThread().getName() + ": " + i);
  }
}

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 4
Program to compute the sum of all prime numbers in a given range.
The range value will be positive.Follow the naming convention as given in the main method of the suffix code.

Solution:

public static int primeSum(int x, int y){
  int sum = 0;
  for(int n=x;n<=y;n++){
    int r = 1;
    for(int i=n/2;i>1;i--){
      r=n%i;
      if(r==0)
        break;
    }
    if(r!=0)
      sum+=n;
  }
  return sum;
}

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 5
Write a program that converts temperatures between Celsius and Fahrenheit. Implement two methods, celsiusToFahrenheit and fahrenheitToCelsius, to perform the conversions.

Solution:

public static double celsiusToFahrenheit(double celsius) throws TemperatureException {
  if ( celsius < -273.15 )
    throw new TemperatureException("Invalid Celsius temperature (below absolute zero)");
  else
    return (celsius * 9 / 5) + 32;
}
 
public static double fahrenheitToCelsius (double fahrenheitInput) throws TemperatureException {
  if ( fahrenheitInput < -459.67 )
    throw new TemperatureException("Invalid Fahrenheit temperature (below absolute zero)");
  else
    return (fahrenheitInput - 32) * 5 / 9;
}
 
}

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

These are NPTEL Programming In Java Week 10 Assignment 10 Answers

See also  Programming in Java | Week 6

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 10 Assignment 10 Answers


Programming Assignment

Question 1
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.

Solution:

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

Question 2
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 ‘isAlive(timeout)’ method to generate the output, which is either ‘true’ or ‘false’.
Note the following points carefully:
1. Name the connection object as ‘conn’ only.
2. Use timeout value as 1.

Solution:

conn = DriverManager.getConnection(DB_URL);
boolean isAlive = conn.isWrapperFor(java.sql.Connection.class)
  && conn.unwrap(java.sql.Connection.class).isValid(1);
if (isAlive)
{
  System.out.println("true");
}
else
{
  System.out.println("false");
}
conn.close();

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 3
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.

Solution:

import java.sql.*;
import java.lang.*;
import java.util.Scanner;
public class Question103 {
    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");
              conn = DriverManager.getConnection(DB_URL);
              conn.close();
              System.out.print(conn.isClosed());
       }
       catch(Exception e){ System.out.println(e);}
    }
}

Question 4
Complete the code segment to create a new table named ‘PLAYERS’ in SQL database using the following information.

ColumnUIDFirst_NameLast_NameAge
TypeIntegerVarchar (45)Varchar (45)Integer

Solution:

String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
stmt.executeUpdate(CREATE_TABLE_SQL);

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 5
Complete the code segment to rename an already created table named ‘PLAYERS’ into ‘SPORTS’.

Solution:

String alter="ALTER TABLE players RENAME TO sports;";
stmt.executeUpdate(alter);

These are NPTEL Programming In Java Week 10 Assignment 10 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 10 Assignment 10 Answers


Q1. Which of the following statement(s) is(are) true?
a. TCPis not reliable.
b. UDP is most reliable.
c. TCPis fast and UDP is slow.
d. In HTTPS, all communication between two computers are encrypted.

Answer: d. In HTTPS, all communication between two computers are encrypted.


Q2. Which of the following statement(s) is/are true?
a. DatagramSocket implements Object.
b. DatagramSocket implements Closeable.
c. DatagramSocket extends Object.
d. DatagramSocket extends Closeable.
e. DatagramSocket extends Object implements Closeable

Answer: e. DatagramSocket extends Object implements Closeable


These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q3. Which of the following is/are interface(s) present in java.net package?
a. InterfaceAddress
b. FileNameMap
c. DatagramPacket
d. DatagramSocket

Answer: b. FileNameMap


Q4. Which of the following statement(s) is/are true?
a. Addresses belongs to a Low-Level API.
b. Sockets belong to High-Level API.
c. URIs and URLs are Low-Level API.
d. Interfaces are High-Level API.

Answer: a. Addresses belongs to a Low-Level API.

See also  Programming in Java Nptel Week 4 Assignment Answers

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q5. In context of the following URL, identify the correct option.
https://nptel.ac.in

a. There is no protocol provided in the above link.
b. The website provides a secure connection.
c. The given link is incomplete and hence cannot open a website.
d. The ac.in refers to the website path.

Answer: b. The website provides a secure connection.


Q6. Which of the following is/are application layer protocol(s)?
a. TCP
b. UDP
c. ARP
d. SMTP

Answer: d. SMTP


These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q7. What is true about IP:PORT in the following options?
a. Port number 21 is the default FTP port.
b. Only port number is required for FTP connections and no IP is required.
c. 127.0.0.1 and localhost are same.
d. There is no concept of PORT in IPv6.

Answer: a, c


Q8. Which of the following is/are valid Data Definition Language (DDL) command(s)?
a. SELECT
b. INSERT
c. UPDATE
d. ALTER TABLE

Answer: d. ALTER TABLE


These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Q9. In JDBC, all raw data types (including binary documents or images) should be read and uploaded to the database as an array of
a. byte
b. vector
c. char
d. file

Answer: a. byte


Q10. The package, which is required to be imported for the JDBC programming?
a. java.net
b. java.lang
c. java.io
d. java.sql

Answer: d. java.sql


Programming Assignment of Programming in Java

Question 1

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.

Solution:

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

Question 2

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 ‘isAlive(timeout)’ method to generate the output, which is either ‘true’ or ‘false’.
Note the following points carefully:
1. Name the connection object as ‘conn’ only.
2. Use timeout value as 1.

Solution:

conn = DriverManager.getConnection (DB_URL);
System.out.print(conn.isValid(1));

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 3

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.

Solution:

import java.sql.*;
import java.lang.*;
import java.util.Scanner;
public class Question103 {
    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");

              conn = DriverManager.getConnection(DB_URL);
              conn.close();
              System.out.print(conn.isClosed());
       }
       catch(Exception e){ System.out.println(e);}
    }
}

Question 4
Complete the code segment to create a new table named ‘PLAYERS’ in SQL database using the following information.

Solution:

String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
stmt.executeUpdate(CREATE_TABLE_SQL);

These are NPTEL Programming In Java Week 10 Assignment 10 Answers


Question 5

Complete the code segment to rename an already created table named ‘PLAYERS’ into ‘SPORTS’.

Solution:

String alter="ALTER TABLE players RENAME TO sports;";
stmt.executeUpdate(alter);

These are NPTEL Programming In Java Week 10 Assignment 10 Answers

More Weeks of Programming In Java: Click Here

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


These are NPTEL Programming In Java Week 10 Assignment 10 Answers