Here is a question that has quietly cost ICSE students marks for years. What does this print?
System.out.println(5 / 2);
If you said 2.5, you have just met the reason this unit exists. Java answers 2. Not because it cannot divide, but because you told it you were working with whole numbers and it took you at your word. Data types are not paperwork. They are the promise you make to the compiler about what kind of value a variable holds, and the compiler keeps that promise even when you wish it would not.
This unit is where Section A marks are won and lost in bulk. Almost every output-prediction question rests on a data type detail: an integer division, a char quietly turning into a number, a double printing 350.0 where you wrote 350. Get this unit right and those questions become free marks. Skim it and they stay a lottery.
Every program on this page was compiled and executed on JDK 11 before publication. The output shown under each one is what the machine actually printed.
- Where This Unit Sits in the ICSE Class 10 Syllabus
- The Character Set, ASCII and Unicode
- Escape Sequences — and Why the Backslash Matters
- Tokens — the Five Kinds of Word in a Java Program
- Literals, Constants and Variables
- The Eight Primitive Types, With Sizes and Ranges
- Primitive vs Non-Primitive (Composite) Types
- Type Conversion — Implicit Widening and Explicit Casting
- Four Worked Programs With Verified Output
- Output Prediction — Where the Traps Are Hidden
- Practice Worksheet — 10 Questions With Model Answers
Your Game Plan
- Memorise the eight primitive types with their sizes before anything else. Eight names, eight sizes. Ten minutes of honest effort, and half this unit is done.
- Learn the four ASCII anchors — ‘A’ is 65, ‘a’ is 97, ‘0’ is 48, space is 32. Everything else you can work out from these four.
- Understand widening and narrowing as a picture — a small glass pouring into a big one needs no help; a big glass pouring into a small one needs your permission and spills.
- Type the four programs. Then deliberately break one line and read the compiler error. Errors teach types faster than notes do.
- Do the worksheet with answers hidden and write your prediction before revealing.
Where This Unit Sits in the ICSE Class 10 Syllabus
In the official CISCE syllabus for Computer Applications (subject code 86), the Class X course opens with Revision of Class IX Syllabus, and Values and Data types is the third item in that list. The Class IX scope names exactly what is examinable: character set, ASCII code, Unicode, escape sequences, tokens, constants and variables, data types, and type conversions.
Within the paper, CISCE’s published analysis of the Computer Applications examination shows Section A worth 40 marks with all questions compulsory, and Section B worth 60 marks from which any four questions are attempted. This unit is Section A territory almost entirely — short definitions and output questions.
How those 100 internal marks are actually split: class design 10, variable description 10, coding and documentation 10, and execution or output 20 — by each of the two examiners. Notice that execution or output is worth double any other criterion. A file full of elegant-looking programs that were never actually run is the single most expensive mistake in this subject.
Syllabus year note: this is the CISCE syllabus for Examination Year 2027. From Year 2028 the internal assessment changes to 15 laboratory assignments plus a 20-mark written project on Disruptive Technologies, and the Class X theory course drops Encapsulation and double dimensional arrays. If you are in Class 9 now, use the 2028 document instead.
The Character Set, ASCII and Unicode
A computer stores numbers. Only numbers. So before it can store the letter A, somebody has to agree on which number will stand for A. That agreement is a character set — a table matching characters to numeric codes.
ASCII was the old agreement: 7 bits, so 128 codes, enough for English letters, digits, punctuation and a few control characters. Four values from that table are worth committing to memory because every ASCII question is built from them:
| Characters | ASCII codes |
|---|---|
| ‘A’ to ‘Z’ | 65 to 90 |
| ‘a’ to ‘z’ | 97 to 122 |
| ‘0’ to ‘9’ | 48 to 57 |
| space | 32 |
Notice the gap between an uppercase letter and its lowercase partner is always 32. That single fact answers a whole family of questions, and it is why (char)('A' + 32) gives ‘a’.
ASCII’s problem is obvious the moment you leave English. There is no room in 128 codes for Devanagari, Tamil, Bengali, Arabic or Chinese. Unicode solved that by using a much larger code space and assigning a unique number to every character of virtually every writing system in the world. Java stores a char in 16 bits precisely because it uses Unicode rather than ASCII, and the first 128 Unicode values are deliberately identical to ASCII, which is why ‘A’ is still 65.
Escape Sequences — and Why the Backslash Matters
Some characters cannot be typed straight into a String. You cannot press the Tab key inside quotation marks and expect it to survive, and you cannot type a double quote inside a double-quoted String because the compiler would think the String had ended. So Java offers escape sequences: a backslash followed by a letter or symbol, which the compiler reads as a single special character.
| Escape sequence | Meaning |
|---|---|
\n | new line — moves the cursor to the beginning of the next line |
\t | horizontal tab — moves to the next tab stop |
\\ | prints a single backslash |
\" | prints a double quotation mark |
\' | prints a single quotation mark |
The one that catches people is \\. To print one backslash you must write two, because a lone backslash means something special is coming next. Counting backslashes carefully is the whole of that question.
Tokens — the Five Kinds of Word in a Java Program
Read an English sentence and your brain splits it into words and punctuation without effort. A compiler does the same thing to your program, and the smallest individual unit it recognises is called a token. There are five kinds, and the syllabus names all five.
- Keywords — words reserved by the language with a fixed meaning, which you may not use as names. Examples:
class,int,void,if,new,static. - Identifiers — the names you invent for classes, variables and methods, such as
Student,marks,display. - Literals — fixed values written directly into the code, such as
25,3.14,'A',"Java",true. - Operators — symbols that perform operations, such as
+,-,*,/,%,=,>. - Punctuators — also called separators:
;,.( ){ }[ ].
marks and Marks are two different identifiers.Valid:
total, _count, marks1, myClass. Invalid: 1marks (starts with a digit), my marks (space), class (keyword).Literals, Constants and Variables
A literal is a fixed value written directly in the program — 25, 3.14, 'A', "ICSE", true. A variable is a named memory location whose value can change while the program runs. A constant is a named memory location whose value cannot be changed once assigned; in Java you make one with the final keyword, as in final double PI = 3.14;.
Two small pieces of syntax that appear in output questions. A whole number literal is int by default, so a value too large for an int needs an L suffix: long big = 9000000000L;. A decimal literal is double by default, so a float needs an f suffix: float f = 3.14f;. Forget those letters and the compiler refuses, which is itself a nice way to remember them.
The Eight Primitive Types, With Sizes and Ranges
Java has exactly eight primitive data types. Learn them as four groups and the list stops feeling arbitrary: four for whole numbers, two for decimals, one for characters, one for true or false.
| Type | Size | Holds | Range |
|---|---|---|---|
byte | 1 byte / 8 bits | whole numbers | -128 to 127 |
short | 2 bytes / 16 bits | whole numbers | -32768 to 32767 |
int | 4 bytes / 32 bits | whole numbers | about -2.1 billion to 2.1 billion |
long | 8 bytes / 64 bits | whole numbers | very large whole numbers |
float | 4 bytes / 32 bits | decimal numbers | single precision, about 7 significant digits |
double | 8 bytes / 64 bits | decimal numbers | double precision, about 15 significant digits |
char | 2 bytes / 16 bits | a single character | one Unicode character |
boolean | 1 bit of information | a logical value | only true or false |
Three things students get wrong here every year. char is 2 bytes in Java, not 1 — it is 1 byte in C and C++, and that is where the confusion comes from. Java uses Unicode, which needs 16 bits. Second, boolean holds only true or false, never 0 or 1; writing boolean b = 1; is a compile error. Third, the default type of a decimal literal is double, not float.
A memory trick for the four integer sizes: 1, 2, 4, 8 bytes for byte, short, int, long. They double each time, in the order the names get longer. Once you have that, float 4 and double 8 follow the same doubling.
Default values — a high-yield detail. A member variable of a class that you never initialise does not contain rubbish; Java gives it a defined default. A local variable inside a method gets no such favour, and using one before assigning it is a compile error. Knowing both halves of that sentence answers a lot of one-mark questions.
| Type | Default value of a member variable |
|---|---|
byte, short, int, long | 0 |
float, double | 0.0 |
char | the null character, whose Unicode code is 0 (it prints as blank) |
boolean | false |
String and other objects | null |
All of the above were printed by an executed program, including the char, which showed a blank with the code 0, and the String, which printed the word null.
Primitive vs Non-Primitive (Composite) Types
The eight above are primitive: built into the language, holding a single value, of a fixed size. Everything else is non-primitive, also called composite or reference type — String, arrays, and every class you or anyone else writes.
| Primitive type | Non-primitive (composite) type |
|---|---|
| Built into the language; there are exactly eight. | Created by the user or supplied in a library; unlimited in number. |
| Stores a single value of a fixed size. | Can group several values and methods together. |
| The variable holds the value itself. | The variable holds a reference to an object stored elsewhere in memory. |
| Has no methods of its own. | Has methods that can be called on it. |
Names begin with a small letter: int, char. | Class names conventionally begin with a capital: String, Student. |
That last row is a genuinely useful test in an exam. If the type name starts with a capital letter, it is a class, therefore non-primitive. String begins with a capital S for exactly this reason — it is a class, not a primitive type, and that is a favourite one-mark trap.
One practical reason to know your types cold: almost every board program reads input from the user through the Scanner class, and Scanner has a different method for each type. Choose the wrong one and the program will not compile.
| To read a… | Use |
|---|---|
int | nextInt() |
long / short | nextLong() / nextShort() |
float / double | nextFloat() / nextDouble() |
| a single word | next() |
| a whole line, spaces included | nextLine() |
| a single character | next().charAt(0) |
Note that there is no nextChar() — a character is read by taking a word and picking out its first letter. And remember the import line import java.util.*; at the top, without which none of this compiles.
Type Conversion — Implicit Widening and Explicit Casting
Picture two glasses, a small one and a large one. Pouring the small glass into the large one is safe — nothing spills, and you need nobody’s permission. Pouring the large glass into the small one risks losing liquid, so somebody has to take responsibility for it.
Implicit conversion, also called widening or coercion, is the safe direction. Java performs it automatically when a smaller type is assigned to a larger one, because no information can be lost. The order of widening is:
(char also widens to int, and from there onwards)
Explicit conversion, also called narrowing or type casting, is the risky direction. You must write the target type in brackets before the value, which is your way of telling the compiler that you know data may be lost and you accept it: int q = (int) 9.87; stores 9. Notice that casting a double to an int truncates — it chops the decimal part off, it does not round. (int) 9.87 is 9, not 10. If you want rounding you must ask for it with Math.round().
5 / 2 is 2, because both are int and the answer must therefore be an int. But 5.0 / 2 is 2.5, because the presence of one double promotes the whole expression. (double) 5 / 2 is also 2.5 for the same reason.Watch the brackets, though.
(double)(5 / 2) is 2.0, not 2.5 — the division happens first inside the brackets, in integer arithmetic, and only the finished answer 2 is converted. One pair of brackets, a completely different answer. Examiners love this.Four Worked Programs With Verified Output
class PrimitiveSizes
{
public static void main(String args[])
{
byte b = 100;
short s = 20000;
int i = 1000000;
long l = 9000000000L;
float f = 3.14f;
double d = 3.14159265358979;
char c = 'A';
boolean flag = true;
System.out.println("byte b = " + b);
System.out.println("short s = " + s);
System.out.println("int i = " + i);
System.out.println("long l = " + l);
System.out.println("float f = " + f);
System.out.println("double d = " + d);
System.out.println("char c = " + c);
System.out.println("boolean flag = " + flag);
}
}
Output
byte b = 100
short s = 20000
int i = 1000000
long l = 9000000000
float f = 3.14
double d = 3.14159265358979
char c = A
boolean flag = true
What to notice: the L after 9000000000 and the f after 3.14 are compulsory. Remove either one and the program will not compile — without the L the literal is treated as an int and is far too large, and without the f the decimal is a double being squeezed into a float. Also notice that printing a char shows the character A, not the number 65. It only becomes 65 when you force it to.class EscapeDemo
{
public static void main(String args[])
{
System.out.println("Name\tClass\tSection");
System.out.println("Aarav\tX\tB");
System.out.println("Line one\nLine two");
System.out.println("She said, \"Java is easy\"");
System.out.println("Path : C:\\ICSE\\Java");
System.out.println("It\'s done");
}
}
Output
Name Class Section
Aarav X B
Line one
Line two
She said, "Java is easy"
Path : C:\ICSE\Java
It's done
What to notice: one println containing \n produced two lines of output. That is the trap in output questions — students count the number of println statements and forget that a single one can print several lines. And look at the path line: four backslashes were typed in the source and two appeared on screen, because each pair produces one.class TypeConversion
{
public static void main(String args[])
{
// implicit (widening) - Java does it on its own
int a = 7;
double b = a;
System.out.println("Implicit : int 7 becomes double " + b);
char ch = 'A';
int code = ch;
System.out.println("Implicit : char 'A' becomes int " + code);
// explicit (narrowing) - the programmer forces it with a cast
double p = 9.87;
int q = (int) p;
System.out.println("Explicit : double 9.87 becomes int " + q);
int n = 66;
char m = (char) n;
System.out.println("Explicit : int 66 becomes char " + m);
// a classic trap
System.out.println("5 / 2 = " + (5 / 2));
System.out.println("(double)5/2 = " + ((double) 5 / 2));
}
}
Output
Implicit : int 7 becomes double 7.0
Implicit : char 'A' becomes int 65
Explicit : double 9.87 becomes int 9
Explicit : int 66 becomes char B
5 / 2 = 2
(double)5/2 = 2.5
What to notice: the int 7 printed as 7.0 once it became a double — the type changed the appearance of the value, which is exactly the sort of detail a one-mark output question turns on. And 9.87 became 9, not 10: casting truncates, it never rounds.class CharAscii
{
public static void main(String args[])
{
char upper = 'A';
char lower = 'a';
char digit = '0';
char space = ' ';
System.out.println("'A' -> " + (int) upper);
System.out.println("'a' -> " + (int) lower);
System.out.println("'0' -> " + (int) digit);
System.out.println("' ' -> " + (int) space);
System.out.println("'A' + 1 gives " + (upper + 1));
System.out.println("(char)('A' + 1) gives " + (char) (upper + 1));
System.out.println("Difference 'a' - 'A' = " + (lower - upper));
}
}
Output
'A' -> 65
'a' -> 97
'0' -> 48
' ' -> 32
'A' + 1 gives 66
(char)('A' + 1) gives B
Difference 'a' - 'A' = 32
What to notice — this is the heart of the unit. upper + 1 printed 66, a number, not the letter B. The moment you do arithmetic on a char, Java widens it to an int, so the result is an int. To see the letter you must cast the whole expression back with (char). Students lose marks by writing B where the answer is 66, and by writing 66 where the answer is B. Read the brackets before you answer.The case-conversion idiom you must know. Because the gap between an uppercase letter and its lowercase partner is exactly 32, you can switch case with plain arithmetic and a cast. Both of these were executed:
char up = 'G';
System.out.println((char)(up + 32)); // upper to lower
char low = 'm';
System.out.println((char)(low - 32)); // lower to upper
g
M
Add 32 to go down to lowercase, subtract 32 to go up to uppercase, and never forget the (char) cast — without it you get the number.Output Prediction — Where the Traps Are Hidden
Cover the outputs, predict each one on paper, then check. All three fragments below were compiled and run.
System.out.println('A' + 'B');
System.out.println("A" + "B");
System.out.println((char)('A' + 2));
System.out.println(10 + 20 + "30" + 10 + 20);
Output
131
AB
C
30301020
Reasoning. Single quotes make chars, so 'A' + 'B' is arithmetic: 65 + 66 = 131. Double quotes make Strings, so "A" + "B" is concatenation, giving AB. In the third line the cast turns 67 back into the letter C.The fourth line rewards care. Java works left to right.
10 + 20 are both numbers, so that is arithmetic: 30. Then a String appears, so from that point onward every plus is concatenation: 30, then “30”, then 10, then 20, giving 30301020. Read it slowly and you will see the arithmetic 30 at the front and the literal “30” behind it.int a = 7;
double b = 7.0;
System.out.println(a / 2);
System.out.println(b / 2);
System.out.println((int) 7.9 + (int) 0.9);
System.out.println((char) 97);
System.out.println(Math.round(7.5) + " " + Math.round(-7.5));
Output
3
3.5
7
a
8 -7
Reasoning. 7 / 2 is int division, so 3. 7.0 / 2 has a double in it, so 3.5. The third line is the cruel one: (int) 7.9 truncates to 7 and (int) 0.9 truncates to 0, so the sum is 7 and not 9. Casting 97 gives the letter a.The last line is worth a moment.
Math.round() adds 0.5 and then takes the floor, so 7.5 rounds up to 8 — but -7.5 becomes -7, not -8, because -7 is the larger of the two neighbours. Positive halves round away from zero, negative halves round towards it. One more nuance worth knowing: Math.round() returns a long when given a double, and an int when given a float. So long r = Math.round(7.5); compiles, while assigning it straight to an int would need a cast. The printed value is 8 either way.byte b = 10;
short s = 20;
int r = b + s;
System.out.println("r = " + r);
float f = 5;
System.out.println("f = " + f);
long big = 100L;
int small = (int) big;
System.out.println("small = " + small);
System.out.println(5 > 3);
System.out.println((5 > 3) + " and " + (5 == 3));
Output
r = 30
f = 5.0
small = 100
true
true and false
Reasoning. A byte plus a short is not stored as a byte or a short — Java promotes both to int before adding, which is why r must be declared int. The whole number 5 assigned to a float prints as 5.0, because widening happened automatically. The cast from long to int is safe here only because 100 comfortably fits; a value beyond the int range would be corrupted. And a comparison produces a genuine boolean, which prints as the words true and false.Practice Worksheet — 10 Questions With Model Answers
Write your answer on paper first, then reveal. Every output below was produced by an executed program, so if your prediction differs, the reason is worth finding.
Q1. (2 marks) What is a character set? State two reasons why Java uses Unicode rather than ASCII.
Java uses Unicode because: (i) Unicode can represent characters from virtually every writing system in the world, which makes Java suitable for globalisation and localisation, whereas ASCII with its 128 codes covers little beyond English; and (ii) because all systems agree on the same Unicode code for the same character, text can be exchanged reliably between different platforms and applications.
Q2. (2 marks) Give the output.
System.out.println("Roll\tName");
System.out.println("1\tAarav\n2\tDiya");
System.out.println("Marks : \"90\"");
System.out.println("1\tAarav\n2\tDiya");
System.out.println("Marks : \"90\"");
Roll Name
1 Aarav
2 Diya
Marks : "90"Three println statements produced four lines, because the second one contains \n. The \t inserts a tab, and \" prints a double quotation mark without ending the String.Q3. (2 marks) What is a token? Name the five types of token in Java with one example of each.
Keyword —
class; Identifier — marks; Literal — 3.14; Operator — +; Punctuator or separator — the semicolon.Q4. (3 marks) State the size in bytes of byte, short, int, long, float, double, char and boolean. Which of these is not a number?
boolean stores only a single bit of information — the logical values true or false — and its exact storage size is not defined by the language.boolean is not a number; it holds only true or false and can never hold 0 or 1. char is not a number either in the way you write it, but it is stored as a Unicode code number, which is why arithmetic on it produces an int.Q5. (2 marks) Give the output.
System.out.println('P' + 'Q');
System.out.println("P" + "Q");
System.out.println((char)('P' + 1));
System.out.println("P" + "Q");
System.out.println((char)('P' + 1));
161
PQ
Q‘P’ is 80 and ‘Q’ is 81, and single quotes make chars, so the first line is arithmetic: 80 + 81 = 161. Double quotes make Strings, so the second line concatenates. In the third line 80 + 1 = 81, and casting 81 back to char gives the letter Q.Q6. (2 marks) Give the output.
System.out.println(9 / 2);
System.out.println(9 % 2);
System.out.println(9.0 / 2);
System.out.println((double)(9 / 2));
System.out.println(9 % 2);
System.out.println(9.0 / 2);
System.out.println((double)(9 / 2));
4
1
4.5
4.0The first two are ordinary integer division and remainder. The third has a double operand, so the whole expression becomes double and the fractional part survives. The fourth is the trap: the brackets force 9 / 2 to be worked out first in integer arithmetic, giving 4, and only that finished 4 is converted to a double. So the answer is 4.0, not 4.5.Q7. (3 marks) Distinguish between implicit and explicit type conversion, giving one example of each.
int a = 7; double b = a; — b holds 7.0.Explicit conversion, also called narrowing or type casting, must be written by the programmer using the target type in brackets, because data may be lost. Example:
double p = 9.87; int q = (int) p; — q holds 9, the decimal part being truncated.Third difference: implicit conversion is always safe, while explicit conversion may cause loss of precision or of value, and the programmer accepts responsibility for that by writing the cast.
Q8. (2 marks) Give the output.
char c = 'e';
int x = c;
System.out.println(x);
System.out.println(c + 1);
System.out.println((char)(c - 32));
int x = c;
System.out.println(x);
System.out.println(c + 1);
System.out.println((char)(c - 32));
101
102
E‘a’ is 97, so ‘e’ is 101. Assigning a char to an int is implicit widening, so x holds 101. Adding 1 to a char promotes it to int, giving 102 as a number rather than the letter f. Subtracting 32 turns a lowercase code into its uppercase partner, and the cast brings it back to a char, so E is printed.Q9. (2 marks) State whether each of these is a valid identifier, and give a reason where it is not: 2ndTerm, total_marks, void, my Name, _result.
2ndTerm — invalid, an identifier may not begin with a digit.total_marks — valid.void — invalid, it is a reserved keyword.my Name — invalid, an identifier may not contain a space._result — valid, the underscore is permitted, including as the first character.Q10. (4 marks) Write a program that stores the marks of three subjects as integers, and displays their sum and their average correct to the decimal place. Explain why a cast is needed.
class Average
{
public static void main(String args[])
{
int a = 70, b = 85, c = 92;
int sum = a + b + c;
double avg = (double) sum / 3;
System.out.println("Sum = " + sum);
System.out.println("Average = " + avg);
System.out.println("Wrong way (int division) = " + (sum / 3));
}
}OutputSum = 247
Average = 82.33333333333333
Wrong way (int division) = 82Why the cast is needed: sum and 3 are both int, so sum / 3 would be integer division and the fractional part would be silently thrown away, giving 82. Casting sum to double promotes the whole expression, so the division is carried out in double arithmetic and the true average survives. The third line is printed here only to show you the mistake side by side — do not include it in an exam answer.Before You Close This Page
You started this page thinking 5 / 2 ought to be 2.5. You now know why it is 2, and more importantly you know how to make it 2.5 when you need it. You can list eight primitive types with their sizes, explain why a char takes two bytes in Java, count backslashes without flinching, and read an expression left to right the way the compiler reads it.
If one thing is still shaky, it is almost certainly the promotion rule. Go back to the Key Idea box at the top and reread that single sentence, then redo Trap 1 and Trap 2 with a pen. Do not reread the whole page.
