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.



1 comment:

  1. This is a very useful collection. You did a great job by collecting so many questions in one place..

    ReplyDelete

Please provide your precious comments and suggestion