Set - 3

Question 1 :

What is the purpose of the File class?

Answer :

The File class is used to create objects that provide access to the files and directories of a local file system.

Question 2 :

Why would you use a synchronized block vs. synchronized method?

Answer :

Synchronized blocks place locks for shorter periods than synchronized methods.

Question 3 :

What restrictions are placed on method overriding?

Answer :

Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides. The overriding method may not throw any exceptions that may not be thrown by the overridden method.

Question 4 :

What is casting?

Answer :

There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

Question 5 :

Explain the usage of the keyword transient?

Answer :

This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).

Question 6 :

What class allows you to read objects directly from a stream?

Answer :

The ObjectInputStream class supports the reading of objects from input streams.

Question 7 :

How are this() and super() used with constructors?

Answer :

this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.

Question 8 :

How is it possible for two String objects with identical values not to be equal under the == operator? How are this() and super() used with constructors?

Answer :

The == operator compares two objects to determine if they are the same objects in memory. It is possible for two String objects to have the same value, but located in different areas of memory.

Question 9 :

What is an IO filter?

Answer :

An IO filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.

Question 10 :

What is the Set interface?

Answer :

The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

Question 11 :

How can you force garbage collection?

Answer :

You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.

Question 12 :

What is the purpose of the enableEvents() method?

Answer :

The enableEvents() method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents() method is used by objects that handle events by overriding their event-dispatch methods.

Question 13 :

What is the difference between the File and RandomAccessFile classes?

Answer :

The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.

Question 14 :

What interface must an object implement before it can be written to a stream as an object?

Answer :

An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

Question 15 :

What is the ResourceBundle class?

Answer :

The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run.

Question 16 :

How do you know if an explicit object casting is needed?

Answer :

If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:

Object a; Customer b; b = (Customer) a;

When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.

Question 17 :

What is a Java package and how is it used?

Answer :

A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.

Question 18 :

How do you restrict a user to cut and paste from the html page?

Answer :

Using Servlet or client side scripts to lock keyboard keys. It is one of solutions.

Question 19 :

What are the Object and Class classes used for?

Answer :

The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

Question 20 :

What is Serialization and deserialization ?

Answer :

Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.

Question 21 :

Explain the usage of Java packages.

Answer :

This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

Question 22 :

Does the code in finally block get executed if there is an exception and a return statement in a catch block?

Answer :

If an exception occurs and there is a return statement in catch block, the finally block is still executed. The finally block will not be executed when the System.exit(1) statement is executed earlier or the system shut down earlier or the memory is used up earlier before the thread goes to finally block.

Question 23 :

Is Java a super set of JavaScript?

Answer :

No. They are completely different. Some syntax may be similar.

Question 24 :

What is a Container in a GUI?

Answer :

A Container contains and arranges other components (including other containers) through the use of layout managers, which use specific layout policies to determine where components should go as a function of the size of the container.

Question 25 :

How the object oriented approach helps us keep complexity of software development under control?

Answer :

We can discuss such issue from the following aspects:
Objects allow procedures to be encapsulated with their data to reduce potential interference.
Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places.
The well-defined separations of interface and implementation allow constraints to be imposed on inheriting classes while still allowing the flexibility of overriding and overloading.

Question 26 :

What is polymorphism?

Answer :

Polymorphism means "having many forms". It allows methods (may be variables) to be written that needn't be concerned about the specifics of the objects they will be applied to. That is, the method can be specified at a higher level of abstraction and can be counted on to work even on objects of un-conceived classes.

Question 27 :

What is design by contract?

Answer :

The design by contract specifies the obligations of a method to any other methods that may use its services and also theirs to it. For example, the preconditions specify what the method required to be true when the method is called. Hence making sure that preconditions are. Similarly, postconditions specify what must be true when the method is finished, thus the called method has the responsibility of satisfying the post conditions.
In Java, the exception handling facilities support the use of design by contract, especially in the case of checked exceptions. The assert keyword can be used to make such contracts.

Question 28 :

What are use cases?

Answer :

A use case describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance. It is part of the analysis of a program. The collection of use cases should, ideally, anticipate all the standard circumstances and many of the extraordinary circumstances possible so that the program will be robust.

Question 29 :

What is scalability and performance?

Answer :

Performance is a measure of "how fast can you perform this task." and scalability describes how an application behaves as its workload and available computing resources increase.

Question 30 :

What is the benefit of subclass?

Answer :

Generally: The sub class inherits all the public methods and the implementation.
The sub class inherits all the protected methods and their implementation.
The sub class inherits all the default(non-access modifier) methods and their implementation.
The sub class also inherits all the public, protected and default member variables from the super class.
The constructors are not part of this inheritance model.

Question 31 :

How to add menushortcut to menu item?

Answer :

If you have a button instance called aboutButton, you may add menu short cut by calling aboutButton.setMnemonic('A'), so the user may be able to use Alt+A to click the button.

Question 32 :

In System.out.println(),what is System,out and println,pls explain?

Answer :

System is a predefined final class,out is a PrintStream object acting as a field member and println is a built-in overloaded method in the out object.

Question 33 :

Can you write a Java class that could be used both as an applet as well as an application?

Answer :

Yes. Add a main() method to the applet.

Question 34 :

Can you make an instance of an abstract class? For example - java.util.Calender is an abstract class with a method getInstance() which returns an instance of the Calender class.

Answer :

No! You cannot make an instance of an abstract class. An abstract class has to be sub-classed. If you have an abstract class and you want to use a method which has been implemented, you may need to subclass that abstract class, instantiate your subclass and then call that method.

Question 35 :

What is the output of 

x > y? a:b = p*q when x=1,y=2,p=3,q=4?

Answer :

When this kind of question has been asked, find the problems you think is necessary to ask back before you give an answer. Ask if variables a and b have been declared or initialized. If the answer is yes. You can say that the syntax is wrong. If the statement is rewritten as: x

Question 36 :

What is the difference between Swing and AWT components?

Answer :

AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

Question 37 :

Why Java does not support pointers?

Answer :

Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers. This is why Java and C-sharp shine.

Question 38 :

Parsers? DOM vs SAX parser

Answer :

Parsers are fundamental xml components, a bridge between XML documents and applications that process that XML. The parser is responsible for handling xml syntax, checking the contents of the document against constraints established in a DTD or Schema.
1. Tree of nodes
2. Memory: Occupies more memory, preffered for small XML documents
3. Slower at runtime
4. Stored as objects
5. Programmatically easy
6. Ease of navigation
1. Sequence of events
2. Doesn't use any memory preferred for large documents
3. Faster at runtime
4. Objects are to be created
5. Need to write code for creating objects
6. Backward navigation is not possible as it sequentially processes the document

Question 39 :

Can you declare a class as private?

Answer :

Yes, we can declare a private class as an inner class. For example,

class MyPrivate {
 private static class MyKey {
  String key = "12345";
 public static void main(String[] args) {
  System.out.println(new MyKey().key);//prints 12345

Question 40 :

What is the difference between shallow copy and deep copy?

Answer :

Shallow copy shares the same reference with the original object like cloning, whereas the deep copy get a duplicate instance of the original object. If the shallow copy has been changed, the original object will be reflected and vice versa.

Question 41 :

Can one create a method which gets a String and modifies it?

Answer :

No. In Java, Strings are constant or immutable; their values cannot be changed after they are created, but they can be shared. Once you change a string, you actually create a new object. For example:

String s = "abc"; //create a new String object representing "abc"
s = s.toUpperCase(); //create another object representing "ABC"

Question 42 :

Why is multiple inheritance not possible in Java?

Answer :

It depends on how you understand "inheritance". Java can only "extends" one super class, but can "implements" many interfaces; that doesn't mean the multiple inheritance is not possible. You may use interfaces to make inheritance work for you. Or you may need to work around. For example, if you cannot get a feature from a class because your class has a super class already, you may get that class's feature by declaring it as a member field or getting an instance of that class. So the answer is that multiple inheritance in Java is possible.

Question 43 :

What's the difference between constructors and other methods?

Answer :

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.

Question 44 :

What is the relationship between synchronized and volatile keyword?

Answer :

The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic.(Some JVM might treat reads and writes of data of 64 bits or less as atomic in future) For long or double variable, programmers should take care in multi-threading environment. Either put these variables in a synchronized method or block, or declare them volatile.

Question 45 :

This class (IncrementImpl) will be used by various threads concurrently; can you see the inherent flaw(s)? How would you improve it?

Answer :

public class IncrementImpl { 
	private static int counter = 0; 
	public synchronized void increment() { 
	public int getCounter() { 
		return counter; 

The counter is static variable which is shared by multiple instances of this class. The increment() method is synchronized, but the getCounter() should be synchronized too. Otherwise the Java run-time system will not guarantee the data integrity and the race conditions will occur. The famous producer/consumer example listed at Sun's thread tutorial site will tell more. 
one of solutions

public class IncrementImpl { 
	private static int counter = 0; 
	public synchronized void increment() { 
	public synchronized int getCounter() { 
		return counter; 


Question 46 :

What are the drawbacks of inheritance?

Answer :

Since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation. In addition, the inheritance may make peers hardly understand your code if they don't know how your super-class acts and add learning curve to the process of development.
Usually, when you want to use a functionality of a class, you may use subclass to inherit such function or use an instance of this class in your class. Which is better, depends on your specification.

Question 47 :

Is there any other way that you can achieve inheritance in Java?

Answer :

There are a couple of ways. As you know, the straight way is to "extends" and/or "implements". The other way is to get an instance of the class to achieve the inheritance. That means to make the supposed-super-class be a field member. When you use an instance of the class, actually you get every function available from this class, but you may lose the dynamic features of OOP

Two methods have key words static synchronized and synchronized separately. What is the difference between them?
Both are synchronized methods. One is instance method, the other is class method. Method with static modifier is a class method. That means the method belongs to class itself and can be accessed directly with class name and is also called Singleton design. The method without static modifier is an instance method. That means the instance method belongs to its object. Every instance of the class gets its own copy of its instance method.
When synchronized is used with a static method, a lock for the entire class is obtained. When synchronized is used with a non-static method, a lock for the particular object (that means instance) of the class is obtained.
Since both methods are synchronized methods, you are not asked to explain what is a synchronized method. You are asked to tell the difference between instance and class method. Of course, your explanation to how synchronized keyword works doesn't hurt. And you may use this opportunity to show your knowledge scope.

Question 48 :

How do you create a read-only collection?

Answer :

The Collections class has six methods to help out here:
1. unmodifiableCollection(Collection c)
2. unmodifiableList(List list)
3. unmodifiableMap(Map m)
4. unmodifiableSet(Set s)
5. unmodifiableSortedMap(SortedMap m)
6. unmodifiableSortedSet(SortedSet s)
If you get an Iterator from one of these unmodifiable collections, when you call remove(), it will throw an UnsupportedOperationException.

Question 49 :

Can a private method of a superclass be declared within a subclass?

Answer :

Sure. A private field or method or inner class belongs to its declared class and hides from its subclasses. There is no way for private stuff to have a runtime overloading or overriding (polymorphism) features.

Question 50 :

Why Java does not support multiple inheritance ?

Answer :

This is a classic question. Yes or No depends on how you look at Java. If you focus on the syntax of "extends" and compare with C++, you may answer 'No' and give explanation to support you. Or you may answer 'Yes'. Recommend you to say 'Yes'.
Java DOES support multiple inheritance via interface implementation. Some people may not think in this way. Give explanation to support your point.

Question 51 :

What is the difference between final, finally and finalize?

Answer :

Short answer:
final - declares constant
finally - relates with exception handling
finalize - helps in garbage collection
If asked to give details, explain:
final field, final method, final class
try/finally, try/catch/finally
protected void finalize() in Object class