Missed Questions Flashcards
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
ExecutorService
Executors
Callable
Future
Which of the following method(s) of java.util.stream.Stream interface is/are used for reduction?
collect count distinct findAny findFirst
collect
count
findAny and findFirst are terminal operations
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);
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
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);
Map» and Book::getGenre, Collectors.groupingBy(Book::getAuthor)
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));
3
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));
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
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); } }
13 13 3.234567
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));
- List> list = new ArrayList<>(Arrays.asList(names));
- List list = new ArrayList<>(Arrays.asList(names));
- List> list = new ArrayList>(Arrays.asList(names));
- List<> list = new ArrayList(Arrays.asList(names));
1,2
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);
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.
Which of the following options correctly create an ExecutorService instance?
- ExecutorService es = ExecutorService.getInstance();
- ExecutorService es = new ExecutorService();
- ExecutorService es = Executors.newFixedThreadPool(2);
- ExecutorService es = Executor.getSingleThreadExecutor();
- ExecutorService es = Executors.getSingleThreadExecutor();
3
Date d = new Date();
DateFormat //1 INSERT CODE HERE
String s = //2 INSERT CODE HERE
System.out.println(s);
df = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.UK);
df.format(d);
Which of the following options correctly create an ExecutorService instance?
- ExecutorService es = ExecutorService.getInstance();
- ExecutorService es = new ExecutorService();
- ExecutorService es = Executors.newFixedThreadPool(2);
- ExecutorService es = Executor.getSingleThreadExecutor();
- ExecutorService es = Executors.getSingleThreadExecutor();
3: ExecutorService es = Executors.newFixedThreadPool(2);
What can be added at line 1? public class Counter{ static AtomicInteger ai = new AtomicInteger(0); public static void increment(){ //1 } //other valid code }
- ai.increment();
- ai.incrementAndGet();
- ai.set(ai.get() + 1);
- ai.addAndGet(1);
- ai.add(1);
- ai++
2, 4
Name the constructors for a CyclicBarrier and their uses.
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.
What is the difference between a RecursiveAction and RecursiveTask?
A RecursiveTask does yield a return value while a RecursiveAction does not.