Core Java Study guide and interview questions - Part 10


This is the 10th part of the 500+ Core JAVA interview questions and answers. These questions can also be used as a quick study guide for preparing for an interview or for any Core JAVA concepts revision. These questions cover all the topics in core JAVA from basics to advanced concepts.

51)Do I need to import java.lang package any time? Why ?
No. It is by default loaded internally by the JVM.

52)Can we define private and protected modifiers for variables in interfaces?
No, they are implicitely public.

53)Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.

54)What is package?
A package is a group of similar type of classes interfaces and subpackages. It provides access protection and removes naming collision.

55)What is static import ?
By static import, we can access the static members of a class directly, there is no to qualify it with the class name.

56) What is Exception Handling?
Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

57)What is difference between Checked Exception and Unchecked Exception?
1)Checked Exception:
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.
2)Unchecked Exception:
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

58)What is the base class for Error and Exception?
Throwable.

59)Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

60)What is finally block?
finally block is a block that is always executed.

61)Can finally block be used without catch?
Yes, by try block. finally must be followed by either try or catch.

62)Is there any case when finally will not be executed?
finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

63)What is the meaning of immutable in terms of String?
The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed..

64)What is shutdown hook?
The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.

65)Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?
Yes but only unchecked exception not checked. more details...

66)What is exception propagation ?
Forwarding the exception object to the invoking method is known as exception propagation.

67)Can an exception be rethrown?
Yes.

68)Why java uses the concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

69)How can we create immutable class in java ?
We can create immutable class as the String class by defining final class

70)How many ways we can create the string object?
There are two ways to create the string object, by string literal and by new keyword.

71)Can a class have an interface?
Yes, it is known as nested interface.

72)How many objects will be created in the following code?
String s1="Welcome";
String s2="Welcome";
String s3="Welcome";
Only one object.

73)What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.

74)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class. .

75) Can we access the non-final local variable, inside the local inner class?
No, local variable must be constant if you want to access it in local inner class.

76) What is multithreading?
Multithreading is a process of executing multiple threads simultaneously.Its main advantage is:
 •Threads share the same address space.
 •Thread is lightweight.
 •Cost of communication between process is low.

77)Can we make the user thread as daemon thread if thread is started?
No, if you do so, it will throw IllegalThreadStateException

78)Can an Interface have a class?
Yes, they are static implicitely.

79) What is the purpose of Synchnorized block?
•Synchronized block is used to lock an object for any shared resource.
•Scope of synchronized block is smaller than the method.

80) What is static synchronization?
If you make any static method as synchronized, the lock will be on the class not on object.

81) Is it possible to start a thread twice?
No, there is no possibility to start a thread twice. If we does, it throws an exception.

82)How many objects will be created in the following code?
String s=new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).

83)Why string objects are immutable in java?
Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

84) Is there any difference between nested classes and inner classes?
Yes ofcourse! inner classes are non-static nested classes i.e. inner classes are the part of nested classes.

85)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.

86) What is synchronization?
Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
1.To prevent thread interference.
2.To prevent consistency problem.

87) What about the daemon threads?
The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads. .

88) What does join() method?
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.

89)What is the difference between preemptive scheduling and time slicing?
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under 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.

90)What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.

91) Can we call the run() method instead of start()?
yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.

92)What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

93)What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.

94) What is thread?
A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

95)When should we interrupt a thread?
We should interrupt a thread if we want to break out the sleep or wait state of a thread.

96)Can Java object be locked down for exclusive use by a given thread?
Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

97)What is the difference between notify() and notifyAll()?
notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.

98) What is Garbage Collection?
Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.

99) What is gc()?
gc() is a daemon thread.gc() method is defined in System class that is used to send request to JVM to perform garbage collection.

100) What is the purpose of finalize() method?
finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

Next : 1000+ core JAVA quick study material & interview Questions and Answers




What Others are Reading Now...

0 Comment to "Core Java Study guide and interview questions - Part 10"

Post a Comment