Q1. Given:
public class Emp {
String fName;
String lName;
public Emp (String fn, String ln) {
fName = fn;
lName = ln;
}
public String getfName() { return fName; }
public String getlName() { return lName; }
}
and the code fragment:
List<Emp> emp = Arrays.asList (
new Emp (“John”, “Smith”),
new Emp (“Peter”, “Sam”),
new Emp (“Thomas”, “Wale”));
emp.stream()
//line n1
.collect(Collectors.toList());
Which code fragment, when inserted at line n1, sorts the employees list in descending order of fName and then ascending order of lName?
A. .sorted (Comparator.comparing(Emp::getfName).reserved().thenComparing(Emp::getlName))
B. .sorted (Comparator.comparing(Emp::getfName).thenComparing(Emp::getlName))
C. .map(Emp::getfName).sorted(Comparator.reserveOrder())
D.
.map(Emp::getfName).sorted(Comparator.reserveOrder().map(Emp::getlName).reserved
Answer: A
Q2. 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
Q3. Given the code fragment
Which code fragments, inserted independently, enable the code compile?
A. t.fvar = 200;
B. cvar = 400;
C. fvar = 200; cvar = 400;
D. this.fvar = 200; this.cvar = 400;
E. t.fvar = 200; Test2.cvar = 400;
F. this.fvar = 200;
Test2.cvar = 400;
Answer: B
Q4. 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
Q5. Given the code fragment:
What is the result?
A. Found Red Found Default
B. Found Teal
C. Found Red Found Blue Found Teal
D. Found Red Found Blue Found Teal Found Default
E. Found Default
Answer: B
Q6. Given:
public class Counter {
public static void main (String[ ] args) {
int a = 10;
int b = -1;
assert (b >=1) : “Invalid Denominator”;
int = a / b;
System.out.println (c);
}
}
What is the result of running the code with the –ea option?
A. -10
B. 0
C. An AssertionError is thrown.
D. A compilation error occurs.
Answer: B
Q7. Given the definition of the Country class:
public class country {
public enum Continent {ASIA, EUROPE}
String name;
Continent region;
public Country (String na, Continent reg) {
name = na, region = reg;
}
public String getName () {return name;}
public Continent getRegion () {return region;}
}
and the code fragment:
List<Country> couList = Arrays.asList (
new Country (“Japan”, Country.Continent.ASIA),
new Country (“Italy”, Country.Continent.EUROPE),
new Country (“Germany”, Country.Continent.EUROPE)); Map<Country.Continent, List<String>> regionNames = couList.stream () .collect(Collectors.groupingBy (Country ::getRegion, Collectors.mapping(Country::getName, Collectors.toList())))); System.out.println(regionNames);
What is the output?
A. {EUROPE = [Italy, Germany], ASIA = [Japan]}
B. {ASIA = [Japan], EUROPE = [Italy, Germany]}
C. {EUROPE = [Germany, Italy], ASIA = [Japan]}
D. {EUROPE = [Germany], EUROPE = [Italy], ASIA = [Japan]}
Answer: A
Q8. 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
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:
public class ScopeTest {
int j, int k;
public static void main(String[] args) {
ew ScopeTest().doStuff(); }
void doStuff() {
nt x = 5;
oStuff2();
System.out.println("x");
}
void doStuff2() {
nt y = 7;
ystem.out.println("y");
or (int z = 0; z < 5; z++) {
ystem.out.println("z");
ystem.out.println("y");
}
Which two items are fields?
A. j
B. k
C. x
D. y
E. z
Answer: A,B
Q11. Given the code fragments:
class Employee {
Optional<Address> address;
Employee (Optional<Address> address) {
this.address = address;
}
public Optional<Address> getAddress() { return address; }
}
class Address {
String city = “New York”;
public String getCity { return city: }
public String toString() {
return city;
}
}
and
Address address = null;
Optional<Address> addrs1 = Optional.ofNullable (address);
Employee e1 = new Employee (addrs1);
String eAddress = (addrs1.isPresent()) ? addrs1.get().getCity() : “City Not
available”;
What is the result?
A. New York
B. City Not available
C. null
D. A NoSuchElementException is thrown at run time.
Answer: C
Q12. The data.doc, data.txt and data.xml files are accessible and contain text.
Given the code fragment:
Stream<Path> paths = Stream.of (Paths. get(“data.doc”),
Paths. get(“data.txt”),
Paths. get(“data.xml”));
paths.filter(s-> s.toString().endWith(“txt”)).forEach(
s -> {
try {
Files.readAllLines(s)
.stream()
.forEach(System.out::println); //line n1
} catch (IOException e) {
System.out.println(“Exception”);
}
}
);
What is the result?
A. The program prints the content of data.txt file.
B. The program prints:
Exception
<<The content of the data.txt file>>
Exception
C. A compilation error occurs at line n1.
D. The program prints the content of the three files.
Answer: D
Q13. Given:
What is the result?
A. 2 4 6 8
B. 2 4 6 8 9
C. 1 3 5 7
D. 1 3 5 7 9
Answer: D
Q14. Given the code fragments:
public class Book implements Comparator<Book> {
String name;
double price;
public Book () {}
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public int compare(Book b1, Book b2) {
return b1.name.compareTo(b2.name);
}
public String toString() {
return name + “:” + price;
}
}
and
List<Book>books = Arrays.asList (new Book (“Beginning with Java”, 2), new book (“A
Guide to Java Tour”, 3));
Collections.sort(books, new Book());
System.out.print(books);
What is the result?
A. [A Guide to Java Tour:3, Beginning with Java:2]
B. [Beginning with Java:2, A Guide to Java Tour:3]
C. A compilation error occurs because the Book class does not override the abstract method compareTo().
D. An Exception is thrown at run time.
Answer: A
Q15. Given: A. ns = 50 S = 125 ns = 125 S = 125 ns = 100 S = 125
B. ns = 50 S = 125 ns = 125 S = 125 ns = 0 S = 125
C. ns = 50 S = 50 ns = 125 S = 125 ns = 100 S = 100
D. ns = 50 S = 50 ns = 125 S = 125 ns = 0 S = 125
Answer: B