Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 476
Topics in Java Programming
Q.171 Medium JDBC
What does the setAutoCommit(false) method do in JDBC?
A Disables automatic transaction commit
B Enables automatic transaction commit
C Rolls back current transaction
D Closes the connection
Correct Answer:  A. Disables automatic transaction commit
EXPLANATION

setAutoCommit(false) disables automatic commit, allowing multiple statements to be part of single transaction. Manual commit() or rollback() must be called.

Take Test
Q.172 Medium JDBC
Which DriverManager method is used to establish a database connection?
A createConnection()
B getConnection()
C establishConnection()
D openConnection()
Correct Answer:  B. getConnection()
EXPLANATION

DriverManager.getConnection() is the standard method that takes URL, username, and password to establish a connection to the database.

Take Test
Q.173 Medium JDBC
What is Connection pooling in JDBC?
A Storing multiple database connections in a pool for reuse
B Combining multiple queries into one
C Creating backup connections
D Automatic connection switching
Correct Answer:  A. Storing multiple database connections in a pool for reuse
EXPLANATION

Connection pooling maintains a pool of reusable database connections to improve performance and reduce overhead of creating new connections repeatedly.

Take Test
Q.174 Medium JDBC
Which method must be called to move cursor to the next row in ResultSet?
A next()
B moveNext()
C advance()
D fetch()
Correct Answer:  A. next()
EXPLANATION

The next() method moves the ResultSet cursor to the next row and returns true if a row exists, false if no more rows. It's essential for iterating through results.

Take Test
Q.175 Medium JDBC
What is the primary advantage of using PreparedStatement over Statement?
A Faster execution and SQL injection prevention
B Larger result set size
C Better memory management
D Automatic type conversion
Correct Answer:  A. Faster execution and SQL injection prevention
EXPLANATION

PreparedStatement offers faster execution due to precompilation and prevents SQL injection through parameterized queries using placeholders (?).

Take Test
Q.176 Medium JDBC
Which interface is used to execute precompiled SQL statements in JDBC?
A Statement
B PreparedStatement
C CallableStatement
D BatchStatement
Correct Answer:  B. PreparedStatement
EXPLANATION

PreparedStatement is used for precompiled SQL statements with parameters. It provides better performance and prevents SQL injection compared to Statement.

Take Test
Q.177 Medium JDBC
What does the executeUpdate() method return in JDBC?
A A ResultSet object
B Number of rows affected
C A boolean value
D Query execution time
Correct Answer:  B. Number of rows affected
EXPLANATION

executeUpdate() returns an integer representing the number of rows affected by INSERT, UPDATE, or DELETE operations. executeQuery() returns ResultSet.

Take Test
Q.178 Medium I/O Streams
Consider a scenario where you're processing a 5GB log file and need to count specific error messages. Which I/O strategy would be most memory-efficient?
A Load the entire file into a byte array and process it
B Use BufferedReader with readLine() in a loop to process line-by-line
C Use FileInputStream to read all bytes at once
D Convert the entire file to a String and use split() method
Correct Answer:  B. Use BufferedReader with readLine() in a loop to process line-by-line
EXPLANATION

BufferedReader with readLine() processes the file line-by-line, maintaining a constant memory footprint regardless of file size. Options A, C, and D would attempt to load the entire 5GB file into memory, causing OutOfMemoryError. This is the standard approach for large file processing.

Take Test
Q.179 Medium I/O Streams
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
A The program will compile successfully but throw NotSerializableException at runtime
B The compiler will generate a compile-time error
C Only the serializable fields will be written to the stream
D The non-serializable field will be automatically converted to String
Correct Answer:  A. The program will compile successfully but throw NotSerializableException at runtime
EXPLANATION

Java serialization requires all instance variables to be serializable or marked as transient. If a non-serializable object is encountered during serialization, NotSerializableException is thrown at runtime, not compile-time. There's no automatic conversion.

Take Test
Q.180 Medium I/O Streams
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
A Use FileOutputStream with DataOutputStream for object serialization
B Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
C Use PrintWriter to write object toString() values
D Use ByteArrayOutputStream to store objects in memory
Correct Answer:  B. Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
EXPLANATION

ObjectOutputStream and ObjectInputStream are specifically designed for object serialization and deserialization. They handle the complete object state preservation. Other options are either inefficient or unsuitable for preserving object state.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips