Java Interview Questions Answers

Question - 61 : - What is the catch or declare rule for method declarations?

Answer - 61 : - If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

Question - 62 : - What is the purpose of the enableEvents() method?

Answer - 62 : - The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.

Question - 63 : - What is the purpose of the File class?

Answer - 63 : - The File class is used to create objects that provide access to the files and directories of a local file system.

Question - 64 : - Which class should you use to obtain design information about an object?

Answer - 64 : - The Class class is used to obtain information about an object's design.

Question - 65 : - In which case would you choose a static inner class?

Answer - 65 : - Interesting one, static inner classes can access the outer class's protected and private fields. This is both a positive and a negative point for us since we can, in essence, violate the encapsulation of the outer class by mucking up the outer class's protected and private fields. The only proper use of that capability is to write white-box tests of the class -- since we can induce cases that might be very hard to induce via normal black-box tests (which don't have access to the internal state of the object). Second advantage,if I can say, is that, we can this static concept to impose restriction on the inner class. Again as discussed in earlier point, an Inner class has access to all the public, private and protected members of the parent class. Suppose you want to restrict the access even to inner class, how would you go ahead? Making the inner class static enforces it to access only the public static members of the outer class( Since, protected and private members are not supposed to be static and that static members can access only other static members). If it has to access any non-static member, it has to create an instance of the outer class which leads to accessing only public members.

Question - 66 : - What is a task's priority and how is it used in scheduling?

Answer - 66 : - A task's priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.

Question - 67 : - Can an anonymous class be declared as implementing an interface and extending a class?

Answer - 67 : - An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

Question - 68 : - Why do threads block on I/O?

Answer - 68 : - Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.

Question - 69 : - ArithmeticException?

Answer - 69 : - The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Question - 70 : - What is the difference between Process and Thread?

Answer - 70 : - A process can contain multiple threads. In most multithreading operating systems, a process gets its own memory address space; a thread doesn't. Threads typically share the heap belonging to their parent process. For instance, a JVM runs in a single process in the host O/S. Threads in the JVM share the heap belonging to that process; that's why several threads may access the same object. Typically, even though they share a common heap, threads have their own stack space. This is how one thread's invocation of a method is kept separate from another's. This is all a gross oversimplification, but it's accurate enough at a high level. Lots of details differ between operating systems. Process vs. Thread A program vs. similar to a sequential program an run on its own vs. Cannot run on its own Unit of allocation vs. Unit of execution Have its own memory space vs. Share with others Each process has one or more threads vs. Each thread belongs to one process Expensive, need to context switch vs. Cheap, can use process memory and may not need to context switch More secure. One process cannot corrupt another process vs. Less secure. A thread can write the memory used by another thread