Q1. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = i * i + j;
System.out.println(b);
A1. The code snippet will NOT compile. The right hand side (RHS) expression is an int, and int cannot be stored in the byte variable on the left hand side (LHS).
Q2. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)i * i + j;
System.out.println(b);
A2. The code snippet will NOT compile. The typecast (byte) i the RHS does not apply to the complete RHS, but applies only to the first i. The result of the RHS is still type int. The LHS is of type byte. Since it cannot be stored into byte, the code snippet will not compile.
Q3.
int i = 4;
byte j = 2;
byte b = (byte)(i * i) + j;
System.out.println(b);
A3. The code snippet will NOT compile. The first term i*i in the RHS is typecase to byte. The RHS now is byte + byte which is of type int. The LHS is of type byte. Since int cannot be stored into byte, the code snippet will not compile.
Q4. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)(i * i) + (byte)2;
System.out.println(b);
A4. The code snippet will NOT compile. With all the typecasts shown, the RHS is still byte+byte which is of type int. The LHS is of type byte. Since int cannot be stored into byte, the code snippet will not compile.
Q5. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)(i * i + j);
System.out.println(b);
A5. Yes, the code snippet will compile. The i*i + j = 16 + 2 = 18 of type int. This is typecast into byte. The LHS is byte. Hence, a byte is being assigned to a byte, which is allowed.
Q6. Will the following code snippet compile? Why?
byte b1 = 2, b2 = 3;
byte b = b1 + b2;
System.out.println(b);
A6. The code snippet will NOT compile. The RHS is byte + byte, which is int. The LHS is byte. An int cannot be assigned to a byte.
Q7. Will the following code snippet compile? Why?
byte b = 4;
b++;
System.out.println(b);
A7. Yes, the code will compile. The increment/decrement operators are allowed for byte, short, char. The output will be 5.
Q8. Will the following code snippet compile? Why?
byte b = 4;
byte p;
p = ++b;
System.out.println(p);
A8 Yes, the code will compile. An increment / decrement operator on a byte, results in another byte value only. So, we have RHS as a byte, LHS as a byte, and hence this will compile.
Q9. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)2*++b;
System.out.println(p);
A9. The code snippet will NOT compile. The RHS is (byte)2 * ++b. ++b is ++4 = 5 as a byte. This is multiplied by (byte)2. So, we have byte * byte, which will yield int. The LHS is a byte. Since int cannot be assigned to a byte, the code will not compile and the compiler will give a possible loss of precision error.
Q10. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)b*b;
System.out.println(p);
A10. The code snippet will NOT compile. The RHS is (byte)b * b = byte * byte which is of tyep int. The (byte) typecast is applicable to the first operand only, not to the result of the RHS. The LHS is of type byte. Since int cannot be assigned to a byte, the code will not compile.
Q11. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)(b*b);
System.out.println(p);
A11. Yes, the code snippet will compile. The output is 16. The RHS is b * b = 4 * 4 which is 16 (int). This has got typecasted to byte, and hence can be assigned to the variable on the LHS.
Q12. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 127;
b++;
System.out.println(b);
A12. Yes, the code snippet will compile. The output is -128. Why so? A byte is 8 bits. 127 is 01111111. When we do b++, it becomes 1000000 which is -128.
Q13. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b1 = 127, b2 = 127;
byte b = (byte)(b1 + ++b2);
System.out.println(b);
A13. Yes, the code will compile, since the RHS is typecast into byte. The RHS is b1 + ++b2 = 127 + ++127 = 127 + 128. This is evaluated as an int, and yields 255. 255 is represented in binary as 11111111. When this is typecast into byte, we can consider this as 1000000 + 01111111 = byte value of -128 plus byte value of +127, which is -1. The output is -1.
Q14. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 1;
for(int i=1; i<= 127; i++)
b++;
System.out.println(b);
A14. Yes, the code snippet will compile. The b++ happens 127 times, so the result will be 1+127 = 128 as a byte, which is 10000000 which is -128. The output is -128.
Q15. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 0;
for(; b<5; b++)
System.out.println(b);
A15. Yes, the code snippet will compile. The b<5 boolean expression works. The output is:
0
1
2
3
4
Q16. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 0;
int limit = 5;
for(; b<limit-2; b++)
System.out.println(b);
A16. Yes, the code snippet will compile. The output will be:
0
1
2
Q17. The code snippet below does not compile. What are the changes needed to ensure it does compile, and give an answer of 19?
int i = 6;
byte b = i;
short s = b+1;
byte answer = i+b+s;
System.out.println("Answer: " + answer);
A17. The three typecasts needed are shown below as comments // (1), // (2), and // (3) respectively.
int i = 6;
byte b = (byte)i; // (1)
short s = (short)(b+1); // (2)
byte answer = (byte)(i+b+s); // (3)
System.out.println("Answer: " + answer);
Q18. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b1 = 17, b2 = 5;
byte b = (byte)b1 / (byte)b2;
System.out.println("b: " + b);
A18. The code snippet will NOT compile. On the 2nd line, even though b1 and b2 are typecasted to (byte), the result of the division is still an int data type. So, the RHS evaluates to an int, and the LHS is a byte. Since int to byte implicit conversion is not allowed, the code snippet will not compile.
Q19. Explain the difference between explicit and implicit typecast with an example.
A19. An explicit typecast is when a typecast is necessary, and without which the code will not compile. An implicit typecast is when Java can automatically take care of the conversion between data types.
Consider the example below:
byte b1 = 4, b2 = 5, b;
long i;
b = (byte)(b1 + b2); // (1)
long = b1 + b2 + b; // (2)
In (1), the RHS is an “int”. This cannot be stored into a “byte”, which has a lower range of values. Hence, we need to explicitly typecast the result into a “byte” using (byte), otherwise the compiler will complain of a “possible loss of precision” error.
In (2), the RHS is “int”, which is lower than the datatype of the LHS, which is “long”. The copy from “int” to “long” happens without any loss of precision, and Java takes care of this automatically (implicitly). This means that a typecast is not needed here.
int i = 4;
byte j = 2;
byte b = i * i + j;
System.out.println(b);
A1. The code snippet will NOT compile. The right hand side (RHS) expression is an int, and int cannot be stored in the byte variable on the left hand side (LHS).
Q2. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)i * i + j;
System.out.println(b);
A2. The code snippet will NOT compile. The typecast (byte) i the RHS does not apply to the complete RHS, but applies only to the first i. The result of the RHS is still type int. The LHS is of type byte. Since it cannot be stored into byte, the code snippet will not compile.
Q3.
int i = 4;
byte j = 2;
byte b = (byte)(i * i) + j;
System.out.println(b);
A3. The code snippet will NOT compile. The first term i*i in the RHS is typecase to byte. The RHS now is byte + byte which is of type int. The LHS is of type byte. Since int cannot be stored into byte, the code snippet will not compile.
Q4. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)(i * i) + (byte)2;
System.out.println(b);
A4. The code snippet will NOT compile. With all the typecasts shown, the RHS is still byte+byte which is of type int. The LHS is of type byte. Since int cannot be stored into byte, the code snippet will not compile.
Q5. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = (byte)(i * i + j);
System.out.println(b);
A5. Yes, the code snippet will compile. The i*i + j = 16 + 2 = 18 of type int. This is typecast into byte. The LHS is byte. Hence, a byte is being assigned to a byte, which is allowed.
Q6. Will the following code snippet compile? Why?
byte b1 = 2, b2 = 3;
byte b = b1 + b2;
System.out.println(b);
A6. The code snippet will NOT compile. The RHS is byte + byte, which is int. The LHS is byte. An int cannot be assigned to a byte.
Q7. Will the following code snippet compile? Why?
byte b = 4;
b++;
System.out.println(b);
A7. Yes, the code will compile. The increment/decrement operators are allowed for byte, short, char. The output will be 5.
Q8. Will the following code snippet compile? Why?
byte b = 4;
byte p;
p = ++b;
System.out.println(p);
A8 Yes, the code will compile. An increment / decrement operator on a byte, results in another byte value only. So, we have RHS as a byte, LHS as a byte, and hence this will compile.
Q9. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)2*++b;
System.out.println(p);
A9. The code snippet will NOT compile. The RHS is (byte)2 * ++b. ++b is ++4 = 5 as a byte. This is multiplied by (byte)2. So, we have byte * byte, which will yield int. The LHS is a byte. Since int cannot be assigned to a byte, the code will not compile and the compiler will give a possible loss of precision error.
Q10. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)b*b;
System.out.println(p);
A10. The code snippet will NOT compile. The RHS is (byte)b * b = byte * byte which is of tyep int. The (byte) typecast is applicable to the first operand only, not to the result of the RHS. The LHS is of type byte. Since int cannot be assigned to a byte, the code will not compile.
Q11. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 4;
byte p;
p = (byte)(b*b);
System.out.println(p);
A11. Yes, the code snippet will compile. The output is 16. The RHS is b * b = 4 * 4 which is 16 (int). This has got typecasted to byte, and hence can be assigned to the variable on the LHS.
Q12. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 127;
b++;
System.out.println(b);
A12. Yes, the code snippet will compile. The output is -128. Why so? A byte is 8 bits. 127 is 01111111. When we do b++, it becomes 1000000 which is -128.
Q13. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b1 = 127, b2 = 127;
byte b = (byte)(b1 + ++b2);
System.out.println(b);
A13. Yes, the code will compile, since the RHS is typecast into byte. The RHS is b1 + ++b2 = 127 + ++127 = 127 + 128. This is evaluated as an int, and yields 255. 255 is represented in binary as 11111111. When this is typecast into byte, we can consider this as 1000000 + 01111111 = byte value of -128 plus byte value of +127, which is -1. The output is -1.
Q14. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 1;
for(int i=1; i<= 127; i++)
b++;
System.out.println(b);
A14. Yes, the code snippet will compile. The b++ happens 127 times, so the result will be 1+127 = 128 as a byte, which is 10000000 which is -128. The output is -128.
Q15. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 0;
for(; b<5; b++)
System.out.println(b);
A15. Yes, the code snippet will compile. The b<5 boolean expression works. The output is:
0
1
2
3
4
Q16. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 0;
int limit = 5;
for(; b<limit-2; b++)
System.out.println(b);
A16. Yes, the code snippet will compile. The output will be:
0
1
2
Q17. The code snippet below does not compile. What are the changes needed to ensure it does compile, and give an answer of 19?
int i = 6;
byte b = i;
short s = b+1;
byte answer = i+b+s;
System.out.println("Answer: " + answer);
A17. The three typecasts needed are shown below as comments // (1), // (2), and // (3) respectively.
int i = 6;
byte b = (byte)i; // (1)
short s = (short)(b+1); // (2)
byte answer = (byte)(i+b+s); // (3)
System.out.println("Answer: " + answer);
Q18. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b1 = 17, b2 = 5;
byte b = (byte)b1 / (byte)b2;
System.out.println("b: " + b);
A18. The code snippet will NOT compile. On the 2nd line, even though b1 and b2 are typecasted to (byte), the result of the division is still an int data type. So, the RHS evaluates to an int, and the LHS is a byte. Since int to byte implicit conversion is not allowed, the code snippet will not compile.
Q19. Explain the difference between explicit and implicit typecast with an example.
A19. An explicit typecast is when a typecast is necessary, and without which the code will not compile. An implicit typecast is when Java can automatically take care of the conversion between data types.
Consider the example below:
byte b1 = 4, b2 = 5, b;
long i;
b = (byte)(b1 + b2); // (1)
long = b1 + b2 + b; // (2)
In (1), the RHS is an “int”. This cannot be stored into a “byte”, which has a lower range of values. Hence, we need to explicitly typecast the result into a “byte” using (byte), otherwise the compiler will complain of a “possible loss of precision” error.
In (2), the RHS is “int”, which is lower than the datatype of the LHS, which is “long”. The copy from “int” to “long” happens without any loss of precision, and Java takes care of this automatically (implicitly). This means that a typecast is not needed here.