Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

JAVA TRICKY QUESTIONS

1. public class Test {


public static void main(String[] args) {
System.out.println(Math.min(Double.MIN_VALUE, 0.0d));
}
}
O/P: 0.0

1. public class Test {


public static void main(String[] args) {
System.out.println(Math.max(Integer.MAX_VALUE,0));
}
}

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.

3. public class Test {


public static void main(String[] args) throws Exception {
char[] chars = new char[] {'\u0097'};
String str = new String(chars);
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));
}
}
Answer: The trickiness of this question lies on character encoding and how String to byte
array conversion works. In this program, we are first creating a String from a character array,
which just has one character '\u0097', after that we are getting the byte array from that
String and printing that byte. Since \u0097 is within the 8-bit range of byte primitive type,
it is reasonable to guess that the str.getBytes() call will return a byte array that
contains one element with a value of -105 ((byte) 0x97).

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);
}

public static void method(Object o) {


System.out.println(Object impl);
}

public static void method(int s) {


System.out.println(Integer impl);
}

public static void method(String s) {


System.out.println(String impl);
}

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.

5. long longWithL = 1000*60*60*24*365L;


long longWithoutL = 1000*60*60*24*365;
System.out.println(longWithL);
System.out.println(longWithoutL);

The output of the code snippet will be:

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.

Binary equivalent of 1000*60*60*24*365L = 011101010111101100010010110000000000


(36 bits)
Removing 4 most significant bits to accommodate in 32-bit int, value =
01010111101100010010110000000000 (32 bits)
Which is equal to 1471228928 and hence the output.
6. public class HelloWorld {
public static void main(String args[]){
boolean a = false;

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";
}

class Fruit implements IFruit


{

public class Application {


public static void main(String[] args) {
System.out.println(Fruit.TYPE);
}
}

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";
}

class Apple extends Fruit {

public class Application {


public static void main(String[] args) {
System.out.println(Apple.name);
}
}
The protected access modifier allows variables to be accessed by derived
classes or classes within the same package. Since all our code here is in
the default package, it compiles.
Objects and Primitive Data
A primitive data type uses a small amount of memory to represent a single item of data. All
data of the same primitive type are the same size.

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

You might also like