You are on page 1of 7


How could Java classes direct program messages to the system console, but error messages, say to a file? The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: Stream st = new Stream(new FileOutputStream(“output.txt”)); System.setErr(st); System.setOut(st); 2.How would you create a button with rounded edges? There’s 2 ways. The first thing is to know that a JButton’s edges are drawn by a Border. so you can override the Button’s paintComponent(Graphics) method and draw a circle or rounded rectangle (whatever), and turn off the border. Or you can create a custom border that draws a circle or rounded rectangle around any component and set the button’s border to it. 3.Why should the implementation of any Swing callback (like a listener) execute quickly? A: Because callbacks are invoked by the event dispatch thread which will be blocked processing other events for as long as your method takes to execute. 4. Question: How you can force the garbage collection? Garbage collection automatic process and can’t be forced. You could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. Garbage collection is one of the most important feature of Java, Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can’t directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. 5. What’s the difference between constructors and normal methods? Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void. 6. When should the method invokeLater()be used? This method is used to ensure that Swing components are updated through the event-dispatching thread. 7.Explain the usage of Java packages.

arrays. puts thread aside for exactly one second.e.Explain the usage of the keyword transient? Transient keyword indicates that the value of this member variable does not have to be serialized with the object.What’s the difference between the methods sleep() and wait() The code sleep(1000). HashMap.Why would you use a synchronized block vs. more powerful. TreeSet and TreeMap. zero for integers). and to nowhere else on the network Despite all of the useful and helpful Java features. 12. this variable will be initialized with a default value of its data type (i. and more regular than the vectors. and hashtables if effectively replaces. Some disadvantages of Java Sockets: Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from.Can an inner class declared inside of a method access local variables of this method? It’s possible if these variables are final. Efficient socket based programming can be easily implemented for general communications. List and Map. 9. These classes and interfaces are more flexible. Sockets cause low network traffic. The code wait(1000). 14. Example of classes: HashSet.What are some advantages and disadvantages of Java Sockets? Some advantages of Java Sockets: Sockets are flexible and sufficient. Packages access level also allows you to protect data from being used by the non-authorized classes. ArrayList. Example of interfaces: Collection.What is Collection API? The Collection API is a set of classes and interfaces that support operation on collections of objects. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread. synchronized method? Synchronized blocks place locks for shorter periods than synchronized methods.Explain the user defined Exceptions? . Socket based communications allows only to send packets of raw data between applications. Set. Unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request. It also helps resolve naming conflicts when different packages have classes with the same names. 10. When the class will be de-serialized. LinkedList. Java applets can send only necessary updated information. causes a wait of up to one second. 13.This is a way to organize files when a project consists of multiple modules. Both the client-side and server-side have to provide mechanisms to make the data useful in any way. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. 11. 8.

lang. What is a daemon thread? – These are the threads which can run without user intervention.lang.Character double – java.exec(….Float int – java.lang.Double float – java.Byte char – java. b) public static void main( String args ).lang. Garbage collector is responsible for this process 2. What is garbage collection? What is the process that is responsible for doing that in java? – Reclaiming the unused memory by the invalid objects. What kind of thread is the Garbage collector thread? – It is a daemon thread.Describe the wrapper classes in Java. a primitive value of the corresponding type. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions. d) public static void main( Graphics g ).User defined Exceptions are the separate Exception classes defined by the user for specific purposed.Boolean byte – java.lang. 1.getRuntime().Void 16. . Wrapper class is wrapper around a primitive data type. 4. 5.Short void – java. The JVM can exit when there are daemon thread by killing them abruptly.lang. e) public static boolean main( String args[] ). Example: class myCustomException extends Exception { / The class simply has to exist to be an exception } 15. or wraps.lang. Following table lists the primitive types and the corresponding wrapper classes: Primitive Wrapper boolean – java.Long short – java. How will you invoke any external process in Java? – Runtime. c) public static void main( String args[] ).Which of the following are valid definitions of an application’s main( ) method? a) public static void main(). An user defined can created by simply sub-classing it to the Exception class.lang. 3. An instance of a wrapper class contains.Integer long – java.lang. the JVM give the user a chance to clean up some resources before it got garbage collected.) What is the finalize method do? – Before the invalid objects get garbage collected.

16. 10. and the use reflected fields. methods and constructors of loaded classes. is it native or java code? – This method is not implemented. 15. String. just a very fast compiler… In this incarnation.4. So you can’t look at the whole method. 24. But that is no more in use… it is deprecated long before in JDK. and constructors to operate on their underlying counterparts on objects. or cultural region How will you load a specific locale? – Using ResourceBundle. Skeleton is the server side representation. What is the byte range? -128 to 127 12. What is the basic difference between string and stringbuffer object? – String is an immutable object..6. what is the best way to findout the time/memory consuming process? – Using profiler 21. This keyword should be used for the critical arguments. What is JIT and its use? – Really. 9. 13. What is skeleton and stub? what is the purpose of those? – Stub is a client side representation of the server. …) 7. rank the expressions according to which ones are re-used the most. 8. pretty much a one-pass compiler — no offline computations. 22. What are the approaches that you will follow for making a program very efficient? – By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more. and then generate code. What is the purpose of Void class? – The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void. Float. (Ex. You can’t override that method/variable/class any more. 14. it’s an on-line problem. What is Locale? – A Locale object represents a specific geographical. In theory terms. StringBufferis a mutable object.separator. When you think about optimization..separator. What is reflection? – Reflection allows programmatic access to information about the fields. StringBuffer. ? – Using Sytem. How will you get the platform dependent values like line separator. Is JVM a compiler or an interpreter? – Interpreter 20. What is a package? – To group set of classes into a single unit is known as packaging.getProperty(…) (line.. methods. which takes care of communicating with the remote server. political. What is the final keyword denotes? – final keyword denotes that it is the final implementation for that method or variable or class. path. What is the purpose of assert keyword used in JDK1. etc. …) If you are not allowed to change the value of an object. What is mutable object and immutable object? – If a object value is changeable then we can call it as Mutable object.x? – In order to validate certain expressions. without that the method does nothing. within security restrictions. 19. Meaning. …) 23. What is a DatabaseMetaData? – Comprehensive information about the database as a whole. Integer. Packages provides wide namespace ability. 18. (Ex.. What is the base class for Error and Exception? – Throwable 11. What is the implementation of destroy method in java. 17. It effectively replaces the if block and automatically throws the AssertionError on failure. it is immutable object. path separator.getBundle(…). .

What is Class.. There will be only one instance for that entire JVM. This falls in the creational pattern of the design pattern. 29. then it is called aggregation. private Singleton() { } public static Singleton getInstance() { return s. wait. What is singleton? – It is one of the design pattern. getClass. hashCode. 31. Can you instantiate the Math class? – You can’t instantiate the math class. What is DriverManager? – The basic service to manage set of JDBC drivers. then it is called inner class. What is the significance of ListIterator? – You can iterate back and forth. It returns the Class. If you expose all the methods of a composite class and route the method call to the composite method through its reference. 36. For eg.forName() does and how it is useful? – It loads the class into the ClassLoader.newInstance() ). notify. What is the major difference between LinkedList and ArrayList? – LinkedList are meant for sequential accessing. Using that you can get the instance ( “class-instance”. 33. ArrayList are meant for random accessing. What is composition? – Holding the reference of the other class within some other class is known as composition. finalize. 30. What is inner class? – If the methods of the inner class can only be accessed via the instance of the inner class. 27. toString 32. You can achieve this by having the private constructor in the class. 28. 35. 26. equals. All the methods in this class are static. public class Singleton { private static final Singleton s = new Singleton(). Inq adds a question: Expain the reason for each keyword ofpublic static void main(String args[]) . What is aggregation? – It is a special type of composition. And the constructor is not public. What are the methods in Object? – clone. What is nested class? – If all the methods of a inner class is static then it is a nested class. } // all non static methods … } 34. notifyAll.25.

handler is evaluated. If thunk throws to the symbol key. However. (If no matching handler is found.. If an exception is thrown in body. which must be an instance of a sub-class of .) key may be a symbol. The result of the try-catch is the result of body if no exception is thrown. If no handler is selected.) Once a handler is selected. then a throw to any symbol will match this call to catch. passing args to the handler. and the exp in the handler are executed.. If thunk returns normally.. the exception is propagated through the dynamic execution context until a matching handler is found. in the context of the given handler specifications. The thunk takes no arguments. Each handler has the form: var type exp . an error is signaled. Even if body returns abnormally (by throwing an exception).. the var is bound to the thrown exception. If the key is a symbol it will match catches of the same symbol or of #t. This procedure is part of SRFI-23... Syntax: try-finally body handler Evaluate body. then handler is invoked this way: (handler key args . procedure: error message args .Exception handling Function: catch key thunk handler Invoke thunk in the dynamic context of handler for exceptions matching key. Raise an error with key misc-error and a message constructed by displaying msg and writing args..) Function: dynamic-wind in-guard thunk out-guard . and the computation terminated. the first handler is selected such that the thrown exception is an instance of the handlers type. and return its result. Handler is invoked outside the scope of its own catch. If there is no handler at all.) Syntax: try-catch body handler . Invoke the catch form matching key. (This is implemented just like Javatry-catch. evaluate handler. a new handler from further up the call chain is invoked.. before it returns. that is the return value of catch. Function: throw key &rest args . If the key is #t. or the value of the last exp in the selected handler if an exception is thrown.. Function: primitive-throw exception Throws the exception. If handler again throws to the same key. then an error message is printed. This normally prints a stack trace. Evaluate body. and brings you back to the top level. (This is implemented just like Java try-finally.. or exits kawa if you are not running interactively. and other Scheme implementations.

Incorrectly use mb as the unit. then out-guard. Heap size is larger than your computers physical memory. with no whitespace nor =. then thunk. For example. where = is used. The result of the expression is that of thunk. -Xmx128 is a valid setting for very small apps. For example. -X options are different than -Dkey=value system properties. The default minimum heap size seems to be 0. For example.All three arguments must be 0-argument procedures. like HelloWorld. This function was added in R5RS. which is 64m. Here are some common mistakes made by developers while using them: 1. java -Xmx=512m HelloWorld Invalid maximum heap size: -Xmx=512m Could not create the Java virtual machine. M. 3. java -Xms128m BigApp Error occurred during initialization of VM Incompatible initial and maximum heap sizes specified The correct command should be java -Xms128m -Xmx128m BigApp. g or G at the end (they are case insensitive). If the continuation of the dynamic-wind is re-entered (which is not yet possible in Kawa). I guess you really mean -Xmx128m 2.lang. and -Xms for initial heap size. java -Xms256mb -Xmx256mb BigApp Invalid initial heap size: -Xms256mb Could not create the Java virtual machine. The correct command should be java -Xmx128m BigApp. Only setting -Xms JVM option and its value is greater than the default maximum heap size. outguard is called. Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size. java -Xmx128 BigApp java. . First calls in-guard. or incorrectly use =. the in-guard is called again. Missing m. The fix is to make it lower than the physical memory: java -Xmx1g BigApp 5. For example. Extra space in JVM options. dont let the minimum heap size exceed the maximum heap size.OutOfMemoryError: Java heap space The correct command should be: java -Xmx128m BigApp. In any case. But in real life. To be precise. where m or M should be used instead. If thunk is exited abnormally (by throwing an exception or invoking a continuation). Its a good idea to set the minimum and maximum heap size to the same value. java -Xmx 128m BigApp Invalid maximum heap size: -Xmx Could not create the Java virtual machine. java -Xmx2g BigApp Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine. 4.