Unit 6 - Practice Quiz

CSE310

1 Which 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>();

2 What 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.

3 In an ArrayList, what is the time complexity for accessing an element by its index using the get(int index) method?

A.
B.
C.
D.

4 What 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.

5 Which interface must the elements of a TreeSet implement to define their natural ordering?

A. Comparator
B. Serializable
C. Comparable
D. Sortable

6 Consider 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)

7 What 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.

8 When 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.

9 Given 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]

10 Which 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)

11 How 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.

12 What 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

13 Which 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.

14 Which data structure is the underlying implementation of ArrayDeque?

A. Linked List
B. Resizable Array
C. Hash Table
D. Binary Tree

15 Which interface does Deque extend?

A. List
B. Queue
C. Set
D. Map

16 Which method is used to add an element to the beginning of a Deque?

A. addLast()
B. offerLast()
C. addFirst()
D. pushBack()

17 If 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

18 What does JDBC stand for?

A. Java Database Connectivity
B. Java Data Base Connector
C. Java Data Binding Connection
D. Java DB Connectivity

19 Which package contains the core JDBC interfaces like Connection, Statement, and ResultSet?

A. javax.sql
B. java.db
C. java.sql
D. java.jdbc

20 Which class is used to load a JDBC driver dynamically?

A. DriverManager
B. Class
C. Driver
D. SQLException

21 How many types of JDBC drivers are defined by Oracle/Sun Microsystems?

A. 2
B. 3
C. 4
D. 5

22 Which 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

23 Which 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

24 Which 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

25 What is the return type of DriverManager.getConnection(...)?

A. Statement
B. Result
C. Connection
D. Driver

26 Which interface is used to execute static SQL statements with no parameters?

A. PreparedStatement
B. CallableStatement
C. Statement
D. SQLStatement

27 Which interface should be used to execute precompiled SQL statements with input parameters?

A. Statement
B. PreparedStatement
C. CallableStatement
D. DynamicStatement

28 In a PreparedStatement, which character is used as a placeholder for parameters?

A. %
B. *
C. ?
D. #

29 Which method of the Statement interface is used to execute SELECT queries?

A. execute()
B. executeUpdate()
C. executeQuery()
D. executeSelect()

30 What does the executeUpdate() method return?

A. A ResultSet object
B. A boolean value
C. An integer representing the number of rows affected
D. Void

31 Which object holds the data returned by a database query?

A. RowSet
B. ResultSet
C. Statement
D. DataContainer

32 Which method moves the cursor to the next row in a ResultSet?

A. moveNext()
B. next()
C. hasNext()
D. forward()

33 To 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")

34 Which exception is thrown by most JDBC methods?

A. IOException
B. DatabaseException
C. SQLException
D. ClassNotFoundException

35 Which interface allows the execution of stored procedures?

A. PreparedStatement
B. StoredStatement
C. CallableStatement
D. ProcedureCall

36 What is the correct sequence of steps to perform a database operation?

A. Load Driver -> Create Statement -> Get Connection -> Execute Query -> Close
B. Get Connection -> Load Driver -> Create Statement -> Execute Query -> Close
C. Load Driver -> Get Connection -> Create Statement -> Execute Query -> Close
D. Create Statement -> Load Driver -> Get Connection -> Execute Query -> Close

37 Which method is used to clean up database resources like Connection and Statement?

A. end()
B. stop()
C. close()
D. release()

38 When 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

39 If 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.

40 Which SQL command is generally NOT executed using executeUpdate()?

A. INSERT
B. UPDATE
C. DELETE
D. SELECT

41 What 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.

42 In 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.

43 When 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;

44 Which 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

45 Which method ensures that changes made in a JDBC transaction are permanently saved to the database?

A. save()
B. persist()
C. commit()
D. flush()

46 If 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.

47 Why 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.

48 In 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.

49 Which method creates a scrollable ResultSet?

A. createStatement()
B. createStatement(int resultSetType, int resultSetConcurrency)
C. createScrollableStatement()
D. getScrollable()

50 What is the URL format generally used for a MySQL JDBC connection?

A. jdbc:mysql://hostname:port/dbname
C. mysql:jdbc://hostname:port/dbname
D. jdbc:odbc:mysql://hostname:port/dbname