Sunday, April 3, 2016

Answers to Java based questions posted...


Q1: Can modulus operation be applied on real numbers in Java?
Ans: Yes, We can use it.  Actually we do not use it for real numbers in  C/C++ languages. But Java allows to use it for real numbers.
Ex: double x=5.123456 , y=2, z;
      z=x%y;
     Here we get z as 1.123456. As it returns remainder, when x is divided by y.

Q2: What error is flagged , when a final variable is tried to modify?
Ans: The error is : " final variable can not be assigned a (new)value", as it is been declared as a constant.        If it were a C/C++ program the error flagged would be " Lvalue required error"(for more info read my post on it), bit difficult to understand the meaning. But Java message for it is simple, we can make out what is going wrong. This is the one reason of Java being SIMPLE.

Q3: Can we use float x=1.234567 ; in Java ?
Ans: No. It flags an error "Loss of precision". Java treats by default real number as of double data type.
We know the size of float is 4 bytes and size of double is 8 bytes. Here float is destination type(left side of assignment) and the real number(literal constant 1.234567)  which is by default of type double, is source.

To take place an implicit conversion the criteria is that, the destination type must be larger or equal to the source type. Here the case is reversed. So the compiler fails to perform implicit conversion and alerts the programmer about losing of data by flagging a message " Loss of precision".

Then how to initialize floating point literals in Java? for this we need to append letter ' f ' to the literal.
float x=1.234567f ; // Now it is correct

Q4: When to declare data/method as static ?
Ans: Since, in Java everything is done inside a class, we must have an object to access the data/methods.
What if we do not have an object in our hand? or else if some data/behavior is not object specific.
For such scenarios  Java provide keyword static. Anything which is not object specific, but common to the entire class  of objects, we can use static. Hence it is accessed with class name not with object/reference.
For example and  detailed discussion on it read my post here..

Q5: Can instance variables be accessed directly from static context without using object/reference?
Ans: No. Since the context is static, we invoke with help of class name. Hence no implicit object is available for this context. So explicit use of object name/reference is must.
For example and detailed discussion read my post here...

Q6: Can constructor be defined with private scope?
Ans: Yes. But usually when we define a class, our requirement would be to create objects of it somewhere outside the class/package. In such scenarios a constructor defined with private scope creates an issue of accessibility. When object is created using keyword new, a constructor gets called. If it is private, then from outside class, you can not invoke it. 
private constructor is useful as long as you restrict object creation inside class.

Q7: Which is the Java equivalent of destructor of C++?
Ans: We know garbage collector is program which performs the job of clean-up of memory. It finds in memory those objects which are no more referred/ or have become unreachable, claims them back. It will not bother, whether the unreachable object has kept opened a file/socket/thread/data base connection etc. This must be addressed by programmer. For this Java provide finalize() method, which the garbage collector calls before taking the unreachable object back to his pocket.

So, For detailed discussion of these topics,subscribe to IT Academy by visiting


How did you find the explanation?  please comment ?


No comments:

Post a Comment