Q1. Given the code fragment:
List<String> listVal = Arrays.asList(“Joe”, “Paul”, “Alice”, “Tom”);
System.out.println (
// line n1
);
Which code fragment, when inserted at line n1, enables the code to print the count of string elements whose length is greater than three?
A. listVal.stream().filter(x -> x.length()>3).count()
B. listVal.stream().map(x -> x.length()>3).count()
C. listVal.stream().peek(x -> x.length()>3).count().get()
D. listVal.stream().filter(x -> x.length()>3).mapToInt(x -> x).count()
Answer: C
Q2. Given:
class RateOfInterest {
public static void main (String[] args) {
int rateOfInterest = 0;
String accountType = “LOAN”;
switch (accountType) {
case “RD”;
rateOfInterest = 5;
break;
case “FD”;
rateOfInterest = 10;
break;
default: assert false: “No interest for this account”; //line n1 } System.out.println (“Rate of interest:” + rateOfInterest); } }
and the command:
java –ea RateOfInterest
What is the result?
A. Rate of interest: 0
B. An AssertionError is thrown.
C. No interest for this account
D. A compilation error occurs at line n1.
Answer: C
Q3. 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
Q4. Given the code fragment:
LocalDate valentinesDay =LocalDate.of(2015, Month.FEBRUARY, 14);
LocalDate nextYear = valentinesDay.plusYears(1);
nextYear.plusDays(15); //line n1
System.out.println(nextYear);
What is the result?
A. 2021-02-14
B. A DateTimeException is thrown.
C. 2021-02-29
D. A compilation error occurs at line n1.
Answer: B
Q5. You want to create a singleton class by using the Singleton design pattern. Which two statements enforce the singleton nature of the design?
A. Make the class static.
B. Make the constructor private.
C. Override equals() and hashCode() methods of the java.lang.Object class.
D. Use a static reference to point to the single instance.
E. Implement the Serializable interface.
Answer: A,B
Q6. View the exhibit.
Given the code fragment:
Which change enables the code to print the following?
James age: 20
Williams age: 32
A. Replacing line 5 with public static void main (String [] args) throws MissingInfoException, AgeOutofRangeException {
B. Replacing line 5 with public static void main (String [] args) throws.Exception {
C. Enclosing line 6 and line 7 within a try block and adding: catch(Exception e1) { //code goes here} catch (missingInfoException e2) { //code goes here} catch (AgeOutofRangeException e3) {//code goes here}
D. Enclosing line 6 and line 7 within a try block and adding: catch (missingInfoException e2) { //code goes here} catch (AgeOutofRangeException e3) {//code goes here}
Answer: C
Q7. Given:
public final class IceCream {
public void prepare() {}
}
public class Cake {
public final void bake(int min, int temp) {}
public void mix() {}
}
public class Shop {
private Cake c = new Cake ();
private final double discount = 0.25;
public void makeReady () { c.bake(10, 120); }
}
public class Bread extends Cake {
public void bake(int minutes, int temperature) {}
public void addToppings() {}
}
Which statement is true?
A. A compilation error occurs in IceCream.
B. A compilation error occurs in Cake.
C. A compilation error occurs in Shop.
D. A compilation error occurs in Bread
E. All classes compile successfully.
Answer: D
Q8. Given the code fragment:
BiFunction<Integer, Double, Integer> val = (t1, t2) -> t1 + t2;//line n1
System.out.println(val.apply(10, 10.5));
What is the result?
A. 20
B. 20.5
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: C
Q9. The protected modifier on a Field declaration within a public class means that the field ______________.
A. Cannot be modified
B. Can be read but not written from outside the class
C. Can be read and written from this class and its subclasses only within the same package
D. Can be read and written from this class and its subclasses defined in any package
Answer: D
Reference:
http://beginnersbook.com/2013/05/java-access-modifiers/
Q10. Given: What is the result?
A. Initialized Started
B. Initialized Started Initialized
C. Compilation fails
D. An exception is thrown at runtime
Answer: B