Q1. Given the class definitions:
And the code fragment of the main() method,
What is the result?
A. Java Java Java
B. Java Jeve va
C. Java Jeve ve
D. Compilation fails
Answer: D
Q2. Given:
class Vehicle {
int vno;
String name;
public Vehicle (int vno, String name) {
this.vno = vno,;
this.name = name;
}
public String toString () {
return vno + “:” + name;
}
}
and this code fragment:
Set<Vehicle> vehicles = new TreeSet <> ();
vehicles.add(new Vehicle (10123, “Ford”));
vehicles.add(new Vehicle (10124, “BMW”));
System.out.println(vehicles);
What is the result?
A. 10123 Ford 10124 BMW
B. 10124 BMW 10123 Ford
C. A compilation error occurs.
D. A ClassCastException is thrown at run time.
Answer: B
Q3. Which action can be used to load a database driver by using JDBC3.0?
A. Add the driver class to the META-INF/services folder of the JAR file.
B. Include the JDBC driver class in a jdbc.properties file.
C. Use the java.lang.Class.forName method to load the driver class.
D. Use the DriverManager.getDriver method to load the driver class.
Answer: D
Q4. Given the code fragment:
List<String> nL = Arrays.asList(“Jim”, “John”, “Jeff”);
Function<String, String> funVal = s -> “Hello : “.contact(s);
nL.Stream()
.map(funVal)
.peek(System.out::print);
What is the result?
A. Hello : Jim Hello : John Hello : Jeff
B. Jim John Jeff
C. The program prints nothing.
D. A compilation error occurs.
Answer: D
Q5. Given the code fragment:
Path file = Paths.get (“courses.txt”); // line n1
Assume the courses.txt is accessible.
Which code fragment can be inserted at line n1 to enable the code to print the content of
the courses.txt file?
A. List<String> fc = Files.list(file); fc.stream().forEach (s - > System.out.println(s));
B. Stream<String> fc = Files.readAllLines (file); fc.forEach (s - > System.out.println(s));
C. List<String> fc = readAllLines(file); fc.stream().forEach (s - > System.out.println(s));
D. Stream<String> fc = Files.lines (file); fc.forEach (s - > System.out.println(s));
Answer: B
Q6. Given:
interface Rideable {Car getCar (String name); }
class Car {
private String name;
public Car (String name) {
this.name = name;
}
}
Which code fragment creates an instance of Car?
A. Car auto = Car (“MyCar”): : new;
B. Car auto = Car : : new;
Car vehicle = auto : : getCar(“MyCar”);
C. Rideable rider = Car : : new;
Car vehicle = rider.getCar(“MyCar”);
D. Car vehicle = Rideable : : new : : getCar(“MyCar”);
Answer: C
Q7. Given the code fragments:
interface CourseFilter extends Predicate<String> {
public default boolean test (String str) {
return str.equals (“Java”);
}
}
and
List<String> strs = Arrays.asList(“Java”, “Java EE”, “Java ME”);
Predicate<String> cf1 = s - > s.length() > 3;
Predicate cf2 = new CourseFilter() { //line n1
public boolean test (String s) {
return s.contains (“Java”);
}
};
long c = strs.stream()
.filter(cf1)
.filter(cf2//line n2
.count();
System.out.println(c);
What is the result?
A. 2
B. 3
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: A
Q8. Given:
public interface Moveable<Integer> {
public default void walk (Integer distance) {System.out.println(“Walking”);)
public void run(Integer distance);
}
Which statement is true?
A. Moveable can be used as below:
Moveable<Integer> animal = n - > System.out.println(“Running” + n);
animal.run(100);
animal.walk(20);
B. Moveable can be used as below:
Moveable<Integer> animal = n - > n + 10;
animal.run(100);
animal.walk(20);
C. Moveable can be used as below:
Moveable animal = (Integer n) - > System.out.println(n);
animal.run(100);
Moveable.walk(20);
D. Movable cannot be used in a lambda expression.
Answer: B
Q9. Given:
interface Doable {
public void doSomething (String s);
}
Which two class definitions compile?
A. public abstract class Task implements Doable {
public void doSomethingElse(String s) { }
}
B. public abstract class Work implements Doable {
public abstract void doSomething(String s) { }
public void doYourThing(Boolean b) { }
}
C. public class Job implements Doable {
public void doSomething(Integer i) { }
}
D. public class Action implements Doable {
public void doSomething(Integer i) { }
public String doThis(Integer j) { }
}
E. public class Do implements Doable {
public void doSomething(Integer i) { }
public void doSomething(String s) { }
public void doThat (String s) { }
}
Answer: C,D
Q10. Given the code fragment:
List<Integer> codes = Arrays.asList (10, 20);
UnaryOperator<Double> uo = s -> s +10.0;
codes.replaceAll(uo);
codes.forEach(c -> System.out.println(c));
What is the result?
A. 20.0
30.0
B. 10 20
C. A compilation error occurs.
D. A NumberFormatException is thrown at run time.
Answer: A