Tricky Java Questions
Java is most popular programming language. It is widely used programming languange and has many job opportunities. Interviewers often ask tricky questions to check the candidate knowldege about Java programming language. Sometimes experienced programmers also fail to answer tricky questions. In this post, I have picked few conceptual questions that will helped you to have better understanding of Java language.
Q-1: what will be output of following code ?
public static void main(String[] args) {
Integer number1 = 100;
Integer number2 = 100;
if (number1 == number2) {
System.out.println("number1 == number2");
} else {
System.out.println("number1 != number2");
}
Answer:
number1 == number2"
We have compared the references of two Integers. The result is surprising !!!. This is because of the autoboxing. Java uses IntegerCache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive). If you provide value of Integer variables outside of the range (128 and 127) then the output of above question will be differnt. The size of cache may be controlled by the following option:
-XX:AutoBoxCacheMax=[size]
Q-2: What will be the output of following code?
public static void main(String[] args) {
boolean flag = false;
if (flag = true) {
System.out.println("a is true");
} else {
System.out.println("a is false");
}
}
Answer:
The output of the code will be:
a is true
The value of variable flag is assigned as true, Hence condition will be true and it will execute if block. Lets say flag value is assigned as false ( if (flag = false) ) then else block will get executed.
Q-3: What is the output of below code ?
public static void main(String[] args) {
System.out.println(0.1*7 == 0.7);
System.out.println(0.1*4 == 0.4);
}
Answer:
The first statement will print true and second will print false. This is because the rounding issue of floating point numbers. Java uses double to represent decimal value by default.So only 64 bits are available to represent a number. Only numbers which are power of 2 can represent by simple binary representation. Numbers which are not in power of 2 should be rounded to fit into limited numbers for bits.
System.out.println(0.1*7);
System.out.println(0.1*4);
The above statement will print:
0.7000000000000001
0.4
Q-4: What will be output of following code?
public static void main(String[] args) {
double a = 0;
double b = 0.1 * 8;
for (int i = 1; i <= 8; i++) {
a += 0.1;
}
if ( a == b) {
System.out.println("a == b");
} else {
System.out.println("a != b");
}
}
Answer:
The output will print
a != b
This is because the rounding issue with double type values. The comparison operator == will give inacurate result because of the way the double is stored into computer memory. Double must be rounded and saved in limited memory space of 64 bits. Hence, we should use java.math.BigDecimal for better precision and control over rounding.
The double comparision questions are tricky and quite often asked during interview.
Q-5: Guess the output ?
public static void main(String[] args) {
String str = new String("55");
System.out.println(10 + 10 + str + 10 + 10);
}
Answer:
20551010
Explaination:
During String concatenation, if both operands are numbers then, it will perform addition else toString() method is called to perform concatenation. The expression will evaluate from left to right.
Hence, first two numbers in above example will be added and remaining will be concatenated as the vairable str is String type.
Q-6: As ASCII code for 'A' is 65. What will be output of following code?
char a1 = 'A';
float b1 = a1 + 0.9f;
System.out.println( b1);
char c = (char) b1;
System.out.println("Char value: "+ c);
Answer:
Char value: A
Explaination:
This questions is related to narrowing primitive conversion in Java. Narrowing primitive type conversion never results in runtime exceptions though overflow, underflow or other loss of information might occure.
Conversion of float to char is two step process.In first step, floating point number is converted either to long (if target type is long) or int, if target type is byte,short,char, or int. In second step, the result from first step is further converted into target type (if type is other than long and int). In the given question float value is converted to int and then converted to char and hence the output is A.
Java is most popular programming language. It is widely used programming languange and has many job opportunities. Interviewers often ask tricky questions to check the candidate knowldege about Java programming language. Sometimes experienced programmers also fail to answer tricky questions. In this post, I have picked few conceptual questions that will helped you to have better understanding of Java language. #### Q-1: what will be output of following code ? ``` public static void main(String[] args) { Integer number1 = 100; Integer number2 = 100; if (number1 == number2) { System.out.println("number1 == number2"); } else { System.out.println("number1 != number2"); } ``` #### Answer: ``` number1 == number2" ``` We have compared the references of two Integers. The result is surprising !!!. This is because of the autoboxing. Java uses IntegerCache to support the object identity semantics of autoboxing for values between **-128 and 127 (inclusive)**. If you provide value of Integer variables outside of the range (128 and 127) then the output of above question will be differnt. The size of cache may be controlled by the following option: ``` -XX:AutoBoxCacheMax=[size] ``` #### Q-2: What will be the output of following code? ``` Java public static void main(String[] args) { boolean flag = false; if (flag = true) { System.out.println("a is true"); } else { System.out.println("a is false"); } } ``` #### Answer: The output of the code will be: ``` a is true ``` The value of variable *flag* is assigned as *true*, Hence condition will be *true* and it will execute *if* block. Lets say *flag* value is assigned as *false* ( *if (flag = false)* ) then else block will get executed. #### Q-3: What is the output of below code ? ``` Java public static void main(String[] args) { System.out.println(0.1*7 == 0.7); System.out.println(0.1*4 == 0.4); } ``` #### Answer: The first statement will print *true* and second will print *false*. This is because the rounding issue of floating point numbers. Java uses *double* to represent decimal value by default.So only 64 bits are available to represent a number. Only numbers which are power of *2* can represent by simple binary representation. Numbers which are not in power of *2* should be rounded to fit into limited numbers for bits. ``` Java System.out.println(0.1*7); System.out.println(0.1*4); ``` The above statement will print: ``` 0.7000000000000001 0.4 ``` #### Q-4: What will be output of following code? ```Java public static void main(String[] args) { double a = 0; double b = 0.1 * 8; for (int i = 1; i <= 8; i++) { a += 0.1; } if ( a == b) { System.out.println("a == b"); } else { System.out.println("a != b"); } } ``` #### Answer: The output will print ``` a != b ``` This is because the rounding issue with *double* type values. The comparison operator == will give inacurate result because of the way the double is stored into computer memory. Double must be rounded and saved in limited memory space of 64 bits. Hence, we should use *java.math.BigDecimal* for better precision and control over rounding. The double comparision questions are tricky and quite often asked during interview. #### Q-5: Guess the output ? ``` Java public static void main(String[] args) { String str = new String("55"); System.out.println(10 + 10 + str + 10 + 10); } ``` #### Answer: ``` 20551010 ``` ##### Explaination: During String concatenation, if both operands are numbers then, it will perform addition else *toString()* method is called to perform concatenation. The expression will evaluate from **left to right**. Hence, first two numbers in above example will be added and remaining will be concatenated as the vairable *str* is *String* type. #### Q-6: As ASCII code for 'A' is 65. What will be output of following code? ``` Java char a1 = 'A'; float b1 = a1 + 0.9f; System.out.println( b1); char c = (char) b1; System.out.println("Char value: "+ c); ``` #### Answer: ``` Char value: A ``` ##### Explaination: This questions is related to narrowing primitive conversion in Java. Narrowing primitive type conversion never results in runtime exceptions though overflow, underflow or other loss of information might occure. Conversion of *float* to *char* is two step process.In first step, floating point number is converted either to **long** (if target type is *long*) or **int**, if target type is *byte,short,char,* or *int*. In second step, the result from first step is further converted into target type (if type is other than long and int). In the given question *float* value is converted to *int* and then converted to *char* and hence the output is **A**.
Comments
Post a Comment