1Which of the following is the correct syntax to create an ArrayList of Strings using Java Generics?
A.ArrayList list = new ArrayList<String>();
B.ArrayList<String> list = new ArrayList();
C.List<String> list = new ArrayList<String>();
D.List<String> list = new List<String>();
Correct Answer: List<String> list = new ArrayList<String>();
Explanation:To achieve polymorphism and type safety, it is best practice to use the interface List<String> as the reference type and ArrayList<String> as the implementation. Option D is incorrect because List is an interface and cannot be instantiated directly.
Incorrect! Try again.
2What is the primary benefit of using Generics in Java Collections?
A.It reduces the memory usage of the collection.
B.It eliminates the need for type casting and provides compile-time type safety.
C.It allows storing primitive data types directly without wrapper classes.
D.It automatically sorts the elements in the collection.
Correct Answer: It eliminates the need for type casting and provides compile-time type safety.
Explanation:Generics enforce type checking at compile time, preventing insertion of incorrect types and removing the need for explicit casting when retrieving elements.
Incorrect! Try again.
3In an ArrayList, what is the time complexity for accessing an element by its index using the get(int index) method?
A.
B.
C.
D.
Correct Answer:
Explanation:ArrayList is backed by an array. Accessing an element by index in an array is a constant time operation, denoted as .
Incorrect! Try again.
4What happens when an ArrayList exceeds its current capacity?
A.It throws an ArrayIndexOutOfBoundsException.
B.It automatically creates a new, larger array and copies the elements over.
C.It creates a linked list structure to hold the overflow.
D.It overwrites the oldest elements.
Correct Answer: It automatically creates a new, larger array and copies the elements over.
Explanation:When the capacity is filled, ArrayList grows dynamically (usually by 50% of the current size) by creating a new array and copying existing data using System.arraycopy.
Incorrect! Try again.
5Which interface must the elements of a TreeSet implement to define their natural ordering?
A.Comparator
B.Serializable
C.Comparable
D.Sortable
Correct Answer: Comparable
Explanation:To use a TreeSet with natural ordering, the objects stored must implement the Comparable interface and override the compareTo method.
Incorrect! Try again.
6Consider a class Student. Which method signature is correct for implementing the Comparable interface?
A.public int compare(Student s1, Student s2)
B.public int compareTo(Object o)
C.public int compareTo(Student s)
D.public boolean compare(Student s)
Correct Answer: public int compareTo(Student s)
Explanation:The Comparable<T> interface defines a single method: public int compareTo(T o).
Incorrect! Try again.
7What is the key difference between Comparable and Comparator?
A.Comparable is in java.util, while Comparator is in java.lang.
B.Comparable defines natural ordering within the class itself, while Comparator defines custom ordering externally.
C.Comparable allows multiple sorting sequences, while Comparator allows only one.
D.There is no difference; they are interchangeable.
Correct Answer: Comparable defines natural ordering within the class itself, while Comparator defines custom ordering externally.
Explanation:Comparable modifies the class whose instances are to be sorted (natural order). Comparator is a separate class used to define custom/alternative sorting logic.
Incorrect! Try again.
8When initializing a TreeSet with a custom sorting order, what argument is passed to the constructor?
A.An instance of a class implementing Comparable.
B.An instance of a class implementing Comparator.
C.The size of the set.
D.A collection of integers.
Correct Answer: An instance of a class implementing Comparator.
Explanation:To override natural ordering or sort objects that are not Comparable, you pass a Comparator instance to the TreeSet constructor.
Incorrect! Try again.
9Given the following code, what is the output (assuming import statements are correct)?
java
TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(1);
set.add(5);
System.out.println(set);
A.[5, 1, 5]
B.[1, 5, 5]
C.[1, 5]
D.[5, 1]
Correct Answer: [1, 5]
Explanation:TreeSet does not allow duplicates and stores elements in sorted order. The duplicate 5 is ignored, and 1 comes before 5.
Incorrect! Try again.
10Which method in the Comparator interface is used to compare two arguments?
A.compareTo(T o1, T o2)
B.compare(T o1, T o2)
C.equals(T o1, T o2)
D.sort(T o1, T o2)
Correct Answer: compare(T o1, T o2)
Explanation:The Comparator interface defines int compare(T o1, T o2) to determine the order of two objects.
Incorrect! Try again.
11How does HashMap handle duplicate keys?
A.It throws a DuplicateKeyException.
B.It stores both keys.
C.It replaces the old value associated with the key with the new value.
D.It ignores the new key-value pair.
Correct Answer: It replaces the old value associated with the key with the new value.
Explanation:In a HashMap, keys must be unique. If put() is called with an existing key, the old value is overwritten by the new value.
Incorrect! Try again.
12What is the default initial capacity and load factor of a HashMap?
A.Capacity: 10, Load Factor: 1.0
B.Capacity: 16, Load Factor: 0.75
C.Capacity: 20, Load Factor: 0.5
D.Capacity: 8, Load Factor: 0.8
Correct Answer: Capacity: 16, Load Factor: 0.75
Explanation:According to Java documentation, the default initial capacity is 16 and the default load factor is 0.75.
Incorrect! Try again.
13Which of the following statements about HashMap is true regarding null?
A.It cannot store null keys or values.
B.It can store one null key and multiple null values.
C.It can store multiple null keys but no null values.
D.It is thread-safe by default.
Correct Answer: It can store one null key and multiple null values.
Explanation:HashMap allows exactly one null key and any number of null values. Hashtable (older class) does not allow null.
Incorrect! Try again.
14Which data structure is the underlying implementation of ArrayDeque?
A.Linked List
B.Resizable Array
C.Hash Table
D.Binary Tree
Correct Answer: Resizable Array
Explanation:ArrayDeque (Array Double Ended Queue) is backed by a resizable array that functions as a circular buffer.
Incorrect! Try again.
15Which interface does Deque extend?
A.List
B.Queue
C.Set
D.Map
Correct Answer: Queue
Explanation:Deque (Double Ended Queue) extends the Queue interface, adding methods to insert and remove from both ends.
Incorrect! Try again.
16Which method is used to add an element to the beginning of a Deque?
A.addLast()
B.offerLast()
C.addFirst()
D.pushBack()
Correct Answer: addFirst()
Explanation:addFirst() inserts the specified element at the front of the deque.
Incorrect! Try again.
17If you want to use a Deque as a Stack (LIFO), which methods correspond to push and pop?
A.addLast and removeFirst
B.addFirst and removeFirst
C.addFirst and removeLast
D.offer and poll
Correct Answer: addFirst and removeFirst
Explanation:Using Deque as a stack involves pushing onto the head (addFirst or push) and popping from the head (removeFirst or pop).
Incorrect! Try again.
18What does JDBC stand for?
A.Java Database Connectivity
B.Java Data Base Connector
C.Java Data Binding Connection
D.Java DB Connectivity
Correct Answer: Java Database Connectivity
Explanation:JDBC stands for Java Database Connectivity, an API for connecting and executing queries on a database.
Incorrect! Try again.
19Which package contains the core JDBC interfaces like Connection, Statement, and ResultSet?
A.javax.sql
B.java.db
C.java.sql
D.java.jdbc
Correct Answer: java.sql
Explanation:The java.sql package contains the core JDBC API.
Incorrect! Try again.
20Which class is used to load a JDBC driver dynamically?
A.DriverManager
B.Class
C.Driver
D.SQLException
Correct Answer: Class
Explanation:Class.forName("driver_class_name") is traditionally used to load the driver class dynamically, although modern JDBC drivers often load automatically via SPI.
Incorrect! Try again.
21How many types of JDBC drivers are defined by Oracle/Sun Microsystems?
A.2
B.3
C.4
D.5
Correct Answer: 4
Explanation:There are 4 types: Type 1 (JDBC-ODBC Bridge), Type 2 (Native API), Type 3 (Network Protocol), and Type 4 (Thin/Pure Java).
Incorrect! Try again.
22Which JDBC driver type converts JDBC calls directly into the network protocol used by the DBMS?
A.Type 1
B.Type 2
C.Type 3
D.Type 4
Correct Answer: Type 4
Explanation:Type 4 drivers (Thin drivers) are written entirely in Java and communicate directly with the database using the database's specific networking protocol.
Incorrect! Try again.
23Which JDBC driver type relies on an ODBC driver installed on the client machine?
A.Type 1
B.Type 2
C.Type 3
D.Type 4
Correct Answer: Type 1
Explanation:Type 1 is the JDBC-ODBC bridge, which translates JDBC calls to ODBC calls. It requires ODBC configuration on the client.
Incorrect! Try again.
24Which JDBC driver type is generally considered the most efficient and platform-independent for web applications?
A.Type 1
B.Type 2
C.Type 3
D.Type 4
Correct Answer: Type 4
Explanation:Type 4 is pure Java, requires no client-side native installation, and communicates directly with the DB, making it ideal for web apps.
Incorrect! Try again.
25What is the return type of DriverManager.getConnection(...)?
A.Statement
B.Result
C.Connection
D.Driver
Correct Answer: Connection
Explanation:The DriverManager.getConnection() method returns an implementation of the Connection interface.
Incorrect! Try again.
26Which interface is used to execute static SQL statements with no parameters?
A.PreparedStatement
B.CallableStatement
C.Statement
D.SQLStatement
Correct Answer: Statement
Explanation:Statement is used for general-purpose access to the database using static SQL statements.
Incorrect! Try again.
27Which interface should be used to execute precompiled SQL statements with input parameters?
A.Statement
B.PreparedStatement
C.CallableStatement
D.DynamicStatement
Correct Answer: PreparedStatement
Explanation:PreparedStatement is used for precompiled SQL statements. It is more efficient for repeated execution and prevents SQL injection.
Incorrect! Try again.
28In a PreparedStatement, which character is used as a placeholder for parameters?
A.%
B.*
C.?
D.#
Correct Answer: ?
Explanation:The question mark ? is the placeholder used in parameterized SQL queries within PreparedStatement.
Incorrect! Try again.
29Which method of the Statement interface is used to execute SELECT queries?
A.execute()
B.executeUpdate()
C.executeQuery()
D.executeSelect()
Correct Answer: executeQuery()
Explanation:executeQuery() is used for statements that return a result set (like SELECT).
Incorrect! Try again.
30What does the executeUpdate() method return?
A.A ResultSet object
B.A boolean value
C.An integer representing the number of rows affected
D.Void
Correct Answer: An integer representing the number of rows affected
Explanation:executeUpdate() (used for INSERT, UPDATE, DELETE) returns an int indicating the row count or 0 for DDL statements.
Incorrect! Try again.
31Which object holds the data returned by a database query?
A.RowSet
B.ResultSet
C.Statement
D.DataContainer
Correct Answer: ResultSet
Explanation:The ResultSet interface represents the result set of a database query.
Incorrect! Try again.
32Which method moves the cursor to the next row in a ResultSet?
A.moveNext()
B.next()
C.hasNext()
D.forward()
Correct Answer: next()
Explanation:ResultSet.next() moves the cursor forward one row. It returns true if the new current row is valid; false if there are no more rows.
Incorrect! Try again.
33To retrieve an integer value from the column named "age" in a ResultSet, which method is used?
A.getInteger("age")
B.getInt("age")
C.getValue("age")
D.fetchInt("age")
Correct Answer: getInt("age")
Explanation:getInt(String columnLabel) is the standard method to retrieve an integer value from a result set column.
Incorrect! Try again.
34Which exception is thrown by most JDBC methods?
A.IOException
B.DatabaseException
C.SQLException
D.ClassNotFoundException
Correct Answer: SQLException
Explanation:java.sql.SQLException is a checked exception thrown when database access errors occur.
Incorrect! Try again.
35Which interface allows the execution of stored procedures?
A.PreparedStatement
B.StoredStatement
C.CallableStatement
D.ProcedureCall
Correct Answer: CallableStatement
Explanation:CallableStatement extends PreparedStatement and is used to execute stored procedures in the database.
Incorrect! Try again.
36What is the correct sequence of steps to perform a database operation?
A.Load Driver -> Create Statement -> Get Connection -> Execute Query -> Close
C.Load Driver -> Get Connection -> Create Statement -> Execute Query -> Close
D.Create Statement -> Load Driver -> Get Connection -> Execute Query -> Close
Correct Answer: Load Driver -> Get Connection -> Create Statement -> Execute Query -> Close
Explanation:Standard JDBC workflow: Load the driver class, establish a connection, create a statement object, execute the query, and close resources.
Incorrect! Try again.
37Which method is used to clean up database resources like Connection and Statement?
A.end()
B.stop()
C.close()
D.release()
Correct Answer: close()
Explanation:The close() method is used to release JDBC resources immediately instead of waiting for them to be automatically released.
Incorrect! Try again.
38When connecting to a non-conventional database (like a NoSQL DB or a generic data source) via JDBC, what abstraction is often preferred over DriverManager in enterprise environments?
A.DataSource
B.ConnectionPool
C.DataFactory
D.DriverBridge
Correct Answer: DataSource
Explanation:javax.sql.DataSource is the preferred factory for connections. It allows for connection pooling and distributed transactions, and hides implementation details (often used for both conventional and non-conventional setups).
Incorrect! Try again.
39If you are connecting to a text file (CSV) as if it were a database using JDBC, what type of driver are you likely using?
A.A Type 4 thin driver for Oracle.
B.A JDBC-ODBC bridge or a specific Text/CSV JDBC driver.
C.A Binary Stream Driver.
D.An XML Parser Driver.
Correct Answer: A JDBC-ODBC bridge or a specific Text/CSV JDBC driver.
Explanation:Connecting to non-conventional flat-file 'databases' is often done via specialized drivers (like CsvJdbc) or historically via the JDBC-ODBC bridge connected to a Text ODBC DSN.
Incorrect! Try again.
40Which SQL command is generally NOT executed using executeUpdate()?
A.INSERT
B.UPDATE
C.DELETE
D.SELECT
Correct Answer: SELECT
Explanation:SELECT queries return data, so they are executed using executeQuery(). executeUpdate() is for data manipulation (DML) or definition (DDL).
Incorrect! Try again.
41What is type erasure in the context of Java Generics?
A.Removing elements from a collection.
B.The process where the compiler removes generic type information after compilation.
C.Deleting the database after a connection closes.
D.Clearing a HashMap.
Correct Answer: The process where the compiler removes generic type information after compilation.
Explanation:Java generics are implemented via erasure. The compiler validates types at compile-time and then inserts casts or removes type parameters in the bytecode to ensure backward compatibility.
Incorrect! Try again.
42In a generic class Box<T>, what does T represent?
A.A specific primitive type.
B.A type parameter (placeholder for a class type).
C.A thread.
D.A transient variable.
Correct Answer: A type parameter (placeholder for a class type).
Explanation:T is a standard naming convention for a generic type parameter.
Incorrect! Try again.
43When implementing a Comparable for a class Person to sort by age (int) ascending, the compareTo logic is roughly:
A.return this.age - other.age;
B.return other.age - this.age;
C.return this.age > other.age;
D.return 0;
Correct Answer: return this.age - other.age;
Explanation:For ascending order: return a negative if this < other, zero if equal, and positive if this > other. Subtracting integers () achieves this naturally (ignoring overflow).
Incorrect! Try again.
44Which of the following is valid syntax for a Wildcard using generics?
A.List<?> list
B.List<*> list
C.List<all> list
D.List<any> list
Correct Answer: List<?> list
Explanation:The question mark ? is the wildcard character in Java Generics, representing an unknown type.
Incorrect! Try again.
45Which method ensures that changes made in a JDBC transaction are permanently saved to the database?
A.save()
B.persist()
C.commit()
D.flush()
Correct Answer: commit()
Explanation:Connection.commit() makes all changes made since the previous commit/rollback permanent.
Incorrect! Try again.
46If autoCommit is set to false on a JDBC Connection, what happens if you close the connection without calling commit()?
A.The changes are committed automatically.
B.The changes are rolled back.
C.The database enters a locked state.
D.An exception is thrown.
Correct Answer: The changes are rolled back.
Explanation:If auto-commit is disabled and the connection is closed without an explicit commit, the transaction is implicitly rolled back.
Incorrect! Try again.
47Why is PreparedStatement preferred over Statement for preventing security risks?
A.It encrypts the data.
B.It prevents SQL Injection attacks.
C.It uses a secure socket layer.
D.It hides the database schema.
Correct Answer: It prevents SQL Injection attacks.
Explanation:PreparedStatement treats parameters as strictly data, not executable code, thereby neutralizing SQL injection attempts.
Incorrect! Try again.
48In the context of TreeMap (which uses similar logic to TreeSet), what happens if you try to insert a key that is not comparable with existing keys?
A.It is inserted at the end.
B.It is inserted at the beginning.
C.A ClassCastException is thrown.
D.A NullPointerException is thrown.
Correct Answer: A ClassCastException is thrown.
Explanation:Tree-based collections in Java require keys to be mutually comparable. If they are not, runtime comparison fails with ClassCastException.
Incorrect! Try again.
49Which method creates a scrollable ResultSet?
A.createStatement()
B.createStatement(int resultSetType, int resultSetConcurrency)
C.createScrollableStatement()
D.getScrollable()
Correct Answer: createStatement(int resultSetType, int resultSetConcurrency)
Explanation:To get a scrollable result set, you must specify the result set type (e.g., TYPE_SCROLL_INSENSITIVE) during statement creation.
Incorrect! Try again.
50What is the URL format generally used for a MySQL JDBC connection?