Java FAQ: Snappy Answers to Some of the Top Java Programming Questions

Computer Science | Java Programming

There are numerous frequently asked java assignment questions when it comes to the field of Java programming. We asked experts to answer some of the most burning ones. As well you can see java assignment sample to make sure.

Q) Is there a way to develop a Java class that directs all program messages over to the system console but sends error messages elsewhere?

A) There’s a variable out command provided by the class System that represents the standard program output. The variable err represents the error device. While they’re both pointed toward the system console by default, this can be changed if desired. The standard output could be redirected just about anywhere:

Stream sT =

new Stream (new

FileOutputStream (“error_messages.txt”));

System.setErr(sT);

System.setOut(sT);

Q) Is there a difference between an abstract class and an interface?

A) Multiple interfaces can always be implemented in a class. An abstract class can contain code in method bodies. This is never allowed in an interface. Abstract classes have to inherit their classes from it. Java doesn’t ever allow multiple inheritance functions at any time.

Q) What’s the difference between a synchronized method and a synchronized block?

A) Synchronized methods place locks for much longer periods of time than synchronized blocks.

Q) Is it possible to write a class in Java that could be used as an application in addition to an applet?

A) Yes, this is easily accomplished by writing applet code and then adding a main() method to it.

Q) What’s the difference between the wait() and sleep() methods?

A) When sleep(1000); is called it puts the thread aside for one second, but wait(1000) will wait up to one second. Any information from the notify() or notfiyAll() calls would end the wait earlier. This method is defined in the class Object, as opposed to sleep(), which is defined in the class Thread.

Q) Is there a way to force garbage collection?

A) There’s no way to force garbage collection, but it can be requested by making a call to System.gc(). There’s no guarantee that the Java Virtual Machine will start the GC system right away, however.

Q) Is it possible to call one constructor from another in the cass of a class with multiple constructors?

A) Yes; simply use the this() syntax.

Q) What’s the difference between J2SDK 5.0 and the original J2SDK 1.5?

A) There’s no difference. They refer to the same package, but Sun Microsystems has changed up the way that they number them.

Q) How do Java packages work?

A) Packages serve as a way to organize files whenever a project consists of a number of different modules. It helps to resolve naming conflicts, and package access levels allow programmers to protect data from being called on by any class that’s not authorized to do so.

Q) What exactly is the concept of the keyword transient?

A) This particular keyword indicates that the value of a member variable that it points to doesn’t have to necessarily be serialized with the object in question. The class will ultimately be de-serialized and this variable will then get initialized with it’s own data type’s default value. This would be zero for integers, for instance.

Q) What’s the best way to compare two different String variables?

Use the method equals() to compare the value of the two separate String variables, and then use == to check if the two variables point to the same instance of an individual String object.

java assignment help

Q) Does it matter if the catch statements for IOException and FileNotFoundException are written out of order?

A) Yes, the FileNoFoundException actually gets inherited from the IOException, so it certainly does matter.

Q) What difference is there between an ArrayList and a Vector?

A) ArrayList systems aren’t internally synchronized, but the Java Vector class is.

Q) When should invokeLater() be used as a method?

A) This method is only used when working with Swing components. It’s used to make sure they’re updates throughout the thread.

Q) What difference is there between constructors from other methods?

A) Constructors have to have the same name as the class, and they can never return any value at all. They’re only called once. Regular methods can be called indefinitely.

Q) Can an inner class declared from inside of a method access any local variables of the original method?

A) It can, but only if the variables are final.

Q) What happens if the && is replaced with & in this particular code snippet:

String c=null;

if (c!=null && c.length()>10)

{…}

Replacing it with & would cause a NullPointerException.

Q) What comes to mind whenever someone says shallow copy in Java?

A) Object cloning comes to mind!

Q) What comes to mind when you hear someone say young generation in Java?

A) Garbage collection!

Q) How can you copy an entire Java object with its state?

A) Have the class implement the Cloneable interface and then call the method clone().

Q) Is ArrayList or LinkedList better for doing an indexed search in a large list of objects?

A) ArrayList is better.

Q) How can a subclass call a method or a constructor that’s been defined in a larger superclass?

A) Use this syntax: super.theMethod(); but just write super(); in the first line of the subclass’ constructor to call a constructor of the superclass.

Q) How can you tell if an explicitly object casting is needed?

A) Any time you assign a superclass object to a variable of some subclass data type the casting has to be explicit. For example:

Object b;Customer c; c = Customer) b;

Whenever you assign a subclass to a variable that has a superclass type the casting will be done for you.

Q) When a class is located inside of a package, what needs to be changed in the OS environment to make it usable?

A) You have to add a directory or some kind of JAR file that contains the package directors over to the CLASSPATH environment variable. For instance, let’s say a class called Person belongs to a package called per.abc.hr; and it’s located in the file at c:/code/per.abc.hr.Person.java then you would have to add c:/code to the variable CLASSPATH. If this contains the method main() then you could test it from the command prompt by going:

c:\>java per.abc.hr.Person

Q) When you override the method equals() of an object, what other method could you try?

A) hashCode()

Q) Is there a way to minimize the need of the garbage collector and make memory use more effective?

A) Make sure to use object pooling and make weak object references.

Q) What’s the difference between stacks and queues?

A) A stack works by the LIFO rule: last-in-first-out. Queues use the FIFO rule: first-in-first-out.

Q) It’s possible to create an abstract class that only has abstract methods. It’s also possible to create an interface that declares the same methods. Can abstract classes be used in place of interfaces then?

A) Sometimes, but the class may end up being a descendent of another class, which ultimately necessitates the use of an interface.

Q) There are two primary classes: 1 and 2. The 2 class needs to inform the 1 class when something happens. What Java technique should be used here?

A) You had best consider using notify() or notfiyAll() if the two classes as threads, but otherwise the Observer interface should be rather sufficient.

Q) What IDEs can I use for my project in Java?

A) For your project in Java you can use: Eclipse, NetBeans, IntelliJ IDEA Community Edition and other popular IDEs.

Q) What access level is needed to ensure that only classes from the same directory can access a class from its declaration?

A) Don’t specify any individual access level in this case. Instead, allow Java to use a default package access level.

Are you interested in it? So, do not waste your time and ask us more for java assignment help.