Unit 3: File I/O and JDBC - Practice Quiz

CSE406 — Advanced Java Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which method is commonly used to create a Path object from a path string?

Using the Path interface to operate on file and directory paths Easy
A. Files.create()
B. Path.of()
C. Paths.open()
D. File.path()

2 Which Path method returns the final element of a file path?

Using the Path interface to operate on file and directory paths Easy
A. getParent()
B. getFileName()
C. getNameCount()
D. getRoot()

3 Which Path method combines the current path with another path?

Using the Path interface to operate on file and directory paths Easy
A. normalize()
B. relativize()
C. toAbsolutePath()
D. resolve()

4 Which method checks whether a file or directory exists?

Using the Files class to check, delete, copy, or move a file or directory Easy
A. Files.available()
B. Files.contains()
C. Files.exists()
D. Files.isOpen()

5 Which method deletes a file only when it exists and avoids an exception when it is missing?

Using the Files class to check, delete, copy, or move a file or directory Easy
A. Files.clearIfExists()
B. Files.removeIfFound()
C. Files.eraseIfFound()
D. Files.deleteIfExists()

6 Which Files method is used to copy a file to another path?

Using the Files class to check, delete, copy, or move a file or directory Easy
A. Files.copy()
B. Files.clone()
C. Files.transfer()
D. Files.duplicate()

7 Which method returns a stream of lines from a text file?

Using Stream API with NIO2 Easy
A. Files.lines()
B. Files.tokens()
C. Files.records()
D. Files.entries()

8 Which method returns a stream containing entries directly inside a directory?

Using Stream API with NIO2 Easy
A. Files.list()
B. Files.lines()
C. Files.find()
D. Files.walk()

9 Why should a stream returned by Files.lines() usually be used in a try-with-resources statement?

Using Stream API with NIO2 Easy
A. It starts a background thread
B. It modifies the source file
C. It holds an open file resource
D. It creates a database transaction

10 Which Java package contains core JDBC interfaces such as Connection and Statement?

Defining the layout of the JDBC API Easy
A. javax.jdbc
B. java.jdbc
C. java.database
D. java.sql

11 Which JDBC interface represents an active session with a database?

Defining the layout of the JDBC API Easy
A. Connection
B. DriverManager
C. Statement
D. ResultSet

12 Which method is commonly used to obtain a JDBC database connection?

Connecting to a database by using a JDBC driver Easy
A. DriverManager.openDatabase()
B. DriverManager.getConnection()
C. Connection.createSession()
D. DataSource.openDriver()

13 What is the main purpose of a JDBC driver?

Connecting to a database by using a JDBC driver Easy
A. Translate JDBC calls for a database
B. Create SQL tables automatically
C. Store query results permanently
D. Replace the database server

14 Which method is normally used to execute a SQL SELECT statement?

Submitting queries and getting results from the database Easy
A. executeQuery()
B. executeInsert()
C. executeUpdate()
D. executeSelect()

15 Which JDBC interface stores rows returned by a database query?

Submitting queries and getting results from the database Easy
A. ResultSet
B. Connection
C. DriverManager
D. PreparedStatement

16 Which ResultSet method moves the cursor to the next row?

Submitting queries and getting results from the database Easy
A. next()
B. advance()
C. forward()
D. move()

17 Which file type is commonly used to store a JDBC URL, username, and password outside Java source code?

Specifying JDBC driver information externally Easy
A. A class file
B. A bytecode file
C. A manifest file
D. A properties file

18 What is a key benefit of storing JDBC configuration externally?

Specifying JDBC driver information externally Easy
A. Tables are created at startup
B. Queries execute without a driver
C. Settings can change without recompiling
D. Connections never need credentials

19 Which CRUD operation adds a new row to a database table?

Performing CRUD operations using the JDBC API Easy
A. Read
B. Update
C. Delete
D. Create

20 Which JDBC method is commonly used for SQL INSERT, UPDATE, and DELETE statements?

Performing CRUD operations using the JDBC API Easy
A. executeQuery()
B. executeUpdate()
C. readRows()
D. fetchResults()

21 Given Path base = Paths.get("/data/reports");, which expression refers to the file /data/reports/2024/summary.txt without converting the path to a string?

Using the Path interface to operate on file and directory paths Medium
A. base.append("2024/summary.txt")
B. base.concat("2024").concat("summary.txt")
C. base.join("2024", "summary.txt")
D. base.resolve("2024").resolve("summary.txt")

22 What is the result of Paths.get("/home/user/docs/report.txt").getParent() on a Unix-like system?

Using the Path interface to operate on file and directory paths Medium
A. /home/user/docs
B. report.txt
C. /home/user/docs/report.txt
D. /home/user

23 A program needs to compare two paths that may contain . and .. elements, but it must not access the file system. Which operation is appropriate?

Using the Path interface to operate on file and directory paths Medium
A. Files.isSameFile()
B. Files.readAttributes()
C. toRealPath()
D. normalize()

24 Which statement correctly checks whether a path exists while avoiding an exception if the path is inaccessible?

Using the Files class to check, delete, copy, or move a file or directory Medium
A. Files.exists(path)
B. Files.valid(path)
C. Files.isAvailable(path)
D. Files.check(path)

25 What happens when Files.copy(source, target) is used and target already exists as a regular file?

Using the Files class to check, delete, copy, or move a file or directory Medium
A. It throws FileAlreadyExistsException
B. It returns false without changing either file
C. It appends source bytes to the target file
D. It silently replaces the target file

26 Which option allows an existing target file to be replaced during a copy operation?

Using the Files class to check, delete, copy, or move a file or directory Medium
A. StandardCopyOption.OVERWRITE
B. StandardCopyOption.UPDATE_TARGET
C. StandardCopyOption.ALLOW_REPLACE
D. StandardCopyOption.REPLACE_EXISTING

27 Why can Files.delete(directory) fail even when the directory exists?

Using the Files class to check, delete, copy, or move a file or directory Medium
A. Directories cannot be deleted by Java
B. The directory must first be copied
C. The delete method only handles regular files
D. The directory is not empty

28 Which statement correctly creates a stream containing the entries directly inside the directory represented by dir?

Using Stream API with NIO2 Medium
A. dir.listFiles()
B. Files.entries(dir)
C. Files.list(dir)
D. Files.stream(dir)

29 Which code correctly finds all regular .java files under sourceDir, including files in nested directories?

Using Stream API with NIO2 Medium
A. Files.find(sourceDir, Integer.MAX_VALUE, (p, a) -> a.isRegularFile() && p.toString().endsWith(".java"))
B. Files.walkFileTree(sourceDir).filter(p -> p.toString().endsWith(".java"))
C. Files.search(sourceDir, p -> p.toString().endsWith(".java"))
D. Files.list(sourceDir).filter(p -> p.toString().endsWith(".java"))

30 A method uses Files.lines(path) to process a large text file. What is the most appropriate resource-management approach?

Using Stream API with NIO2 Medium
A. Store the stream in a static field
B. Use it inside a try-with-resources statement
C. Close only the Path object after processing
D. Convert it to a list before processing

31 Which expression counts regular files in the directory tree rooted at root?

Using Stream API with NIO2 Medium
A. Files.walk(root).filter(Files::isRegularFile).count()
B. Files.readAllLines(root).filter(Files::isRegularFile).count()
C. Files.list(root).filter(Files::isRegularFile).size()
D. Files.walk(root).filter(Files::isDirectory).count()

32 Which JDBC component is primarily responsible for creating Connection objects from database URLs?

Defining the layout of the JDBC API Medium
A. DriverManager
B. Statement
C. ResultSet
D. SQLException

33 Which JDBC object represents the active session between an application and a database?

Defining the layout of the JDBC API Medium
A. DatabaseMetaData
B. ResultSet
C. Driver
D. Connection

34 Which sequence is generally required before an application can execute a SQL statement through JDBC?

Connecting to a database by using a JDBC driver Medium
A. Create a result set, obtain a connection, execute SQL
B. Obtain a connection, create a statement, execute SQL
C. Close a connection, create a statement, execute SQL
D. Execute SQL, create a statement, obtain a connection

35 What is the main advantage of using PreparedStatement instead of concatenating user input into SQL?

Connecting to a database by using a JDBC driver Medium
A. It removes the need for a JDBC driver
B. It automatically commits every transaction
C. It converts every query into a stored procedure
D. It helps prevent SQL injection

36 Which method is normally used to execute a SELECT statement and obtain its rows?

Submitting queries and getting results from the database Medium
A. executeUpdate()
B. executeRows()
C. executeInsert()
D. executeQuery()

37 When iterating through a ResultSet, what does resultSet.next() do?

Submitting queries and getting results from the database Medium
A. Executes the SQL statement again
B. Moves to the next row and reports whether one exists
C. Closes the current database connection
D. Moves to the next column and returns its value

38 An application reads the database URL, username, and password from a properties file. What is the primary benefit?

Specifying JDBC driver information externally Medium
A. The JDBC driver is automatically installed
B. Database settings can change without recompiling code
C. Transactions are committed without application code
D. SQL statements no longer require validation

39 Which configuration item identifies the JDBC implementation class when explicit driver loading is required?

Specifying JDBC driver information externally Medium
A. The driver class name
B. The column label
C. The result set type
D. The transaction isolation level

40 Which mapping correctly associates CRUD operations with common SQL commands?

Performing CRUD operations using the JDBC API Medium
A. Create-SELECT, Read-INSERT, Update-DELETE, Delete-UPDATE
B. Create-INSERT, Read-SELECT, Update-UPDATE, Delete-DELETE
C. Create-DELETE, Read-UPDATE, Update-INSERT, Delete-SELECT
D. Create-UPDATE, Read-DELETE, Update-SELECT, Delete-INSERT

41 Given Path base = Paths.get("/srv/app"); and Path input = Paths.get("../logs/app.log");, what is the result of base.resolve(input) on a Unix-like system?

Using the Path interface to operate on file and directory paths Hard
A. ../logs/app.log
B. /srv/app/logs/app.log
C. /srv/app/../logs/app.log
D. /srv/logs/app.log

42 Which statement best describes Path.normalize() when applied to a path containing . and .. elements?

Using the Path interface to operate on file and directory paths Hard
A. It verifies that the normalized path exists
B. It converts every path to an absolute path
C. It removes redundant name elements lexically
D. It resolves symbolic links before simplifying

43 A program must compare two paths to determine whether they identify the same existing file, even when one path contains symbolic links. Which operation is most appropriate?

Using the Path interface to operate on file and directory paths Hard
A. path1.getFileName().equals(path2.getFileName())
B. path1.normalize().equals(path2.normalize())
C. path1.toRealPath().equals(path2.toRealPath())
D. path1.toAbsolutePath().equals(path2.toAbsolutePath())

44 A directory contains files and subdirectories. The program calls Files.delete(directory). What is the normal result?

Using the Files class to check, delete, copy, or move a file or directory Hard
A. AccessDeniedException is always thrown
B. The directory and all descendants are deleted
C. DirectoryNotEmptyException is thrown
D. Only the directory entry is deleted

45 Which behavior is guaranteed when copying an existing file with Files.copy(source, target) and no copy options?

Using the Files class to check, delete, copy, or move a file or directory Hard
A. The source is always deleted afterward
B. An existing target normally causes FileAlreadyExistsException
C. The target is always overwritten
D. Symbolic links are always followed recursively

46 A move must fail rather than silently degrade if the file system cannot perform an atomic rename. Which option should be supplied to Files.move?

Using the Files class to check, delete, copy, or move a file or directory Hard
A. StandardCopyOption.ATOMIC_MOVE
B. LinkOption.NOFOLLOW_LINKS
C. StandardCopyOption.COPY_ATTRIBUTES
D. StandardCopyOption.REPLACE_EXISTING

47 What is the most important resource-management requirement when processing a file with Files.lines(path)?

Using Stream API with NIO2 Hard
A. The stream should be sorted before closing
B. The stream should be marked parallel before reading
C. The stream should be consumed inside try-with-resources
D. The stream should be converted to an array immediately

48 Which statement correctly distinguishes Files.list(directory) from Files.walk(directory)?

Using Stream API with NIO2 Hard
A. list recursively visits descendants, while walk visits siblings only
B. list requires a regular file, while walk requires an empty directory
C. list returns direct children, while walk can recursively traverse descendants
D. list follows every symbolic link, while walk never follows links

49 A program uses Files.find(root, 3, matcher) to locate files. Which paths can be examined when root has depth zero?

Using Stream API with NIO2 Hard
A. Only descendants exactly three levels below it
B. Only root itself
C. root and descendants up to three levels below it
D. All descendants regardless of their depth

50 A Files.walk(root) pipeline uses filter(Files::isRegularFile) and then count(). What is the primary limitation of this approach for a very large tree?

Using Stream API with NIO2 Hard
A. It loads every file's contents into memory
B. It automatically follows all symbolic links
C. It may retain an open traversal resource until the stream closes
D. It cannot inspect nested directories

51 Which JDBC component is primarily responsible for managing a set of JDBC drivers and selecting an appropriate driver for a connection URL?

Defining the layout of the JDBC API Hard
A. DatabaseMetaData
B. Connection
C. ResultSet
D. DriverManager

52 Which JDBC object represents the database session through which statements are created and transactions are controlled?

Defining the layout of the JDBC API Hard
A. ResultSet
B. Connection
C. Driver
D. SQLException

53 A JDBC 4-compliant driver is packaged correctly and available on the class path. Which statement is generally true before calling DriverManager.getConnection?

Connecting to a database by using a JDBC driver Hard
A. The driver is loaded only after the first ResultSet is created
B. The driver can be discovered through the JDBC service-provider mechanism
C. The driver must always be registered with Class.forName
D. The driver must be instantiated through Connection

54 A connection URL is valid, but no installed driver accepts its subprotocol. What is the most likely result of DriverManager.getConnection(url)?

Connecting to a database by using a JDBC driver Hard
A. A ResultSet containing the URL is returned
B. A SQLException indicating no suitable driver is available
C. A read-only connection is returned
D. A connection using the default system driver is returned

55 Why is a PreparedStatement generally preferred over string concatenation when inserting user-supplied values into a SQL statement?

Submitting queries and getting results from the database Hard
A. It automatically commits every successful query
B. It guarantees that every query uses an index
C. It disables transaction isolation changes
D. It separates SQL structure from parameter values

56 After executing a query, the cursor of a newly created JDBC ResultSet is initially positioned where?

Submitting queries and getting results from the database Hard
A. On the first row
B. After the final row
C. On the last row
D. Before the first row

57 Which design best separates JDBC connection configuration from application code?

Specifying JDBC driver information externally Hard
A. Store URL, user, and password in external configuration
B. Place credentials in SQL comments next to each query
C. Hard-code the URL and password in every DAO
D. Derive the password from the database product name

58 An application loads JDBC settings from a Properties object and calls DriverManager.getConnection(url, properties). Which statement is correct?

Specifying JDBC driver information externally Hard
A. The driver may use standard and vendor-specific properties
B. The properties convert all values into SQL literals
C. The properties force auto-commit to remain enabled
D. The properties replace the URL's subprotocol

59 An application inserts a row and must obtain the database-generated primary key. Which approach is most appropriate?

Performing CRUD operations using the JDBC API Hard
A. Read the largest existing key after insertion
B. Use executeQuery on an INSERT statement
C. Use executeUpdate with RETURN_GENERATED_KEYS
D. Call getString on the Connection object

60 A transfer updates two account rows. Which sequence provides the required all-or-nothing behavior when auto-commit is disabled?

Performing CRUD operations using the JDBC API Hard
A. Execute both updates without calling either commit or rollback
B. Call rollback after each successful update
C. Commit after the first update, then execute the second update
D. Execute both updates, then call commit; call rollback on failure