CORE JAVA INTERVIEW QUESTIONS 3

CORE JAVA INTERVIEW QUESTIONS

151. Can you give few examples of final classes defined in Java API? 
         java.lang.String,java.lang.Math are final classes. 
152. 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. 
153. 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. 
154. 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. 
155. What is the importance of static variable?
        static variables are class level variables where all objects of the class refer to the same               variable. If one object changes the value then the change gets reflected in all the objects. 
156. Can we declare a static variable inside a method? 
        Static variables are class level variables and they can't be declared inside a method. If                 declared, the class will not compile. 
157. 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. 
158. Can an Interface implement another Interface? 
        Interfaces doesn't provide implementation hence a interface cannot implement another             interface. 
159. Can an Interface extend another Interface? 
         Yes an Interface can inherit another Interface, for that matter an Interface can extend                more than one Interface. 
160. Can a Class extend more than one Class? 
         Not possible. A Class can extend only one class but can implement any number of                        Interfaces. 
161. 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. So an Interface is allowed to extend more than one Interface. 
162. Can an Interface be final? 
         Not possible. Doing so so will result in compilation error. 
163. Can a class be defined inside an Interface? 
         Yes it's possible. 
164. Can an Interface be defined inside a class? 
         Yes it's possible. 
165. What is a Marker Interface? 
         An Interface which doesn't have any declaration inside but still enforces a mechanism. 
166. Which OO Concept is achieved by using overloading and overriding? 
        Polymorphism. 
167. Why does Java not support operator overloading?
         Operator overloading makes the code very difficult to read and maintain. To maintain                code simplicity, Java doesn't support operator overloading. 
168. Can we define private and protected modifiers for variables in interfaces? 
         No 
169. What modifiers are allowed for methods in an Interface? 
         Only public and abstract modifiers are allowed for methods in interfaces. 
170. What is an abstract method? 
        An abstract method is a method whose implementation is deferred to a subclass.
171. 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. 
172. What is casting? 
         There are two types of casting, casting between primitive numeric types and casting                    between object references. Casting between numeric types is used to convert larger                  values, such as double values, to smaller values, such as byte values. Casting between                object references is used to refer to an object by a compatible class, interface, or array              type reference. 
173. What is the return type of a program's main() method? 
         void.
174. If a variable is declared as private, where may the variable be accessed? 
        A private variable may only be accessed within the class in which it is declared. 
175. What is a native method? 
        A native method is a method that is implemented in a language other than Java. 
176. Is null a keyword? 
        The null value is not a keyword. 
177. Does a class inherit the constructors of its superclass? 
         A class does not inherit constructors from any of its superclasses. 
178. When does the compiler supply a default constructor for a class? 
        The compiler supplies a default constructor for a class if no other constructors are                     provided.
179.  Can a for statement loop indefinitely? 
          Yes, a for statement can loop indefinitely. For example, consider the following: for(;;).
180. What is a transient variable? 
         transient variable is a variable that may not be serialized. 
181. What method is used to specify a container's layout?

         The setLayout() method is used to specify a container's layout.

182. What state does a thread enter when it terminates its processing?

         When a thread terminates its processing, it enters the dead state.

183. What is the List interface?

         The List interface provides support for ordered collections of objects.

184. What is the Vector class?

         The Vector class provides the capability to implement a growable array of objects

185. What is an Iterator interface?

         The Iterator interface is used to step through the elements of a Collection.

186. Which method of the Component class is used to set the position and size of a component?

         setBounds() method is used to set the position and size of a component.

187. What is the difference between yielding and sleeping?

         When a task invokes its yield() method, it returns to the ready state. When a task invokes          its sleep()method, it returns to the waiting state.

188. Which java.util classes and interfaces support event handling?

         The EventObject class and the EventListener interface support event processing.

189. What is the immediate superclass of the Applet class?
         Panel.

190. Name three Component subclasses that support painting.

         The Canvas, Frame, Panel, and Applet classes support painting.

191. What value does readLine() return when it has reached the end of a file?

          The readLine() method returns null when it has reached the end of a file.

192. What is the immediate superclass of the Dialog class?
         Window.

193. What is clipping?

         Clipping is the process of confining paint operations to a limited area or shape.

194. What class is the top of the AWT event hierarchy?

         The java.awt.AWTEvent class is the highest-level class in the AWT event-class hierarchy.

195. 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.

196. What is the immediate superclass of Menu?
         MenuItem.

197. Which class is the immediate superclass of the MenuComponent class?
         Object.

198. What invokes a thread's run() method?

        After a thread is started, via its start() method or that of the Thread class, the JVM invokes         the thread'srun() method when the thread is initially executed

199. Name two subclasses of the TextComponent class.
         TextField and TextArea.

200. Which containers may have a MenuBar?

         Frame