Q1. Given:
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicCounter {
private AtomicInteger c = new AtomicInteger(0);
public void increment() {
// insert code here
}
}
Which line of code, inserted inside the increment () method, will increment the value of c?
A. c.addAndGet();
B. c++;
C. c = c+1;
D. c.getAndIncrement ();
Answer: D
Explanation: getAndIncrement
public final int getAndIncrement()
Atomically increment by one the current value.
Reference:java.util.concurrent.atomic
Q2. Given:
ConcurrentMap <String, String> PartList = new ConcurrentMap<>();
Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?
A. partList.out(key,"Blue Shirt");
B. partList.putIfAbsent(key,"Blue Shirt");
C. partList.putIfNotLocked (key,"Blue Shirt");
D. partList.putAtomic(key,"Blue Shirt")
E. if (!partList.containsKey(key)) partList.put (key,"Blue Shirt");
Answer: B
Explanation:
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value.
Reference:java.util.concurrent,Interface ConcurrentMap<K,V>
Q3. 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)
Q4. 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
Q5. Sam has designed an application. It segregates tasks that are critical and executed frequently from tasks thatare non critical and executed less frequently. He has prioritized these tasks based on their criticality andfrequency of execution. After close scrutiny, he finds that the tasks designed to be non critical are rarely gettingexecuted.
From what kind of problem is the application suffering?
A. race condition
B. starvation
C. deadlock
D. livelock
Answer: C
Explanation:
Starvation describes a situation where a thread is unable to gain regular access to sharedresources and is unable to make progress. This happens when shared resources are made unavailable forlong periods by "greedy" threads. For example, suppose an object provides a synchronized method that oftentakes a long time to return. If one thread invokes
this method frequently, other threads that also need frequentsynchronized access to the same object will often be blocked. Reference: The Java Tutorial, Starvation and Livelock
Q6. Given the code fragment: What is the result, if the file myfile.txt does not exist?
A. A runtime exception is thrown at line 4
B. A runtime exception is thrown at line 7
C. Creates a new file and prints no output
D. Compilation fails
Answer: D
Explanation:
!! Compilation fails if FileNotFoundException is tried to catch (Line 12)
(The exception FileNotFoundException is already caught by the alternative IOException)
if this is removed will be thrown a FileNotFoundException at line 4.
Q7. 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.
Q8. Given the class?
What is the result?
A. Jane Doe John Doe Joe Shmoe
B. John Doe Jane Doe Joe Shmoe
C. Joe Shmoe John Doe Jane Doe
D. Joe Shmoe Jane Doe John Doe
E. Jane Doe Joe Shmoe John Doe
F. John Doe Joe Shmoe Jane Doe
Answer: A
Explanation: The list will be sorted alphabetically (Lastname / Firstname). first sorted by Lastname if Lastname equals, sorted by firstname Output will be: Jane Doe John Doe Joe Shmoe
Q9. Given:
Which two classes correctly override the getDepth method?
A. public class deep extends Deeper {
protected Integer getDepth(){
return 5;
}}
B. public class deep extends Deeper {
public Double getDepth() {
return 5d;
}}
C. public class deep extends Deeper {
public String getDepth () {
}}
D. public class deep extends Deeper {
public Long getDepth (int d) {
return 5L;
}}
E. public class deep extends Deeper {
public Short getDepth () {
return 5;
}}
Answer: A,E
Explanation:
Note: The abstract class Number is the superclass of classes Byte, Double, Float, Integer, Long, and Short.
Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.
When class C extends B, we say that C is a "subclass" of B, and B is the "superclass" of C. This is called inheritence, because C inherited from B.
Q10. Given:
What is the result?
A. riding riding tolting
B. riding riding cantering
C. tolting cantering tolting
D. tolting cantering cantering
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E
Explanation:
The compilation fails at:
interface Rideable {
public String ride() { return "riding ";}
}
Error due to: interface methods cannot have body.