Programming In Java | Week 11
Programming In Java Week 11 Answers NPTEL
Quiz
Q1. Which of the following interfaces is used to manage transactions in JDBC?
a. Connection
b. Statement
c. Transaction
d. ResultSet
Q2. Which of the following interfaces is used to execute parameterized SQL statements in JDBC?
a. ResultSet
b. PreparedStatement
c. Statement
d. Connection
Q3. Which of the following statements is true?
a. The executeQuery( ) method of java.sql.Statement interface is used to execute a DELETE statement.
b. The executeQuery( ) method of java.sql.Statement interface is used to execute a SELECT statement.
c. The executeQuery( ) method of java.sql.Statement interface is used to execute a INSERT statement.
d. The executeQuery( ) method of java.sql.Statement interface is used to execute a UPDATE statement.
Q4. Which of the following statements is true?
a. The executeUpdate( ) method of java.sql.Statement interface is used to execute a DELETE statement.
b. The executeUpdate( ) method of java.sql. Statement interface is used to execute a SELECT statement.
c. The executeUpdate( ) method of java.sql.Statement interface is used to execute a INSERT statement.
d. The executeUpdate( ) method of java.sql.Statement interface is used to execute a UPDATE statement.
Q5. Which of the following statements is true about JDBC?
a. JDBC is a programming language.
b. JDBC is a type of database.
c. JDBC is an API for accessing relational databases from Java programs.
d. JDBC is used to create graphical user interfaces.
Q6. Which of the following statements is true about the PreparedStatement interface in JDBC?
a. PreparedStatement objects are precompiled before they are executed.
b. PreparedStatement objects can only be used for SELECT statements.
c. PreparedStatement objects cannot accept parameters at runtime.
d. PreparedStatement objects can be reused for multiple SQL statements.
Q7. Which of the following statements is true about batch updates in JDBC?
a. Batch updates can only be used for INSERT statements.
b. Batch updates allow multiple SQL statements to be executed in a single transaction.
c. Batch updates are executed immediately as soon as they are added to the batch.
d. Batch updates can be rolled back.
Q8. Consider the following code.
What is the output of the above code?
a. Compilation error
b. Runtime error
c. 1, “one” is replaced by 2.”two™ in the table.
d. “one” and “two” both are inserted in different columns of same row.
Q9. Which type of JDBC driver translates JDBC calls into native database API calls?
a. Type 1 driver
b. Type 2 driver
c. Type 3 driver
d. Type 4 driver
Q10. Which of the following method is static and synchronized in JDBC API?
a. getConnection( )
b. prepareCall()
c. executeUpdate( )
d. executeQuery()
Assignment
1. Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS’.
Solution:
PreparedStatement preparedStmt = conn.prepareStatement ("insert into Players values(?,?,?,?)");
preparedStmt.setInt (1, 1);
preparedStmt.setString (2, "Ram");
preparedStmt.setString (3, "Gopal");
preparedStmt.setInt(4, 26);
int xxx= preparedStmt.executeUpdate();
PreparedStatement preparedStmt2 = conn.prepareStatement ("insert into Players values(?,?,?,?)");
preparedStmt2.setInt (1, 2);
preparedStmt2.setString (2, "John");
preparedStmt2.setString (3, "Mayer");
preparedStmt2.setInt(4, 22);
int xyz= preparedStmt2.executeUpdate();
2. Write the required code in order to update the following data in the table ‘PLAYERS’.
Solution:
String a1="update players "+"set AGE =24 WHERE UID==1";
stmt.execute(a1);
String bb="update players "+"set LAST_NAME ='Gopala' WHERE UID==1";
stmt.execute(bb);
String c_z="update players "+"set FIRST_NAME='Rama' WHERE UID==1";
stmt.execute(c_z);
3. Write the appropriate code in order to delete the following data in the table ‘PLAYERS’.
Solution:
String My_fav_Query="DELETE FROM PLAYERS "+"WHERE UID=1";
stmt.executeUpdate(My_fav_Query);
4. Complete the following program to calculate the average age of the players in the table ‘PLAYERS’.
Solution:
ResultSet ans=stmt.executeQuery("SELECT * from players");
int ccdd=0;
int sum_is_sum=0;
while(ans.next())
{
ccdd++;
sum_is_sum+=Integer.parseInt (ans.getString(4));
}
System.out.println("Average age of players is " + sum_is_sum/ccdd);
conn.close();
5. Complete the code segment to drop the table named ‘PLAYERS’.
Solution:
query = "DROP TABLE players;";
stmt.executeUpdate(query);
* The material and content uploaded on this website are for general information and reference purposes only and don’t copy the answers of this website to any other domain without any permission or else copyright abuse will be in action.
Please do it by your own first!
