Thursday 17 May 2012

Core Java Interview Questions4





76 Q What do you mean by immutable ? How to create an immutable object ?

A
Immutability means an object cannot be modified after it has been initialized.  There will not be any setter methods in an immutable class. And normally these classes will be final.



77 Q What is class loader in java ?

A
A class loader is a class that is responsible for loading the class. All JVM contains one class loader called primordial class loader.



78 Q What is a weak reference ?

A
A weak reference is the one that does nor prevent the referenced object from being garbage collected. The weak reference will not keep the object that it refers to alive. A weak reference is not counted as a reference in garbage collection. This will make the memory use more effective.



79 Q What is object cloning?

A
It is the process of duplicating an object so that two identical objects will exist in the memory at the same time.



80 Q What is object pooling?

A
Creating a large number of identical short lived objects is called object pooling. This helps to minimize the need of garbage collection and makes the memory use more effective.



81 Q What is garbage collection?

A
Garbage collection is the process of releasing memory used by unreferenced objects. It relieves the programmer from the process of manually releasing the memory used by objects .



82 Q What is the disadvantage of garbage collection?

A
It adds an overhead that can affect performance. Additionally there is no guarantee that the object will be garbage collected.



83 Q What is a Dictionary?

A
Dictionary is a parent class for any class that maps keys to values., In a dictionary every key is associated with at most one value.



84 Q What is JAR file ?

A
JAR stands for Java Archive. This is a file format that enables you to bundle multiple files into a single archive file.  A jar file will contains a manifest.mf file inside META-INF folder that describes the version and other features of jar file.



85 Q Why Java is not fully objective oriented ?

A
Due to the use of primitives in java, which are not objects.



86 Q What is a marker interface ?

A
An interface that contains no methods. Eg: Serializable, Cloneable, SingleThreadModel etc. It is used to just mark java classes that support certain capability.



87 Q What are tag interfaces?

A
Tag interface is an alternate name for marker interface.



88 Q What are the restrictions placed on static method ?

A
We cannot override static methods. We cannot access any object variables inside static method. Also the this reference also not available in static methods. 



89 Q What is JVM?

A
JVM stands for Java Virtual Machine. It is the run time for java programs. All are java programs are running inside this JVM only. It converts java byte code to OS specific commands. In addition to governing the execution of an application's byte codes, the virtual machine handles related tasks such as managing the system's memory, providing security against malicious code, and managing multiple threads of program execution.



90 Q What is JIT?

A JIT stands for Just In Time compiler. It compiles java byte code to native code.


Core Java interview questions – Part 6

91. What is java byte code?
Byte code is an sort of intermediate code. The byte code is processed by virtual machine.
92. What is method overloading?
Method overloading is the process of creating a new method with the same name and different signature.
93. What is method overriding?
Method overriding is the process of giving a new definition for an existing method in its child class.

94. What is finalize() ?
Finalize is a protected method in java. When the garbage collector is executes , it will first call finalize( ), and on the next garbage-collection it reclaim the objects memory. So finalize( ), gives you the chance to perform some cleanup operation at the time of garbage collection.
95. What is multi-threading?
Multi-threading is the scenario where more than one threads are running.
96. What is deadlock?
Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
97. What is the difference between Iterator and Enumeration?
Iterator differ from enumeration in two ways Iterator allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. And , method names have been improved.
98. What is the Locale class?
A Locale object represents a specific geographical, political, or cultural region
99. What is internationalization?
Internationalization is the process of designing an application so that it can be adapted to various languages and regions without changes.
100. What is anonymous class ?
An anonymous class is a type of inner class that don’t have any name.
101. What is the difference between URL and URLConnection?
A URL represents the location of a resource, and a URLConnection represents a link for accessing or communicating with the resource at the location.
102. What are the two important TCP Socket classes?
ServerSocket and Socket. ServerSocket is useful for two-way socket communication. Socket class help us to read and write through the sockets. getInputStream() and getOutputStream() are the two methods available in Socket class.
103. Strings are immutable. But String s=”Hello”; String s1=s+”World” returns HelloWorld how ?
Here actually a new object is created with the value of HelloWorld
104. What is classpath?
Classpath is the path where Java looks for loading class at run time and compile time.
105. What is path?
It is an the location where the OS will look for finding out the executable files and commands.


Core Java interview questions – Part 7

106. What is java collections?
Java collections is a set of classes, that allows operations on a collection of classes.
107. Can we compile a java program without main?
Yes, we can. In order to compile a java program, we don’t require any main method. But to execute a java program we must have a main in it (unless it is an applet or servlet). Because main is the starting point of a java program.

108. What is a java compilation unit.
A compilation unit is a java source file.
109. What are the restrictions when overriding a method ?
Overridden methods must have the same name, argument list, and return type (i.e., they must have the exact signature of the method we are going to override, including return type.) The overriding method cannot be less visible than the method it overrides( i.e., a public method cannot be override to private). The overriding method may not throw any exceptions that may not be thrown by the overridden method
110. What is static initializer block? What is its use?
A static initializer block is a block of code that declares with the static keyword. It normally contains the block of code that must execute at the time of class loading. The static initializer block will execute only once at the time of loading the class only.
111. How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown , the catch block of the try statement are examined in the order in which they appear. The first catch block that is capable of handling the exception is executed. The remaining catch blocks are ignored
112. How parameters are passed to methods in java program ?
All java method parameters in java are passed by value only. Obviously primitives are passed by value. In case of objects a copy of the reference is passed and so all the changes made in the method will persist.
113. If a class doesn’t have any constructors, what will happen?
If a class doesn’t have a constructor, the JVM will provide a default constructor for the class.
114. What will happen if a thread cannot acquire a lock on an object?
It enters to the waiting state until lock becomes available.
115. How does multithreading occurring on a computer with a single CPU?
The task scheduler of OS allocates an execution time for multiple tasks. By switching between different executing tasks, it creates the impression that tasks execute sequentially. But actually there is only one task is executed at a time.
116. What will happen if you are invoking a thread’s interrupt method while the thread is waiting or sleeping?
When the task enters to the running state, it will throw an InterruptedException.
117. What are the different ways in which a thread can enter into waiting state?
There are three ways for a thread to enter into waiting state. By invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method.
118. What are the the different ways for creating a thread?
A thread can be created by sub classing Thread, or by implementing the Runnable interface.
119. What is the difference between creating a thread by extending Thread class and by implementing Runnable interface? Which one should prefer?
When creating a thread by extending the Thread class, it is not mandatory to override the run method (If we are not overriding the run method , it is useless), because Thread class have already given a default implementation for run method. But if we are implementing Runnable , it is mandatory to override the run method. The preferred way to create a thread is by implementing Runnable interface, because it give loose coupling.
120. What is coupling?
Coupling is the dependency between different components of a system

Core Java interview questions – Part 8

121. What is an interface?
An interface is a collection of method declarations and constants. In java interfaces are used to achieve multiple inheritance. It sets a behavioral protocol to all implementing classes.
122. What is an abstract class?
An abstract class is an incomplete class. An abstract class is defined with the keyword abstract . We cannot create an object of the abstract class because it is not complete. It sets a behavioral protocol for all its child classes.

123. How will you define an interface?
An interface is defined with the keyword interface. Eg:
public interface MyInterface { }
124. How will you define an abstract class?
An abstract class is defined with the keyword abstract Eg:
public abstract class MyClass { }
125. What is any an anonymous class?
An anonymous class is a local class with no name.
126. What is a JVM heap?
The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap may be of a fixed size or may be expanded. The heap is created on virtual machine start-up.
127. What is difference between string and StringTokenizer?
StringTokenizer as its name suggests tokenizes a String supplied to it as an argument to its constructor and the character based on which tokens of that string are to be made. The default tokenizing character is space ” “.
128. What is the difference between array and ArrayList ?
Array is collection of same data type. Array size is fixed, It cannot be expanded. But ArrayList is a grow able collection of objects. ArrayList is a part of Collections Framework and can work with only objects.
129. What is difference between java.lang .Class and java.lang.ClassLoader? What is the hierarchy of ClassLoader ?
Class ‘java.lang.Class’ represent classes and interfaces in a running Java application. JVM construct ‘Class’ object when class in loaded. Where as a ClassLoader is also a class which loads the class files into memory in order for the Java programs to execute properly. The hierarchy of ClassLoaders is:
Bootstrap ClassLoaders
Extensive ClassLoaders
System Classpath ClassLoaders
Application ClassLoaders
130. What is daemon thread?
Threads which are running on the background are called demon threads. daemon thread is a thread which doesn’t give any chance to run other threads once it enters into the run state it doesn’t give any chance to run other threads. Normally it will run forever, but when all other non-daemon threads are dead, daemon thread will be killed by JVM
131. What is a green thread?
Native threads can switch between threads preemptively. Green threads switch only when control is explicitly given up by a thread ( Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU. Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads – they are all using the same memory. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits.
132. What is volatile variable?
A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in “main” memory. Volatile modifier requests the JVM to always access the shared copy of the variable so the its most current value is always read.
133. Why java does not support multiple inheritance?
Because the multiple inheritance causes the redundancy. Also we cannot solve diamond problem.
134. What is diamond problem?
The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common super class
135. How many JVM’s we can run in a system?
Any number of JVMs can run in a system. Whenever we issue the command ‘java’ a new JVM will start.


Core Java interview questions – Part 9

136. Why Java is not 100% pure object oriented language?
Because java uses primitives.
137. Why ArrayList is faster than Vector?
Because Vector is synchronized. Synchronization reduces the performance.
138. What is the security mechanism used in java?
Java uses sand box security model.

139. What is sandbox?
A sandbox is a security mechanism for safely running programs. The sandbox typically provides a tightly-controlled set of resources for guest programs to run in, such as scratch space on disk and memory.
140. What is phantom memory?
Phantom memory is the memory that does not exist in reality.
141. What is reflection?
Reflection is the process of finding out the different features of a class dynamically.
142. What are the differences between JIT and HotSpot?
The Hotspot VM is a collection of techniques, the most important of which is called adaptive optimization. The original JVMs interpreted byte codes one at a time. Second-generation JVMs added a JIT compiler, which compiles each method to native code upon first execution, then executes the native code. Thereafter, whenever the method is called, the native code is executed. The adaptive optimization technique used by Hotspot is a hybrid approach, one that combines byte code interpretation and run-time compilation to native code. Hotspot, unlike a regular JIT compiling VM, doesn’t do “premature optimization”
143. What are the advantages and disadvantages of reference counting in garbage collection?
An advantage of this scheme is that it can run in small chunks of time closely linked with the execution of the program. These characteristic makes it particularly suitable for real-time environments where the program can’t be interrupted for very long time. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another. Another disadvantage is the overhead of incrementing and decrement the reference count each time. Because of these disadvantages, reference counting currently is out of favor.
144. How would you implement a thread pool?
The ThreadPool class is a generic implementation of a thread pool, which takes the following input Size of the pool to be constructed and name of the class which implements Runnable (which has a visible default constructor) and constructs a thread pool with active threads that are waiting for activation. once the threads have finished processing they come back and wait once again in the pool.
145. What is the difference between throw and throws clause?
throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to tell the compiler that we haven’t handled the exception, so that the exception will be handled by the calling function.
156. What is JAR file?
A JAR file (short for Java Archive) is a ZIP file used to distribute a set of Java classes. It is used to store compiled Java classes and associated meta data that can constitute a program
147. What is a classloader?
A class loader is an object that is responsible for loading classes.

148. What is the difference between Comparable and Comparator ?

The Comparable is for natural ordering and Comparator is for custom ordering. But we can override the compareTo method of comparable interface to give a custom ordering.
149. What is the difference between List, Set and Map?
A Set is a collection that has no duplicate elements. A List is a collection that has an order associated with its elements. A map is a way of storing key/value pairs. The way of storing a Map is similar to two-column table.
150. What is the difference between Exception and Error ?
Error is unrecoverable.

No comments:

Post a Comment