Friday, July 12, 2013

15 Mandatory Core Java Interview Questions - Part 2

15 Mandatory Core Java Interview Questions - Part 1

16. What is Java Byte Code?

JVM does not understand *.java files. JDL compiled *.java files and create *.class files, which contain bytecode and understood by JVM. If we take the same bytecode and run it on different enviroments, due to the JVM which is uniform, we will get same results.

This make life simpler for developers,  isn’t it? Why worry about where the code will run. Just write the code and be done.

Compiler compiles the code in a bytecode for a Virtual Processor (JVM) which in turn converts it into Machine code for a Real Processor (LinuX/Windows platform).

17. Can we define main() as private?

Yes, It can be defined as private. Program will compile fine. But we will get a runtime Error.

18. Differentiate between final, finally, finalize.

final: final variable cannot be changed once initialized, final method can't be overridden, final Class can't be extended.

finally: code inside finally block executes after the try block (Even when there is an exception). Clean up code is normally put inside finalize to close open references in case of Exceptions.

finalize: This method is at Object level. When the Object is Garbage collected, this method is called. This method is overridden in special circumstances only for system resources clean-up.

19. List down the OOPs concepts.

a) Polymorphism
b) Inheritance
c) Encapsulation

20. Explain the concept of Polymorphism.

Well as name suggests, Polymorphism means poly-forms, means Many Forms. When same name is used in many forms, we call it polymorphism. Same entity can be used for multiple actions. It happens with Method Overloading and Method Overriding.

21. Explain the concept of Inheritance.

In Java, once class can acquire properties of another class via inheritance. Sub class (which acquires properties) inherits all methods of Super Class (parent class), In Java Only Single inheritance is allowed. A class can extend only one class.

22. Explain the concept of Encapsulation.

Encapsulation is what Java Class stands for. The data of an Object is kept safe from outside world via encapsulation. Access to it is regulated, public/private/protected modifiers are used to regulate the access. It is like putting a child in a protective wool wrap to protect him/her from the world.

23. What happen if within static context, we call non-static variable.

This will result in an Error. Static Variable belongs to the Class. This variable is initialized when Clss is loaded. At that time the Object of the class is not created. Hence we will get an Error while compilation as non-static variable does not exist at that time.

24. What is Pass by reference, Pass by value?

When an object is passed during the function call, either a replica of that object can be passes or the memory location of that object (reference) can be passed.

When passed by value, the original object is safe. Any alteration to the passed object is not reflected back to the original object. When passed by reference, original object is worked upon and all changes are reflected back.

25, Is Java Pass by Value or  Pass by Reference?

Java is Pass by Value. Period.
You will find a lot of literature on this across the spectrum, bottom line is java is just pass by value. For primitive types, the value is copied over while calling a method. For user define data types, address of the memory where the object is stored is pass by value. We don't call it pass by reference as reference is like a constant pointer (we cannot reassign a reference once assigned).

26. Can constructor be overloaded in Java?

Yes, constructor overloading is permitted in Java. Each constructor need to have it's own unique parameter list.

27. De-construct and explain public static void main (String args[]).

public: main() needs to be called by JVM outside the application, hence it needs to be public
static: main() needs to be called before instantiating any object, hence it is static
void: main() is designed not to return anything hence void
String args[]: main takes as input stream of input from cmd line (as string).

28. How to calculate size of an object in Java?

Java does not have a sizeof type Operator that is present in C. Calculating the size of a User Defined Object is tricky and we are just able to approximate by calculating size of member variables. No straightforward approach. It leads me to say that Java designers didn't think we would need to calculate the sizeof Object as memory allocation/deallocation is done by Java rather than the user.

29. Explain System.out.println

System is a final class from java.lang package. out is a public member variable of System class, of type PrintStream. println is a public method of PrintStream class.

30. Can we have global variables in Java?

The answer is No. By design global variables does not exist in Java. They create unnecessary nuisance. We can use public final static which can be used as Global Constants in a separate Class (say We can use like Constants.x, Constants.y.

No comments:

Post a Comment