Java Tricky Questions
Java Tricky Questions
o/p: 2147483647
2. Does Java support multiple inheritances?
This is the trickiest question in Java if C++ can support direct multiple inheritances than why
not Java is the argument Interviewer often give. Answer of this question is much more subtle
then it looks like, because Java does support multiple inheritances of Type by allowing an
interface to extend other interfaces, what Java doesn't support is multiple inheritances of
implementation. This distinction also gets blur because of default method of Java 8, which
now provides Java, multiple inheritances of behavior as well. See why multiple inheritances
are not supported in Java to answer this tricky Java question.
On a Windows XP with the US locale, the above program prints [63], if you run this program
on Linux or Solaris, you will get different values.
4. public static void main(String[] args) {
method(null);
}
The method with String argument is taking precedence over the method with int argument
is because of the primitive nature of int. If you decalre a method with the wrapper class
pointed, then it will show you the same error with ambiguity.
31536000000
1471228928
In case of first variable, we are explicitly making it a long by placing a L at the end, so
compiler will treat this at long and assign it to first variable.
In second case, compiler will do the calculation and treat it as a 32-bit integer, since the
output is outside the range of integer max value (2147483647), compiler will truncate the
most significant bits and then assign it to the variable.
if(a=true){
System.out.println("a is true");
}else{
System.out.println("a is false");
}
}
}
you cant assign a null object to boolean first of all. You used if(a=null) supposed to
be if(a==null)
7. interface IFruit
{
public String TYPE = "Apple";
}
It's a little strange if you're not used to it, but a class that implements an interface also gets the
interface's data members.
What's even stranger is that these data members are static and final even if you don't say so.
This use of interfaces is arguably a gross misuse; interfaces should not be used to store
constants. But you can still find it in all kinds of APIs.
8. class Fruit {
protected static String name = "Sue";
}
For example, primitive type int represents integers using 32 bits. All variables of type int
use 32 bits.
There are only eight primitive data types in Java: byte, short, int, long, float, double,
char, and boolean. A Java program cannot define any other primitive data types.
An object is a large chunk of memory that can potentially contain a great deal of data along
with methods (little programs) to process that data. There are thousands of object classes that
come standard with Java, and a programmer can easily create additional classes. (Although
there are thousands of standard classes, for this course you only need become familiar with a
dozen or so classes.)
Compile : Yes
Output : String
Compile : No , Error at line 23
Reason : Method is ambiguous
Compile : Yes
Output : ArithmeticException
Compile : Yes
Output : I am born new
Compile : No , Error at line 8
Reason : Any number starting with 0 is an octal number
Compile : No , Error at line 6
Reason : Type mismatch , can not convert from int to boolean
Compile : Yes
Output : try
catch
finally
Compile : Yes
Output : try
finally
Compile : No , Error at line 12
Reason : Local variable x has not been initialized
Compile : Yes
Output : abcd abc false
abcd abcd true