Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
class AlgorithmTest {
    public static int findFirst(List<T> list, int begin, int end, UnaryPredicate<T> p) {
        for (; begin < end; ++begin)
            if (p.test(list.get(begin)))
                return begin;
        return -1;
    }
    public static int highestCommomFactor(int x, int y) {
        for (int r; (r = x % y) != 0; x = y, y = r) {}
        return y;
    }
}
interface UnaryPredicate<T> {
    public boolean test(T t);
}
class RelativelyPrimePredicate implements UnaryPredicate<Integer> {
    public RelativelyPrimePredicate(Collection<Integer> c) {
        this.c = c;
    }
    public boolean test(Integer x) {
        for (Integer i : c)
            if (AlgorithmTest.highestCommomFactor(x, i) != 1)
                return false;
        return c.size() > 0;
    }
    private final Collection<Integer> c;
}
public class Algorithm {
    public static void main(String[] args) {
        List<Integer> li = Arrays.asList(3, 4, 6, 8, 11, 15, 28, 32);
        Collection<Integer> c = Arrays.asList(7, 18, 19, 25);
        UnaryPredicate<Integer> p = new RelativelyPrimePredicate(c);
        System.out.print("Output is: " + li.get(AlgorithmTest.findFirst(li, 0, li.size(), p)));
    }
}


Output is: 11
Output is: -1
Compilation Error
Runtime Error

Doubts

Problems

Topic: java.util.Arrays - Class Arrays In Collection Framework

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App