Which stream class is used to write primitive data types in binary format?
Answer: B
DataOutputStream provides methods like writeInt(), writeFloat(), writeDouble() to write primitive types in binary format.
Q.162Easy
Which of the following is NOT a character stream class in Java?
Answer: C
FileInputStream is a byte stream class, not a character stream. FileReader, BufferedWriter, and PrintWriter are all character stream classes.
Q.163Easy
Which interface must be implemented to make an object serializable in Java?
Answer: B
The Serializable interface must be implemented to serialize objects. It is a marker interface with no methods. Classes implementing it can be serialized using ObjectOutputStream.
Q.164Easy
In Java, what is the primary purpose of the flush() method in output streams?
Answer: B
The flush() method forces any buffered output bytes to be written out. It does not close the stream, allowing further writes after flushing.
Q.165Easy
What will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
Answer: C
The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.
Advertisement
Q.166Easy
Which of the following classes is used to read primitive data types from an input stream in Java?
Answer: A
DataInputStream is specifically designed to read primitive data types like int, double, boolean, etc. from an input stream. BufferedInputStream provides buffering, FileInputStream reads from files, and ObjectInputStream deserializes objects.
Q.167Easy
Which interface must a class implement to be eligible for serialization in Java, and what is the significance of implementing it with no abstract methods?
Answer: B
Serializable is a marker interface (contains no methods) that signals to the JVM that objects of that class can be serialized. The absence of abstract methods means implementing classes don't need to override any methods; they just need to implement the interface to indicate serialization support.
Q.168Easy
Which interface in JDBC is used to execute SQL queries and obtain results?
Answer: A
Statement interface is used to execute SQL queries. Connection is used to establish database connection, ResultSet holds query results, and Driver manages database connections.
Q.169Easy
What is the correct order of JDBC operations?
Answer: A
The correct JDBC sequence is: 1) Load the JDBC driver, 2) Create connection, 3) Create statement, 4) Execute query, 5) Process results, 6) Close resources.
Q.170Easy
Which JDBC driver type is platform-independent and does not require native code?
Answer: D
Type 4 (Thin) drivers are pure Java drivers, platform-independent, and don't require native code. They communicate directly with database using Java sockets.
Q.171Easy
What exception is thrown when a database connection cannot be established?
Answer: A
SQLException is thrown for database-related errors including connection failures. ClassNotFoundException occurs when JDBC driver class is not found.
Q.172Easy
Which method is used to retrieve a String value from a ResultSet object?
Answer: B
getString() method retrieves String values from ResultSet. getInt() gets integer values, and fetchString/readString are not valid JDBC methods.
Q.173Easy
Which JDBC driver type is platform-independent and does not require native code installation?
Answer: D
Type 4 drivers (thin drivers) are pure Java drivers that communicate directly with the database using the database native protocol. They are platform-independent and do not require native code.
Q.174Easy
What is the correct syntax to retrieve an integer value from a ResultSet object at column index 2?
Answer: A
The correct method is getInt(int columnIndex) which retrieves an integer value from the ResultSet at the specified column index (1-based indexing).
Q.175Easy
Which interface represents a single row of data retrieved from a database in JDBC?
Answer: B
ResultSet interface represents the result set of a query. It contains the data returned from a database query and provides methods to access and traverse through the rows.
Q.176Easy
In JDBC, what is the purpose of the Class.forName() method?
Answer: A
Class.forName() is used to load the JDBC driver class dynamically and register it with the DriverManager. For example: Class.forName("com.mysql.cj.jdbc.Driver");
Q.177Easy
What happens when you call rs.next() on a ResultSet after the last row?
Answer: B
The next() method returns false when there are no more rows available. It does not throw an exception or loop back; it simply indicates that there are no additional rows to traverse.
Q.178Easy
Which interface in JDBC is used to execute a single SQL statement and return a ResultSet?
Answer: A
Statement interface is used to execute simple SQL queries without parameters and return results as ResultSet.
Q.179Easy
What is the primary advantage of using PreparedStatement over Statement in JDBC?
Answer: A
PreparedStatement pre-compiles SQL and uses parameterized queries, preventing SQL injection and improving performance.
Q.180Easy
Which JDBC method is used to retrieve the number of rows affected by an INSERT, UPDATE, or DELETE statement?
Answer: B
executeUpdate() returns an int representing the number of rows affected by the SQL statement execution.