Q1. Which statement declares a generic class?
A. public class Example < T > { }
B. public class <Example> { }
C. public class Example <> { }
D. public class Example (Generic) { }
E. public class Example (G) { }
F. public class Example { }
Answer: A
Explanation:
Example:
public class Pocket<T>
{
private T value;
public Pocket() {}
public Pocket( T value ) { this.value = value; }
public void set( T value ) { this.value = value; }
public T get() { return value; }
public boolean isEmpty() { return value != null; }
public void empty() { value = null; }
}
Q2. 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.
Q3. The default file system includes a logFiles directory that contains the following files:
Log-Jan 2009
log_0l_20l0
log_Feb20l0
log_Feb2011
log_10.2012
log-sum-2012
How many files does the matcher in this fragment match?
PathMatcher matcher = FileSystems.getDefault ().getPathMatcher ("glob: *???_*1?" );
A. One
B. Two
C. Three
D. Four
E. Five
F. Six
Answer: B
Explanation:
The pattern to match is *???_*1? (regex ".*..._.*1.")
This means at least three characters before the symbol _ , followed by any amount of
characters. The next tolast character must be 1. The last character can by any character.
The following file names match this pattern:
log_Feb2011
log_10.2012 Trap !! l is not 1 !!
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. 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
Q6. Given the code fragment:
What is the result when the result.txt file already exists in c:\student?
A. The program replaces the file contents and the file's attributes and prints Equal.
B. The program replaces the file contents as well as the file attributes and prints Not equal.
C. An UnsupportedOperationException is thrown at runtime.
D. The program replaces only the file attributes and prints Not equal.
Answer: B
Explanation:
Assuming there is a file D:\\faculty\\report.txt then this file will be copied and will be replacing C:\\student\\report.txt.
Q7. Which two properly implement a Singleton pattern?
A. class Singleton {
private static Singleton instance;
private Singleton () {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton ();
}
return instance;
}
}
B. class Singleton {
private static Singleton instance = new Singleton();
protected Singleton () {}
public static Singleton getInstance () {
return instance;
}
}
C. class Singleton {
Singleton () {}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton ();
}
public static Singleton getInstance () {
return SingletonHolder.INSTANCE;
}
}
D. enum Singleton {
INSTANCE;
}
Answer: A,D
Explanation:
A: Here the method for getting the reference to the SingleTon object is correct.
B: The constructor should be private
C: The constructor should be private
Note: Java has several design patterns Singleton Pattern being the most commonly used.
Java Singletonpattern belongs to the family of design patterns, that govern the instantiation process. This design patternproposes that at any time there can only be one instance of a singleton (object) created by the JVM.
The class's default constructor is made private, which prevents the direct instantiation of the object by others(Other Classes). A static modifier is applied to the instance method that returns the object as it then makes thismethod a class level method that can be accessed without creating an object. OPTION A == SHOW THE LAZY initialization WITHOUT DOUBLE CHECKED LOCKING TECHNIQUE ,BUT ITS CORRECT OPTION D == Serialzation and thraead-safety guaranteed and with couple of line of code enum Singletonpattern is best way to create Singleton in Java 5 world. AND THERE ARE 5 WAY TO CREATE SINGLETON CLASS IN JAVA 1>>LAZY LOADING (initialization) USING SYCHRONIZATION 2>>CLASS LOADING (initialization) USINGprivate static final Singleton instance = new Singleton(); 3>>USING ENUM 4>>USING STATIC NESTED CLASS 5>>USING STATIC BLOCK AND MAKE CONSTRUCTOR PRIVATE IN ALL 5 WAY.
Q8. Given the code fragment:
What is the result of the employees table has no records before the code executed?
A. 1 Sam
B. 4 Jack
C. 3 John 4 Jack
D. 1 Sam 3 John 4 Jack
Answer: C
Explanation:
AutoCommit is set to false. The two following statements will be within the same
transaction.
stmt.executeUpdate("insert into employees values(1,'Sam')");
stmt.executeUpdate("insert into employees values(2,'Jane')");
These two statements are rolled-back through (the savepoint is ignored! the savepoint
must be specified (e.g.
conn.rollback(save1); ) in the rollback if you want to rollback to the savepoint):
conn.rollback() ;
The next two insert statements are executed fine. Their result will be in the output.
Q9. Given: Which two are true?
A. A runtime exception is thrown on line 9.
B. No output is produced.
C. Greeting is printed once.
D. Greeting is printed twice.
E. No new threads of execution are started within the main method.
F. One new thread of execution is started within the main method.
G. Two new threads of execution are started within the main method.
Answer: C,E
Explanation:
Thread t2 is executed. Execution of T2 starts executionen of t1. Greeting is printed during theexecution of t1.
Q10. 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: B
Explanation: The program compiles and runs fine.
At runtime the NameList is built and then sorted by natural Order (String >> alphabetically).