java is an Object  Oriented Programming Language. Which is mostly used to develop Windows based Software  as well as Web Based Project. It is good to have good knowledge about a programming language and practical knowledge for developing program in that particular language. In the technical interview for the post of  Java Developer some basic questions are asked. In this post I am telling about questions at basic level. These questions are related to Variable, operators, JVM and Jdk, Interface and access specifier, methods , class, use of encapsulation,etc.





                     


Q             How the private constructor is called in the main java program?  
A             Have a look at this demo.
public class Test2 {
            private Test2(){
                        System.out.println("Test2class");
            }
            class Subclass extends Test2{
                        public Subclass(){
                                    System.out.println("Subclass");
                        }
            }
            public static void main(String[] args){
                        Subclass s = new Test2().new Subclass();
            }
}
This works because an inner class is allowed to access private members of its enclosing instance, including the private constructor.

Q             Which is garbage collected first: Normal variables or static variables?  
A             Normal variables will be collected first. Lets take a simple example:
Class A is having a static variable s which is used by obj1, obj2 and obj3 of Class B. Each object of class B is having instance variables a and b (normal variables). Lets say if obj1 is not being in use since long time, then automatically the garbage collector will collect the space occupied by obj1. It will not destroy the static variable S as it is being used by the other two objects obj2 and obj3. Therefore only normal variables will be destroyed first.
We can say it in a simple statement that "Variables having less scope will be destroyed first"

Q             Difference between JRE And JVM AND JDK   
A             The "JDK" is the Java Development Kit. I.e., the JDK is bundle of software that you can use to develop Java based software. The "JRE" is the Java Runtime Environment. I.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs. Typically, each JDK contains one (or more) JRE's along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.

Q             Is it necessary to initialize a final variable at the time of declaration?  
A             NO, it's not necessary.
Many text books say like this but that’s not true. Value of a final variable can be instance specific also, but in this case we have to initialize the variable in all the constructors. If we want to have a common final value of a variable for all the instances then there are two ways.
1.      Initialize the variable at class level (at the time of declaration) or
2.      just declare variable at class level and initialize it in any one of the instance blocks i.e.
a.             class A { final int a; {a=5;}}
b.             class A { final int a = 5;}

Q             What is the output of the following line? System.out.println(-5<<-2);  
A             At This point of time I am not sure about the answer. If anybody knows, please tell me at
raja8top@yahoo.com OR raja_choudhary@rediffmail.com I shall be highly thankful to you.

Q             What is a compilation unit?  
A             A compilation unit is a Java source code file.

Q             What restrictions are placed on method overriding?  
A             Overridden methods must have the same name, argument list, and return type.
The overriding method may not limit the access of the method it overrides.
The overriding method may not throw any exceptions that may not be thrown by the overridden method.

Q             How can a dead thread be restarted?  
A             A dead thread cannot be restarted.

Q             What happens if an exception is not caught?  
A             An uncaught exception results in the uncaughtException () method of the thread's ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown.

Q             Which arithmetic operations can result in the throwing of an ArithmeticException?  
A             Integer / and % can result in the throwing of an ArithmeticException

Q             Can an abstract class be final?  
A             An abstract class may not be declared as final

Q             What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?  
A             The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination

Q             What is numeric promotion?  
A             Numeric promotion is the conversion of a smaller numeric type to a larger numeric type, so that integer and floating-point operations may take place. In numerical promotion, byte, char, and short values are converted to int values. The int values are also converted to long values, if necessary. The long and float values are converted to double values, as required

Q             What is the difference between a public and a non-public class?  
A             A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

Q             To what value is a variable of the boolean type automatically initialized?  
A             The default value of the boolean type is false

Q             Can try statements be nested?  
A             Yes

Q             What is the difference between the prefix and postfix forms of the ++ operator?  
A             The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value.

Q             What is the purpose of a statement block?  
A             A statement block is used to organize a sequence of statements as a single statement group

Q             What is a Java package and how is it used?  
A             A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.

Q             What modifiers may be used with a top-level class?  
A             A top-level class may be public, abstract, or final.

Q             What are the Object and Class classes used for?  
A             The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

Q             How does a try statement determine which catch clause should be used to handle an exception?  
A             When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.

Q             What are synchronized methods and synchronized statements?  
A             Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Q             What is the difference between an if statement and a switch statement?  
A             The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

Q             What gives java it's "write once and run anywhere" nature?  
A             Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating system, the code platform specific machine code is generated thus making java platform independent.

Q             What are the four corner stones of OOP?  
A             Abstraction, Encapsulation, Polymorphism and Inheritance

Q             Difference between a Class and an Object?  
A             A class is a definition or prototype whereas an object is an instance or living representation of the prototype

Q             What is the difference between method overriding and overloading?   
A             Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments

Q             What is a "stateless" protocol?  
A             Without getting into lengthy debates, it is generally accepted that protocols like HTTP are stateless i.e. there is no retention of state between a transaction which is a single request response combination

Q             What is constructor chaining and how is it achieved in Java?  
A             A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

Q             What is passed by ref and what by value?  
A             All Java method arguments are passed by value. However, Java does manipulate objects by reference, and all object variables themselves are references

Q             You can create a String object as String str = "abc"; Why can’t a button object be created as Button bt = "abc";? Explain  
A             The main reason you cannot create a button by Button bt1= "abc"; is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. The only object in Java that can be assigned a literal String is java.lang.String. Important to note that you are NOT calling a java.lang.String constructor when you type String s = "abc";

Q             What does the "abstract" keyword mean in front of a method? A class?  
A             Abstract keyword declares either a method or a class. If a method has an abstract keyword in front of it, it is called abstract method. Abstract method has no body. It has only arguments and return type. Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can't be instantiated. If a class is declared as abstract, no objects of that class can be created. If a class contains any abstract method it must be declared as abstract

Q             How many methods do u implement if implement the Serializable Interface?  
A             The Serializable interface is just a "marker" interface, with no methods of its own to implement. Other 'marker' interfaces are
java.rmi.Remote
java.util.EventListener

Q             What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import java.net.* versus import java.net.Socket)?  
A             It makes no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the same name. Take java.util.Timer and javax.swing.Timer, for example. If I import java.util.* and javax.swing.* and then try to use "Timer", I get an error while compiling (the class name is ambiguous between both packages). Let's say what you really wanted was the javax.swing.Timer class, and the only classes you plan on using in java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing java.util.*. This will now allow them to use Timer, Collection, HashMap, and other javax.swing classes without using fully qualified class names in.

Q             What is the difference between logical data independence and physical data independence?  
A             Logical Data Independence - meaning immunity of external schemas to changes in conceptual schema. Physical Data Independence - meaning immunity of conceptual schema to changes in the internal schema.

Q             What is user defined exception?  
A             Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.

Q             Difference Between Abstraction and Encapsulation  
A             Abstraction is removing some distinctions between objects, so as to show their commonalities.
Encapsulation is hiding the details of the implementation of an object so that there are no external dependencies on the particular implementation.

Q             What are Checked and Un-Checked Exceptions? Explain.  
A             Throwable extends Object (checked)
Exception extends Throwable (checked)
RuntimeException extends Exception (un-checked)
Error extends Throwable (un-checked)
So anything that extends Throwable or Exception (except RuntimeException) will be checked. Anything that extends Error or RuntimeException will be un-checked
Checked exceptions are problems that arise in correct code and may be due to technical problems such as IO problems or user mistakes such as opening a socket when the remote machine does not exist. Because these problems can occur at anytime, say due to network outage, you must have code that can handle and recover from these. In fact, the Java compiler checks that you have trapped them hence checked exceptions.
Runtime exceptions are typically bugs in the program. Errors are severe problems such as out of memory and sufficiently rare, that you are not required to handle them as they are usually unrecoverable.