Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 351–360 of 958
Topics in Java Programming
Q.351 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.352 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.353 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.354 Easy JDBC
Which method is used to retrieve a String value from a ResultSet object?
A getInt()
B getString()
C fetchString()
D readString()
Correct Answer:  B. getString()
EXPLANATION

getString() method retrieves String values from ResultSet. getInt() gets integer values, and fetchString/readString are not valid JDBC methods.

Take Test
Q.355 Easy JDBC
What exception is thrown when a database connection cannot be established?
A SQLException
B ClassNotFoundException
C IOExceptionn
D NullPointerException
Correct Answer:  A. SQLException
EXPLANATION

SQLException is thrown for database-related errors including connection failures. ClassNotFoundException occurs when JDBC driver class is not found.

Take Test
Q.356 Easy JDBC
Which JDBC driver type is platform-independent and does not require native code?
A Type 1 Driver
B Type 2 Driver
C Type 3 Driver
D Type 4 Driver
Correct Answer:  D. Type 4 Driver
EXPLANATION

Type 4 (Thin) drivers are pure Java drivers, platform-independent, and don't require native code. They communicate directly with database using Java sockets.

Take Test
Q.357 Easy JDBC
What is the correct order of JDBC operations?
A Load Driver → Create Connection → Create Statement → Execute Query → Close Resources
B Create Connection → Load Driver → Execute Query → Create Statement → Close Resources
C Load Driver → Execute Query → Create Connection → Create Statement → Close Resources
D Create Statement → Load Driver → Create Connection → Execute Query → Close Resources
Correct Answer:  A. Load Driver → Create Connection → Create Statement → Execute Query → Close Resources
EXPLANATION

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.

Take Test
Q.358 Easy JDBC
Which interface in JDBC is used to execute SQL queries and obtain results?
A Statement
B Connection
C ResultSet
D Driver
Correct Answer:  A. Statement
EXPLANATION

Statement interface is used to execute SQL queries. Connection is used to establish database connection, ResultSet holds query results, and Driver manages database connections.

Take Test
Q.359 Hard I/O Streams
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
A FileOutputStream is thread-safe by default, no issues will occur
B Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
C Only the last thread's data will be written; use multiple FileOutputStreams
D FileOutputStream will throw ConcurrentModificationException automatically
Correct Answer:  B. Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
EXPLANATION

FileOutputStream is NOT thread-safe. When multiple threads write simultaneously, race conditions can cause data corruption or interleaved writes. Solutions include: synchronizing access using synchronized blocks, using thread-safe wrapper classes, or employing a single-threaded writer pattern with a queue. PrintWriter with autoFlush provides some buffering protection.

Take Test
Q.360 Easy I/O Streams
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?
A Cloneable interface; it marks the class as eligible for deep copying
B Serializable interface; it's a marker interface with no abstract methods that indicates serialization capability
C Externalizable interface; it provides default serialization behavior
D Comparable interface; it enables object comparison during serialization
Correct Answer:  B. Serializable interface; it's a marker interface with no abstract methods that indicates serialization capability
EXPLANATION

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.

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