Q1. Given the code fragment:
What is the result?
A. Java 7
B. Java 6
C. Java 7, Java 6
D. Java 7 java 6
E. Java
Answer: C
Explanation:
regex: Java / one or more anything !!! / ends with a digit so it is the source string
Q2. Given:
What is the result?
A. Cue sports, Cue sports
B. Compilation fails at line 9
C. Compilation fails at line 11
D. Compilation fails at line 12
E. Compilation fails at line 13
Answer: B
Explanation:
Class Snooker is public. Should be declared in a separate file. // Line 9 getCategory() >>> GetCategory() Line 13
Q3. Given:
Which group of method is moved to a new class when implementing the DAO pattern?
A. public in getId ()
public String getContractDetails ()
public Void setContractDetails(String contactDetails)
public String getName ()
public void setName (String name)
B. public int getId ()
public String getContractDetails()
public String getName()
public Person getPerson(int id) throws Exception
C. public void setContractDetails(String contractDetails) public void setName(String name)
D. public Person getPerson(int id) throws Exception
public void createPerson(Person p) throws Exception
public void deletePerson(int id) throws Exception
public void updatePerson(Person p) throws Exception
Answer: D
Explanation:
The methods related directly to the entity Person is moved to a new class.
CRUD
Note:DAO Design Pattern
*Abstracts and encapsulates all access to a data source *Manages the connection to the
data source to obtain
and store data *Makes the code independent of the data sources and data vendors (e.g.
plain-text, xml, LDAP,
MySQL, Oracle, DB2)
D:\Documents and Settings\useralbo\Desktop\1.jpg
Example (here Customer is the main entity):
public class Customer {
private final String id;
private String contactName;
private String phone;
public void setId(String id) { this.id = id; }
public String getId() { return this.id; }
public void setContactName(String cn) { this.contactName = cn;} public String
getContactName() { return
this.contactName; } public void setPhone(String phone) { this.phone = phone; } public
String getPhone()
{ return this.phone; }
}
public interface CustomerDAO {
public void addCustomer(Customer c) throws DataAccessException; public Customer
getCustomer(String id)
throws DataAccessException; public List getCustomers() throws DataAccessException;
public void
removeCustomer(String id) throws DataAccessException; public void
modifyCustomer(Customer c) throws
DataAccessException; }
Q4. Given two classes in separate files:
Which two import statements can make the a.b.parent class compliable?
A. import a.b.c.Parent;
B. import a.b.c.Child;
C. import a.b.c.*;
D. import a.b.*;
E. import a.*;
Answer: B,C
Explanation:
To import a specific member into the current file, put an import statement at the beginning of thefile before any type definitions but after the package statement, if there is one.C:To import all the types contained in a particular package, use the import statement with the asterisk (*)wildcard character.
Reference: The Java Tutorials,Using Package Members
Q5. Given: What is the result?
A. 1 1 1 1 1
B. 1 2 3 4 5
C. 0 1 2 3 4
D. 0 1 4 3 4
Answer: A
Explanation:
first for-loop set 0 0 0 0 0 second for-loop increments each to 1 1 1 1 1 if condition is not given
Q6. Given the code fragment:
If the file userguide.txt does not exist, what is the result?
A. An empty file iscreated and success is printed
B. class java.io.FileNotFoundException
C. class java.io.IOException
D. class java.lang.Exception
E. Compilation fails
Answer: E
Explanation:
Compilation fails : Exception Exception is not compatible with throws clause in Base.process() IOExceptiondie Exception in der Derived Class Methode muss zur Base
Class Methode passen.
Q7. Given the code fragment:
Which two try statements, when inserted at line ***, enable the code to successfully move the file info.txt to thedestination directory, even if a file by the same name already exists in the destination directory?
A. try (FileChannel in = new FileInputStream (source). getChannel(); FileChannel out =
new FileOutputStream
(dest).getChannel()) { in.transferTo(0, in.size(), out);
B. try ( Files.copy(Paths.get(source),Paths.get(dest));
Files.delete (Paths.get(source));
C. try ( Files.copy(Paths.get(source),
Paths.get(dest),StandardCopyOption.REPLACE_EXISTING); Files.delete
(Paths.get(source));
D. try (Files.move(Paths.get(source),Paths.get(dest));
E. try(BufferedReader br = Files.newBufferedReader(Paths.get(source),
Charset.forName("UTF- 8"));
BufferedWriter bw = Files.newBufferedWriter(Paths.get(dest), Charset.forName("UTF-8"));
String record =
"";
while ((record = br.readLine()) ! = null) {
bw.write(record);
bw.newLine();
}
Files.delete(Paths.get(source));
Answer: C,E
Explanation:
A: copies only, don’t move operation
B,C,D (no try-with-resource !) syntax change to: try { …
B: throws FileAlreadyExistsException
C: correct if syntax change to : StandardCopyOption.REPLACE_EXISTING (before
REPLACE_Existing)
D: throws FileAlreadyExistsException
E: works properly if the sourcefile has the correct format, utf-8 here (else throws
MalformedInputException)
AND syntax is corrected to:
try ( BufferedReader br = Files.newBufferedReader(Paths.get(source),
Charset.forName(“UTF-8));
BufferedWriter bw = Files.newBufferedWriter(Paths.get(dest), Charset.forName(“UTF-8));
){
String record = “”;
…..
Q8. Given that myFile.txt contains:
What is the result?
A. 1: First
2: Second
3:
Third
B.
1: First
2: Second
3:
First
C.
1: First
2: First
3:
First
D.
IOExcepton
E.
Compilation fails
Answer: B
Explanation:
BufferedReader: mark() : Marks the present position in the stream. Subsequent calls to
reset() will attempt toreposition the stream to this point.
reset() : Resets the stream to the most recent mark.
!! After last Line is read (readLine()), a trial to reset() throws IOException : Mark invalid
Q9. An application is waiting for notification of changes to a tmp directory using the following code statements:
Path dir = Paths.get("tmp")
WatchKey key = dir.register (watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) ;
In the tmp directory, the user renames the file testA to testB,
Which statement is true?
A. The events received and the order of events are consistent across all platforms.
B. The events received and the order of events are consistent across all Microsoft Windows versions.
C. The events received and the order of events are consistent across all UNIX platforms.
D. The events received and the order of events are platform dependent.
Answer: A
Explanation:
Most file system implementations have native support for file change notification. The
WatchService API takesadvantage of this support where available.
However, when a file system does not support this mechanism, the WatchService will poll
the file system,waiting for events.
Note:
WatchKey : When a Watchable entity is registered with a WatchService a key which is a
WatchKey isgenerated. Initially the key is in ready state waiting to be notified of any events
on the Watchable entity. Oncean event occurs the key goes into signaled state and allows
to access the events using its pollEvents method.
After processing the poll events the key has to be reset by invoking its reset method.
Reference: The Java Tutorials,Watching a Directory for Changes
Q10. Given the code fragment:
Which three are true?
A. On line 3, the current thread stops and waits until the t1 thread finishes.
B. On line 3, the t1 thread stops and waits until the current thread finishes.
C. On line 4, the t1 thread is dead.
D. On line 4, the t1 thread is waiting to run.
E. This code cannot throw a checked exception.
F. This code may throw a checked exception.
Answer: A,C,F
Explanation:
Thejoin()methods waits for this thread to die.