Short answerModerate2 marks
What are the types of casting shown by the following examples? (i) char c = (char)120; (ii) int x = 't';
Topic: Type casting
Previous-year question practice database
Answer
Exam answer
(i) Explicit type casting (ii) Implicit type casting
Explanation
(i) The type written in brackets, (char), forces the conversion. The programmer does it deliberately, so it is explicit casting. It is needed here because an int holds more than a char, and Java will not narrow a value silently. The result is the character with code 120, which is 'x'. (ii) A char occupies 2 bytes and an int 4, so the value widens safely and Java performs the conversion by itself — implicit casting, also called coercion. x becomes 116, the code for 't'. The rule: widening is implicit, narrowing must be explicit.
More from Revision Tour I
The full form of JVM is:2026Which of the following occupies 2 bytes of storage?2026In a statement c = c + (x * d + e); which variable is an accumulator?2026Assertion (A): The result of the Java expression 3 + 7/2 is 6.
Reason (R): According to the hierarchy of operators in Java, addition is done first followed by division.2026Evaluate the Java expression:
x = a * b % (++c) + (++a) + (--b);
if a = 7, b = 8, c = 22026System.out.println('Z' + 32); will display:2025