Q1. 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
Q2. Given:
class Student {
String course, name, city;
public Student (String name, String course, String city) {
this.course = course; this.name = name; this.city = city;
}
public String toString() {
return course + “:” + name + “:” + city;
}
and the code fragment:
List<Student> stds = Arrays.asList(
new Student (“Jessy”, “Java ME”, “Chicago”),
new Student (“Helen”, “Java EE”, “Houston”),
new Student (“Mark”, “Java ME”, “Chicago”));
stds.stream()
.collect(Collectors.groupingBy(Student::getCourse))
.forEach(src, res) -> System.out.println(scr));
What is the result?
A. [Java EE: Helen:Houston]
[Java ME: Jessy:Chicago, Java ME: Mark:Chicago]
B. Java EE
Java ME
C. [Java ME: Jessy:Chicago, Java ME: Mark:Chicago]
[Java EE: Helen:Houston]
D. A compilation error occurs.
Answer: C
Q3. Which three statements are benefits of encapsulation?
A. Allows a class implementation to change without changing t he clients
B. Protects confidential data from leaking out of the objects
C. Prevents code from causing exceptions
D. Enables the class implementation to protect its invariants
E. Permits classes to be combined into the same package
F. Enables multiple instances of the same class to be created safely
Answer: A,B,D
Q4. Given the code fragment:
public class FileThread implements Runnable {
String fName;
public FileThread(String fName) { this.fName = fName; }
public void run () System.out.println(fName);}
public static void main (String[] args) throws IOException, InterruptedException {
ExecutorService executor = Executors.newCachedThreadPool();
Stream<Path> listOfFiles = Files.walk(Paths.get(“Java Projects”));
listOfFiles.forEach(line -> {
executor.execute(new FileThread(line.getFileName().toString())); //
line n1
});
executor.shutdown();
executor.awaitTermination(5, TimeUnit.DAYS);//
line n2
}
}
The Java Projects directory exists and contains a list of files.
What is the result?
A. The program throws a runtime exception at line n2.
B. The program prints files names concurrently.
C. The program prints files names sequentially.
D. A compilation error occurs at line n1.
Answer: A
Q5. Given the code fragment:
Stream<Path> files = Files.walk(Paths.get(System.getProperty(“user.home”)));
files.forEach (fName -> {//line n1
try {
Path aPath = fName.toAbsolutePath();//line n2
System.out.println(fName + “:”
+ Files.readAttributes(aPath, Basic.File.Attributes.class).creationTime
());
} catch (IOException ex) {
ex.printStackTrace();
});
What is the result?
A. All files and directories under the home directory are listed along with their attributes.
B. A compilation error occurs at line n1.
C. The files in the home directory are listed along with their attributes.
D. A compilation error occurs at line n2.
Answer: A
Q6. Given the code fragment:
Path source = Paths.get (“/data/december/log.txt”);
Path destination = Paths.get(“/data”);
Files.copy (source, destination);
and assuming that the file /data/december/log.txt is accessible and contains:
10-Dec-2014 – Executed successfully
What is the result?
A. A file with the name log.txt is created in the /data directory and the content of the /data/december/log.txt file is copied to it.
B. The program executes successfully and does NOT change the file system.
C. A FileNotFoundException is thrown at run time.
D. A FileAlreadyExistsException is thrown at run time.
Answer: B
Q7. Given: What is the result?
A. Marrown String out of limits JesOran
B. Marrown String out of limits Array out of limits
C. Marrown String out of limits
D. Marrown NanRed JesOran
Answer: A
Q8. Given the code fragments:
class MyThread implements Runnable {
private static AtomicInteger count = new AtomicInteger (0);
public void run () {
int x = count.incrementAndGet();
System.out.print (x+” “);
}
}
and
Thread thread1 = new Thread(new MyThread());
Thread thread2 = new Thread(new MyThread());
Thread thread3 = new Thread(new MyThread());
Thread [] ta = {thread1, thread2, thread3};
for (int x= 0; x < 3; x++) {
ta[x].start();
}
Which statement is true?
A. The program prints 1 2 3 and the order is unpredictable.
B. The program prints 1 2 3.
C. The program prints 1 1 1.
D. A compilation error occurs.
Answer: B
Q9. Given the fragments:
Which line causes a compilation error?
A. Line n1
B. Line n2
C. Line n3
D. Line n4
Answer: A
Q10. 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