Java Developer Questionnaire

Java Developer Questionnaire

Java Developer Questionnaire

Java Developer Questionnaire

1.How is Collection different from Collections in Java?

Collection is an interface whereas Collections is a java utility class containing only static methods that can operate on collections like ArrayList, Set, Queue, etc. and both belong to Collection Framework and are present in the java.util package.

Since Collection is an interface dealing with the data type of iterable objects, the interface extends the Iterable interface as shown below:

public interface Collection<E

extends Iterable<E>

Since Collections is a java class, it extends the Object class as shown below:

public class Collections

extends Object

2. How do you reverse a string in Java?

There is no reverse() utility method in the String class. However, you can create a character array from the string and then iterate it from the end to the start. You can append the characters to a string builder and finally return the reversed string.

The following example code shows one way to reverse a string:

public class StringPrograms {


public static void main(String[] args) {

String str = "123";


System.out.println(reverse(str));

}


public static String reverse(String in) {

if (in == null)

throw new IllegalArgumentException("Null is not valid input");


StringBuilder out = new StringBuilder();


char[] chars = in.toCharArray();


for (int i = chars.length - 1; i >= 0; i--)

out.append(chars[i]);


return out.toString();

}


}


Copy

Bonus points for adding null check in the method and using StringBuilder for appending the characters. Note that the indexing in Java starts from 0, so you need to start at chars.length - 1 in the for loop

3. Write a Java program to check if a vowel is present in a string.

The following example code shows how to use a regular expression to check whether the string contains vowels:

public class StringContainsVowels {


public static void main(String[] args) {

System.out.println(stringContainsVowels("Hello")); // true

System.out.println(stringContainsVowels("TV")); // false

}


public static boolean stringContainsVowels(String input) {

return input.toLowerCase().matches(".*[aeiou].*");

}


}

4. How do you check if a list of integers contains only odd numbers in Java?

You can use a for loop and check whether each element is odd:

public static boolean onlyOddNumbers(List<Integer> list) {

for (int i : list) {

if (i % 2 == 0)

return false;

}


return true;

}


Copy

If the list is large, you can use parallel stream for faster processing, as shown in the following example code:

public static boolean onlyOddNumbers(List<Integer> list) {

return list

.parallelStream() // parallel stream for faster processing

.anyMatch(x -> x % 2 != 0); // return as soon as any elements match the condition

}

5. Can you explain what a Java class is?

A Java developer needs to know the core elements of the language. Be concise when answering to show your understanding. An interviewer is likely to use a question like this to transition to more complex questions.

Example answer: "Class represents a category for objects with similar methods or properties. It's used as a blueprint for design in any object-oriented programming."

6. What are some common classes in Java?

This is also a question that reveals your core knowledge of Java. Be concise and use simple language to show you have a thorough understanding of the language. Prepare a detailed response in the event that the hiring manager wants to know how you use each class in your work.

Example answer: "There are so many classes in Java. A few essentials are final, static, concrete, abstract, inner and POJO. Would you like me to elaborate on how I use these in app development?"

7. Can you explain what platform independence is?

Java is designed to be platform-independent. If an interviewer asks this question, briefly explain what this means.

Example answer: "Platform independence is when you design a program that can run on any operating system. It makes things easier to use and market. This is why I think Java is superior to other programming languages."

8. Differentiate between JVM and JDK.

Answer: A JVM, or a Java Virtual Machine, runs Java on a device. A JDK, or Java Development Kit, is a software development package used to create tools that can be run on phones and tablets.

9. What is the main reason why strings are made immutable?

Answer: An immutable string is thread-safe. If one does not make a string immutable, then a change in one reference will inevitably affect the values of other references.

10. What do you understand about a ternary operator?

Answer: Java has several conditional operators. A ternary operator is a conditional operator responsible for deciding which values will be assigned to a variable.

11.  What are wrapper classes in Java?

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.

12. What is singleton class in Java and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

13. What is the difference between equals() and == in Java?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

14. Why is synchronization necessary? Explain with the help of a relevant example.

Java allows multiple threads to execute. They may be accessing the same variable or object. Synchronization helps to execute threads one after another.
It is important as it helps to execute all concurrent threads while being in sync. It prevents memory consistency errors due to access to shared memory. An example of synchronization code is-


1234 public synchronized void increment(){a++;}

15. Explain the term “Double Brace Initialization” in Java?

Double Brace Initialization is a Java term that refers to the combination of two independent processes. There are two braces used in this. The first brace creates an anonymous inner class. The second brace is an initialization block. When these both are used together, it is known as Double Brace Initialization. The inner class has a reference to the enclosing outer class, generally using the ‘this’ pointer. It is used to do both creation and initialization in a single statement. It is generally used to initialize collections. It reduces the code and also makes it more readable.