Set - 4

Question 1 :

What kind of security tools are available in J2SE 5.0?

Answer :

There are three tools that can be used to protect application working within the scope of security policies set at remote sites.
keytool -- used to manage keystores and certificates.
jarsigner -- used to generate and verify JAR signatures.
policytool -- used for managing policy files.
There are three tools that help obtain, list and manage Kerberos tickets.
kinit -- used to obtain Kerberos V5 tickets.
tklist -- used to list entries in credential cache and key tab.
ktab -- used to help manage entries in the key table.

Question 2 :

How to make an array copy from System?

Answer :

There is a method called arraycopy in the System class. You can do it:

System.arraycopy(sourceArray, srcOffset, destinationArray, destOffset, numOfElements2Copy);

When you use this method, the destinationArray will be filled with the elements of sourceArray at the length specified.

Question 3 :

Can we use System.arraycopy() method to copy the same array?

Answer :

Yes, you can. The source and destination arrays can be the same if you want to copy a subset of the array to another area within that array.

Question 4 :

What is shallow copy or shallow clone in array cloning?

Answer :

Cloning an array invloves creating a new array of the same size and type and copying all the old elements into the new array. But such copy is called shallow copy or shallow clone because any changes to the object would be reflected in both arrays.

Question 5 :

When is the ArrayStoreException thrown?

Answer :

When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.

Question 6 :

How to check two arrays to see if contents have the same types and contain the same elements?

Answer :

One of options is to use the equals() method of Arrays class.

Arrays.equals(a, b);

If the array types are different, a compile-time error will happen.

Question 7 :

Can you call one constructor from another if a class has multiple constructors?

Answer :

Yes. Use this() syntax.

Question 8 :

What are the different types of inner classes?

Answer :

There are four different types of inner classes in Java. They are: a)Static member classes , a static member class has access to all static methods of the parent, or top-level, class b) Member classes, the member class is instance specific and has access to any and all methods and members, even the parent's this reference c) Local classes, are declared within a block of code and are visible only within that block, just as any other method variable. d) Anonymous classes, is a local class that has no name

Question 9 :

In which case would you choose a static inner class?

Answer :

Interesting one, static inner classes can access the outer class's protected and private fields. This is both a positive and a negative point for us since we can, in essence, violate the encapsulation of the outer class by mucking up the outer class's protected and private fields. The only proper use of that capability is to write white-box tests of the class -- since we can induce cases that might be very hard to induce via normal black-box tests (which don't have access to the internal state of the object). Second advantage,if I can say, is that, we can this static concept to impose restriction on the inner class. Again as discussed in earlier point, an Inner class has access to all the public, private and protected members of the parent class. Suppose you want to restrict the access even to inner class, how would you go ahead? Making the inner class static enforces it to access only the public static members of the outer class( Since, protected and private members are not supposed to be static and that static members can access only other static members). If it has to access any non-static member, it has to create an instance of the outer class which leads to accessing only public members.

Question 10 :

What is weak reference in Java

Answer :

A weak reference is one that does not prevent the referenced object from being garbage collected. You might use them to manage a HashMap to look up a cache of objects. A weak reference is a reference that does not keep the object it refers to alive. A weak reference is not counted as a reference in garbage collection. If the object is not referred to elsewhere as well, it will be garbage collected.

Question 11 :

What is the difference between final, finally and finalize?

Answer :

final is used for making a class no-subclassable, and making a member variable as a constant which cannot be modified. finally is usually used to release all the resources utilized inside the try block. All the resources present in the finalize method will be garbage collected whenever GC is called. Though finally and finalize seem to be for a similar task there is an interesting tweak here, usually I prefer finally than finalize unless it is unavoidable. This is because the code in finally block is guaranteed of execution irrespective of occurrence of exception, while execution of finalize is not guarenteed.finalize method is called by the garbage collector on an object when the garbage collector determines that there are no more references to the object. Presumably the garbage collector will, like its civil servant namesake, visit the heap on a regular basis to clean up resources that are no longer in use. Garbage collection exists to prevent programmers from calling delete. This is a wonderful feature. For example, if you can't call delete, then you can't accidentally call delete twice on the same object. However, removing delete from the language is not the same thing as automatically cleaning up. To add to it, Garbage collection might not ever run. If garbage collection runs at all, and an object is no longer referenced, then that object's finalize will run. Also, across multiple objects, finalize order is not predictable. The correct approach to resource cleanup in Java language programs does not rely on finalize. Instead, you simply write explicit close methods for objects that wrap native resources. If you take this approach, you must document that the close method exists and when it should be called. Callers of the object must then remember to call close when they are finished with a resource.

Question 12 :

What's the difference between the methods sleep() and wait()

Answer :

The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Question 13 :

The following statement prints true or false, why?

Answer :

byte[] a = { 1, 2, 3 };,
byte[] b = (byte[]) a.clone();
System.out.println(a == b);

The false will be printed out. Because the two arrays have distinctive memory addresses. Starting in Java 1.2, we can use java.util.Arrays.equals(a, b) to compare whether two arrays have the same contents.

Question 14 :

Why do we need to use getSystemResource() and getSystemResources() method to load resources?

Answer :

Because we want to look for resources strictly from the system classpath, These methods use the system ClassLoader to locate resources, which gives you stricter control of the resources used by the application.

Question 15 :


Answer :

The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

Question 16 :

What is a transient variable?

Answer :

A transient variable is a variable that may not be serialized.

Question 17 :

Which containers use a border Layout as their default layout?

Answer :

The window, Frame and Dialog classes use a border layout as their default layout.

Question 18 :

Why do threads block on I/O?

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 19 :

What is the output from System.out.println("Hello"+null);?

Answer :


Question 20 :

What is synchronization and why is it important?

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 21 :

Can a lock be acquired on a class?

Answer :

Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Question 22 :

What's new with the stop(), suspend() and resume() methods in JDK 1.2?

Answer :

The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

Question 23 :

Is null a keyword?

Answer :

The null value is not a keyword.

Question 24 :

What is the preferred size of a component?

Answer :

The preferred size of a component is the minimum component size that will allow the component to display normally.

Question 25 :

What method is used to specify a container's layout?

Answer :

The setLayout() method is used to specify a container's layout.

Question 26 :

Which containers use a FlowLayout as their default layout?

Answer :

The Panel and Applet classes use the FlowLayout as their default layout.

Question 27 :

What state does a thread enter when it terminates its processing?

Answer :

When a thread terminates its processing, it enters the dead state.

Question 28 :

What is the Collections API?

Answer :

The Collections API is a set of classes and interfaces that support operations on collections of objects.

Question 29 :

Which characters may be used as the second character of an identifier, but not as the first character of an identifier?

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 30 :

What is the List interface?

Answer :

The List interface provides support for ordered collections of objects.

Question 31 :

How does Java handle integer overflows and underflows?

Answer :

It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

Question 32 :

What is the Vector class?

Answer :

The Vector class provides the capability to implement a growable array of objects

Question 33 :

What modifiers may be used with an inner class that is a member of an outer class?

Answer :

A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.

Question 34 :

What is an Iterator interface?

Answer :

The Iterator interface is used to step through the elements of a Collection.

Question 35 :

What is the difference between the >> and >>> operators?

Answer :

The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.

Question 36 :

Which method of the Component class is used to set the position and size of a component?

Answer :


Question 37 :

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

Answer :

Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

Question 38 :

What is the difference between yielding and sleeping?

Answer :

When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.

Question 39 :

Which java.util classes and interfaces support event handling?

Answer :

The EventObject class and the EventListener interface support event processing.

Question 40 :

Is sizeof a keyword?

Answer :

The sizeof operator is not a keyword.

Question 41 :

What are wrapper classes?

Answer :

Wrapper classes are classes that allow primitive types to be accessed as objects.

Question 42 :

Does garbage collection guarantee that a program will not run out of memory?

Answer :

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

Question 43 :

What restrictions are placed on the location of a package statement within a source code file?

Answer :

A package statement must appear as the first line in a source code file (excluding blank lines and comments).

Question 44 :

Can an object's finalize() method be invoked while it is reachable?

Answer :

An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.

Question 45 :

What is the immediate superclass of the Applet class?

Answer :


Question 46 :

What is the difference between preemptive scheduling and time slicing?

Answer :

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

Question 47 :

Name three Component subclasses that support painting.

Answer :

The Canvas, Frame, Panel, and Applet classes support painting.

Question 48 :

What value does readLine() return when it has reached the end of a file?

Answer :

The readLine() method returns null when it has reached the end of a file.

Question 49 :

What is the immediate superclass of the Dialog class?

Answer :


Question 50 :

What is clipping?

Answer :

Clipping is the process of confining paint operations to a limited area or shape.