Java Software Engineer

  Home  Java Programing  Java Software Engineer


“Java Software Engineer based Frequently Asked Questions in various Java Software Engineer job interviews by interviewer. These professional questions are here to ensures that you offer a perfect answers posed to you. So get preparation for your new job hunting”



45 Java Software Engineer Questions And Answers

4⟩ Tell us what do you mean by aggregation?

Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.

 134 views

6⟩ Do you know what is the advantage of generic collection?

They enable stronger type checks at compile time.

A Java compiler applies strong type checking to generic code, and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

 143 views

7⟩ Explain me why doesn’t the following code generate a NullPointerException even when the instance is null?

Test t = null;

t.someMethod();

public static void someMethod() {

...

}

There is no need for an instance while invoking a static member or method, since static members belongs to a class rather than an instance.

A null reference may be used to access a class (static) variable without causing an exception.

 156 views

11⟩ Explain me what is method overloading and method overriding?

☛ Method Overloading:

In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.

Method Overloading is to “add” or “extend” more to method’s behavior.

It is a compile time polymorphism.

The methods must have different signature.

It may or may not need inheritance in Method Overloading.

Let’s take a look at the example below to understand it better.

class Adder {

Static int add(int a, int b)

{

return a+b;

}

Static double add( double a, double b)

{

return a+b;

}

public static void main(String args[])

{

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(12.3,12.6));

}}

☛ Method Overriding:

In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.

Method Overriding is to “Change” existing behavior of method.

It is a run time polymorphism.

The methods must have same signature.

It always requires inheritance in Method Overriding.

Let’s take a look at the example below to understand it better.

class Car {

void run(){

System.out.println(“car is running”);

}

Class Audi extends Car{

void run()

{

System.out.prinltn(“Audi is running safely with 100km”);

}

public static void main( String args[])

{

Car b=new Audi();

b.run();

}

}

 167 views

12⟩ Explain me what is the volatile keyword? How and why would you use it?

In Java, each thread has its own stack, including its own copy of variables it can access. When the thread is created, it copies the value of all accessible variables into its own stack. The volatile keyword basically says to the JVM “Warning, this variable may be modified in another Thread”.

In all versions of Java, the volatile keyword guarantees global ordering on reads and writes to a variable. This implies that every thread accessing a volatile field will read the variable’s current value instead of (potentially) using a cached value.

In Java 5 or later, volatile reads and writes establish a happens-before relationship, much like acquiring and releasing a mutex.

Using volatile may be faster than a lock, but it will not work in some situations. The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.

The volatile keyword is also useful for 64-bit types like long and double since they are written in two operations. Without the volatile keyword you risk stale or invalid values.

One common example for using volatile is for a flag to terminate a thread. If you’ve started a thread, and you want to be able to safely interrupt it from a different thread, you can have the thread periodically check a flag (i.e., to stop it, set the flag to true). By making the flag volatile, you can ensure that the thread that is checking its value will see that it has been set to true without even having to use a synchronized block. For example:

public class Foo extends Thread {

private volatile boolean close = false;

public void run() {

while(!close) {

// do work

}

}

public void close() {

close = true;

// interrupt here if needed

}

}

 134 views

13⟩ Tell us why would it be more secure to store sensitive data (such as a password, social security number, etc.) in a character array rather than in a String?

In Java, Strings are immutable and are stored in the String pool. What this means is that, once a String is created, it stays in the pool in memory until being garbage collected. Therefore, even after you’re done processing the string value (e.g., the password), it remains available in memory for an indeterminate period of time thereafter (again, until being garbage collected) which you have no real control over. Therefore, anyone having access to a memory dump can potentially extract the sensitive data and exploit it.

In contrast, if you use a mutable object like a character array, for example, to store the value, you can set it to blank once you are done with it with confidence that it will no longer be retained in memory.

 160 views

14⟩ Explain me what is association?

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.

 150 views

15⟩ Can you give real world examples of when to use an ArrayList and when to use LinkedList?

ArrayList is preferred when there are more get(int), or when search operations need to be performed as every search operation runtime is O(1).

If an application requires more insert(int) and delete(int) operations, then LinkedList is preferred, as LinkedList does not need to maintain back and forth to preserve continued indices as arraylist does. Overall this question tests the proper usage of collections.

 189 views

16⟩ Can you explain me what is Polymorphism?

Polymorphism is briefly described as “one interface, many implementations”. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

☛ Compile time polymorphism

☛ Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

 150 views

17⟩ Explain me what is the difference between String s = "Test" and String s = new String("Test")? Which is better and why?

In general, String s = "Test" is more efficient to use than String s = new String("Test").

In the case of String s = "Test", a String with the value “Test” will be created in the String pool. If another String with the same value is then created (e.g., String s2 = "Test"), it will reference this same object in the String pool.

However, if you use String s = new String("Test"), in addition to creating a String with the value “Test” in the String pool, that String object will then be passed to the constructor of the String Object (i.e., new String("Test")) and will create another String object (not in the String pool) with that value. Each such call will therefore create an additional String object (e.g., String s2 = new String("Test") would create an addition String object, rather than just reusing the same String object from the String pool).

 186 views

18⟩ Explain me what does it mean for a collection to be “backed by” another? Give an example of when this property is useful?

If a collection backs another, it means that changes in one are reflected in the other and vice-versa.

For example, suppose we wanted to create a whitelist function that removes invalid keys from a Map. This is made far easier with Map.keySet, which returns a set of keys that is backed by the original map. When we remove keys from the key set, they are also removed from the backing map:

public static <K, V> Map<K, V> whitelist(Map<K, V> map, K... allowedKeys) {

Map<K, V> copy = new HashMap<>(map);

copy.keySet().retainAll(asList(allowedKeys));

return copy;

}

retainAll writes through to the backing map, and allows us to easily implement something that would otherwise require iterating over the entries in the input map, comparing them against allowedKey, etcetera.

Note, it is important to consult the documentation of the backing collection to see which modifications will successfully write through. In the example above, map.keySet().add(value) would fail, because we cannot add a key to the backing map without a value.

 134 views

19⟩ Explain me what is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let’s take a look at the example below to understand it better.

class Car {

void run()

{

System.out.println(“car is running”);

}

}

class Audi extends Car {

void run()

{

System.out.prinltn(“Audi is running safely with 100km”);

}

public static void main(String args[])

{

Car b= new Audi(); //upcasting

b.run();

}

}

 133 views

20⟩ Tell us what are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

☛ Default constructor

☛ Parameterized constructor

 152 views