You are on page 1of 9

Q: What is the purpose of garbage collection in Java, and when is it used?

A: The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Q: Describe synchronization in respect to multithreading A: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors. Q: Explain different way of using thread?

A: The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help. Q: A: What are pass by reference and passby

value?
assby #alue

ass !y "eference means the passing the address itself rather than passing the value. means passing a copy of the value to be passed. What is !ash"ap

Q:

and "ap?

A: $ap is %nterface and &ashmap is class that implements that. Q: Difference between !ash"ap

and !ash#able?

A: The &ash$ap class is roughly e'uivalent to &ashtable, e(cept that it is unsynchronized and permits nulls. )&ash$ap allows null values as *ey and value whereas &ashtable doesnt allow+. &ash$ap does not guarantee that the order of the map will remain constant over time. &ash$ap is non synchronized and &ashtable is synchronized. Q: Difference between Vector and ArrayList?

A: #ector is synchronized whereas arraylist is not. Q: Difference between Swing and Awt?

A: AWT are heavy,weight componenets. -wings are light,weight components. &ence swing wor*s faster than AWT. Q: What is the difference between a constructor and a method? A: A constructor is a member function of a class that is used to create objects of that class. %t has the same name as the class itself, has no return type, and is invo*ed using the new operator. A method is an ordinary member function of a class. %t has its own name, a return type )which may be void+, and is invo*ed using the dot operator.

Q:

What is an $terators?

A: -ome of the collection classes provide traversal of their contents via a java.util.%terator interface. This interface allows you to wal* a collection of objects, operating on each object in turn. "emember when using %terators that they contain a snapshot of the collection at the time the %terator was obtained. generally it is not advisable to modify the collection itself while traversing an %terator.

Q:

State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

A: public : ublic class is visible in other pac*ages, field is visible everywhere )class must be public too+ private : rivate variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature. protected : %s available to all classes in the same pac*age and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different pac*age from the class that owns the protected feature. default :What you get by default ie, without any access modifier )ie, public private or protected+.%t means that it is visible to all within a particular pac*age. Q: What is an abstract class? A: Abstract class must be e(tended/subclassed )to be useful+. %t serves as a template. A class that is abstract may not be instantiated )ie, you may not call its constructor+, abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. Q: What is static in %ava?

A: -tatic means one per class, not one for each object no matter how many instance of a class might e(ist. This means that you can use them without creating an instance of a class.-tatic methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. &owever, you can't override a static method with a nonstatic method. %n other words, you can't change a static method into an instance method in a subclass. Q: What is final? A: A final class can't be e(tended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. 0ou can't change value of a final variable )is a constant+.

What if the main method is declared as private? A: The program compiles properly but at runtime it will give 1$ain method not public.1 message. Q: What if the static modifier is removed from the signature of the main method? A: rogram compiles. !ut at runtime throws an error 12o-uch$ethod3rror1.

Q: What if $ write static public void instead of public static void? A: rogram compiles and runs properly.

Q: What if $ do not provide the &tring array as the argument to the method? A: rogram compiles but throws a runtime error 12o-uch$ethod3rror1.

Q: What is the first argument of the &tring array in main method? A: The -tring array is empty. %t does not have any element. This is unli*e 4/455 where the first element by default is the program name. Q: $f $ do not provide any arguments on the command line, then the &tring array of "ain method will be empty of null? A: %t is empty. !ut not null. Q: !ow can one prove that the array is not null but empty? A: rint args.length. %t will print 6. That means it is empty. !ut if it would have been null then it would have thrown a 2ull ointer3(ception on attempting to print args.length.

Q: What environment variables do $ need to set on my machine in order to be able to run Java programs? A: 47A-- AT& and AT& are the two variables. Q: 'an an application have multiple classes having main method? A: 0es it is possible. While starting the application we mention the class name to be run. The J#$ will loo* for the $ain method only in the class whose name you have mentioned. &ence there is not conflict amongst the multiple classes having main method. Q: 'an $ have multiple main methods in the same class? A: 2o the program fails to compile. The compiler says that the main method is already defined in the class. Q: Do $ need to import %ava

lang pac(age any time? Why ?

A: 2o. %t is by default loaded internally by the J#$. Q: 'an $ import same pac(age)class twice? Will the J*"

load the pac(age twice at runtime?

A: 8ne can import the same pac*age or same class multiple times. 2either compiler nor J#$ complains abt it. And the J#$ will internally load the class only once no matter how many times you import the same class. Q: What are 'hec(ed and +n'hec(ed

,-ception?

A: A chec*ed e(ception is some subclass of 3(ception )or 3(ception itself+, e(cluding class "untime3(ception and its subclasses. $a*ing an e(ception chec*ed forces client programmers to deal with the possibility that the e(ception will be thrown. eg, %83(ception thrown by java.io.9ile%nput-tream's read)+ method: ;nchec*ed e(ceptions are "untime3(ception and any of its subclasses. 4lass 3rror and its subclasses also are unchec*ed. With an unchec*ed e(ception, however, the compiler doesn't force client programmers either to catch the e(ception or declare it in a throws clause. %n fact, client programmers may not even *now that the e(ception could be thrown. eg, -tring%nde(8ut8f!ounds3(ception thrown by -tring's charAt)+ method: 4hec*ed e(ceptions must be caught at compile time. "untime e(ceptions do not need to be. 3rrors often cannot be.

Q: What is .verriding? A: When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass. When the method is invo*ed for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. $ethods may be overridden to be more public, not more private. Q: What are different types of inner classes? A: Nested top-level classes,

Member classes, Local classes, Anonymous classes

Nested top-level classes, %f you declare a class within a class and specify the static modifier, the compiler treats the class just li*e any other top,level class. Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a pac*age. eg, outer.inner. Top,level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested top,level variety.

Member classes , $ember inner classes are just li*e other member methods and member variables

and access to the member class is restricted, just li*e methods and variables. This means a public member class acts similarly to a nested top,level class. The primary difference between member classes and nested top,level classes is that member classes have access to the specific instance of the enclosing class.

Local classes , 7ocal classes are li*e local variables, specific to a bloc* of code. Their visibility is only
within the bloc* of their declaration. %n order for the class to be useful beyond the declaration bloc*, it would need to implement a more publicly available interface.!ecause local classes are not members, the modifiers public, protected, private, and static are not usable. Anonymous classes , Anonymous inner classes e(tend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor. Are the imports chec(ed for validity at compile time? e g will the code containing an import such as %ava

lang A/'D

compile?
A: 0es the imports are chec*ed for the semantic validity at compile time. The code containing above line of import will not compile. %t will throw an error saying,can not resolve symbol symbol < class A!4= location< pac*age io import java.io.A!4=. Q: Does importing a pac(age imports the

subpac(ages as well? e g Does importing com "y#est 0 also import

com "y#est +nit#ests 0?


A: 2o you will have to import the subpac*ages e(plicitly. %mporting com.$yTest.> will import classes in the pac*age $yTest only. %t will not import any class in any of it's subpac*age. Q: What is the difference between declaring a variable and defining a variable? A: %n declaration we just mention the type of the variable and it's name. We do not initialize it. !ut defining means declaration 5 initialization. e.g -tring s. is just a declaration while -tring s ? new -tring )1abcd1+. 8r -tring s ? 1abcd1. are both definitions. Q: What is the default value of an ob%ect reference declared as an instance variable? A: null unless we define it e(plicitly.

Q: 'an a top level class be private or protected? A: 2o. A top level class can not be private or protected. %t can have either 1public1 or no modifier. %f it does not have a modifier it is supposed to have a default access.%f a top level class is declared as private the compiler will complain that the 1modifier private is not allowed here1. This means that a top level class can not be private. -ame is the case with protected. Q: What type of parameter passing does Java support? A: Java supports both pass by value as well as pass by reference. Q: 1rimitive data types are passed by reference or pass by value? A: rimitive data types are passed by value.

Q: .b%ects are passed by value or by reference? A: 8bjects are always passed by reference. Thus any modifications done to an object inside the called method will always reflect in the caller method. Q: What is serialization? A: -erialization is a mechanism by which you can save the state of an object by converting it to a byte stream. Q: !ow do $ serialize an ob%ect to a file? A: The class whose instances are to be serialized should implement an interface -erializable. Then you pass the instance to the 8bject8utput-tream which is connected to a fileoutputstream. This will save the object to a file. Q: Which methods of &erializable

interface should $ implement?

A: The serializable interface is an empty interface, it does not contain any methods. -o we do not implement any methods. Q: !ow can $ customize the

seralization process? i e how can one have a control over the serialization

process?
A: 0es it is possible to have control over serialization process. The class should implement 3(ternalizable interface. This interface contains two methods namely read3(ternal and write3(ternal. 0ou should implement these methods and write the logic for customizing the serialization process. Q: What is the common usage of serialization? A: Whenever an object is to be sent over the networ*, objects need to be serialized. $oreover if the state of an object is to be saved, objects need to be serilazed. Q: What is ,-ternalizable

interface?

A: 3(ternalizable is an interface which contains two methods read3(ternal and write3(ternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods. Q: What happens to the ob%ect references included in the ob%ect? A: The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object

is serialized, all the included objects are also serialized alongwith the original obect. Q:

What one should take care of while serializing the object?

A: 8ne should ma*e sure that all the included objects are also serializable. %f any of the objects is not serializable then it throws a 2ot-erializable3(ception. Q:

What happens to the static fields of a class during serialization? serialized object?

re these fields serialized as a part of each

A: 0es the static fields do get serialized. %f the static field is an object then it must have implemented -erializable interface. The static fields are serialized as a part of every object. !ut the commonness of the static fields across all the instances is maintained even after serialization.

Does Java provide any construct to find out the size of an ob%ect? A: 2o there is not sizeof operator in Java. -o there is not direct way to determine the size of an object directly in Java. Q: Does importing a pac(age imports the

subpac(ages as well? e g Does importing com "y#est 0 also import

com "y#est +nit#ests 0?


A: "ead the system time just before the method is invo*ed and immediately after method returns. Ta*e the time difference, which will give you the time ta*en by a method for e(ecution.

To put it in code...
long start ? -ystem.currentTime$illis )+. method )+. long end ? -ystem.currentTime$illis )+. -ystem.out.println )1Time ta*en for e(ecution is 1 5 )end , start++. "emember that if the time ta*en for e(ecution is too small, it might show that it is ta*ing zero milliseconds for e(ecution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing. Q: What are wrapper classes? A: Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. %nteger, 4haracter, =ouble etc. Q: Why do we need wrapper classes? A: %t is sometimes easier to deal with primitives as objects. $oreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. !ecause of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method e(pects an object. Q: What are chec(ed e-ceptions? A: 4hec*ed e(ception are those which the Java compiler forces you to catch. e.g. %83(ception are chec*ed 3(ceptions. Q: What are runtime e-ceptions?

A: "untime e(ceptions are those e(ceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not chec*ed by the compiler at compile time. Q: What is the difference between error and an e-ception? A: An error is an irrecoverable condition occurring at runtime. -uch as 8ut8f$emory error. These J#$ errors and you can not repair them at runtime. While e(ceptions are conditions that occur because of bad input etc. e.g. 9ile2ot9ound3(ception will be thrown if the specified file does not e(ist. 8r a 2ull ointer3(ception will ta*e place if you try using a null reference. %n most of the cases it is possible to recover from an e(ception )probably by giving user a feedbac* for entering proper values etc.+. Q: !ow to create custom e-ceptions? A: 0our class should e(tend class 3(ception, or some more specific type thereof. Q: $f $ want an ob%ect of my class to be thrown as an e-ception ob%ect, what should $ do? A: The class should e(tend from 3(ception class. 8r you can e(tend your class from some more precise e(ception type also. Q: $f my class already e-tends from some other class what should $ do if $ want an instance of my class to be thrown as an e-ception ob%ect? A: 8ne can not do anytihng in this scenarion. !ecause Java does not allow multiple inheritance and does not provide any e(ception interface as well. Q: What happens to an unhandled

e-ception?

A: 8ne can not do anytihng in this scenarion. !ecause Java does not allow multiple inheritance and does not provide any e(ception interface as well. Q: !ow does an e-ception permeate through the code? A: An unhandled e(ception moves up the method stac* in search of a matching When an e(ception is thrown from a code which is wrapped in a try bloc* followed by one or more catch bloc*s, a search is made for matching catch bloc*. %f a matching type is found then that bloc* will be invo*ed. %f a matching type is not found then the e(ception moves up the method stac* and reaches the caller method. -ame procedure is repeated if the caller method is included in a try catch bloc*. This process continues until a catch bloc* handling the appropriate type of e(ception is found. %f it does not find such a bloc* then finally the program terminates. Q: What are the different ways to handle e-ceptions? A: There are two ways to handle e(ceptions, @. !y wrapping the desired code in a try bloc* followed by a catch bloc* to catch the e(ceptions. and A. 7ist the desired e(ceptions in the throws clause of the method and let the caller of the method hadle those e(ceptions. Q: Q: What is the basic difference between the 2 approaches to e-ception

handling 34 try catch bloc( and 24

specifying the candidate e-ceptions in the throws clause? When should you use which approach?
A: %n the first approach as a programmer of the method, you urself are dealing with the e(ception. This is fine if you are in a best position to decide should be done in case of an e(ception. Whereas if it is not the responsibility of the method to deal with it's own e(ceptions, then do not use this approach. %n this case use the second approach. %n the second approach we are forcing the caller of the method to catch the e(ceptions, that the method is li*ely to throw. This is often the approach library creators use. They list the e(ception in the throws clause and we must catch them. 0ou will find the same approach throughout the java libraries we use.

Q: $s it necessary that each try bloc( must be followed by a catch bloc(? A: %t is not necessary that each try bloc* must be followed by a catch bloc*. %t should be followed by either a catch bloc* 8" a finally bloc*. And whatever e(ceptions are li*ely to be thrown should be declared in the throws clause of the method. Q: $f $ write return at the end of the try bloc(, will the finally bloc( still e-ecute? A: 0es even if you write return as the last statement in the try bloc* and no e(ception occurs, the finally bloc* will e(ecute. The finally bloc* will e(ecute and then the control return. Q: $f $ write &ystem

e-it 5678 at the end of the try bloc(, will the finally bloc( still e-ecute?

A: 2o in this case the finally bloc* will not e(ecute because when you say -ystem.e(it )6+. the control immediately goes out of the program, and thus finally never e(ecutes !ow are .bserver and .bservable used? A: 8bjects that subclass the 8bservable class maintain a list of observers. When an 8bservable object is updated it invo*es the update)+ method of each of its observers to notify the observers that it has changed state. The 8bserver interface is implemented by objects that observe 8bservable objects.

Q: What is synchronization and why is it important? A: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors. Q: !ow does Java handle integer overflows and underflows? A: %t uses those low order bytes of the result that can fit into the size of the type allowed by the operation. Q: Does garbage collection guarantee that a program will not run out of memory? A: Barbage collection does not guarantee that a program will not run out of memory. %t is possible for programs to use up memory resources faster than they are garbage collected. %t is also possible for programs to create objects that are not subject to garbage collection . Q: What is the difference between preemptive scheduling and time slicing? A: ;nder preemptive scheduling, the highest priority tas* e(ecutes until it enters the waiting or dead states or a higher priority tas* comes into e(istence. ;nder time slicing, a tas* e(ecutes for a predefined slice of time and then reenters the pool of ready tas*s. The scheduler then determines which tas* should e(ecute ne(t, based on priority and other factors. Q: When a thread is created and started, what is its initial state? A: A thread is in the ready state after it has been created and started. Q: What is the purpose of finalization?

A: The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

Q: What is the 9ocale class? A: The 7ocale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region. Q: What is the difference between a while statement and a do statement? A: A while statement chec*s at the beginning of a loop to see whether the ne(t loop iteration should occur. A do statement chec*s at the end of a loop to see whether the ne(t iteration of a loop should occur. The do statement will always e(ecute the body of a loop at least once. Q: What is the difference between static and non:static variables? A: A static variable is associated with the class as a whole rather than with specific instances of a class. 2on,static variables ta*e on uni'ue values with each object instance. Q: !ow are this57 and super57 used with constructors? A: this)+ is used to invo*e a constructor of the same class. super)+ is used to invo*e a superclass constructor. Q: What are synchronized methods and synchronized statements? A: -ynchronized methods are methods that are used to control access to an object. A thread only e(ecutes a synchronized method after it has ac'uired the loc* for the method's object or class. -ynchronized statements are similar to synchronized methods. A synchronized statement can only be e(ecuted after a thread has ac'uired the loc* for the object or class referenced in the synchronized statement.

You might also like