What is the result if the report.txt file contains
Welcome to the world of Java?
A. The file contains the first five characters. B. The file contains the first four characters. C. The contents of the file remain unchanged. D. A NonWritableChannelException is thrown at runtime.
C. The contents of the file remain unchanged.
The truncate line will not change the file since the file size is less than 30. Reference: Interface SeekableByteChannel
Question 22:
Given:
class Fibonacci extends RecursiveTask {
final int n;
Fibonacci (int n) { this.n = n }
Integer compute () {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci (n ?1);
f1.fork; // Line X
Fibonacci f2 = new Fibonacci (n ?2); // Line Y
return f2.compute() + f1.join;
}
}
Suppose that lines X and Y are transposed:
Fibonacci f2 = new Fibonacci (n ?2); // Line Y
f1.fork; // Line X
What is the likely result?
A. The program produces the correct result, with similar performance to the original B. The program produces the correct result, with performance degraded to the equivalent of being single-threaded. C. The program produces an incorrect result D. The program goes into an infinite loop E. An exception is thrown at runtime F. The program produces the correct result, the better performance than the original.
A. The program produces the correct result, with similar performance to the original
The degree of parallelism is not changed. Functionality is the same.
Question 23:
Given the following code fragment:
public static void getInfo() {
//insert code here
List fontCatalog = new ArrayList();
fontCatalog.add("Algerian");
fontCatalog.add("Cambria");
fontCatalog.add("Lucida Bright");
category.put("firstCategory",fontCatalog);
List entrySet = new ArrayList(category.entrySet());
Iterator it = entrySet.iterator();
while(it.hasNext())
{ System.out.println(it.next)
);
}
}
Which two code fragments, when inserted independently at line **, enable the code to compile?
A. Map category = new HashMap (); B. Map category = new HashMap(); C. Map category = new HashMap (); D. Map category = new HashMap (); E. Map category = new HashMap (); F. Map category = new HashMap ();
C. Map category = new HashMap (); E. Map category = new HashMap ();
E: Redundant type arguments in new expressions. Use diamond operator instead.
Question 24:
Given three resource bundles with these values set for menu1: ( The default resource bundle is English US resource Bundle Menu1 = small French resource Bundle Menu1 = petit Chinese Resource Bundle Menu = 1 And given the code fragment: Locale.setDefault (new Locale("es", "ES")); // Set default to Spanish and Spain loc1 = Locale.getDefault(); ResourceBundle messages = ResourceBundle.getBundle ("messageBundle", loc1); System.out.println (messages.getString("menu1")); What is the result?
A. No message is printed B. petit C. : D. Small E. A runtime error is produced
E. A runtime error is produced
Compiles fine, but runtime error when trying to access the Spanish Resource bundle (which does not exist):
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messageBundle, locale es_ES
Which two try statements, when inserted at line **, enable the code to successfully move the file info.txt to the destination 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)); 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));
B. try {Files.copy(Paths.get(source), Paths.get(dest)); Files.delete(Paths.get(source)); D. try {Files.move(Paths.get(source),Paths.get(dest));
Question 26:
Given the error message when running you application:
Exception in thread "main" java.util.MissingResourceException: can't find bundle for base name messageBundle, Locale
And given that the Message Bundle.properties file has been created, exists on your disk, and is properly formatted.
What is the cause of the error message?
A. The file is not in the environment PATH. B. The file is not in the CLASSPATH. C. The file is not in the JJAVAPATH. D. You cannot use a file to store a ResourceBundle.
C. The file is not in the JJAVAPATH.
Your language file should be in the classpath.
Question 27:
What are two benefits of a Factory design pattern?
A. Eliminates direct constructor calls in favor of invoking a method B. Provides a mechanism to monitor objects for changes C. Eliminates the need to overload constructors in a class implementation D. Prevents the compile from complaining about abstract method signatures E. Prevents tight coupling between your application and a class implementation
A. Eliminates direct constructor calls in favor of invoking a method E. Prevents tight coupling between your application and a class implementation
Factory methods are static methods that return an instance of the native class. Factory methods :
*
have names, unlike constructors, which can clarify code.
*
do not need to create a new object upon each invocation - objects can be cached and reused, if necessary.
*
can return a subtype of their return type - in particular, can return an object whose implementation class is unknown to the caller. This is a very valuable and widely used feature in many frameworks which use interfaces as the return type of static factory methods.
Note: The factory pattern (also known as the factory method pattern) is a creational design pattern. A factory is a JavaSW class that is used to encapsulate object creation code. A factory class instantiates and returns a particular type of
object based on data passed to the factory. The different types of objects that are returned from a factory typically are subclasses of a common parent class.
The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This creational method is often called something such as getInstance or
getClass .
Question 28:
Given the code fragment: public class TestString { public static void main(String[] args)
{ String str=null;
switch(str) { case "":
System.out.println("blank"); break;
case "null":
System.out.println("NULL"); break;
default: System.out.println("invalid");
break;
}
}
}
What is the result?
A. Compilation fails B. Blank C. NULL D. An exception is thrown at runtime E. Invalid
D. An exception is thrown at runtime
A java.lang.NullPointerException will be thrown at runtime at line:
switch(str) {
Ensure that the expression in any switch statement is not null to prevent a NullPointerException from being thrown.
Reference: The Java Tutorials, The switch Statement
Question 29:
Which is a key aspect of composition?
A. Using inheritance B. Method delegation C. Creating abstract classes D. Implementing the composite interface
B. Method delegation
In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition, the act of composing a class out of references to other objects. With composition, references to the constituent
objects become fields of the containing object.
To use composition in Java, you use instance variables of one object to hold references to other objects.
The relationship modeled by composition is often referred to as the "has-a" relationship. Delegation involves re-exporting methods; in a composition relationship, the inner objects methods may be used only privately and not re-exposed.
Question 30:
Given:
public class MyGrades {
private final List myGrades = new ArrayList();
private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
public void addGrade(Integer grade) {
// acquire lock
myGrades.add(grade);
// release lock
}
public void averageGrades() {
// acquire lock Line **
double sum = 0;
int i = 0;
for (i = 0; i < myGrades.size(); i++) {
sum += myGrades.get(i);
}
// release lock Line ***
System.out.println("The average is: " + sum/(i+1));
}
}
Which pair's statements should you insert at lines ** and lines *** (respectively) to acquire and release the most appropriate lock?
A. rwlock.readLock().acquire(); rwlock.readLock().release(); B. rwlock.readLock().lock(); rwlock.readLock().unlock(); C. rwlock.getLock().acquire(); rwlock.getLock().release(); D. rwlock.getLock().lock(); rwlock.getLock().Unlock(); E. relock.WriteLock().acquire(); rwlock.writeLock().release(); F. rwlock.writeLock().lock(); rwlock.WriteLock().unlock();
B. rwlock.readLock().lock(); rwlock.readLock().unlock();
We need a read lock, not a write lock, we are just reading data, not writing/updating data. To aquire and release the lock the method lock() and unlock are used.
Nowadays, the certification exams become more and more important and required by more and more
enterprises when applying for a job. But how to prepare for the exam effectively? How to prepare
for the exam in a short time with less efforts? How to get a ideal result and how to find the
most reliable resources? Here on Vcedump.com, you will find all the answers.
Vcedump.com provide not only Oracle exam questions,
answers and explanations but also complete assistance on your exam preparation and certification
application. If you are confused on your 1Z0-805 exam preparations
and Oracle certification application, do not hesitate to visit our
Vcedump.com to find your solutions here.