java inside
Question :What is the difference between an Interface and an Abstract class?
Answer : An Abstract class declares have at least one instance method that is declared abstract which will be implemented by the subclasses. An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior.
Question :What is the purpose of garbage collection in Java, and when is it used?
Answer : 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.
Question :Describe synchronization in respect to multithreading.
Answer : 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.
Question :Explain different way of using thread?
Answer : 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.
Question :What are pass by reference and passby value?
Answer : Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed.
Question :What is HashMap and Map?
Answer : Map is Interface and Hashmap is class that implements that.
Question :Difference between HashMap and HashTable?
Answer : The HashMap class is roughly eQuestion uivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is non synchronized and Hashtable is synchronized.
Question :Difference between Vector and ArrayList?
Answer : Vector is synchronized whereas arraylist is not.
Question :Difference between Swing and Awt?
Answer : AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT.
Question :What is the difference between a constructor and a method?
Answer : A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
Question :What is an Iterators?
Answer : Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
Question :State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items Question ualified by these modifiers.
Answer : public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private 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 : Is available to all classes in the same package 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 package from the class that owns the protected featu
Question :What if the main method is declared as private?
Answer : The program compiles properly but at runtime it will give "Main method not public." message.
Question :What if the static modifier is removed from the signature of the main method?
Answer : Program compiles. But at runtime throws an error "NoSuchMethodError".
Question :What if I write static public void instead of public static void?
Answer : Program compiles and runs properly.
Question :What if I do not provide the String array as the argument to the method?
Answer : Program compiles but throws a runtime error "NoSuchMethodError".
Question :What is the first argument of the String array in main method?
Answer : The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.
Question :If I do not provide any arguments on the command line, then the String array of Main method will be empty of null?
Answer : It is empty. But not null.
Question :How can one prove that the array is not null but empty?
Answer : Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.
Question :What environment variables do I need to set on my machine in order to be able to run Java programs?
Answer : CLASSPATH and PATH are the two variables.
Question :Can an application have multiple classes having main method?
Answer : Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.
Question :Can I have multiple main methods in the same class?
Answer : No the program fails to compile. The compiler says that the main method is already defined in the class.
Question :Do I need to import java.lang package any time? Why ?
Answer : No. It is by default loaded internally by the JVM.
Question :Can I import same package/class twice? Will the JVM load the package twice at runtime?
Answer : One can import the same package or same class multiple times. Neither compiler nor JVM complains abt it. And the JVM will internally load the class only once no matter how many times you import the same class.
Question :What are Checked and UnChecked Exception?
Answer : A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method·
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the
exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.
Question :What is Overriding?
Answer : 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 invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. Methods may be overridden to be more public, not more private.
Question :What are different types of inner classes?
Answer : Nested -level classes, Member classes, Local classes, Anonymous classes
Nested -level classes- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other -level class.
Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. -level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested -level variety.
Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested -level class. The primary difference between member classes and nested -level classes is that member classes have access to the specific instance of the enclosing class.
Local classes - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a
more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.
Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.
Question :Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?
Answer : Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol
symbol : class ABCD
location: package io
import java.io.ABCD;
Question :Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?
Answer : No you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it's subpackage.
Question :What is the difference between declaring a variable and defining a variable?
Answer : In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.
Question :What is the default value of an object reference declared as an instance variable?
Answer : null unless we define it explicitly.
Question :Can a level class be private or protected?
Answer : No. A level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.If a level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a level class can not be private. Same is the case with protected.
Question :What type of parameter passing does Java support?
Answer : Java supports both pass by value as well as pass by reference.
Question :Primitive data types are passed by reference or pass by value?
Answer : Primitive data types are passed by value.
Question :Objects are passed by value or by reference?
Answer : Objects are always passed by reference. Thus any modifications done to an object inside the called method will always reflect in the caller method.
Question :What is serialization?
Answer : Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.
Question :How do I serialize an object to a file?
Answer : The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.
Question :Which methods of Serializable interface should I implement?
Answer : The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods.
Question :How can I customize the seralization process? i.e. how can one have a control over the serialization process?
Answer : Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.
Question :What is the common usage of serialization?
Answer : Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilazed.
Question :What is Externalizable interface?
Answer : Externalizable is an interface which contains two methods readExternal and writeExternal. 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.
Question :What happens to the object references included in the object?
Answer : 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.
Question :What one should take care of while serializing the object?
Answer : One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.
Question :What happens to the static fields of a class during serialization? Are these fields serialized as a part of each serialized object?
Answer : Yes the static fields do get serialized. If the static field is an object then it must have implemented Serializable interface. The static fields are serialized as a part of every object. But the commonness of the static fields across all the instances is maintained even after serialization.
Question :Does Java provide any construct to find out the size of an object?
Answer : No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.
Question :Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?
Answer : Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution.
To put it in code...
long start = System.currentTimeMillis ();
method ();
long end = System.currentTimeMillis ();
System.out.println ("Time taken for execution is " + (end - start));
Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing.
Question :What are wrapper classes?
Answer : Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.
Question :Why do we need wrapper classes?
Answer : It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because 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 expects an object.
Question :What are checked exceptions?
Answer : Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.
Question :What are runtime exceptions?
Answer : Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.
Question :What is the difference between error and an exception?
Answer : An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).
Question :How to create custom exceptions?
Answer : Your class should extend class Exception, or some more specific type thereof.
Question :If I want an object of my class to be thrown as an exception object, what should I do?
Answer : The class should extend from Exception class. Or you can extend your class from some more precise exception type also.
Question :If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?
Answer : One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well.
Question :What happens to an unhandled exception?
Answer : One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well.
Question :How does an exception permeate through the code?
Answer : An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates.
Question :What are the different ways to handle exceptions?
Answer : There are two ways to handle exceptions,
1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and
2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.
Question : What is the basic difference between the 2 approaches to exception handling...1> try catch block and 2> specifying the candidate exceptions in the throws clause?
When should you use which approach?
Answer : In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.
Question :Is it necessary that each try block must be followed by a catch block?
Answer : 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.
Question :If I write return at the end of the try block, will the finally block still execute?
Answer : Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.
Question :If I write System.exit (0); at the end of the try block, will the finally block still execute?
Answer : No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.
Question :What is the protocol used by server and client?
Answer : The protocol that is mainly used in java is HTTP and UDP.
Question :Diff between Application and Applet?
Answer : An application is a program that can be run on the client only. On the other hand Applets are the small programs that are transferred through internet, automatically installed and run as a part of web browser. We can also test the applet using appletviewer utility provided by the jdk.
Question :What is the difference between CGI and Servlet?
Answer : The main difference between the two is first process the client requests (to the server) one at a time where as in second requests are lined up and send to the server as a collection. The main advantage of using servlets is its security features. [Servlets are small programs that are executed on the server side of the connection]
Question :What is the use of Interface?
Answer : Interface are used when we know the functionality of a program but its implementation is not known to us. This way we can declare the constants and functions in the interface and later on they can be implemented by any program. [We can fully abstract a class from its implementation using this way. Thats why they are called interface]
Question :Why Java is not fully object oriented?
Answer : Because it doesn\’t support many features of OO paradigm like operater overloading. But it to some extent is better than C++ as it has the main function in the class itself thus providing a extra security to a program.
Question :Why java does not support multiple Inheritance?
Answer : Java do support a multiple inheritance but not in a way as C++ support. Instead it support by the means of interfaces. [The main reason why java doesn\’t support Multiple Inheritance is it allows same features to be inherited again and again and java being a secure language didn\’t allow anything that will lead to insecurity.
Question :What it the root class for all Java classes?
Answer : This the Object class which is the root of all classes in java.
Question :What is polymorphism?
Answer : As name suggest polymorphism consist of 2 words \’poly\’ means many and \’morphism\’ means form. Polymorphism is ability to take more than one form. In java one way to implement polymorphism is function overloading and other way is dynamic method dispatch (in which methods for the appropriate class are called at runtime).
Question :Suppose If we have variable ‘I‘ in run method, If I can create one or more thread each thread will occupy a separate copy or same variable will be shared?
Answer : I think this copy must be shared for each thread.
Question :What are virtual functions?
Answer : Virtual function is a way to achieve run time polymorphism in C++. It is defined in the base class with a keyword virtual and we need to override the same function in the derived class also. When called the function is called based on the object reference rather than object itself.
Question :Write down how will you create a binary Tree?
Answer : There is class in java that provides binary tree features using which we can create an empty binary tree and later on we can add, delete the elements into it. In C++ first we have to define a structure
struct tree
{
int element;
struct tree *left;
struct tree *right;
}
After that we can contruct the tree using tree algorithm.
Question :What are the traverses in Binary Tree?
Answer : There are 3 ways to traverese a binary tree:
1. Pre-order
2. In-order
3. Post-order
Question :Write a program for recursive Traverse?
Answer : I am writing the code snippet for inorder:
void inorder(node *tree)
{
if (tree!=NULL)
{
inorder(tree->left);
printf(\"%d \",tree->element); //process the root
inorder(tree->right);
}
}
The procedure for pre and post orders are same.
Question :What is client server computing?
Answer : Its a technology where one machine known as client request the service from another machine (called server) and server returns the appropriate response to client.
Question :What is Constructor and Virtual function? Can we call Virtual function in a constructor?
Answer : Constructor is a special method that used for initializing variables of the class. This is special as its name is same as the name of the class.
Question :Why we use OOPS concepts? What is its advantage?
Answer : OOPS defines real world entities called object which has data and methods to operate on that data. Its advantages are:
1. Being a real world entity object can define a problem in well defined manner which leads to a ease in programming.
2. It follows a bottom up approach.
3. Encapsulation is one of the key feature of OOPS i.e wrapping up data and functions in a single unit called class.
4. Data hiding: Data is hidden from the external world.
Question :What is the middleware ? What is the functionality of Webserver?
Middleware is a component that sits between client and server and allows every request from client to server through it. This not only increases performance but also reduces trafic between client and server as many request may be handled by the middleware itself.
Java implements middleware using Servlets at the lower level and using J2ee at the higher level.
Question :When we will use an Interface and Abstract class ?
Answer : Abstract class is used whenever we are having one or more methods in a class that are not abstract (means their implementation is given or think of). On the other hand interface is used when we don\’t know anything about the implementation of methods to be defined in the class.
Question :What is an RMI?
Answer : Remote Method Invocation (RMI) is a java Mechanism that allows a program to invoke a method that is being executed on a remote machine. [The important thing to rememeber here remote object whose method is being invoked must be executing on the remote machine)
Question :What is the difference in between C++ and Java ? can u explain in detail?
Answer : The main difference between the 2 are following:
1. C++ supports pointers, java doesn\’t.
2. C++ supports mutiple inheritance direclty, java supports the same but by way of implementing the interface.
3. C++ supports operator overloading, java doesn\’t.
Question :What is the main functionality of the Prepared Statement?
Answer : If you want to execute a Statement object many times, it will normally reduce execution time to use a PreparedStatement object instead. The following code snippet will give a better idea
PreparedStatement updateSales = con.prepareStatement(
\"UPDATE EMP SET SAL = ? WHERE EMP_NO LIKE ? \");
updateSales.setInt(1, 10000);
updateSales.setString(2, \"E%\");
updateSales.executeUpdate():
Question :What is meant by static query and dynamic query?
Answer : Static query is a query that is fixed and doesn\’t varry at the run time. Where as Dynamic query varries with the change in the values at the run time.
For example:
\"select * from emp where name = \’sachin\’\” is a static query and always returns the same result.
\"select * from emp where name = \’\” + vName + \"\’\” is a dynamic query as the result of this query will be based on the value of vName.
Question :What are the Normalization Rules? Define the Normalization?
Answer : Normalization is the process of putting things right, making them normal. This is needed for:
1. Avoid Redundancy
2. Data Integrity
There are many forms in which a relation can be normalized:
1. INF
2. 2NF
3. 3NF
4. BCNF
5. 4NF
6. 5NF
Question :What is meant by Servlet? What are the parameters of the service method?
Answer : Servlet is a small prgram that is executed at server end of the network connection. Two parameters of service methods are:
service(ServletRequest req, ServletResponse res)
ServletRequest : for service request
ServletResponse : for response from the server
Question :How do you invoke a Servlet? What is the difference in between doPost and doGet methods?
Answer : Servlet is always invoked through the browser either through url string or by pressing a button through action tag in html.
doGet method: is used when we use get attribute in the form tag in the html page calling the servlet. When used doGet, the parameters from the client to the server are passed as a part of url string. The main limitations here the size and number of parameters may create some problem.
doPost method: is used when we use post attribute in the form tag in the html page calling the servlet. When used doPost, the parameters from the client to the server are passed as encoded string not as a part of url string.
Question :What is the difference in between the HTTPServlet and Generic Servlet? Explain their methods? Tell me their parameter names also ?
Have you used threads in Servlet?
Answer : The main difference between the HTTPServlet and Generic Servlet is first is used whenever there is an client and server communicate using HTTP protocol where as second is used for normal communication.
Question :Have you used any version control?
Answer : Version contorl is a process of keeping many versions of a project so that whenever any error occurs due to some addtion/deletion/modification of the code the problem can be sorted out easily.
Question :Explain 2 tier and 3 -tier Architecture?
Answer : 2 tier means communication between 2 components client and server. 3 tier means all the requests/responses from/to client to/from the server are done through middleware that contains the business logic.
Question :How have you done validation of the fields in your project?
Answer : Its a simple technique in which whether the inputs provided by the users are valid or invalid.
Question :What is the main difficulties that you are faced in your project?
Answer : The main difficult and time consuming phase of any project is testing. But if we spend more time on previous 2 phases (analysis and design) then we won\’t face any problem in coding and so in testing.
0 Comments:
Post a Comment
<< Home