Missed Questions Flashcards

1
Q

You want to execute a task that returns a result without blocking. Which of the following types from java.util.concurrent package will be required to achieve this?

Executor
Executors
ExecutorService
Callable
Future
A

ExecutorService
Executors
Callable
Future

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following method(s) of java.util.stream.Stream interface is/are used for reduction?

collect
count
distinct
findAny
findFirst
A

collect
count

findAny and findFirst are terminal operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What will the following code print?  
ReentrantLock rlock = new ReentrantLock(); 
boolean f1 = rlock.lock(); 
System.out.println(f1); 
boolean f2 = rlock.lock(); 
System.out.println(f2);
A
It will not compile
Lock.lock() returns void. Lock.tryLock() returns boolean. Had the code been: 
ReentrantLock rlock = new ReentrantLock();
boolean f1 = rlock.tryLock(); 
System.out.println(f1); 
boolean f2 = rlock.tryLock(); 
System.out.println(f2); 
It would have printed: true true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Assuming that books is a List of Book objects, what can be inserted in the code below at DECLARATION and EXPRESSION so that it will classify the books by genre and then also by author?
DECLARATION classified = null;
classified = books.stream().collect(Collectors.groupingBy( EXPRESSION
));
System.out.println(classified);

A

Map» and Book::getGenre, Collectors.groupingBy(Book::getAuthor)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Assume that dt refers to a valid java.util.Date object and that df is a reference variable of class DateFormat.
Which of the following code fragments will print the country and the date in the correct local format?

1.
Locale l = Locale.getDefault(); DateFormat df = DateFormat.getDateInstance(l); System.out.println(l.getCountry()+” “+ df.format(dt));
2.
Locale l = Locale.getDefault(); DateFormat df = DateFormat.getDateInstance(); System.out.println(l.getCountry()+” “+ df.format(dt, l));
3.
Locale l = Locale.getDefault(); DateFormat df = DateFormat.getDateInstance(); System.out.println(l.getCountry()+” “+ df.format(dt));
4.
Locale l = new Locale(); DateFormat df = DateFormat.getDateInstance(); System.out.println(l.getCountry()+” “+ df.format(dt));

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given that Book is a valid class with appropriate constructor and getTitle and getPrice methods that return a String and a Double respectively, what will the following code print?

List books = Arrays.asList( 
new Book("Gone with the wind", 5.0), 
new Book("Gone with the wind", 10.0), 
new Book("Atlas Shrugged", 15.0) 
); 

books.stream().collect(Collectors.toMap((b->b.getTitle()), b->b.getPrice()))
.forEach((a, b)->System.out.println(a+” “+b));

A

Exception at runtime.

The Collector created by Collectors.toMap throws java.lang.IllegalStateException if an attempt is made to store a key that already exists in the Map. If you want to collect items in a Map and if you expect duplicate entries in the source, you should use Collectors.toMap(Function, Function, BinaryOperator) method. The third parameter is used to merge the duplicate entries to produce one entry. For example, in this case, you can do: Collectors.toMap(b->b.getTitle(), b->b.getPrice(), (v1, v2)->v1+v2) This Collector will sum the values of the entries that have the same key. Therefore, it will print : Gone with the wind 15.0 Atlas Shrugged 15.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is the result:
public class TestClass { 
public static void main(String[] args) {  
int x = 1\_\_\_\_3;//1  
long y = 1_3;//2  
float z = 3.234_567f; //3  
System.out.println(x+" "+y+" "+z); 
}
}
A

13 13 3.234567

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What can be inserted at line 2 in the code snippet given below?  
String[] names = {"Alex", "Bob", "Charlie" }; 
//Insert code here 
System.out.println(list.get(0));
  1. List> list = new ArrayList<>(Arrays.asList(names));
  2. List list = new ArrayList<>(Arrays.asList(names));
  3. List> list = new ArrayList>(Arrays.asList(names));
  4. List<> list = new ArrayList(Arrays.asList(names));
A

1,2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will the following code snippet print? Stream strm1 = Stream.of(2, 3, 5, 7, 11, 13);
double av = strm1.filter(x->{ if(x>10)return true; else return false;}) //1
.peek() //2
.collect(Collectors.averagingInt(y->y)); //3 System.out.println(av);

A

Compilation failure due to code at //2

peek expects a Consumer object as an argument. Calling peek() without any argument will, therefore, fail to compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following options correctly create an ExecutorService instance?

  1. ExecutorService es = ExecutorService.getInstance();
  2. ExecutorService es = new ExecutorService();
  3. ExecutorService es = Executors.newFixedThreadPool(2);
  4. ExecutorService es = Executor.getSingleThreadExecutor();
  5. ExecutorService es = Executors.getSingleThreadExecutor();
A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Date d = new Date();
DateFormat //1 INSERT CODE HERE
String s = //2 INSERT CODE HERE
System.out.println(s);

A

df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.UK);
df.format(d);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following options correctly create an ExecutorService instance?

  1. ExecutorService es = ExecutorService.getInstance();
  2. ExecutorService es = new ExecutorService();
  3. ExecutorService es = Executors.newFixedThreadPool(2);
  4. ExecutorService es = Executor.getSingleThreadExecutor();
  5. ExecutorService es = Executors.getSingleThreadExecutor();
A

3: ExecutorService es = Executors.newFixedThreadPool(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What can be added at line 1?
public class Counter{ 
   static AtomicInteger ai = new AtomicInteger(0); 
   public static void increment(){ //1 } 
   //other valid code 
}
  1. ai.increment();
  2. ai.incrementAndGet();
  3. ai.set(ai.get() + 1);
  4. ai.addAndGet(1);
  5. ai.add(1);
  6. ai++
A

2, 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Name the constructors for a CyclicBarrier and their uses.

A

CyclicBarrier(int parties);
CyclicBarrier(int parties, Runnable barrierAction);

Make all parties (threads) wait at a certain point (barrier). When the last party arrives a optional barrierAction is executed before the threads are released.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between a RecursiveAction and RecursiveTask?

A

A RecursiveTask does yield a return value while a RecursiveAction does not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly