Please click on any Advertisement, if you like this blog.

Saturday, July 23, 2011

Interview Questions : Core Java : Volume 1

A: The main method is declared public so that it is accessible as part of the public interface of the program. It is static because it must be called before the class that hosts the method is instantiated. It returns void because the Java interpreter does not expect to receive or process any output from the class; any output intended for end users will normally be sent to the system output and error streams, or via a graphical user interface or supplementary logging systems.

Q: What does public static void main(String[]) mean?
A: This is a special static method signature that is used to run Java programs from a command line interface (CLI). There is nothing special about the method itself, it is a standard Java method, but the Java interpreter is designed to call this method when a class reference is given on the command line.

Q: In which class is the main() method declared?
A: The static void main(String[]) method can be declared in any Java class to create a running Java application. Where you choose to place the main method will depend on the nature and structure of your application. It is possible for numerous classes to have their own main entry point methods, but only one entry point can be used to start the program. An application will typically have a top level application class, an "Editor", "Viewer", "Server" or "Monitor" type that will be used to start and run the program, for example.

Q: Why do we only use the main method to start a program?
A: The entry point method main is used to the provide a standard convention for starting Java programs. The choice of the method name is somewhat arbitrary, but is partly designed to avoid clashes with the Thread start() and Runnable run() methods, for example.

Q: Can the main method be overloaded?
A: Yes, any Java method can be overloaded, provided there is no final method with the same signature already. The Java interpreter will only invoke the standard entry point signature for the main method, with a string array argument, but your application can call its own main method as required.

Main method arguments                

Please click on any advertisement if you like this blog.                                                                                                        

 Q: Why are command line arguments passed as a String?                                                         

A: Command line arguments are passed to the application's main method by the Java runtime system before the application class or any supporting objects are instantiated. It would be much more complex to define and construct arbitrary object types to pass to the main method and primitive values alone are not versatile enough to provide the range of input data that strings can. String arguments can be parsed for primitive values and can also be used for arbitrary text input, file and URL references.

Q: Why doesn't the main method throw an error with no arguments?
A: When you invoke the Java Virtual Machine on a class without any arguments, the class' main method receives a String array of zero length. Thus, the method signature is fulfilled. Provided the main method does not make any reference to elements in the array, or checks the array length before doing so, no exception will occur.

A: The number of arguments that may be passed to a Java program will vary from one interpreter to another and will also be affected by the operating system's command interpreter. In a simple test, 169 arguments were passed to the Sun Java interpreter via a Windows batch script before it reported "The input line is too long". If your program requires a great number of input parameters, it would be better to pass a reference to a Java properties file on the command line and extract the input from that. 

Q: Can the main method be declared final?  
A: Yes, the static void main(String[]) method can be declared final.

What is the difference between a JDK and a JVM?
JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

Are arrays primitive data types?
In Java, Arrays are objects.

What are local variables?
Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them. 

Please click on any advertisement on right side if you like this blog.

What are instance variables?
Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

Can a class declared as private be accessed outside it's package?
Not possible.

Can a class be declared as protected?
A class can't be declared as protected. only methods can be declared as protected.

How is final different from finally and finalize?
final is a modifier which can be applied to a class or a method or a variable. final class can't be inherited, final method can't be overridden and final variable can't be changed.
finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment. 
finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

Can a class be declared as static?
No a class cannot be defined as static. Only a method,a variable or a block of code can be declared as static.

When will you define a method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.

What are the restriction imposed on a static method or a static block of code?
A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.

I want to print "Hello" even before main is executed. How will you acheive that?
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main method. And it will be executed only once.

Please click on any advertisement if you like this blog.

Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.

Can you create an object of an abstract class?
Not possible. Abstract classes can't be instantiated.

Can a method inside a Interface be declared as final?
No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.

Can an Interface implement another Interface?
Intefaces doesn't provide implementation hence a interface cannot implement another interface.

Can an Interface extend another Interface?
Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.

Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class?
Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.

Can an Interface be final?
Not possible. Doing so so will result in compilation error.

Can a class be defined inside an Interface?
Yes it's possible.

Can an Interface be defined inside a class?
Yes it's possible.

What is a Marker Interface?
An Interface which doesn't have any declaration inside but still enforces a mechanism.

What is Externalizable?
Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)

What value does read() return when it has reached the end of a file?
The read() method returns -1 when it has reached the end of a file.

Can a Byte object be cast to a double value?
No, an object cannot be cast to a primitive value.

What is the difference between a static and a non-static inner class?
A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

When can an object reference be cast to an interface reference?
An object reference be cast to an interface reference when the object implements the referenced interface.

Which class is extended by all other classes?
The Object class is extended by all other classes.

Which non-Unicode letter characters may be used as the first character of an identifier?
The non-Unicode letter characters $ and _ may appear as the first character of an identifier

Please click on any advertisement if you like this blog.

What restrictions are placed on method overloading?
Two methods may not have the same name and argument list but different return types.

Can an anonymous class be declared as implementing an interface and extending a class?
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

What is the range of the char type?
The range of the char type is 0 to 2^16 - 1.

What is the range of the short type?
The range of the short type is -(2^15) to 2^15 - 1.

Is null a keyword?
The null value is not a keyword.

What restrictions are placed on the values of each case of a switch statement?
During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.                      

What modifiers can be used with a local inner class?
A local inner class may be final or abstract.

Which Java operator is right associative?
The = operator is right associative.

Can a for statement loop indefinitely?
Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) ;

Is sizeof a keyword?
The sizeof operator is not a keyword.

Please click on any advertisement on right side if you like this blog.

Transition with different Effects like Resize, Fade in View State in Flex

Transition in View State

In my previous blog, I have discussed about View State in both Flex3 and Flex4. Now, I am going to discuss the implementation of Transition and Effects while using View State. It is applicable in both Flex3 and Flex4.

Transition: It is the collection of one or more effects grouped together to play when view state change occurs. The trigger point for transitions is View State change. Whenever view state changes, any transition defined on those view states will start to play. Following, is the syntax for using the Transition:

<s:Transition fromState="off" toState="A" autoReverse="true”/>

From syntax, it is visible that it consisted of 3 main attributes.
a)     fromState: it specifies the current state of our component. It takes the current state name.
b)     toState: It specifies the target state to which our current state is going to change. It takes the target state name.
c)     autoReverse: Suppose a transition is playing when state changes from state A to state B and before its completion, user changes the state from state B to State A and due to this a new transition from state B to State A becomes ready to start. Now what will happen? Whether the second transition will starts after the completion of first transition or the first transition will be stopped before its completion and second transition will start. This will be decided by the value of this attribute. If its value is True, the first transition will be stopped and second transition will start just from that same point. But, if the value is False, the first transition will be stopped, but the second transition will not start from just the same point. It will start from beginning.

All effects in a transition are grouped under either <s:Parallel> or <s:Sequence> tag as shown in following example.

          <s:Transition fromState="off" toState="A" autoReverse="true">
                    <s:Parallel duration="1000">                    
                       <s:Resize target="{content}" heightTo="{cA.height}" />                     
                       <s:Fade targets="{cA}"/>                

Please click on any advertisement if you like this blog.
<s:Sequence>: The Sequence effect plays multiple child effects one after the other, in the order in which they are added.
<s:Parallel>: The Parallel effect plays multiple child effects at the same time.

In the above syntax, Resize and Fade classes are different effect classes.
Resize: It is used to change height and width of the component (specified by the target attribute).
Fade: It is used to blur or un blur the corresponding component. Following is the complete code.

Code for Implementing Transition in View State

<?xml version="1.0"?>
<!-- containers\intro\ContainerAddChild.mxml -->
<s:Application xmlns:fx=""
                                xmlns:s="library://" creationComplete="init()">
          protected function handleStateChange(e:Event):void      
                    if (( as ToggleButton).selected)
          <s:State name="close" />     
          <s:State name="open" />  

Please click on any advertisement if you like this blog.
          <s:Transition fromState="close" toState="open" autoReverse="true">
               <s:Parallel duration="1000">                    
                    <s:Resize target="{content}" heightTo="{innerContent.height}" />                    
                    <s:Fade targets="{innerContent}"/>                
          <s:Transition fromState="open" toState="close" autoReverse="true"> 
               <s:Parallel duration="1000">                     
                    <s:Resize target="{content}" heightTo="0" />                    
                    <s:Fade targets="{innerContent}"/>                
<s:Group id="content" width="400" clipAndEnableScrolling="true">           
<s:BorderContainer id="innerContent" includeIn="open" backgroundColor="#00ff00">
                    <s:Group id="contentGroup" width="197">
                                       <s:Label id="abc" text=""/>
                                       <s:Label text=""/>
                                       <s:Label text=""/>
<s:HGroup paddingLeft="200">    
          <s:ToggleButton id="clickMe" label="Click Here" change="handleStateChange(event)"/>  

Please click on any advertisement if you like this blog.

This is the Flex4 specific implementation of Transition and Effects like Resize and Fade with View State. The notable point here is the use of “clipAndEnableScrolling” attribute in outermost container group just below the Transition tag. This attribute is used the keep all the children of outermost Group with in its boundary. If this attribute is made False or if it is removed, then all children of the outermost Group will be visible in background even after the minimizing the Group.