Core Java Interview Question And Answare | Java Interview Question And Answer | Java Interview Question And Answer For Fresher | Set 12

1.What modifiers may be used with top-level class?
 Ans: public, abstract and final can be used for top-level class.

2.What are inner class and anonymous class?
 Ans: Inner class : classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.
        Anonymous class : Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.

3.What is the purpose of the wait(), notify(), and notifyAll() methods?
 Ans: The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate each other.

4.What are the high-level thread states?
 Ans: The high-level thread states are ready, running, waiting, and dead.

5.What is the difference between yielding and sleeping?
 Ans: 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.

6.What happens when a thread cannot acquire a lock on an object?
 Ans: If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object's lock, it enters the waiting state until the lock becomes available.

7.What is the difference between Process and Thread?
 Ans: 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

8.Can an inner class declared inside of a method access local variables of this method?
 
Ans: It's possible if these variables are final.

9.What is a native method?
 Ans: A native method is a method that is implemented in a language other than Java.

10.How can you write a loop indefinitely?
 Ans: for(;;)--for loop; while(true)--always true, etc.

Previous Next


:: Click the links below for similar type Questions and answers ::

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes