Q1. public class StringReplace {
public static void main(String[] args) {
String message = "Hi everyone!";
System.out.println("message = " + message.replace("e", "X")); }
}
What is the result?
A. message = Hi everyone!
B. message = Hi XvXryonX!
C. A compile time error is produced.
D. A runtime error is produced.
E. message =
F. message = Hi Xveryone!
Answer: B
Q2. Given the definition of the Emp class:
public class Emp
private String eName;
private Integer eAge;
Emp(String eN, Integer eA) {
this.eName = eN;
this.eAge = eA;
}
public Integer getEAge () {return eAge;}
public String getEName () {return eName;}
}
and code fragment:
List<Emp>li = Arrays.asList(new Emp(“Sam”, 20), New Emp(“John”, 60), New Emp(“Jim”,
51));
Predicate<Emp> agVal = s -> s.getEAge() > 50;//line n1
li = li.stream().filter(agVal).collect(Collectors.toList());
Stream<String> names = li.stream()map.(Emp::getEName);//line n2
names.forEach(n -> System.out.print(n + “ “));
What is the result?
A. Sam John Jim
B. John Jim
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: A
Q3. Given:
class ImageScanner implements AutoCloseable {
public void close () throws Exception {
System.out.print (“Scanner closed.”);
}
public void scanImage () throws Exception {
System.out.print (“Scan.”);
throw new Exception(“Unable to scan.”);
}
}
class ImagePrinter implements AutoCloseable {
public void close () throws Exception {
System.out.print (“Printer closed.”);
}
public void printImage () {System.out.print(“Print.”); }
}
and this code fragment:
try (ImageScanner ir = new ImageScanner();
ImagePrinter iw = new ImagePrinter()) {
ir.scanImage();
iw.printImage();
} catch (Exception e) {
System.out.print(e.getMessage());
}
What is the result?
A. Scan.Printer closed. Scanner closed. Unable to scan.
B. Scan.Scanner closed. Unable to scan.
C. Scan. Unable to scan.
D. Scan. Unable to scan. Printer closed.
Answer: B
Q4. Given the code fragment:
Map<Integer, String> books = new TreeMap<>();
books.put (1007, “A”);
books.put (1002, “C”);
books.put (1001, “B”);
books.put (1003, “B”);
System.out.println (books);
What is the result?
A. {1007 = A, 1002 = C, 1001 = B, 1003 = B}
B. {1001 = B, 1002 = C, 1003 = B, 1007 = A}
C. {1002 = C, 1003 = B, 1007 = A}
D. {1007 = A, 1001 = B, 1003 = B, 1002 = C}
Answer: D
Q5. 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
Q6. Given the for loop construct:
for ( expr1 ; expr2 ; expr3 ) {
statement;
}
Which two statements are true?
A. This is not the only valid for loop construct; there exits another form of for loop constructor.
B. The expression expr1 is optional. it initializes the loop and is evaluated once, as the loop begin.
C. When expr2 evaluates to false, the loop terminates. It is evaluated only after each iteration through the loop.
D. The expression expr3 must be present. It is evaluated after each iteration through the loop.
Answer: B,C
Explanation:
The for statement have this forms:
for (init-stmt; condition; next-stmt) {
body
}
There are three clauses in the for statement.
The init-stmt statement is done before the loop is started, usually to initialize an iteration
variable.
The condition expression is tested before each time the loop is done. The loop isn't
executed if the boolean expression is false (the same as the while loop).
The next-stmt statement is done after the body is executed. It typically increments an
iteration variable.
Q7. Given:
What is the result?
A. The sum is 2
B. The sum is 14
C. The sum is 15
D. The loop executes infinite times
E. Compilation fails
Answer: E
Q8. Given the code fragment:
List<Integer> list1 = Arrays.asList(10, 20);
List<Integer> list2 = Arrays.asList(15, 30);
//line n1
Which code fragment, when inserted at line n1, prints 10 20 15 30?
A. Stream.of(list1, list2)
.flatMap(list -> list.stream())
.forEach(s -> System.out.print(s + “ “));
B. Stream.of(list1, list2)
.flatMap(list -> list.intStream())
.forEach(s -> System.out.print(s + “ “));
C. list1.stream()
.flatMap(list2.stream().flatMap(e1 -> e1.stream())
.forEach(s -> System.out.println(s + “ “));
D. Stream.of(list1, list2)
.flatMapToInt(list -> list.stream())
.forEach(s -> System.out.print(s + “ “));
Answer: C
Q9. 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
Q10. Given the code fragment:
class CallerThread implements Callable<String> {
String str;
public CallerThread(String s) {this.str=s;}
public String call() throws Exception {
return str.concat(“Call”);
}
}
and
public static void main (String[] args) throws InterruptedException, ExecutionException
{
ExecutorService es = Executors.newFixedThreadPool(4); //line n1
Future f1 = es.submit (newCallerThread(“Call”));
String str = f1.get().toString();
System.out.println(str);
}
Which statement is true?
A. The program prints Call Call and terminates.
B. The program prints Call Call and does not terminate.
C. A compilation error occurs at line n1.
D. An ExecutionException is thrown at run time.
Answer: B