Java Quiz Player

Sample Quiz Questions

Question 1

Which of the following can be used as expression types in a switch statement:

(a) byte
(b) char
(c) enum
(d) Object
(e) String
(f) StringBuilder

Answer Details:
(a), (b), (c) and (e) are correct.
(d) and (f) are incorrect. Its a compile-time error.

Question 2

Which of the following describes singleton design pattern:

(a) A class has only one instance and provides a global point of access to it.
(b) A class uses its subclasses to specify which objects it creates.
(c) All objects that use an instance of a class use the same instance.
(d) None of the above.

Answer Details:
(a) and (c) are correct.
(b) and (d) are incorrect.

Question 3

Conside the code:

line 10: try {
line 11:    String query = "SELECT * FROM a_table";
line 12:    Statement stmt = conn.createStatement();
line 13:    ResultSet rs = stmt.executeQuery(query);
line 14:    while (rs.next()) { 
line 15:        String aName = rs.getString("col_name");
line 16:        double d1 = rs.getDouble(2);
line 17:    }
line 18:    stmt.close();
line 19: } catch (SQLException e) { }
Which of the following statements are true after compiling and running the above code:

(a) The statement on line 18 also closes the ResultSet object rs.
(b) Throws runtime exception, if line 14 and line 17 are commented.
(c) After line 13 is run, the ResultSet's cursor is positioned at the first row.
(d) One of line 15 and line 16 has compile-time errors.

Answer Details:
(a) and (b) are correct.
(b): A ResultSet object's data is accessed through a cursor (not a database cursor). Initially, the cursor is positioned before the first row. The rs.next() statement moves the cursor to first row, and so on. So, the rs.getString() and rs.getDouble() methods throw a SQLException at runtime.
(c) and (d) are incorrect.
(c): Refer answer for (b) above.

Question 4

Which of the following are true of the java.util.concurrent.CopyOnWriteArrayList collection:

(a) A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.
(b) The iterator returned by iterator() of this class, does not support the remove method.
(c) null values are not permitted in this collection.
(d) CopyOnWriteArraySet is a Set that uses an internal CopyOnWriteArrayList for all of its operations.

Answer Details:
(a), (b) and (d) are correct.
(c) is incorrect.
null values are permitted in CopyOnWriteArrayList.

Question 5

Consider the code:

Locale locale = new Locale("en", "US");
// substitute code here
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
String result = formatter.format(new Date());
Sytem.out.println(result);

Which one of the following statements when substituted individually in the above code, runs and prints "Sat, 29 Sep 12 :: 8:07 PM":

(a) String pattern = "WWW, d MMM yy :: h:mm aa";
(b) String pattern = "EEE, d MM yy :: hh:mm a";
(c) String pattern = "EEE, dd MMM yy :: h:mm aa";
(d) String pattern = "www, dd MMM yy :: h:mm aa";

Answer Details:
(c) is correct.
(a), (b) and (d) are incorrect.
(a) and (d): The symbols "WWW" and "www" mean week in month and week in year respectively. They do not mean day in week.
(b): The symbol "MM" results in a number form of month 09. The symbol "hh" results in hour 08.

Question 6

Consider the code:

String pattern = "glob:d**/f?";
PathMatcher pm = FileSystems.getDefault().getPathMatcher(pattern);
Path path = Paths.get(pathString); // statement (1)
pm.matches(path); // statement (2)

Assume the code compiles without errors and runs.

Which of the following pathString variable values when used in the above code at statement (1), individually, returns a true value at statement (2):

(a) "dir1/test1/test2/test3/f1"
(b) "dir1/test1/test2/test3/f"
(c) "dir12/f"
(d) "dir/f2"

Answer Details:
(a) and (d) are correct.
(b) and (c) are incorrect.

Return to top