Easy
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
char→intreveals its Unicode value - English letters / characters match ASCII values
Medium
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:
- ASCII supports emojis
- Unicode supports multiple languages
- Unicode values for English letters match ASCII
Explain your answers briefly
Show answers:
- ❌ ASCII supports emojis → False
- ✅ Unicode supports many languages → True
- ✅ English letters share same values → True
Explanation:
- ASCII is limited
- Unicode is universal
Letter or digit
What will the following lines return?
- Character.isDigit(‘7’)
- Charater.isLetterOrDigit(‘?’)
- 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:
charstores its actual value directly in memoryStringstores a reference to an object in memory
Hard
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