Java Interview Questions and Answers – Part 2

21. Why is an Interface be able to extend more than one Interface but a Class can’t extend more than one Class?

Basically Java doesn’t allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn’t have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.

22. Can a class be defined inside an Interface?

Yes it’s possible.

23. Can an Interface be defined inside a class?

Yes it’s possible.

24. What is a Marker Interface?

An Interface which doesn’t have any declaration inside but still enforces a mechanism.

25. Which object oriented Concept is achieved by using overloading and overriding?


26. Why does Java not support operator overloading?

Operator overloading makes the code very difficult to read and maintain. To maintain code simplicity, Java doesn’t support operator overloading.

27. Can we define private and protected modifiers for variables in interfaces?


28. What is Externalizable?

Externalizable is an Interface that extends Serializable Interface and sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)

29. What modifiers are allowed for methods in an Interface?

Only public and abstract modifiers are allowed for methods in interfaces.

30. What is a local, member and a class variable?

Variables declared within a method are “local” variables.
Variables declared within the class i.e not within any methods are “member” variables (global variables).
Variables declared within the class i.e not within any methods and are defined as “static” are class variables.

31. How to create a simple Jar File from the list of class files in a directory?

We can use below command. Don’t miss the space between / and . at the end

32. How to create Executable Jar Archive (JAR) File ?

Let us say, we have 2 compiled classes Employee and Person inside the folder “SRC” which resides inside the folder “WORK”. Employee class contains static void main method.
Before creating a Jar file, first create a mainclass file in the WORK folder. Specify the name of the class in which main methods resides. Here (“Main-Class: SRC.Employee”) and press enter.
From terminal change directory to WORK Folder. Enter the following command in the command prompt “jar -cmf mainclass jarfilename.jar SRC/*.class” and press enter. Executable jar will created. To test the newly created jar, use following command “java -jar jarfilename.jar”

33. How to convert an object  into string ?

We can use “toString” method to convert an object to string. For example

 34. Can a Byte object be cast to a double value?

No, an object cannot be cast to a primitive value.

35. What is the % operator?

It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.

More Java interview questions and answers will be published in the upcoming posts under this category.

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

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