Nptel Programming in Java Week 11 Assignment Answers
Nptel Programming in Java Week 11 Assignment Answers
Searching for the Week 11 answers of the NPTEL “Programming in Java” course? You’re in the right spot! Here, you’ll find reliable and up-to-date solutions to help you complete your Week 11 assignment with confidence.
Programming In Java
With the growth of Information and Communication Technology, there is a need to develop large and complex software. Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment. Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems. This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.
PREREQUISITES : This course requires that the students are familiar with programming language such as C/C++ and data structures, algorithms.
INDUSTRY SUPPORT : All IT companies.
COURSE LAYOUT – Click To Know More
Nptel Programming in Java Week 11 Assignment Answers
1) What is the full form of JDBC?
a. Java Database Connectivity
b. Java Data Code
c. Java Data Communication
d. Java Development Connectivity
2) Fill in the missing code to establish a connection to a MySQL database.
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try {
// Load JDBC Driver
Class.forName(“com.mysql.cj.jdbc.Driver”);
// Establish Connection
Connection connection = // INSERT CODE HERE;
System.out.println(“Connected to the database.”);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
What should replace // INSERT CODE HERE
?
a. DriverManager.connect(“mysql:localhost:mydb”, “user”, “password”);
b. DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”, “user”, “password”);
c. Connection.get(“jdbc:mysql://localhost:3306/mydb”, “user”, “password”);
d. Driver.connect(“jdbc:mysql://localhost:mydb”, “user”, “password”);
3) Identify the error and select the corrected statement
import java.sql.*;
public class ResultSetExample {
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”, “user”, “password”);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.execute(“SELECT * FROM users”); // Error
while (rs.next()) {
System.out.println(rs.getString(“username”));
}
connection.close();
}
}
Correct line to replace the error:
a. ResultSet rs = stmt.executeQuery(“SELECT * FROM users”);
b. ResultSet rs = stmt.runQuery(“SELECT * FROM users”);
c. ResultSet rs = stmt.execute(“users SELECT * FROM”);
d. ResultSet rs = stmt.fetch(“SELECT * FROM users”);
4) What will the Java program output if the table products
contains a column name
and three rows: Laptop, Phone, and Tablet?
import java.sql.*;
public class DisplayProducts {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/store”, “root”, “pass”);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT name FROM products”);
while (rs.next()) {
System.out.println(rs.getString(“name”));
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
a. Compilation Error
b. Runtime Error
c. Laptop
Phone
Tablet
d. No Output
For Latest Update Join our official channel: Click here to join
5) Complete the following code to insert a new user into a users
table.
import java.sql.*;
public class InsertUser {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”, “root”, “password”);
String query = “INSERT INTO users (username, email) VALUES (?, ?)”;
PreparedStatement pstmt = // INSERT CODE HERE;
pstmt.setString(1, “john doe”);
pstmt.setString(2, “john@example.com”);
pstmt.executeUpdate();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
What should replace // INSERT CODE HERE
?
a. conn.createStatement(query);
b. conn.prepareStatement(query);
c. conn.execute(query);
d. conn.runStatement(query);
6) Which of the following SQL operations can be executed using the executeUpdate
method in JDBC?
I. INSERT INTO users (id, name) VALUES (1, ‘Alice’);
II. UPDATE users SET name=’Bob’ WHERE id=1;
III. DELETE FROM users WHERE id=1;
IV. SELECT * FROM users;
a. I, II, and III
b. Only I and II
c. Only I
d. I, II, III and IV
7) What is the purpose of the DriverManager
class in JDBC?
a. To manage database connections.
b. To execute SQL queries.
c. To fetch data from a database.
d. To represent a database record.
8) How do you establish a connection to a database using JDBC?
a. By creating an instance of the Connection interface
b. By using the DriverManager.getConnection() method
c. By implementing the Connection interface
d. By extending the Connection class
For Latest Update Join our official channel: Click here to join
9) Which method executes a simple query and returns a single Result Set object?
a. executeQuery()
b. executeUpdate()
c. execute()
d. run()
10) Which package in Java contains the classes and interfaces for JDBC?
a. java.sql
b. java.io
c. java.db
d. java.net
Nptel Programming in Java Week 12 Assignment Answers – Click Here