Computer Applications
Chhavi wants to check whether a character variable ch contains an uppercase letter (A-Z).
She writes the following statement, which is incorrect:
if (ch == "A" && ch == "Z")
What will be the correct statement?
A. if (Character.isUpperCase(ch))
B. if (ch >= 65 && ch <= 90)
C. if (ch >= 'A' && ch <= 'Z')
Java Library Classes
1 Like
Answer
A, B and C
Reason — To check if a character variable ch contains an uppercase letter (A-Z), we have three valid approaches, all of which are correct:
A. if (Character.isUpperCase(ch)) — Character.isUpperCase(ch) method from the Character class directly checks if ch is an uppercase letter.
B. if (ch >= 65 && ch <= 90) — Uppercase letters 'A' to 'Z' have ASCII values 65 to 90. As characters are internally stored as their unicode values, hence, this is also a valid way to determine if ch is an uppercase letter.
C. if (ch >= 'A' && ch <= 'Z') — Since Java treats characters as numbers internally, comparing ch with 'A' and 'Z' works just like using ASCII values. So this way is also correct.
Answered By
3 Likes
Related Questions
What is the method to check whether a character is a letter or digit?
- isDigit(char)
- isLetterOrDigit()
- isLetterOrDigit(char)
- isLETTERorDIGIT(char)
Give the output of the following code:
String P = "20", Q ="19"; int a = Integer.parseInt(P); int b = Integer.valueOf(Q); System.out.println(a+""+b);String s1 = "45.50"; String s2 = "54.50"; double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2); int x= (int)(d1+d2);What is value of x?