AdSense

AdSense3

Friday 3 July 2015

Java Important Interview Questions - 2

26. What are the types of ResultSet?


Answer:
The type of a ResultSet object determines the level of its functionality in
two areas: the ways in which the cursor can be manipulated, and how concurrent
changes made to the underlying data source are reflected by the ResultSet object.
The sensitivity of a ResultSet object is determined by one of three different
ResultSet types:

TYPE_FORWARD_ONLY:
 The result set cannot be scrolled; its cursor moves forward only, from
 before the first row to after the last row. The rows contained in the
 result set depend on how the underlying database generates the results.
 That is, it contains the rows that satisfy the query at either the time
 the query is executed or as the rows are retrieved.
 
TYPE_SCROLL_INSENSITIVE:
 The result can be scrolled; its cursor can move both forward and backward
 relative to the current position, and it can move to an absolute position.
 The result set is insensitive to changes made to the underlying data source
 while it is open. It contains the rows that satisfy the query at either the
 time the query is executed or as the rows are retrieved.
 
TYPE_SCROLL_SENSITIVE:
 The result can be scrolled; its cursor can move both forward and backward
 relative to the current position, and it can move to an absolute position.
 The result set reflects changes made to the underlying data source while
 the result set remains open.

27. What is difference between wait and sleep methods in java?


Answer:
sleep():
 It is a static method on Thread class. It makes the current thread into the
 "Not Runnable" state for specified amount of time. During this time, the thread
 keeps the lock (monitors) it has acquired.
 
wait():
 It is a method on Object class. It makes the current thread into the "Not Runnable"
 state. Wait is called on a object, not a thread. Before calling wait() method, the
 object should be synchronized, means the object should be inside synchronized block.
 The call to wait() releases the acquired lock.

28. What is servlet context?


Answer:
The servlet context is an interface which helps to communicate with
other servlets. It contains information about the Web application and
container. It is kind of application environment. Using the context, a
servlet can obtain URL references to resources, and store attributes that
other servlets in the context can use.

29. What happens if one of the members in a class does not implement Serializable interface?


Answer:
When you try to serialize an object which implements Serializable
interface, incase if the object includes a reference of an non
serializable object then NotSerializableException will be thrown.

30. What is race condition?


Answer:
A race condition is a situation in which two or more threads or
processes are reading or writing some shared data, and the final
result depends on the timing of how the threads are scheduled.
Race conditions can lead to unpredictable results and subtle
program bugs. A thread can prevent this from happening by locking
an object. When an object is locked by one thread and another
thread tries to call a synchronized method on the same object,
the second thread will block until the object is unlocked.

31. How to get current time in milli seconds?


Answer:
System.currentTimeMillis() returns the current time in milliseconds.
It is a static method, returns long type.

32. How can you convert Map to List?


Answer:
We know that Map contains key-value pairs, whereas a list contains
only objects. Since Entry class contains both key-value pair,
Entry class will helps us to convert from Map (HashMap) to
List (ArrayList). By using Map.entrySet() you will get Set
object, which intern you can use it to convert to list object.

Code:
public static void main(String a[]){
 Map<String, String> wordMap = new HashMap<String, String>();
 Set<Entry<String, String>> set = wordMap.entrySet();
 List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
}

33. What is strictfp keyword?


Answer:
By using strictfp keyword, we can ensure that floating point operations
take place precisely.

34. What is System.out in Java?


Answer:
Here out is an instance of PrintStream. It is a static member variable in
System class. This is called standard output stream, connected to console.

35. What is difference between ServletOuptputStream and PrintWriter?


Answer:
ServletOutputStream: ServletResponse.getOutputStream() returns a ServletOutputStream
  suitable for writing binary data in the response. The servlet
  container does not encode the binary data, it sends the raw data
  as it is.
  
PrintWriter: ServletResponse.getWriter() returns PrintWriter object which sends
  character text to the client. The PrintWriter uses the character
  encoding returned by getCharacterEncoding(). If the response's
  character encoding has not been specified then it does default
  character encoding.

36. What is java static import?


Answer:
By using static imports, we can import the static members from a class
rather than the classes from a given package.  For example, Thread class has
static sleep method, below example gives an idea:

import static java.lang.Thread;
public class MyStaticImportTest {
 public static void main(String[] a) {
  try{
   sleep(100);

37.When to use String and StringBuffer?


Answer:
We know that String is immutable object. We can not change the value
of a String object once it is initiated. If we try to change the value of
the existing String object then it creates new object rather than changing
the value of the existing object. So incase, we are going to do more
modificatios on String, then use StringBuffer. StringBuffer updates the
existing objects value, rather creating new object.

38. What is difference between StringBuffer and StringBuilder?


Answer:
The only difference between StringBuffer and StringBuilder is StringBuffer
is thread-safe, that is StringBuffer is synchronized.

39. What is wrapper class in java?


Answer:
Everything in java is an object, except primitives. Primitives are
int, short, long, boolean, etc. Since they are not objects, they cannot
return as objects, and collection of objects. To support this, java provides
wrapper classes to move primitives to objects. Some of the wrapper classes
are Integer, Long, Boolean, etc.

40. Is Iterator a Class?


Answer:
Iterator is an interface. It is not a class. It is used to iterate through each and every element
 in a list. Iterator is implemented Iterator design pattern.

41. What is java classpath?


Answer:
The classpath is an environment variable. It is used to let the compiler know where the class
 files are available for import.

42. Can a class in java be private?


Answer:
We can not declare top level class as private. Java allows
only public and default modifier for top level classes in java.
Inner classes can be private.

43. What is the initial state of a thread when it is started?


Answer:
When the thread is createdn and started, initially it will be in the ready state.

44. What is the super class for Exception and Error?


Answer:
The super class or base class for Exception and Error is Throwable.

45. What is Class.forName()?


Answer:
Class.forName() loads the class into the ClassLoader.

46. Can interface be final?


Answer:
No. We can not instantiate interfaces, so in order to make interfaces
useful we must create subclasses. The final keyword makes a class unable
to be extended.

47. What is the difference between exception and error?


Answer:
An error is an irrecoverable condition occurring at runtime like out of
memory error. These kind of jvm errors cannot be handled at runtime.
Exceptions are because of condition failures, which can be handled
easily at runtime.

48. What is default value of a local variables?


Answer:
The local variables are not initialized to any default values. We should
not use local variables with out initialization. Even the java compiler
throws error.

49. What is local class in java?


Answer:
In java, local classes can be defined in a block as in a
method body or local block.

50. Can we initialise uninitialized final variable?


Answer:
Yes. We can initialise blank final variable in constructor, only in construtor.
The condition here is the final variable should be non-static.

No comments:

Post a Comment