Q1. Given the code fragment:
What is the result?
A. getName (0): C:\
subpath (0, 2): C:\education\report.txt
B. getName(0): C:\
subpth(0, 2): C:\education
C. getName(0): education
subpath (0, 2): education\institute
D. getName(0): education
subpath(0, 2): education\institute\student
E. getName(0): report.txt
subpath(0, 2): insritute\student
Answer: C
Explanation:
Example:
Path path = Paths.get("C:\\home\\joe\\foo");
getName(0)
-> home
subpath(0,2)
Reference: The Java Tutorial, Path Operations
Q2. Given:
From what threading problem does the program suffer?
A. deadlock
B. livelock
C. starvation D. race condition
Answer: B
Explanation:
A thread often acts in response to the action of another thread. If the other thread's action is also a response tothe action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to makefurther progress.
However, the threads are not blocked -- they are simply too busy responding to each other to resume work. This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking eachother, Alphone moves to his right, while Gaston moves to his left. They'restill blocking each other, so.
Q3. Given these facts about Java classes in an application:
-
Class X is-a Class SuperX.
-
Class SuperX has-a public reference to a Class Z.
-
Class Y invokes public methods in Class Util.
-
Class X uses public variables in Class Util.
Which three statements are true?
A. Class X has-a Class Z.
B. Class Util has weak encapsulation.
C. Class Y demonstrates high cohesion.
D. Class X is loosely coupled to Class Util.
E. Class SuperX's level of cohesion CANNOT be determined
Answer: B,D,E
Explanation:
B: Has class Util has both public methods and variables, it is an example of weak
encapsulation.
Note:Inheritance is also sometimes said to provide "weak encapsulation," because if you
have code thatdirectly uses a subclass, such as Apple, that code can be broken by
changes to a superclass, such as Fruit.
One of the ways to look at inheritance is that it allows subclass code to reuse superclass
code. For example, if
Apple doesn't override a method defined in its superclass
Fruit, Apple is in a sense reusing Fruit's implementation of the method. But Apple only
"weakly encapsulates"the Fruit code it is reusing, because changes to Fruit's interface can
break code that directly uses Apple.
D:
Note:Tight coupling is when a group of classes are highly dependent on one another.
This scenario arises when a class assumes too many responsibilities, or when one concern
is spread overmany classes rather than having its own class.
Loose coupling is achieved by means of a design that promotes single-responsibility and
separation ofconcerns.
A loosely-coupled class can be consumed and tested independently of other (concrete)
classes.
Interfaces are a powerful tool to use for decoupling. Classes can communicate through
interfaces rather thanother concrete classes, and any class can be on the other end of that
communication simply by implementingthe interface.
E: Not enough information regarding SuperX' to determine the level of cohesion.
Q4. Which two are valid initialization statements?
A. Map<String, String> m = new SortedMap<String, String>();
B. Collection m = new TreeMap<Object, Object>();
C. HashMap<Object, Object> m = new SortedMap<Object, Object>();
D. SortedMap<Object, Object> m = new TreeMap<Object, Object> ();
E. Hashtable m= new HashMap();
F. Map<List, ArrayList> m = new Hashtable<List, ArrayList>();
Answer: D,F
Q5. Given the code fragment:
What is the result?
A. M001, ,
B. M001, null, null
C. M001, Sam,
D. M001, Sam, null
E. M001, Sam, ABC Inc (Frage unvolst.ndig!!!)
F. Compilation fails
G. A NullPointerException is thrown at runtime
Answer: E
Q6. Given:
What is the result?
A. fast slow
B. fast goes
C. goes goes
D. fast fast
E. fast followed by an exception
F. Compilation fails
Answer: F
Explanation:
Line:Vehicle v = new Sportscar();
causes compilation failure:
error: cannot find symbol
Vehicle v = new Sportscar();
symbol: class Sportscar
location: class VehicleTest
Q7. Given:
String s = new String("3");
System.out.print(1 + 2 + s + 4 + 5);
What is the result?
A. 12345
B. 3345
C. 1239
D. 339
E. Compilation fails.
Answer: B
Explanation:
1 and 2 are added.
Then the string s is concatenated.
Finally 3 and 4 are concatenated as strings.
Q8. Given:
What is the result?
A. Pastel Enamel Fresco Gouache B. Pastel *Enamel Fresco *Gouache
C. Pastel Enamel Fresco Gouache
D. Pastel Enamel, Fresco Gouache
Answer: B
Explanation:
regex explanation:
, = ,
\ = masks the following
\s = A whitespace character: [ \t \n \x0B \f \r ]
* = Greedy Quantifier: zero or more times Delimiter: comma + zero or more whitespace characters
Q9. Given the code fragment:
What is the result, if the file salesreport.dat does not exist?
A. Compilation fails only at line 6
B. Compilation fails only at line 13
C. Compilation fails at line 6 and 13
D. Class java.io.IOException
E. Class java.io.FileNotFoundException
Answer: B
Explanation:
Compilation fails Line 13 : The resource br of a try-with-resources statement cannot be assignedresources are final in try-with-resources statements
Q10. 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.
Q11. Which two code blocks correctly initialize a Locale variable?
A. Locale loc1 = "UK";
B. Locale loc2 = Locale.getInstance("ru");
C. Locale loc3 = Locale.getLocaleFactory("RU");
D. Locale loc4 = Locale.UK;
E. Locale loc5 = new Locale("ru", "RU");
Answer: D,E
Explanation:
The Locale class provides a number of convenient constants that you can use to create
Locale objects forcommonly used locales.
For example, the following creates a Locale object for the United States:
Locale.US
E: Create a Locale object using the constructors in this class:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
Reference: java.utilClass Locale
Q12. 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 !!
Q13. Given the directory structure that contains three directories: company, Salesdat, and Finance:
Company
-Salesdat
* Target.dat
-Finance
*
Salary.dat
*
Annual.dat
And the code fragment: If Company is the current directory, what is the result?
A. Prints only Annual.dat
B. Prints only Salesdat, Annual.dat
C. Prints only Annual.dat, Salary.dat, Target.dat
D. Prints at least Salesdat, Annual.dat, Salary.dat, Target.dat
Answer: A
Explanation:
IF !! return FileVisitResult.CONTINUE;
The pattern *dat will match the directory name Salesdat and it will also match the file
Annual.dat.
It will not be matched to Target.dat which is in a subdirectory.
Q14. Given:
What is the result?
A. Nice to see you,be fine
B. Nice,see you,be fine
C. Nice,see you, to, be fine
D. Nice, see you, be fine
E. Nice to see y, u, be fine
Answer: A
Explanation:
The text ",to," is replaced by the ","
Q15. Which four are syntactically correct?
A. package abc; package def; import Java.util . * ; public class Test { }
B. package abc; import Java.util.*; import Java.util.regex.* ; public class Test { }
C. package abc; public class Test {} import Java.util.* ;
D. import Java.util.*; package abc; public class Test {}
E. package abc; import java.util. *; public class Test{}
F. public class Test{} package abc; import java.util.*{}
G. import java.util.*; public class Test{}
H. package abc; public class test {}
Answer: B,E,G,H