AdSense

AdSense3

Saturday, 4 July 2015

Java Important Interview Questions - 3

51. Can we declare abstract method as final?


Answer:
No, we can not declare abstract method as final. We have to proved implementation to 
 abstract methods in subclasses.

52. Can we have finally block without catch block?


Answer:
Yes, we can have finally block without catch block.

53. What is pass by value and pass by reference?


Answer:
Pass by value: Passing a copy of the value, not the original reference.
Pass by reference: Passsing the address of the object, so that you can access the original object

54. Can we declare main method as private?


Answer:
Yes, we can declare main method as private. It compiles without any errors, but in runtime,
 it says main method is not public.

55. What is the difference between preemptive scheduling and time slicing?


Answer:
Preemptive scheduling: The highest priority task executes until it enters
the waiting or dead states or a higher priority task comes into existence.

Time slicing: A task executes for a predefined slice of time and then reenters
the pool of ready tasks. The scheduler then determines which task should execute
next, based on priority and other factors.

56. Can non-static member classes (Local classes) have static members?


Answer:
No, non-static member classes cannot have static members. Because,
an instance of a non-static member class or local class must be
created in the context of an instance of the enclosing class. You
can declare constants, means static final variables.

57. What are the environment variables do we neet to set to run Java?


Answer:
We need to set two environment variables those are PATH and CLASSPATH.

58. Can you serialize static fields of a class?


Answer:
Since static fields are not part of object state, they are part of class, serialization ignores the 
static fields.

59. What is the difference between declaring a variable and defining a variable?


Answer:
When variable declaration we just mention the type of the variable and it's name, it does not have any 
reference to live object. But defining means combination of declaration and initialization. The examples 
are as given below:

Declaration:
List list;
Defining:
List list = new ArrayList();

60. Where can we use serialization?


Answer:
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of 
an object is to be saved, objects need to be serialized.

61. What modifiers are allowed for methods in an Interface?


Answer:
Only public and abstract modifiers are allowed for methods in an interfaces.

62. What is the purpose of Runtime and System class?


Answer:
The purpose of the Runtime class is to provide access to the Java runtime system. The runtime information
 like memory availability, invoking the garbage collector, etc.

The purpose of the System class is to provide access to system resources. It contains accessibility to standard
 input, standart output, error output streams, current time in millis, terminating the application, etc.

63. Which one is faster? ArrayList or Vector? Why?


Answer:
ArrayList is faster than Vector. The reason is synchronization. Vector is synchronized. As we know 
synchronization reduces the performance.

64. What is the difference between static synchronized and synchronized methods?


Answer:
Static synchronized methods synchronize on the class object. If one thread is executing a static synchronized
 method, all other threads trying to execute any static synchronized methods will be blocked.

Non-static synchronized methods synchronize on this i.e. the instance of the class. If one thread is executing a synchronized method, all other threads trying to execute any synchronized methods will be blocked.

65. What is the order of catch blocks when catching more than one exception?


Answer:
When you are handling multiple catch blocks, make sure that you are specifing exception sub classes first,
 then followed by exception super classes. Otherwise we will get compile time error.

66. What is the difference between the prefix and postfix forms of the increment(++) operator?


Answer:
The prefix form first performs the increment operation and then returns the value of the increment operation. 
The postfix form first returns the current value of the expression and then performs the increment operation
 on that value. For example:

int count=1;
System.out.println(++count);

displays 2. And

int count=1;
System.out.println(count++);

displays 1.

67. What is hashCode?


Answer:
The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed
 by a hash-based data structure. We know that hash code is an unique id number allocated to an object by JVM.
 But actually speaking, Hash code is not an unique number for an object. If two objects are equals then these
 two objects should return same hash code. So we have to implement hashcode() method of a class in such way
 that if two objects are equals, ie compared by equal() method of that class, then those two objects must return
 same hash code. If you are overriding hashCode you need to override equals method also.

68. What is the difference between Hashtable and HashMap?


Answer:
The basic differences are Hashtable is synchronized and HashMap is not synchronized. Hashtable does not 
allow null values, and HashMap allows null values.

69. What are the restrictions when overriding a method?


Answer:
Overriding methods must have the same name, parameter list, and same 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.

70. What is the use of assert keyword?


Answer:
Java assertion feature allows developer to put assert statements in Java source code to help unit testing and 
debugging. Assert keyword validates certain expressions. It replaces the if block effectively and throws an AssertionError on failure.

71. What is adapter class?


Answer:
An adapter class provides the default implementation of all methods in an event listener interface. Adapter 
classes are very useful when you want to process only few of the events that are handled by a particular event
 listener interface. You can define a new class by extending one of the adapter classes and implement only those
 events relevant to you.

72. What is difference between break, continue and return statements?


Answer:
The break statement results in the termination of the loop, it will come out of the loop and stops further iterations. 
The continue statement stops the current execution of the iteration and proceeds to the next iteration. 
The return statement takes you out of the method. It stops executing the method and returns from the method execution.

73. When does the compiler provides the default constructor?


Answer:
The compiler provides a default constructor if no other constructors are available in the class. In case the class 
contains parametarized constructors, compiler does not provide the default constructor.

74. What are the differences between C++ and Java.


Answer:
Java doesnot support pointers. Pointers are tricky to use and troublesome.
Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports 
multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
Java does not include structures or unions.
Java does not support destructors but adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
C++ requires explicit memory management, while Java includes automatic garbage collection.

75. What are the advantages of java package.


Answer:
Java packages helps to resolve naming conflicts when different packages have classes with the same names. 
This also helps you organize files within your project. For example, java.io package do something related
 to I/O and java.net package do something to do with network and so on. If we tend to put all .java files into
 a single package, as the project gets bigger, then it would become a nightmare to manage all your files.

No comments:

Post a Comment