100 Core Java Interview Questions and Answers - Part 7

Here are top selected core java interview questions asked in high level interviews, these questions may be helpful for both freshers or experienced professional going for interview.

61) What if we does not pass an String array of objects to main() method ?

Program will compile fine, but at runtime it will give an error ?NoSuchMethodError?.

62) List all primitive types in Java ?

There are a total of 8 primitive types in java; byte, short, char, int, long, Boolean, float and double. In java String is an object and not a primitive type.

63) Can we have multiple main() methods in the same class?

No, We can have multiple overloaded methods in a same class with name main(), but there must be a single method with signature ?public static void main(String args[])?. Unless the compiler says main method already defined.

64) What is difference between ?String? and ?StringBuffer? class in Java?

In java, ?String? is an immutable class whereas ?StringBuffer? is not immutable in nature. This means that once a String object is created we cannot change its state. Every time an operation is performed on a String object, it results the creation of a new Object and the old object remains unchanged. Whereas in ?StringBuffer? because of being a non-immutable class, the changes are reflected to the same object on which the operation is performed.

65) What is transient variable in Java?

If we declare a variable as transient, it prevents that variable from being serialized. If a class is serialized to a stream and one tries to deserialize it again than the variable having transient with it return a null value.

66) Are ?true? ,?false? and ?null? keywords in Java?

No, Neither of the three true, false or null is a keyword in java.

67) Can we declare an abstract class as final.

No, we cannot declare an abstract class as final because abstract classes are meant to be extended by some other class to make it meaningful. If we declare a class final it cannot be extended by some other class and hence an abstract class cannot be declared as final.

68) What is the default value of a boolean type in Java?

In java a Boolean type variable is initialized to ?false? by default.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.