Q1. Which four are true about enums?
A. An enum is typesafe.
B. An enum cannot have public methods or fields.
C. An enum can declare a private constructor.
D. All enums implicitly implement Comparable.
E. An enum can subclass another enum.
F. An enum can implement an interface.
Answer: A,C,D,F Explanation:
C: The constructor for an enum type must be package-private or private access. Reference: Java Tutorials,Enum Types
Q2. Given the classes: What is the result?
A. John Harry
B. unknown Harry
C. john unknown
D. unknown unknown
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: B
Explanation:
getName() is missing in John, hence Pupils getName() is invoked and the String in Pupils scope returned.
Q3. Given the fragment:
If thread a and thread b are running, but not completing, which two could be occurring?
A. livelock
B. deadlock
C. starvation
D. loose coupling
E. cohesion
Answer: A,B
Explanation:
A: A thread often acts in response to the action of another thread. If the other thread's action is also a responseto the action of another thread, then livelock may result. A thread often acts in response to the action ofanother thread. If the other thread's action is also a response to the action of another thread, then livelock mayresult.
B: Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
Q4. Which concept allows generic collections to interoperate with java code that defines collections that use rawtypes?
A. bytecode manipulation
B. casting
C. autoboxing
D. auto-unboxing
E. type erasure
Answer: E
Explanation:
The type erasure of its leftmost bound, or type Object if no bound was specified. Examples: type parameters type erasure List<String> List Map.Entry<String,Long> Map.Entry <T extends Cloneable & Comparable<T>> Cloneable <T extends Object & Comparable<T>> Object
<T> T[] toArray(T[] a) Object[] toArray(Object[] a)
The type erasure process can be imagined as a translation from generic Java source code
back into regularJava code. In reality the compiler is more efficient and translates directly to
Java byte code. But the byte codecreated is equivalent to the non-generic Java code.
Q5. Given the code fragment:
Assume that the SQL queries return records. What is the result of compiling and executing this code fragment?
A. The program prints employee IDs
B. The program prints customer IDs
C. The program prints Error
D. Compilation fails on line ***
Answer: C
Explanation:
!!! The given Code prints Error -- the second query clears the ResultSet !? ErrorMessage: Operation notallowed after ResultSet closed
It would print A, if second Query i set to rs = stmt.executeQuery("SELECT ID FROM Customer"); // Line *** It would print B, if Line *** is missing. // The program compiles and runs fine. Both executeQuery statements will run. The first executeQuery statement (ResultSet rs = stmt.executeQuery(query);) will set the rs Resultset. It will be used in the while loop. EmployIDswill be printed. Note: Executes the given SQL statement, which returns a single ResultSet object. Parameters:sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement Returns:a ResultSet object that contains the data produced by the given query; never null
Q6. Given the following code fragment:
10.
p1 = paths.get("report.txt");
11.
p2 = paths.get("company");
12.
/ / insert code here
Which code fragment, when inserted independently at line 12, move the report.txt file to the company directory,at the same level, replacing the file if it already exists?
A. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
B. Files.move(p1, p2, StandardCopyOption.REPLACE_Existing,
LinkOption.NOFOLLOW_LINKS);
C. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
LinkOption.NOFOLLOW_LINKS);
D. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.copy_ATTRIBUTES,
StandrardCopyOp)
E. Files.move (p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.copy_ATTRIBUTES,
LinkOption.NOF)
Answer: A,C
Explanation:
Moving a file is equally as straight forward move(Path source, Path target, CopyOption... options); The available StandardCopyOptions enums available are: StandardCopyOption.REPLACE_EXISTING StandardCopyOption.ATOMIC_MOVE If Files.move is called with StandardCopyOption.COPY_ATTRIBUTES an UnsupportedOperationException isthrown.
Q7. 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
Q8. Given:
Which fragment, inserted in the Books interface, enables the code to compile?
A. public abstract String type; public abstract String getType();
B. public static String type; public abstract String getType();
C. public String type = "Fiction"; public static String getType();
D. public String type = "Fiction"; public abstract String getType();
Answer: D
Q9. Given: What is the result?
A. p001 Widget p002 X-Large Widget
B. p002 Large Widget p001 Widget
C. p002 X-large Widget p001 Widget
D. p001 Widget p002 Large Widget
E. compilation fails
Answer: A
Explanation: Compiles fine. Output is: P001 Widget P002 X-Large Widget Line: partList.put("P002", "X-Large Widget"); >> overwrites >> line:partList.put("P002", "Large Widget"); put V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). If the map previouslycontained a mapping for the key, the old value is replaced by the specified value. (Amap m is said to contain amapping for a key k if and only if m.containsKey(k) would return true.)
Parameters: key - key with which the specified value is to be associated value - value to be associated with the specified key Returnsthe previous value associated with key, or null if there was no mapping for key. (A null return can alsoindicate that the map previously associated null with key, if the implementation supports null values.)
Q10. Given:
Which statement will iterate through Direction?
A. for (Direction d : Direction.values()){ //
}
B. for (Direction d : Direction.asList()){
//
}
C. for (Direction d : Direction.iterator()){
//
}
D. for (Direction d : Direction.asArray()){
//
}
Answer: A
Explanation:
The static values() method of an enum type returns an array of the enum values. The
foreach loop is a good
way to go over all of them.
//... Loop over all values.
for (Direction d : Direction.values()){
System.out.println(d); // PrintsNORTH, EAST, ...
}
Q11. Which code fragment correctly appends "Java 7" to the end of the file /tmp/msg.txt?
A. FileWriter w = new FileWriter("/tmp/msg.txt");
append("Java 7");
close();
B. FileWriter w = new FileWriter("/tmp/msg.txt", true);
append("Java 7");
close();
C. FileWriter w = new FileWriter("/tmp/msg.txt", FileWriter.MODE_APPEND);
append("Java 7");
close();
D. FileWriter w = new FileWriter("/tmp/msg.txt", Writer.MODE_APPEND);
append("Java 7");
close();
Answer: B
Explanation:
FileWriter(File file, boolean append)
A: clears the file and append "Java7"
Constructs a FileWriter object given a File object.
If the second argument is true, then bytes will be written to the end of the file rather than
the beginning.Parameters:
file - a File object to write toappend - if true, then bytes will be written to the end of the file
rather than the beginning
Q12. View the exhibit:
Given the code fragment:
What is the result?
A. Compilation fails
B. 6
C. 4
D. 1
E. 3
F. Not possible to answer due to missing exhibit.
Answer: C
Explanation:
C: 4 Falls ge.ndert zu: return FileVisitResult.CONTINUEsonst A: weil CONTINUE als Konstante unbekannt Note: TheFileSystems.getDefault() returns the default FileSystem. The default file system creates objects thatprovide access to the file systems accessible to the Java virtual machine. The working directory of the filesystem is the current user directory, named by the system property user.dir.
Q13. Given:
And the commands: javac Counter.java java ea Counter
What is the result?
A. 2
B. 3
C. NullPointException is thrown at runtime
D. AssertionError is thrown at runtime
E. Compilation fails
Answer: B
Explanation:
The command line javac Counter.java
Willcompile the code.
The command line java ea Counter
Willrun the cod with assertions enabled.
Assertion is true because getCount(arr) = 3 and Length of array is 4
The following line:
assert (getCount(arr) < arr.length);
where the Boolean expression getCount(arr) < arr.length will evaluate to false, will ensure
that anAssertionError is thrown at runtime.
Note:The javac command compiles Java source code into Java bytecodes. You then use
the Java interpreter -the java command - to interprete the Java bytecodes.
Note 2: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 3:
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.
Q14. 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.
Q15. Which represents part of a DAO design pattern?
A. interface EmployeeDAO {
int getID();
Employee findByID (intid);
void update();
void delete();
}
B. class EmployeeDAO {
int getID() { return 0;}
Employee findByID (int id) { return null;}
void update () {}
void delete () {}
}
C. class EmployeeDAO {
void create (Employee e) {}
void update (Employee e) {}
void delete (int id) {}
Employee findByID (int id) {return id}
}
D. interface EmployeeDAO {
void create (Employee e);
void update (Employee e);
void delete (int id);
Employee findByID (int id);
}
E. interface EmployeeDAO {
void create (Connection c, Employee e);
void update (Connection c, Employee e);
void delete (Connection c, int id);
Employee findByID (Connection c, int id);
}
Answer: D