Saturday, April 20, 2013

Java Interview Questions and Answers Part 5

Q31. What happens when a thread cannot acquire a lock on an object?

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.

Q32. What is the difference between Process and Thread?

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.

Q33. Can an inner class declared inside of a method access local variables of this method?

It's possible if these variables are final.

Q34. What can go wrong if you replace &emp;&emp; with &emp; in the following code:
String a=null; if (a!=null && a.length()>10) {...}

A single ampersand here would lead to a NullPointerException.

Q35. What is the Vector class?

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

Q36. What modifiers may be used with an inner class that is a member of an outer class?

A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Q37. If a method is declared as protected, where may the method be accessed?

A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.

Q38. What is an Iterator interface?

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

Q39. How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

Q40. What's the main difference between a Vector and an ArrayList?

Java Vector class is internally synchronized and ArrayList is not.

No comments:

Post a Comment

Automatic Traffic Exchange

YallaTech Facebook page