char exercises

Declare chars

Create three char variables:

  • One letter
  • One digit
  • One symbol
Show answer:
char letter = 'A';
char digit = '5';
char symbol = '#';

System.out.println(letter); // prints A
System.out.println(digit);  // prints 5
System.out.println(symbol); // prints #

Spot the error

Which of the following declarations are invalid? Explain why

char a = 'A';
char b = "B";
char c = '12';
char d = '@';
Show answer:
char a = 'A';   // ✅ Valid
char b = "B";   // ❌ Invalid (String, not char)
char c = '12';  // ❌ Too many characters
char d = '@';   // ✅ Valid

Output prediction

What will this code print?

char c = 'Z';
System.out.println(c);
Show answer:
Z // prints letter Z

Numeric value

Write a programme that prints the unicode value of:

  • ‘A’
  • ‘a’
  • ‘?’
Show answer:
System.out.println((int) 'A'); // prints 65
System.out.println((int) 'a'); // prints 97
System.out.println((int) '?'); // prints 63

Explanation:

  • Casting charint reveals its Unicode value
  • English letters / characters match ASCII values

Unicode literal

Use a Unicode escape sequence to create and print a char for:

  • A heart (❤)
  • A copyright symbol (©)

Hint: Use \uXXXX notation.

Show answer:
char heart = '\u2764';
char copyright = '\u00A9';

System.out.println(heart); //prints ❤
System.out.println(copyright); //prints ©

Unicode vs ASCII

True or False:

  1. ASCII supports emojis
  2. Unicode supports multiple languages
  3. Unicode values for English letters match ASCII

Explain your answers briefly

Show answers:
  1. ❌ ASCII supports emojis → False
  2. ✅ Unicode supports many languages → True
  3. ✅ English letters share same values → True

Explanation:

  • ASCII is limited
  • Unicode is universal

Letter or digit

What will the following lines return?

  1. Character.isDigit(‘7’)
  2. Charater.isLetterOrDigit(‘?’)
  3. Character.toLowerCase(‘A’)
Show answer:
System.out.println(Character.isDigit('7')) // returns true
System.out.println(Character.isLetterOrDigit('?')) // returns false
System.out.println(Character.toLowerCase('A')) // returns 'a'

Primitive vs reference

Answer in your own words:

What is the difference between the way a primitive (char) and Object (String) store data in memory?

Show answer:
  • char stores its actual value directly in memory
  • String stores a reference to an object in memory

Compare characters

What will each of the lines below return?

System.out.println(Character.compare('a', 'b'));
System.out.println(Character.compare('b', 'a'));
System.out.println(Character.compare('c', 'c'));
Show answer:
// Internally, characters are compared by their Unicode values: 'a' = 97, 'b' = 98
-1 // returns a negative value if first value is less than the second
1 // returns a positive value if first value is greater than the second
0 // returns 0 as both the characters equal

Check letters

Convert the string "Java145" into a char[] array.
Count the number of letters in the string

Show answer:
String word = "Java145";
char[] chars = word.toCharArray();
int count = 0;

for (char c : chars) {
    if (Character.isLetter(c)) {
        count++;
    }
}

System.out.println(count);


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories