1Z0-804 Premium Bundle

1Z0-804 Premium Bundle

Java SE 7 Programmer II Exam Certification Exam

4.5 
(21135 ratings)
0 QuestionsPractice Tests
0 PDFPrint version
May 20, 2024Last update

Oracle 1Z0-804 Free Practice Questions

Q1. Given these facts about Java types in an application: 

-

Type x is a template for other types in the application. 

-

Type x implements dostuff (). 

-

Type x declares, but does NOT implement doit(). 

-

Type y declares doOther() . 

Which three are true? 

A. Type y must be an interface. 

B. Type x must be an abstract class. 

C. Type y must be an abstract class. 

D. Type x could implement or extend from Type y. 

E. Type x could be an abstract class or an interface. 

F. Type y could be an abstract class or an interface. 

Answer: B,D,F 

Explanation: 

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can containimplemented methods. Such abstract classes are similar to interfaces, except that they provide a partialimplementation, leaving it to subclasses to complete the implementation. If an abstract class contains onlyabstract method declarations, it should be declared as an interface instead. 

Note: An interface in the Java programming language is an abstract type that is used to specify an interface (in thegeneric sense of the term) that classes must implement. Interfaces are declaredusing the interface keyword,and may only contain method signature and constant declarations (variable declarations that are declared tobe both static and final). An interface maynever contain method definitions. 

Note 2: an abstract class is a class that is declared abstract--it may or may not include abstract methods.Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that isdeclared without an implementation (without braces, and followed by a semicolon) 

Q2. Given: 

What is the result? 

A. Compilation succeeds. 

B. Compilation fails due to an error on line 1. 

C. Compilation fails due to an error on line 2. 

D. Compilation fails due to an error on line 3. 

E. Compilation fails due to an error on line 4. 

F. Compilation fails due to an error on line 8. 

Answer:

Q3. Given the cache class: 

A. 101 

B. Compilation fails at line 1. 

C. Compilation fails at line 2. 

D. Compilation fails at line 3. 

Answer:

Explanation: 

Compilation failure at line:1 Incorrect number of arguments for type Cache<T>; it cannot be parameterized with arguments <>illegal start of typetype cache.Cache does not take parameters. 

Q4. Which class(es) safely protects the doIt () method from concurrent thread access? A. Option A 

B. Option B 

C. Option C 

D. Option D 

Answer: A,D 

Explanation: 

only A und D possible 

It should be pointed out that: 

public void blah() { 

synchronized (this) { 

// do stuff 

}} 

is semantically equivalent to: 

public synchronized void blah() { 

// do stuff 

Incorrect answer: 

B: A constructor cannot be synchronized. () Object cannot be resolved to a type 

C: in static context (static void main !) no reference to this possible 

Q5. Given: What is the result? 

A. 1 

B. 0 

C. 2 

D. Compilation fails 

E. An exception is thrown at runtime 

Answer:

Explanation: Section: (none) 

Explanation 

The code compiles fine. 

java.lang.NullPointerException 

because only one element of list is initialized : element [0] 

elements [1] and [2] equals null 

alte Begründung: 

An exception is thrown at runtime due to data type comparison mismatch: 

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast 

to java.lang.Integer 

at java.lang.Integer.compareTo(Integer.java:52) 

at java.util.Arrays.binarySearch0(Arrays.java:1481) 

at java.util.Arrays.binarySearch(Arrays.java:1423) 

at searchtext.SearchText.main(SearchText.java:22) 

Note:binarySearch 

public static int binarySearch(char[] a, 

char key)Searches the specified array of chars for the specified value using the binary 

search algorithm. The array mustbe sorted (as by the sort method, above) prior to making 

this call. If it is not sorted, the results are undefined. Ifthe array contains multiple elements 

with the specified value, there is no guarantee which one will be found. 

Parameters: 

a - the array to be searched. 

key - the value to be searched for. 

Returns: 

Indexof the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The 

insertionpoint is defined as the point at which the key would be inserted into the list: the 

index of the first elementgreater than the key, or list.size(), if all elements in the list are less 

than the specified key. Note that thisguarantees that the return value will be >= 0 if and 

only if the key is found. 

Q6. Given three resources bundles with these values set for menu1: (the default resource bundle in US English.) 

English US Resource Bundle Menu1 = small French Resource Bundle Menu1 = petit Chinese Resource Bundle Menu1 = And given the code fragment: Locale.setDefault(new Locale("es", "ES")); // Set default to Spanish and Spain 

Locale loc1 = Locale.getDefault(); 

ResourceBundle message = ResourceBundle.getBundle("MessageBundle", loc1); 

System.out.println(message.getString("menu1")); 

What is the result? 

A. No message is printed 

B. petit 

C. small 

D. A runtime error is produced 

Answer:

Explanation: Compiles fine, but runtime error when trying to access the Spanish Resource bundle (which doesnot exist): Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messageBundle, locale es_ES 

Q7. Assuming the port statements are correct, which two (three?) code fragments create a one-byte file? 

A. OutputStream fos = new FileOutputStream(new File("/tmp/data.bin")); OutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeByte(0); dos.close(); 

B. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); 

DataOutputStream dos = new DataOutputStream(fos); 

dos.writeByte(0); 

dos.close(); 

C. OutputStream fos = new FileOutputStream (new File ("/tmp/data.bin")); 

DataOutputStream dos = new DataOutputStream(fos); 

dos.writeByte(0); 

dos.close(); 

D. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); fos.writeByte(0); 

fos.close(); 

Answer: A,B,C 

Explanation: 

B:Create DataOutputStream from FileOutputStream public static void main(String[] args) 

throws 

Exception { FileOutputStream fos = new FileOutputS tream("C:/demo.txt"); 

DataOutputStream dos = new 

DataOutputStream(fos); 

Note: 

The FileOutputStream class is a subclass of OutputStream. You can construct a 

FileOutputStream object by 

passing a string containing a path name or a File object. 

You can also specify whether you want to append the output to an existing file. 

public FileOutputStream (String path) 

public FileOutputStream (String path, boolean append) 

public FileOutputStream (File file) 

public FileOutputStream (File file, boolean append) 

With the first and third constructors, if a file by the specified name already exists, the file 

will be overwritten. Toappend to an existing file, pass true to the second or fourth 

constructor. 

Note 2:public class DataOutputStreamextends FilterOutputStreamimplements DataOutput 

A data output stream lets an application write primitive Java data types to an output stream 

in a portable way. 

An application can then use a data input stream to read the data back in. 

Reference:java.io Class DataOutputStream 

Q8. Given the code fragment: Which code fragment inserted at line ***, enables the code to compile? 

A. public void process () throws FileNotFoundException, IOException { super.process (); 

while ((record = br.readLine()) !=null) { 

System.out.println(record); 

}} 

B. public void process () throws IOException { 

super.process (); 

while ((record = br.readLine()) != null) { 

System.out.println(record); 

}} 

C. public void process () throws Exception { 

super.process (); 

while ((record = br.readLine()) !=null) { 

System.out.println(record); 

}} 

D. public void process (){ 

try { 

super.process (); 

while ((record = br.readLine()) !=null) { 

System.out.println(record); 

} catch (IOException | FileNotFoundException e) { } 

E. public void process (){ 

try { 

super.process (); 

while ((record = br.readLine()) !=null) { 

System.out.println(record); 

} catch (IOException e) {} 

Answer:

Explanation: 

A: Compilation fails: Exception IOException is not compatible with throws clause in Base.process() 

B: Compilation fails: Exception IOException is not compatible with throws clause in Base.process() 

C: Compilation fails: Exception Exception is not compatible with throws clause in Base.process() 

D: Compilation fails: Exception FileNotFoundException has already been caught by the alternative IOException Alternatives in a multi-catch statement cannot be related to subclassing Alternative java.io.FileNotFoundException is a subclass of alternative java.io.IOException 

E: compiles ... 

Q9. 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 = “”; 

….. 

Q10. Given the integer implements comparable: 

What is the result? 

A. 4 1 

B. 1 2 

C. 32 

D. 21 

E. 2 3 

Answer:

Explanation: 

binarySearch 

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 

Searches the specified list for the specified object using the binary search algorithm. 

The list must be sorted into ascending order according to the natural ordering of its 

elements (as by the sort(List) method) prior to making this call. If it is not sorted, the results 

are undefined. 

Parameters: 

list - the list to be searched. 

key - the key to be searched for. 

Returns: 

the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). 

Q11. Given the code fragment: SimpleDataFormat sdf; 

Which code fragment displays the three-character month abbreviation? 

A. SimpleDateFormat sdf = new SimpleDateFormat ("mm", Locale.UK); System.out.println 

("Result:" + 

sdf.format(new Date())); 

B. SimpleDateFormat sdf = new SimpleDateFormat ("MM", Locale.UK); System.out.println 

("Result:" + 

sdf.format(new Date())); 

C. SimpleDateFormat sdf = new SimpleDateFormat ("MMM", Locale.UK); 

System.out.println ("Result:" + 

sdf.format(new Date())); 

D. SimpleDateFormat sdf = new SimpleDateFormat ("MMMM", Locale.UK); 

System.out.println ("Result:" + 

sdf.format(new Date())); 

Answer:

Q12. Given: And the commands: 

javac Test.java 

java ea Test 

What is the result? 

A. Compilation fails 

B. Standard Edition Enterprise Edition Micro Edition 

C. Standard Edition class java.lang.AssertionError Micro Edition 

D. Standard Edition is printed and an Assertion Error is thrown 

Answer:

Explanation: 

javac Test.java 

will compile the program. 

As for command line: 

java ea Test 

First the code will produce the output: 

Standard Edition 

See Note below. 

The ea option will enable assertions. This will make the following line in the switch 

statement to be run: 

default: assert false; 

This will throw an assertion error. This error will be caught. An the class of the assertion 

error (classjava.lang.AssertionError) will be printed by the following line: 

System.out.println(e.getClass()); 

Note:The java tool launches a Java application. It does this by starting a Java runtime 

environment, loading aspecified class, and invoking that class's main method. The method 

declaration must look like the following: 

public static void main(String args[]) 

Paramater ea: 

-enableassertions[:<package name>"..." | :<class name> ] -ea[:<package name>"..." | 

:<class name> ] 

Enable assertions. Assertions are disabled by default. With no arguments, 

enableassertions or -ea enablesassertions. 

Note 2: 

An assertion is a statement in the JavaTM programming language that enables you to test 

your assumptionsabout your program. 

Each assertion contains a boolean expression that you believe will be true when the 

assertion executes. If it isnot true, the system will throw an error. 

public class AssertionError extends Error 

Thrown to indicate that an assertion has failed. 

Note 3: 

The javac command compiles Java source code into Java bytecodes. You then use the 

Java interpreter - the 

java command - to interprete the Java bytecodes. 

Reference:java - the Java application launcher 

Reference:java.langClass AssertionError 

Q13. Given: What is the result? 

A. false false 

B. true false 

C. true true 

D. Compilation fails 

E. An exception is thrown at runtime 

Answer:

Explanation: 

(this == obj) is the object implementation of equals() and therefore FALSE, if the reference 

points to variousobjectsand then the super.equals() is invoked, the object method equals() 

what still result in FALSEbetter override of equals() is to compare the attributes like: 

public boolean equals (Object obj) { 

if (obj != null){ 

Product p = (Product)obj; 

return this.id == p.id; 

return false; 

Q14. You have been asked to create a ResourceBundle file to localize an application. 

Which code example specifies valid keys menu1 and menu2 with values of File Menu and View Menu? 

A. <key name ="menu1">File Menu</key> <key name ="menu1">View Menu</key> 

B. <key> menu1</key><File Menu>File Menu </value> <key> menu1</key><File Menu>View Menu </value> 

C. menu1m File menu, menu2, view menu 

D. menu1 = File Menu menu2 = View Menu 

Answer:

Explanation: 

A properties file is a simple text file. You can create and maintain a properties file with just aboutany text editor. 

You should always create a default properties file. The name of this file begins with the base name of your ResourceBundle and ends with the .properties suffix. In the PropertiesDemo program the base name is LabelsBundle. Therefore the default properties file is called LabelsBundle.properties. The following examplefilecontains the following lines: # This is the default LabelsBundle.properties file s1 = computer s2 = disk s3 = monitor s4 = keyboard Note that in the preceding file the comment lines begin with a pound sign (#). The other lines contain key-valuepairs. The key is on the left side of the equal sign and the value is on the right. For instance, s2 is the key thatcorresponds to the value disk. The key is arbitrary. We could have called s2 something else, like msg5 ordiskID. Once defined, however, the key should not change because it is referenced in the source code. Thevalues may be changed. In fact, when your localizers create new properties files to accommodate additionallanguages, they will translate the values into various languages. 

Q15. Given: 

What is the result? 

A. John Adams George Washington Thomas Jefferson 

B. George Washington John Adams Thomas Jefferson 

C. Thomas Jefferson John Adams George Washington 

D. An exception is thrown at runtime 

E. Compilation fails 

Answer:

Explanation: The program compiles and runs fine. 

At runtime the NameList is built and then sorted by natural Order (String >> alphabetically). 

START 1Z0-804 EXAM