Core Java Interview Questions with Answers

In this article, we will see some of the top Core Java Interview Questions with Answers. Let’s start!!!

Core Java Interview Questions with Answers

1. Apart from providing details about the code, where else do you think comments will help in java programming?

Comments help us to maintain the code and to find errors easily. Any explanation regarding the variables or the class can be given inside comments. The program does not get interrupted by the alternative code when e compile it.

2. You would’ve used the Java single-line comment and multi-line comments many times in your code. What do you know about Java documentation comments?

Documentation comments are applied in large programs like a project or software application. With its help of it, we can create the documentation API. Detailed and brief information about classes, methods, and arguments is mentioned in the documentation comment. These comments are placed between /** and */.

Generally, we use the javadoc tool to create documentation comments for a Java code.

3. What are the different data types of databases that are used to store images and files in a database table?

There are two data types to store images and files in the java database.

  • BLOB: We can use this data type to store images and even audio. It stores the binary type of data.
  • CLOB: It allows us to store files in a Java database. The data stored here is present in character format.

4. List some scenarios where a programmer might encounter the NullPointerException.

A programmer would encounter the NullPointerException in situations like:

  • Calling a variable that is not initialized.
  • Passing a null object as an argument for a method
  • Synchronizing an object that is a null
  • Making any kind of modifications in the data field with a null object.
  • Invoking a method that has a null object
  • Throwing a null object.

5. Can we use the comparison (==) operator and not equal to (!=) operators with null in java?

Yes, we can use the comparison (==) operator and not equal to (!=) operators with null in java. This is useful to check the null with the objects present in the code.

Here is a snippet to implement this concept:

public class FirstCodeTest{
public static void main(String args[]) 
{
System.out.println(null == null);
System.out.println(null != null);
}
} 

Output: 

False

True

6. Elucidate on the relationship that is a “belong-to” type of relationship. 

In Java, the concept of Composition is associated with the “belong-to” type of relationship. Here, one object is logically related to the other objects. This means the objects cannot exist in the system’s scope in the absence of each other. 

7. Name the four types of relationships in association and state an example for each. 

  • One-to-One relation: One professor is assigned to one department. 
  • One-to-Many relation: One professor is assigned to multiple departments. 
  • Many-to-One relation: Many professors are assigned to one department. 
  • Many-to-Many relations: Many professors are assigned to work in many departments. 

8. It is obvious that any method that uses the static keyword is referred to as a static method. What is the other way to find if a method is static or not?

The static keyword is used to build methods that are present without considering the presence of the objects. Therefore, such methods that exist regardless of whether or not the objects are generated can be labeled as static methods.

9. What restrictions do you face in implementing the concept of the static method?

Implementing static methods does not allow to use the of non-static data members or non-static methods. It also applies vice-versa.  Keywords like ‘this’ and ‘super’ cannot be used in a static environment. 

10. Why is static binding called as early binding in Java?

The static binding is resolved during the compile time by the compiler. The methods like static, private, and final complete their binding phase during the compile time itself. Therefore, static binding is known as early binding.

11. Which binding uses overriding methods? brief on it.

Dynamic binding or late binding uses the concept of overriding methods. This type of binding takes place without the involvement of the compiler. It uses virtual functions. This binding is mostly used by real objects.

12. Name two applications or areas where we implement the Runnable class.

We can use the runnable class in network programming. This will be easy to represent a separate flow of control for each thread present in the process. We can also use the runnable class in multi-threaded programming.

13. What are the ways to obtain string input in java?

  • Using the nextLine() method present in the Scanner class. 
  • Using the readLine() method present in the BufferedReader class. 
  • We can also obtain input through the Scanner class next() method. 
  • Moreover, the input can also be obtained via the command-line arguments of the main() method.

14. Can we alter the array that is already initialized in a java code?

Java does not provide the feature to alter or modify the array once it is initialized. Therefore, the size of the array is determined during the time of its creation. Rather, we can provide an array reference that points to another array.

15. How can you set the Keys in a KeyStore in java?

The setEntry method in Java helps to set the Keys in a KeyStore. It involves getting a password, an alias, and a secret key entry. Here is a snippet to implement to set the Keys in a KeyStore:

SecretKey secretKey = getSecretkey();
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.StoreKeyEntry(secretKey);
Keystore.setEntry(“keyAlias2”, secretKeyEntry, entryPassword);

16. What are the categories of a lambda expression?

There are two categories of lambda expression:

  • OPEN expression: The symbols present here are not bound. It means, the symbols present in them are free and they contain additional information.
  • CLOSED Expression: Certain expressions are self-contained. These expressions do not require the surrounding context to be evaluated. They are also called combinators.

17. Compare and contrast iterator and ListIterator

  • Both are used for iteration lists
  • Both are capable of forward traversal
  • They provide support for READ and DELETE operations
Iterator ListIterator
It can traverse any type of collection It only traverses list collection implemented classes such as LinkedList, ArrayList, and so on.
Elements cannot be modified. Elements can be modified.

18. Name the file Operations in Java

Some of the operations that can be performed on the file in Java are as follows:

  • Creating a File
  • Reading data or information from the file
  • Writing to a file
  • Deleting a file

19. How to create a JDBC Application

The steps involved in building a JDBC application are

1. Importing the necessary packages
2. JDBC driver registration
3. Establishing a connection
4. Execution of queries
5. Data extraction from the result set.
6. Environmental cleanup

20. How is a checked exception different from an unchecked exception?

A checked exception will be caught during the compilation process whereas an unchecked exception will be caught during the run time or execution process.

An unchecked exception does not need to be handled, whereas a checked exception must be handled by re-throwing or using a try-catch block.

21. Mention the guidelines for wildcards.

  • Upper bound wildcard: Use the ‘extends’ keyword with the wildcard if the variable corresponds to the ‘in’ category
  • Lower bound wildcard: Use the ‘super’ keyword with the wildcard if the variable corresponds to the ‘out’ category
  • Unbounded wildcard: Use an unbound wildcard if a variable can be accessed via an Object class method. 
  • No wildcard: Do not use the wildcard, if the variable corresponds to both ‘in’ and ‘out’ categories. 

22. What do Java JAR files contain?

A Java Archive, or JAR file, comprises a standalone, executable java application, deployable Java applet, or, more commonly, a Java library that can be linked to any Java Runtime Environment.

23. How to write Java Pair class?

The Java Pair class stores value in the key-value pair combination. This key-value pair is referred to as a tuple.

pair<Key Type, Value Type> var_name = new pair<>(key, value); 

pair<Key Type, Value Type>: New Java pair will be created. 

24. State the 3 methods of the Iterator Interface

The three methods in Iterator Interface are

1. Public Boolean hasNext(): Returns true, if there are more elements; otherwise false.
2. Public Object next(): Returns the elements and the cursor pointer is moved to the next element.
3. Public void remove(): Removes the last element returned by the iterator. It is less frequently used.

25. What are the two pieces of information the client must know in Socket Programming?

1. Server IP Address – It is an external IP address that connects the computer system to the Internet Service Provider (ISP).
2. Port Number – A port number helps to identify a particular process to which an internet or other network message should be forwarded.

26. Mention some of the uses of Annotation.

Annotations aid in the association of metadata (information) to program elements such as instance variables, constructors, methods, classes, and so on. These are used for error detection and suppressing warning and provides additional information and could be used in place of XML and Java marker interfaces. 

27. Why do you know about the concept DatabaseMetaData?

The DatabaseMetaData is an interface that contains various methods to get information regarding the database. It is very much helpful to get database-related information like the name of the database, its version, the driver’s name, the total number of tables, views, etc.

28. How do you differentiate RowSet from ResultSet?

RowSet ResultSet
It is not connected to the database for serialization. It handles the connection with the database, therefore it cannot be serialized.
The object here is JavaBean object. The object here is not a JavaBean object.
The object here is scrollable and updateable. The object here is not scrollable and updateable.

29. List down a few roles of the java compiler:

  • It checks the correctness of the program code including its data type errors, syntax, etc. 
  • It scans the entire source code in a single go and then looks for errors. 
  • The compiler also adds additional code to our program if required. 

30. point down a few roles of the java interpreter:

  • It converts the bytecode to the native code of the computer machine. 
  • It checks the code line by line. 
  • Once an error is encountered, the process is stopped.

Summary

Hope you enjoyed the Core Java Interview Questions with answers.

Leave a Reply

Your email address will not be published. Required fields are marked *