Q1. Which is a key aspect of composition?
A. Using inheritance
B. Method delegation
C. Creating abstract classes
D. Implementing the composite interface
Answer: B
Explanation:
In the composition approach, the subclass becomes the "front-end class," and the superclass becomes the"back-end class." With inheritance, a subclass automatically inherits an implemenation of any non-privatesuperclass method that it doesn't override. With composition, by contrast, the front-end class must explicitlyinvoke a corresponding method in the back-end class from its own implementation of the method. This explicitcall is sometimes called "forwarding" or "delegating" the method invocation to the back-end object.Note: Composition means the same as:
*
contains
*
is part of Note 2: As you progress in an object-oriented design, you will likely encounter objects in the problem domainthat contain other objects. In this situation you will be drawn to modeling a similar arrangement in the design ofyour solution. In an object-oriented design of a Java program, the way in which you model objects that containother objects is with composition, the act of composing a class out of references to other objects. Withcomposition, references to the constituent objects become fields of the containing object. To use compositionin Java, you use instance variables of one object to hold references to other objects.
Q2. Given the code format:
SimpleDateFormat sdf;
Which code statements will display the full text month name?
A. sdf = new SimpleDateFormat ("mm", Locale.UK); System.out.println("Result:", sdf.format(new date()));
B. sdf = new SimpleDateFormat ("MM", Locale.UK); System.out.println("Result:", sdf.format(new date()));
C. sdf = new SimpleDateFormat ("MMM", Locale.UK); System.out.println("Result:", sdf.format(new date()));
D. sdf = new SimpleDateFormat ("MMMM", Locale.UK); System.out.println("Result:", sdf.format(new date()));
Answer: D
Explanation:
Typical output would be Current Month in M format: 2 Current Month in MM format: 02 Current Month in MMM format: Feb Current Month in MMMM format: February
Q3. Given:
And the commands: javac Counter.java java ea Counter
What is the result?
A. 2
B. 3
C. NullPointException is thrown at runtime
D. AssertionError is thrown at runtime
E. Compilation fails
Answer: B
Explanation:
The command line javac Counter.java
Willcompile the code.
The command line java ea Counter
Willrun the cod with assertions enabled.
Assertion is true because getCount(arr) = 3 and Length of array is 4
The following line:
assert (getCount(arr) < arr.length);
where the Boolean expression getCount(arr) < arr.length will evaluate to false, will ensure
that anAssertionError is thrown at runtime.
Note:The javac command compiles Java source code into Java bytecodes. You then use
the Java interpreter -the java command - to interprete the Java bytecodes.
Note 2:The java tool launches a Java application. It does this by starting a Java runtime
environment, loading aspecified class, and invoking that class's main method. The method
declaration must look like the following:public static void main(String args[])
Paramater ea:
-enableassertions[:<package name>"..." | :<class name> ] -ea[:<package name>"..." |
:<class name> ]
Enable assertions. Assertions are disabled by default. With no arguments,
enableassertions or -ea enablesassertions.
Note 3:
An assertion is a statement in the JavaTM programming language that enables you to test
your assumptionsabout your program.
Each assertion contains a boolean expression that you believe will be true when the
assertion executes. If it isnot true, the system will throw an error.
Q4. Which two statements are true about RowSet subinterfaces?
A. A JdbcRowSet object provides a JavaBean view of a result set.
B. A CachedRowSet provides a connected view of the database.
C. A FilteredRowSet object filter can be modified at any time.
D. A WebRowSet returns JSON-formatted data.
Answer: A,C
Explanation:
A: a JdbcRowSet object can be one of the Beans that a tool makes available for composing an application. Because a JdbcRowSet is a connected RowSet, that is, it continually maintains its connection to a databaseusing a JDBC technology-enabled driver, it also effectively makes the driver a JavaBeans component.
C: The FilteredRowSet range criterion can be modified by applying a new Predicate object to the FilteredRowSet instance at any time. This is possible if no additional references to the FilteredRowSet objectare detected. A new filter has an immediate effect on criterion enforcement within the FilteredRowSet object,and all subsequent views and updates will be subject to similar enforcement.
Reference: javax.sql Interface RowSet
Q5. Which two forms of abstraction can a programmer use in Java?
A. enums
B. interfaces
C. primitives
D. abstract classes
E. concrete classes
F. primitive wrappers
Answer: B,D
Explanation:
When To Use Interfaces An interface allows somebody to start from scratch to implement your interface or implement your interface insome other code whose original or primary purpose was quite different from your interface. To them, yourinterface is only incidental, something that have to add on to thetheir code to be able to use your package. Thedisadvantage is every method in the interface must be public. You might not want to expose everything.
*When To Use Abstract classes An abstract class, in contrast, provides more structure. It usually defines some default implementations andprovides some tools useful for a full implementation. The catch is, code using it must use your class as thebase. That may be highly inconvenient if the other programmers wanting to use your package have alreadydeveloped their own class hierarchy independently. In Java, a class can inherit from only one base class.*When to Use Both You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore yourabstract class if they choose. The only drawback of doing that is calling methods via their interface name isslightly slower than calling them via their abstract class name.
Reference:http://mindprod.com/jgloss/interfacevsabstract.html
Q6. What will the following class print when run?
A. javajava
B. lavajava
C. javajavac
D. lavajavac
E. None of these.
Answer: C
Q7. Given: Which three values will appear in the output?
A. 5
B. 7
C. a1
D. a2
E. b1
F. b2
Answer: A,D,E
Explanation:
Staticmethod of base class is invoked >>
A myA = new B();
System.out.print(myA.doA() + myA.doA2() + myA.a);
class B String doA() { return "b1 "; }
class A protected static String doA2 () { return "a2 "; }
class B int a = 7;
Q8. Given the code fragment:
DateFormat df;
Which statement defines a new Dateformat object that displays the default date format for the UK Locale?
A. df = DateFormat.getDateInstance (DateFormat.DEFAULT, Locale (UK));
B. df = DateFormat.getDateInstance (DateFormat.DEFAULT, UK);
C. df = DateFormat.getDateInstance (DateFormat.DEFAULT, Locale.UK);
D. df = new DateFormat.getDateInstance (DateFormat.DEFAULT, Locale.UK);
E. df = new DateFormat.getDateInstance (DateFormat.DEFAULT, Locale (UK));
Answer: C
Explanation:
The UK locale is constructed withLocale.UK.
To format a date for a different Locale, specify it in the call to getDateInstance().
DateFormat df =
DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
Note: getDateInstance( int style, Locale aLocale )
Gets the date formatter with the given formatting style for the given locale.
Reference:Class DateFormat
Q9. Given the code fragment: What is the result?
A. Null B D
B. Null B null D
C. B D
D. D
E. An exception is thrown at runtime
Answer: C
Q10. Given: And the commands:
javac Test.java
java ea Test
What is the result?
A. Compilation fails
B. Standard Edition Enterprise Edition Micro Edition
C. Standard Edition class java.lang.AssertionError Micro Edition
D. Standard Edition is printed and an Assertion Error is thrown
Answer: D
Explanation:
javac Test.java
will compile the program.
As for command line:
java ea Test
First the code will produce the output:
Standard Edition
See Note below.
The ea option will enable assertions. This will make the following line in the switch
statement to be run:
default: assert false;
This will throw an assertion error. This error will be caught. An the class of the assertion
error (classjava.lang.AssertionError) will be printed by the following line:
System.out.println(e.getClass());
Note:The java tool launches a Java application. It does this by starting a Java runtime
environment, loading aspecified class, and invoking that class's main method. The method
declaration must look like the following:
public static void main(String args[])
Paramater ea:
-enableassertions[:<package name>"..." | :<class name> ] -ea[:<package name>"..." |
:<class name> ]
Enable assertions. Assertions are disabled by default. With no arguments,
enableassertions or -ea enablesassertions.
Note 2:
An assertion is a statement in the JavaTM programming language that enables you to test
your assumptionsabout your program.
Each assertion contains a boolean expression that you believe will be true when the
assertion executes. If it isnot true, the system will throw an error.
public class AssertionError extends Error
Thrown to indicate that an assertion has failed.
Note 3:
The javac command compiles Java source code into Java bytecodes. You then use the
Java interpreter - the
java command - to interprete the Java bytecodes.
Reference:java - the Java application launcher
Reference:java.langClass AssertionError