Q1. Given the code fragment:
9.
Connection conn = DriveManager.getConnection(dbURL, userName, passWord);
10.
String query = “SELECT id FROM Employee”;
11.
try (Statement stmt = conn.createStatement()) {
12.
ResultSet rs = stmt.executeQuery(query); 13.stmt.executeQuery(“SELECT id FROM Customer”);
14.
while (rs.next()) {
15.
//process the results 16.System.out.println(“Employee ID: “+ rs.getInt(“id”)); 17.}
18.
} catch (Exception e) {
19.
System.out.println (“Error”);
20.
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database is accessible with the dbURL, userName, and passWord exists.
The Employee and Customer tables are available and each table has id column with a few
records and the SQL queries are valid.
What is the result of compiling and executing this code fragment?
A. The program prints employee IDs.
B. The program prints customer IDs.
C. The program prints Error.
D. compilation fails on line 13.
Answer: D
Q2. Given the code format:
Which code fragment must be inserted at line 6 to enable the code to compile?
A. DBConfiguration f; return f;
B. Return DBConfiguration;
C. Return new DBConfiguration;
D. Retutn 0;
Answer: B
Q3. 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
Q4. Given the code fragment:
int b = 3;
if ( !(b > 3)) {
System.out.println("square ");
}{
System.out.println("circle ");
}
System.out.println("...");
What is the result?
A. square...
B. circle...
C. squarecircle...
D. Compilation fails.
Answer: C
Q5. Given the code fragment:
UnaryOperator<Integer> uo1 = s -> s*2;line n1
List<Double> loanValues = Arrays.asList(1000.0, 2000.0);
loanValues.stream()
.filter(lv -> lv >= 1500)
.map(lv -> uo1.apply(lv))
.forEach(s -> System.out.print(s + “ “));
What is the result?
A. 4000.0
B. 4000
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: B
Q6. Given:
class Bird {
public void fly () { System.out.print(“Can fly”); }
}
class Penguin extends Bird {
public void fly () { System.out.print(“Cannot fly”); }
}
and the code fragment:
class Birdie {
public static void main (String [ ] args) {
fly( ( ) -> new Bird ( ));
fly (Penguin : : new);
}
/* line n1 */
}
Which code fragment, when inserted at line n1, enables the Birdie class to compile?
A. static void fly (Consumer<Bird> bird) {
bird :: fly (); }
B. static void fly (Consumer<? extends Bird> bird) {
bird.accept( ) fly ();
}
C. static void fly (Supplier<Bird> bird) {
bird.get( ) fly ();
}
D. static void fly (Supplier<? extends Bird> bird) {
LOST
Answer: C
Explanation: NOTE: Very confusing question. There is no logic in the options.
Q7. Given: What is the result?
A. box
B. nbo
C. bo
D. nb
E. An exception is thrown at runtime
Answer: E
Q8. Given the records from the Employee table:
and given the code fragment:
try {
Connection conn = DriverManager.getConnection (URL, userName, passWord);
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
st.execute(“SELECT*FROM Employee”);
ResultSet rs = st.getResultSet();
while (rs.next()) {
if (rs.getInt(1) ==112) {
rs.updateString(2, “Jack”);
}
}
rs.absolute(2);
System.out.println(rs.getInt(1) + “ “ + rs.getString(2));
} catch (SQLException ex) {
System.out.println(“Exception is raised”);
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database accessible with the URL, userName, and passWord exists.
What is the result?
A. The Employee table is updated with the row:
112 Jack
and the program prints:
112 Jerry
B. The Employee table is updated with the row:
112 Jack
and the program prints:
112 Jack
C. The Employee table is not updated and the program prints:
112 Jerry
D. The program prints Exception is raised.
Answer: D
Q9. 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
Q10. Given:
class Book {
int id;
String name;
public Book (int id, String name) {
this.id = id;
this.name = name;
}
public boolean equals (Object obj) { //line n1
boolean output = false;
Book b = (Book) obj;
if (this.name.equals(b name))}
output = true;
}
return output;
}
}
and the code fragment:
Book b1 = new Book (101, “Java Programing”);
Book b2 = new Book (102, “Java Programing”);
System.out.println (b1.equals(b2)); //line n2
Which statement is true?
A. The program prints true.
B. The program prints false.
C. A compilation error occurs. To ensure successful compilation, replace line n1 with: boolean equals (Book obj) {
D. A compilation error occurs. To ensure successful compilation, replace line n2 with: System.out.println (b1.equals((Object) b2));
Answer: C
Q11. Given the code fragments:
4.
void doStuff() throws ArithmeticException, NumberFormatException, Exception {
5.
if (Math.random() >-1 throw new Exception (“Try again”);
6.
} and
24.
try {
25.
doStuff ( ):
26.
} catch (ArithmeticException | NumberFormatException | Exception e) {
27.
System.out.println (e.getMessage()); }
28.
catch (Exception e) {
29.
System.out.println (e.getMessage()); }
30.
}
Which modification enables the code to print Try again?
A. Comment the lines 28, 29 and 30.
B. Replace line 26 with:
} catch (Exception | ArithmeticException | NumberFormatException e) {
C. Replace line 26 with:
} catch (ArithmeticException | NumberFormatException e) {
D. Replace line 27 with:
throw e;
Answer: C
Q12. 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
Q13. Given: What is the result?
A. 100 210
B. Compilation fails due to an error in line n1
C. Compilation fails due to an error at line n2
D. Compilation fails due to an error at line n3
Answer: C
Q14. Given:
What is the result?
A. Red 0 Orange 0 Green 3
B. Red 0 Orange 0 Green 6
C. Red 0 Orange 1
D. Green 4
E. Compilation fails
Answer: E
Q15. 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