CORE JAVA INTERVIEW QUESTIONS 2

JAVA INTERVIEW QUESTIONS

101. How can one prove that the array is not null but empty using one line of code?

        Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have         thrown a NullPointerException on attempting to print args.length.

102. Can an application have multiple classes having main() method?

         Yes it is possible. While starting the application we mention the class name to be run. The          JVM will look for the Main method only in the class whose name you have mentioned.
         Hence there is not conflict amongst the multiple classes having main() method.

103. Can I have multiple main() methods in the same class?

         No the program fails to compile. The compiler says that the main() method is already                defined in the class.

104. What is the default value of an object reference declared as an instance variable?

         The default value will be null unless we define it explicitly.

105. What is the difference between a constructor and a method?

         A constructor is a member function of a class that is used to create objects of that class. It          has the same name as the class itself, has no return type, and is invoked using the new              operator.

         A method is an ordinary member function of a class. It has its own name, a return type              and is invoked using the dot operator.

106. What is final class?

         A final class can't be extended i.e. final class may not be subclassed. A final method can't            be overridden when its class is inherited. You can't change value of a final variable.

107. Can I import same package/class twice? Will the JVM load the package twice at                  runtime?

         One can import the same package or same class multiple times. Neither compiler nor JVM          complains about it. And the JVM will internally load the class only once no matter how                many times you import the same class.

108. What is the difference between declaring a variable and defining a variable?

         In declaration we just mention the type of the variable and it's name. 
         We do not initialize it. But defining means declaration + initialization.

109. Can a top level class be private or protected?
       
         No. A top level class cannot be private or protected. It can have either "public" or no                  modifier. If it does not have a modifier it is supposed to have a default access.

         If a top level class is declared as private the compiler will complain that the "modifier                  private is not allowed here". This means that a top level class cannot be private. Same is            the case with protected.

110. What is serialization?

         Serialization is a mechanism by which you can save the state of an object by converting it          to a byte stream.

111. Does Java provide any construct to find out the size of an object?
       
         No, there is not sizeof operator in Java. So there is not direct way to determine the size of          an object directly in Java.

112. If I write return at the end of the try block, will the finally block still execute?

        Yes even if you write return as the last statement in the try block and no exception                     occurs,the finally block will execute. The finally block will execute and then the control               return.

113. If I write System.exit(0); at the end of the try block, will the finally block still                        execute?
    
         No. In this case the finally block will not execute because when you                                                say System.exit(0); the control immediately goes out of the program, and thus finally                  never executes.

114. Does garbage collection guarantee that a program will not run out of memory?

          Garbage collection does not guarantee that a program will not run out of memory. It is               possible for programs to use up memory resources faster than they are garbage                         collected. It is also possible for programs to create objects that are not subject to                         garbage collection.

115. When a thread is created and started, what is its initial state?

         A thread is in the ready state after it has been created and started.

116. What is the difference between static and non-static variables?

         A static variable is associated with the class as a whole rather than with specific instances          of a class. Non-static variables take on unique values with each object instance.

117. How are this() and super() used with constructors?
       
         this() is used to invoke a constructor of the same class. 
         super() is used to invoke a superclass constructor.

118. How does a try statement determine which catch clause should be used to handle an exception?

         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.

119. Can an unreachable object become reachable again?

         An unreachable object may become reachable again. This can happen when the                          object's finalize() method is invoked and the object performs an operation which causes            it to become accessible to reachable objects.

120. What method must be implemented by all threads?

         All tasks must implement the run() method, whether they are a subclass of Thread or                implement the Runnable interface.

121. What modifiers are allowed for methods in an Interface?

         Only public and abstract modifiers are allowed for methods in interfaces.

122. What are the different scopes for Java variables?

         The scope of a Java variable is determined by the context in which the variable is                        declared. Thus a java variable can have one of the three scopes at any given point in time.

          1. Instance : - These are typical object level variables, they are initialized to default values                                   at the time of creation of object, and remain accessible as long as the                                             object accessible.

          2. Local : - These are the variables that are defined within a method. They remain                                          accessble only during the course of method execution. When the method                                      finishes execution, these variables fall out of scope.

          3. Static: - These are the class level variables. They are initialized when the class is loaded                             in JVM for the first time and remain there as long as the class remains loaded.                             They are not tied to any particular object instance.

123. What is the Dictionary class?

         The Dictionary class provides the capability to store key-value pairs.

124. What is a native method?
        
         A native method is a method that is implemented in a language other than Java.

125. What is the purpose of System class?

         The purpose of the System class is to provide access to system resources.

126. Which container uses a BorderLayout as their default layout?

         The Window, Frame and Dialog classes use a border layout as their default layout.

127. Which container s use a FlowLayout as their default layout?

         The Panel and Applet classes use the FlowLayout as their default layout.

128. What are peerless components?

         The peerless components are called light weight components.

129. What is the difference between a Scrollbar and a ScrollPane?

         A Scrollbar is a Component, but not a Container.

         A ScrollPane is a Container. A ScrollPane handles its own events and performs its own              scrolling.

130. What is a lightweight component?

         Lightweight components are the one which uses less resources and less memory and                these components are system independent. For example, Swing components.

131. What is a heavy weight component?

         The heavy weight component uses more resources and more memory and these                         components are system dependent. For Example,  AWT.

132. Difference between Swing and Awt?

         AWT are heavy-weight componenets. Swings are light-weight components. Hence swing            works faster than AWT.

133. What is an applet?
         An Applet is a program that come from the server, downloads on client computer and                execute on client computer.

         An applet is a small java program that runs inside the browser and generates dynamic               contents.

134. Can you write a Java class that could be used both as an applet as well as an application?

         Yes. Add a main() method to the applet.

135. What is Locale?

         Locale is the class present in java.util class. A Locale object represents a specific                          geographical, political, or cultural region. For example, Locale.UK

136. How will you load a specific locale?

         By using ResourceBundle.getBundle(?) method. Here ResourceBundle is the abstract c              class present in java.util package.

137. What will be the default values of all the elements of an array defined as an instance variable?

          If the array is an array of primitive types, then all the elements of the array will be                     initialized to the default value corresponding to that primitive type.

          Example: All the elements of an array of int will be initialized to 0(zero), while that of                 boolean type will be initialized to false. Whereas if the array is an array of references (of            any type), all the elements will be initialized to null.

138. What is HashMap and Map?

         Map is an Interface and Hashmap is the class that implements Map.

139. Difference between HashMap and HashTable?

         The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized              and permits nulls.

         HashMap allows null values as key and value whereas Hashtable doesnt allow.

         HashMap does not guarantee that the order of the map will remain constant over time. 

         HashMap is unsynchronized and Hashtable is synchronized.

140. Difference between Vector and ArrayList?
       
         Vector is synchronized whereas arraylist is not.

141. Is Empty .java file a valid source file?

         Yes. An empty .java file is a perfectly valid source file.

142. Can a .java file contain more than one java classes?

         Yes. A .java file contain more than one java classes, provided at the most one of them is a          public class.

143. What is the most important feature of Java? 


         Java is a platform independent language. 
144. What do you mean by platform independence? 


         Platform independence means that we can write and compile the java code in one                      platform (eg Windows) and can execute the class in any other supported platform eg                 (Linux,Solaris,etc). 
145. Are JVM's platform independent?
 

          JVM's are not platform independent. JVM's are platform specific run time                                     implementation provided by the vendor. 
146. Should a main method be compulsorily declared in all java classes?


         No not required. main method should be defined only if the source class is a java                        application. 
147.What is the return type of the main method?
Main method doesn't return anything hence declared void. 
148. Why is the main method declared static?


         main method is called by the JVM even before the instantiation of the class hence it is                declared as static. 
149. Can a main method be overloaded? 

        Yes. You can have any number of main methods with different method signature and                 implementation in the class. 
150. I don't want my class to be inherited by any other class. What should i do? 

         You should declared your class as final. 
         But you can't define your class as final, if it is an abstract class. A class declared as final              can't be extended by any other class.