Type promotion is when Java automatically converts a smaller data type to a larger data type to avoid data loss during operations.
Click to reveal answer
beginner
Which data types are involved in type promotion during arithmetic operations?
Java promotes byte, short, and char to int before performing arithmetic operations.
Click to reveal answer
beginner
What happens when you add a byte and an int in Java?
The byte is promoted to int, and the result is an int.
Click to reveal answer
intermediate
Explain type promotion with an example:
byte b = 10; int i = b + 5;
Here, byte b is promoted to int before addition. So, b + 5 is int + int, resulting in an int assigned to i.
Click to reveal answer
beginner
Does Java promote boolean types during type promotion?
No, boolean types are not promoted because they represent true/false values, not numbers.
Click to reveal answer
Which type does Java promote byte and short to during arithmetic operations?
Adouble
Bint
Cfloat
Dlong
✗ Incorrect
Java promotes byte and short to int before arithmetic operations to ensure consistent calculation.
What is the result type of the expression: byte b = 5; b + 10?
Abyte
Bshort
Cint
Dlong
✗ Incorrect
The byte is promoted to int, so the result of b + 10 is an int.
Does Java automatically promote boolean values during arithmetic operations?
ANo, boolean is not promoted
BYes, to byte
CYes, to int
DYes, to double
✗ Incorrect
Boolean values are not promoted because they represent true or false, not numbers.
If you add an int and a long, what is the type of the result?
Aint
Bdouble
Cfloat
Dlong
✗ Incorrect
The int is promoted to long, so the result is long.
Which of these is NOT automatically promoted by Java during arithmetic operations?
Aboolean
Bshort
Cchar
Dbyte
✗ Incorrect
Boolean is not promoted because it is not a numeric type.
Explain what type promotion is in Java and why it is important.
Think about how Java handles small numbers when doing math.
You got /4 concepts.
Describe what happens when you add a byte and an int in Java.
Consider how Java treats smaller types in expressions.
You got /4 concepts.
Practice
(1/5)
1. Which of the following statements about Java type promotion is correct?
easy
A. long is always promoted to byte in expressions.
B. byte, short, and char are promoted to int before arithmetic operations.
C. double is promoted to float when mixed with float.
D. int is promoted to byte automatically in expressions.
Solution
Step 1: Understand type promotion basics
Java promotes smaller integer types (byte, short, char) to int before arithmetic to avoid data loss.
Step 2: Evaluate each option
byte, short, and char are promoted to int before arithmetic operations. correctly states this rule. Options A, B, and C are incorrect because long is not promoted to byte, int is not promoted to byte, and double is not promoted to float.
Final Answer:
byte, short, and char are promoted to int before arithmetic operations. -> Option B
Quick Check:
byte/short/char -> int promotion = D [OK]
Hint: Remember: small integers become int before math [OK]
Common Mistakes:
Thinking int is promoted to byte
Confusing float and double promotion
Assuming long is promoted to smaller types
2. Which of the following Java code snippets correctly compiles without explicit casting due to type promotion?
easy
A. int result = (byte)10 + (short)20;
B. short s = 10 + 20;
C. byte b = 10 + 20;
D. int sum = 10 + 20;
Solution
Step 1: Analyze type promotion in arithmetic
In expressions, byte and short are promoted to int, so 10 + 20 is int.
Step 2: Check each assignment
int result = (byte)10 + (short)20; casts explicitly, so compiles but casting is manual. byte b = 10 + 20; tries to assign int result to byte without cast - error. int sum = 10 + 20; assigns int sum correctly. short s = 10 + 20; tries to assign int to short without cast - error.
Final Answer:
int sum = 10 + 20; -> Option D
Quick Check:
byte/short -> int, assign to int = B [OK]
Hint: Assign promoted int results to int variables [OK]
Common Mistakes:
Assigning int expression to byte or short without cast
Forgetting byte/short promote to int
Assuming automatic narrowing conversion
3. What is the output of the following Java code?
byte b = 10;
short s = 20;
char c = 30;
int result = b + s + c;
System.out.println(result);
medium
A. 60.0
B. Syntax error
C. 60
D. Runtime error
Solution
Step 1: Understand type promotion in expression
byte, short, and char are promoted to int before addition, so b + s + c is int.
Step 2: Calculate the sum
10 + 20 + 30 = 60, assigned to int variable result, then printed.
Final Answer:
60 -> Option C
Quick Check:
byte+short+char -> int sum = 60 [OK]
Hint: Sum small types promoted to int, print int result [OK]
Common Mistakes:
Expecting byte or short result type
Confusing output with float/double
Thinking code causes error
4. Identify the error in the following Java code and fix it:
byte b = 5;
byte c = 10;
byte sum = b + c;
System.out.println(sum);
medium
A. Add explicit cast: byte sum = (byte)(b + c);
B. Change sum to int: int sum = b + c;
C. Change b and c to int: int b = 5, c = 10;
D. No error, code compiles fine.
Solution
Step 1: Understand type promotion in addition
byte variables b and c are promoted to int during addition, so b + c is int.
Step 2: Fix assignment error
Assigning int result to byte sum causes error. Adding explicit cast (byte)(b + c) fixes it.
Final Answer:
Add explicit cast: byte sum = (byte)(b + c); -> Option A
Quick Check:
byte+byte -> int, cast needed for byte = A [OK]
Hint: Cast int result back to byte to fix assignment error [OK]
Common Mistakes:
Ignoring need for cast when assigning to byte
Assuming automatic narrowing conversion
Changing variable types unnecessarily
5. Consider the following code snippet:
float f = 5.5f;
double d = 10.5;
var result = f + d;
System.out.println(((Object)result).getClass().getSimpleName());
What will be printed and why?
hard
A. double because float is promoted to double in mixed expressions
B. float because float is bigger than double
C. float because var infers float type
D. Compilation error due to var usage
Solution
Step 1: Understand promotion between float and double
In expressions mixing float and double, float is promoted to double.
Step 2: Determine result type and output
Variable result is inferred as double by var. Printing class name outputs "Double".
Final Answer:
double because float is promoted to double in mixed expressions -> Option A
Quick Check:
float + double -> double result = A [OK]
Hint: float + double results in double type [OK]
Common Mistakes:
Assuming var infers float
Thinking float is bigger than double
Expecting compilation error with var
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.