Java SE 8 Programmer II

Here you have the best Oracle 1z0-809 practice exam questions

  • You have 248 total questions to study from
  • Each page has 5 questions, making a total of 50 pages
  • You can navigate through the pages using the buttons at the bottom
  • This questions were last updated on December 15, 2024
Question 1 of 248

Given the definition of the Vehicle class:

Class Vehicle {

int distance;

Vehicle (int x) {

this distance = x;

}

public void increSpeed(int time) {

int timeTravel = time; //line n1

//line n3

class Car {

int value = 0;

public void speed () {

value = distance /timeTravel; //line n2

System.out.println ("Velocity with new speed"+value+"kmph");

}

}

speed(); //line n3

}

}

and this code fragment:

Vehicle v = new Vehicle (100);

v.increSpeed(60);

What is the result?

    Correct Answer: D

    The code will result in a compilation error at line n3. The method speed() is being called without an instance of the nested class Car. To call the speed() method correctly, you need to create an instance of the Car class and then call the method using that instance. Therefore, the correct code would be new Car().speed() at line n3. As it stands, the call to speed() at line n3 is incorrect and will cause a compilation error.

Question 2 of 248

Given:

IntStream stream = IntStream.of (1,2,3);

IntFunction inFu= x -> y -> x*y; //line n1

IntStream newStream = stream.map(inFu.apply(10)); //line n2 newStream.forEach(System.output::print);

Which modification enables the code fragment to compile?

    Correct Answer: B

    To ensure the code fragment compiles correctly, line n1 should be replaced with 'IntFunction<IntUnaryOperator> inFu = x -> y -> x * y;'. This defines 'inFu' as a function that takes an integer and returns an 'IntUnaryOperator'. The 'IntUnaryOperator' is a functional interface that takes an int and returns an int, which matches the expected behavior for the 'map' method in the IntStream class.

Question 3 of 248

Given the code fragment:

List values = Arrays.asList (1, 2, 3);

values.stream ()

.map(n -> n*2) //line n1

.peek(System.out::print) //line n2

.count();

What is the result?

    Correct Answer: B

    The code produces no output. The count() method is a terminal operation that returns the number of elements in the stream. The peek() method is meant to support debugging by performing the action provided on each element as elements are consumed from the resulting stream. However, in this case, since count() is used and it does not consume the stream elements in a way that executes peek(), the peek() operation does not produce any output.

Question 4 of 248

Given the code fragment:

public class Foo {

public static void main (String [ ] args) {

Map unsortMap = new HashMap< > ( );

unsortMap.put (10, "z");

unsortMap.put (5, "b");

unsortMap.put (1, "d");

unsortMap.put (7, "e");

unsortMap.put (50, "j");

Map treeMap = new TreeMap (new

Comparator ( ) {

@Override public int compare (Integer o1, Integer o2) {return o2.compareTo

(o1); } } );

treeMap.putAll (unsortMap);

for (Map.Entry entry : treeMap.entrySet () ) {

System.out.print (entry.getValue () + " ");

}

}

}

What is the result?

    Correct Answer: C

    The code defines a TreeMap with a custom Comparator that sorts the keys in descending order by returning o2.compareTo(o1) in the compare method. When the unsorted HashMap entries are inserted into the TreeMap, they are sorted in descending order based on their keys. The keys in descending order are 50, 10, 7, 5, and 1, corresponding to the values 'j', 'z', 'e', 'b', and 'd', respectively. Thus, the output will be 'j z e b d'.

Question 5 of 248

Which two reasons should you use interfaces instead of abstract classes? (Choose two.)

    Correct Answer: B, E

    When choosing between interfaces and abstract classes, it's important to understand their purposes. Interfaces are ideal when you expect that unrelated classes would implement the interfaces, as they provide a way to specify a set of methods that must be implemented without dictating how the methods should be used (Option B). Additionally, interfaces are suitable when you want to take advantage of multiple inheritance of type, since a class can implement multiple interfaces but can only extend one abstract class (Option E). These two scenarios highlight the flexibility and design patterns that interfaces offer over abstract classes.