Core Java Interview Questions – Set 20

Where is native modifier used

It can refer only to methods and it indicates that the body of the method is to be

found else where and it is usually written in non java language

When do you use continue and when do you use break statements

When continue statement is applied it prematurely completes the iteration of a

loop. When break statement is applied it causes the entire loop to be abandoned.

What are transient variables

A transient variable is not stored as part of objects persistent state and they

cannot be final or static

What are the rules for primitive arithmetic promotion conversion

: For Unary operators

If operant is byte, short or a char it is converted to an int. If it is any other type it is not

converted

What is the Properties class

The properties class is a subclass of Hashtable that can be read from or written to a stream. It also provides the capability to specify a set of default values to be used.

How do I check the CLASSPATH variable is set in my machine?

Checking CLASSPATH on Windows

To check CLASSPATH variable is set on Microsoft Windows, run following command on command prompt

C:> echo %CLASSPATH%

If CLASSPATH variable is not set you will see %CLASSPATH% on windows system.

Checking CLASSPATH on Unix, Linux or Mac

To check CLASSPATH variable is set on Unix/Linux/Mac run following command on shell
$ echo $CLASSPATH

If CLASSPATH variable is not set you will see CLASSPATH: Undefined variable error on Unix/Linux/Mac systems.

How to set Multiple Jar Files in Java Classpath

Java versions Older then Java 6 does not support wildcard characters. Setting Multiple jars using wildcard in Java classpath are allowed in Java 6 and later versions.

For example, to specify all jar files in a directory “lib” the classpath entry should look like this lib/*

The wildcard entry (*) in classpath value will match only jar files NOT class files. To match both class files and JAR files in a same directory lib, you need to specify both values as shown below

Setting Multiple Jars in Classpath on Windows

Windows environment variable values are separated by semicolon, therefore you classpath entry would look like thislib/*;lib

Setting Multiple Jars in Classpath on Unix, Linux or Mac

Unix environment variable values are separated by colon, therefore you classpath entry would look like thislib/*:lib

Older version of Java

In older version of Java(older than Java 6), each jar file needs to be specified in the classpath. It can be a tedious and erroneous task if you are using many third party libraries.

Why calling System.setProperty() does not affect the classpath at run-time

You can easily set any system properties in java using System.setPropoerty method, However it may not have any effect in case of CLASSPATH property. This is mainly because the Java system class loader is initialized very early in the JVM startup sequence. The class loader copies the classpath into its own data structures, and the classpath property is not read again. Therefore changing it after its already copied does not affect anything. There are mainly two reasons for this – First most important reason is security. You do not want a malicious code change the classpath at runtime and load some unwanted classes. Second reason is performance, since reading the classpath every-time its needed may not be efficient.

How will you fix the above memory leak

By providing proper implentation for the key class as shown below with the equals() and hashCode() methods.

…..

static class Key {

private String key;

public Key(String key) {

this.key = key;

}

@Override

public boolean equals(Object obj) {

if (obj instanceof Key)

return key.equals(((Key) obj).key);

else

return false;

}

@Override

public int hashCode() {

return key.hashCode();

}

}

…..

If you rerun it after making the fix shown above to the MemoryLeak class, you will get an output as shown below. The program runs endlessly, and creates only one object in the HashMap.

map size: 1

Free memory after count 1000 is 4MB

map size: 1

Free memory after count 2000 is 4MB

map size: 1

Free memory after count 3000 is 4MB

map size: 1

Free memory after count 4000 is 4MB

Free memory after count 73000 is 4MB

map size: 1

Free memory after count 74000 is 4MB

map size: 1

Free memory after count 75000 is 4MB

Explain modifier final

Final can be applied to classes, methods and variables and the features cannot be

changed. Final class cannot be subclassed, methods cannot be overridden.

How to get a list of resources from a directory in Java classpath

You can use Reflections library

for doing this. Reflections is a open source Java library. It scans Java classpath and indexes it with metadata. This library allows you to query the classpath at runtime and can be very handy for many run-time reflection code needs.