Unit 3: File I/O and JDBC - Practice Quiz
1
Which method is commonly used to create a Path object from a path string?
File.path()
Paths.open()
Files.create()
Path.of()
2
Which Path method returns the final element of a file path?
getFileName()
getParent()
getNameCount()
getRoot()
3
Which Path method combines the current path with another path?
relativize()
normalize()
resolve()
toAbsolutePath()
4 Which method checks whether a file or directory exists?
Files.contains()
Files.isOpen()
Files.available()
Files.exists()
5 Which method deletes a file only when it exists and avoids an exception when it is missing?
Files.clearIfExists()
Files.deleteIfExists()
Files.eraseIfFound()
Files.removeIfFound()
6
Which Files method is used to copy a file to another path?
Files.duplicate()
Files.copy()
Files.clone()
Files.transfer()
7 Which method returns a stream of lines from a text file?
Files.tokens()
Files.lines()
Files.records()
Files.entries()
8 Which method returns a stream containing entries directly inside a directory?
Files.find()
Files.list()
Files.lines()
Files.walk()
9
Why should a stream returned by Files.lines() usually be used in a try-with-resources statement?
10
Which Java package contains core JDBC interfaces such as Connection and Statement?
java.sql
java.database
java.jdbc
javax.jdbc
11 Which JDBC interface represents an active session with a database?
ResultSet
Statement
DriverManager
Connection
12 Which method is commonly used to obtain a JDBC database connection?
DataSource.openDriver()
DriverManager.openDatabase()
Connection.createSession()
DriverManager.getConnection()
13 What is the main purpose of a JDBC driver?
14
Which method is normally used to execute a SQL SELECT statement?
executeSelect()
executeQuery()
executeUpdate()
executeInsert()
15 Which JDBC interface stores rows returned by a database query?
ResultSet
DriverManager
PreparedStatement
Connection
16
Which ResultSet method moves the cursor to the next row?
next()
advance()
forward()
move()
17 Which file type is commonly used to store a JDBC URL, username, and password outside Java source code?
18 What is a key benefit of storing JDBC configuration externally?
19 Which CRUD operation adds a new row to a database table?
20
Which JDBC method is commonly used for SQL INSERT, UPDATE, and DELETE statements?
executeUpdate()
readRows()
fetchResults()
executeQuery()
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?
base.append("2024/summary.txt")
base.resolve("2024").resolve("summary.txt")
base.concat("2024").concat("summary.txt")
base.join("2024", "summary.txt")
22
What is the result of Paths.get("/home/user/docs/report.txt").getParent() on a Unix-like system?
report.txt
/home/user/docs/report.txt
/home/user
/home/user/docs
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?
Files.readAttributes()
normalize()
Files.isSameFile()
toRealPath()
24 Which statement correctly checks whether a path exists while avoiding an exception if the path is inaccessible?
Files.isAvailable(path)
Files.check(path)
Files.valid(path)
Files.exists(path)
25
What happens when Files.copy(source, target) is used and target already exists as a regular file?
FileAlreadyExistsException
false without changing either file
26 Which option allows an existing target file to be replaced during a copy operation?
StandardCopyOption.REPLACE_EXISTING
StandardCopyOption.OVERWRITE
StandardCopyOption.ALLOW_REPLACE
StandardCopyOption.UPDATE_TARGET
27
Why can Files.delete(directory) fail even when the directory exists?
28
Which statement correctly creates a stream containing the entries directly inside the directory represented by dir?
Files.entries(dir)
dir.listFiles()
Files.stream(dir)
Files.list(dir)
29
Which code correctly finds all regular .java files under sourceDir, including files in nested directories?
Files.search(sourceDir, p -> p.toString().endsWith(".java"))
Files.find(sourceDir, Integer.MAX_VALUE, (p, a) -> a.isRegularFile() && p.toString().endsWith(".java"))
Files.walkFileTree(sourceDir).filter(p -> p.toString().endsWith(".java"))
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?
Path object after processing
31
Which expression counts regular files in the directory tree rooted at root?
Files.walk(root).filter(Files::isRegularFile).count()
Files.readAllLines(root).filter(Files::isRegularFile).count()
Files.list(root).filter(Files::isRegularFile).size()
Files.walk(root).filter(Files::isDirectory).count()
32
Which JDBC component is primarily responsible for creating Connection objects from database URLs?
ResultSet
DriverManager
SQLException
Statement
33 Which JDBC object represents the active session between an application and a database?
Connection
DatabaseMetaData
ResultSet
Driver
34 Which sequence is generally required before an application can execute a SQL statement through JDBC?
35
What is the main advantage of using PreparedStatement instead of concatenating user input into SQL?
36
Which method is normally used to execute a SELECT statement and obtain its rows?
executeInsert()
executeQuery()
executeUpdate()
executeRows()
37
When iterating through a ResultSet, what does resultSet.next() do?
38 An application reads the database URL, username, and password from a properties file. What is the primary benefit?
39 Which configuration item identifies the JDBC implementation class when explicit driver loading is required?
40 Which mapping correctly associates CRUD operations with common SQL commands?
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?
/srv/app/logs/app.log
../logs/app.log
/srv/app/../logs/app.log
/srv/logs/app.log
42
Which statement best describes Path.normalize() when applied to a path containing . and .. elements?
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?
path1.toAbsolutePath().equals(path2.toAbsolutePath())
path1.getFileName().equals(path2.getFileName())
path1.toRealPath().equals(path2.toRealPath())
path1.normalize().equals(path2.normalize())
44
A directory contains files and subdirectories. The program calls Files.delete(directory). What is the normal result?
DirectoryNotEmptyException is thrown
AccessDeniedException is always thrown
45
Which behavior is guaranteed when copying an existing file with Files.copy(source, target) and no copy options?
FileAlreadyExistsException
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?
StandardCopyOption.COPY_ATTRIBUTES
StandardCopyOption.REPLACE_EXISTING
StandardCopyOption.ATOMIC_MOVE
LinkOption.NOFOLLOW_LINKS
47
What is the most important resource-management requirement when processing a file with Files.lines(path)?
48
Which statement correctly distinguishes Files.list(directory) from Files.walk(directory)?
list requires a regular file, while walk requires an empty directory
list returns direct children, while walk can recursively traverse descendants
list follows every symbolic link, while walk never follows links
list recursively visits descendants, while walk visits siblings only
49
A program uses Files.find(root, 3, matcher) to locate files. Which paths can be examined when root has depth zero?
root and descendants up to three levels below it
root itself
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?
51 Which JDBC component is primarily responsible for managing a set of JDBC drivers and selecting an appropriate driver for a connection URL?
ResultSet
DatabaseMetaData
Connection
DriverManager
52 Which JDBC object represents the database session through which statements are created and transactions are controlled?
SQLException
ResultSet
Connection
Driver
53
A JDBC 4-compliant driver is packaged correctly and available on the class path. Which statement is generally true before calling DriverManager.getConnection?
Connection
ResultSet is created
Class.forName
54
A connection URL is valid, but no installed driver accepts its subprotocol. What is the most likely result of DriverManager.getConnection(url)?
SQLException indicating no suitable driver is available
ResultSet containing the URL is returned
55
Why is a PreparedStatement generally preferred over string concatenation when inserting user-supplied values into a SQL statement?
56
After executing a query, the cursor of a newly created JDBC ResultSet is initially positioned where?
57 Which design best separates JDBC connection configuration from application code?
58
An application loads JDBC settings from a Properties object and calls DriverManager.getConnection(url, properties). Which statement is correct?
59 An application inserts a row and must obtain the database-generated primary key. Which approach is most appropriate?
executeQuery on an INSERT statement
getString on the Connection object
executeUpdate with RETURN_GENERATED_KEYS
60 A transfer updates two account rows. Which sequence provides the required all-or-nothing behavior when auto-commit is disabled?
commit or rollback
rollback after each successful update
commit; call rollback on failure
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →