Q1. Given the code fragment:
List<Integer> values = Arrays.asList (1, 2, 3);
values.stream ()
.map(n -> n*2)//line n1
.peek(System.out::print)//line n2
.count();
What is the result?
A. 246
B. The code produces no output.
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: A
Q2. Given the code fragment:
public static void main (String [ ] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStremReader (System.in));
System.out.print (“Enter GDP: “);
//line 1
}
Which code fragment, when inserted at line 1, enables the code to read the GDP from the user?
A. int GDP = Integer.parseInt (br.readline());
B. int GDP = br.read();
C. int GDP = br.nextInt();
D. int GDP = Integer.parseInt (br.next());
Answer: C
Q3. A method is declared to take three arguments. A program calls this method and passes only two arguments. What is the results?
A. Compilation fails.
B. The third argument is given the value null.
C. The third argument is given the value void.
D. The third argument is given the value zero.
E. The third argument is given the appropriate falsy value for its declared type. F) An exception occurs when the method attempts to access the third argument.
Answer: A
Q4. 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
Q5. Given the code fragments:
interface CourseFilter extends Predicate<String> {
public default boolean test (String str) {
return str.equals (“Java”);
}
}
and
List<String> strs = Arrays.asList(“Java”, “Java EE”, “Java ME”);
Predicate<String> cf1 = s - > s.length() > 3;
Predicate cf2 = new CourseFilter() { //line n1
public boolean test (String s) {
return s.contains (“Java”);
}
};
long c = strs.stream()
.filter(cf1)
.filter(cf2//line n2
.count();
System.out.println(c);
What is the result?
A. 2
B. 3
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: A
Q6. Given:
Item table
. ID, INTEGER: PK
. DESCRIP, VARCHAR(100)
. PRICE, REAL
. QUANTITY< INTEGER
And given the code fragment:
9. try {
10.Connection conn = DriveManager.getConnection(dbURL, username, password);
11.
String query = “Select * FROM Item WHERE ID = 110”;
12.
Statement stmt = conn.createStatement();
13.
ResultSet rs = stmt.executeQuery(query);
14.while(rs.next()) {
15.System.out.println(“ID:“ + rs.getInt(“Id”));
16.System.out.println(“Description:“ + rs.getString(“Descrip”));
17.System.out.println(“Price:“ + rs.getDouble(“Price”));
18. System.out.println(Quantity:“ + rs.getInt(“Quantity”));
19.}
20.
} catch (SQLException se) {
21.
System.out.println(“Error”);
22.
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database is accessible with the dbURL, userName, and passWord exists.
The SQL query is valid.
What is the result?
A. An exception is thrown at runtime.
B. Compilation fails.
C. The code prints Error.
D. The code prints information about Item 110.
Answer: C
Q7. 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
Q8. Which two reasons should you use interfaces instead of abstract classes?
A. You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public.
B. You expect that unrelated classes would implement your interfaces.
C. You want to share code among several closely related classes.
D. You want to declare non-static on non-final fields.
E. You want to take advantage of multiple inheritance of type.
Answer: A,E
Reference: http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/
Q9. 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
Q10. Which statement is true about java.util.stream.Stream?
A. A stream cannot be consumed more than once.
B. The execution mode of streams can be changed during processing.
C. Streams are intended to modify the source data.
D. A parallel stream is always faster than an equivalent sequential stream.
Answer: B
Q11. 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
Q12. 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
Q13. Given:
class CheckClass {
public static int checkValue (String s1, String s2) {
return s1 length() – s2.length();
}
}
and the code fragment:
String[] strArray = new String [] {“Tiger”, “Rat”, “Cat”, “Lion”}
//line n1
for (String s : strArray) {
System.out.print (s + “ “);
}
Which code fragment should be inserted at line n1 to enable the code to print Rat Cat Lion Tiger?
A. Arrays.sort(strArray, CheckClass : : checkValue);
B. Arrays.sort(strArray, (CheckClass : : new) : : checkValue);
C. Arrays.sort(strArray, (CheckClass : : new).checkValue);
D. Arrays.sort(strArray, CheckClass : : new : : checkValue);
Answer: D
Q14. 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.
Q15. Given:
Class A { } Class B { } Interface X { }
Interface Y { }
Which two definitions of class C are valid?
A. Class C extends A implements X { }
B. Class C implements Y extends B { }
C. Class C extends A, B { }
D. Class C implements X, Y extends B { }
E. Class C extends B implements X, Y { }
Answer: A,E
Explanation: extends is for extending a class.
implements is for implementing an interface. Java allows for a class to implement many interfaces.