Thursday, October 25, 2007

Java Interview Questions -Part 7


Question How can I send user authentication information while makingURLConnection? (Servlets)
Answer You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

Question What are some alternatives to inheritance? (CoreJava)
Answer Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).
Question Why isn't there operator overloading? (CoreJava)
Answer Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn't even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().

Question What does it mean that a method or field is "static"? (CoreJava)
Answer Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class.
Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work. out is a static field in the java.lang.System class.

Question How do I convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com? (Networking)
Answer String hostname = InetAddress.getByName("192.18.97.39").getHostName();

Question Diffrence between JRE And JVM AND JDK (CoreJava)
Answer

Question Why do threads block on I/O? (CoreJava)
Answer Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed.

Question What is synchronization and why is it important? (CoreJava)
Answer 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.

Question Is null a keyword? (CoreJava)
Answer The null value is not a keyword.

Question Which characters may be used as the second character of an identifier,but not as the first character of an identifier? (CoreJava)
Answer The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

Question What modifiers may be used with an inner class that is a member of an outer class? (CoreJava)
Answer A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Question How can my application get to know when a HttpSession is removed? (JSP)
Answer Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method.
Create an instance of that class and put that instance in HttpSession.
Question Whats the difference between notify() and notifyAll()? (CoreJava)
Answer notify() is used to unblock one waiting thread; notifyAll() is used to unblock all of them. Using notify() is preferable (for efficiency) when only one blocked thread can benefit from the change (for example, when freeing a buffer back into a pool). notifyAll() is necessary (for correctness) if multiple threads should resume (for example, when releasing a "writer" lock on a file might permit all "readers" to resume).

Question Why can't I say just abs() or sin() instead of Math.abs() and Math.sin()? (CoreJava)
Answer The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid of them altogether. That's just the way it works, you'll get used to it. It's really a lot safer this way.
However, there is actually a little trick you can use in some cases that gets you what you want. If your top-level class doesn't need to inherit from anything else, make it inherit from java.lang.Math. That *does* bring all the methods into your local name space. But you can't use this trick in an applet, because you have to inherit from java.awt.Applet. And actually, you can't use it on java.lang.Math at all, because Math is a "final" class which means it can't be extended.

Question Is is possible for an EJB client to marshall an object of class java.lang.Class to an EJB? (EJB)
Answer Technically yes, spec. compliant NO! - The enterprise bean must not attempt to query a class to obtain information about the declared members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language.

Question Is it legal to have static initializer blocks in EJB? (EJB)
Answer Although technically it is legal, static initializer blocks are used to execute some piece of code before executing any constructor or method while instantiating a class. Static initializer blocks are also typically used to initialize static fields - which may be illegal in EJB if they are read/write - In EJB this can be achieved by including the code in either the ejbCreate(), setSessionContext() or setEntityContext() methods.

Question How can I implement a thread-safe JSP page? (JSP)
Answer You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" % > within your JSP page.

Question Is it possible to stop the execution of a method before completion in a SessionBean? (EJB)
Answer Stopping the execution of a method inside a Session Bean is not possible without writing code inside the Session Bean. This is because you are not allowed to access Threads inside an EJB.

Question What is the default transaction attribute for an EJB? (EJB)
Answer There is no default transaction attribute for an EJB. Section 11.5 of EJB v1.1 spec says that the deployer must specify a value for the transaction attribute for those methods having container managed transaction. In weblogic, the default transaction attribute for EJB is SUPPORTS.

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com