Thursday, July 7, 2011

Java interview questions:PART1

Q:1. Why would you use a synchronized block vs. synchronized method?
Ans. Synchronized blocks place locks for shorter periods than synchronized methods.

Q:2. How can you force garbage collection?
Ans. You cannot force garbage collection. You can only request by calling System.gc(), Still JVM does not guarantee that GC will start immediately.

Q:3. Can you write a Java class that could be used both as an applet as well as an application?
Ans. Yes. Add a main() method to the applet.

Q:4. Explain "Shallow Copy" and "Deep Copy" in java?
Ans. Both terms are related to object cloning in Java. Follow the following link. It nicely explains this concept Shallow and Deep copy

Q:5. What's the difference between J2SDK 1.5 and J2SDK 5.0?
Ans. No difference, Sun just re-branded this version

Q:6. You want to do an indexed based search on a list of objects. Which of the two Java collections should you choose to store objects: ArrayList or LinkedList?
Ans. ArrayList as LinkedList is good when you always need sequential search.

Q:7. There are two classes: Me and You. The class You need to inform the class Me when some important event has happened. What Java technique would you use to implement it?
Ans. If these classes are threads you can use wait and notify() mechanism. For normal classes make use of Observer interface

Q:8.Can an inner class, declared inside a method, access local variables of this method?
Ans. Only if variables are final (means they are constants).

Q:9. Can we declare main method as final?
Ans. Yes

Q:10. What is the protocol used by RMI?
Ans. RMI-IIOP ( Remote Method Invocation over Internet Inter-Orb Protocol). This protocol delivers CORBA (Common Object Request Broker Architecture)distributed computing capabilities to the Java platform.

Q:11. List some marker interfaces.
Ans. Examples are java.lang.Cloneable, and java.util.EventListener

Q:12. What is the algo used in Thread Scheduling in Java?
Ans. Fixed Priority Scheduling

Q:13. Explain the different types of class loaders?
Ans. Below is the Hierarchy of class loaders:
Class HierarchyDescription
java.lang.ClassLoaderIt is root (abstract) class for all the class loaders
↑ (extends) This is a concrete implementation of ClassLoader class.This class extends ClassLoader with additional support for defining classes with an associated code source and permissions which are retrieved by the system policy by default.
↑ (extends) This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories.
↑ (extends
sun.applet.AppletClassLoaderThis class defines the class loader for loading applet classes and resources. It extends URLClassLoader to search the applet code base for the class or resource after checking any loaded JAR files.

No comments:

Post a Comment