Java character class example questions
- Categories Java, Wrapper Classes
Q. Write a valid Java statement to convert ch = B’ into small letter and ml = ‘a’ into capital letter forms
Ans.
- To convert ch = ‘B’ into small letter the statement is:
char st= Charaeter.toLowerCase( ch ); - To convert ml = „a’ into capital letter the statement is:
char st= Character.toUpperCase( m1 );
- To convert ch = ‘B’ into small letter the statement is:
Q. State the output of the following statements after the execution
- System.out.println (Character.isLowerCase (‘P’));
- System.out.println (Character.isDigit(‘s’));
- iii. System.out.println (Character.isUpperCase(‘R’));
- System.out.println (Character.isSpaceChar(‘ ‘));
Ans.
- False
- False
- True
- True
Q. A character ‘R’ is assigned to a character variable ans. Write valid statements to assign character ‘R’, convert into its ASCII code. Print the character and ASCII code.
Ans.
char ans = „R’; // initializing character R’ to character variable ans
int as = ans; // initializing character variable ‘ans’ to integer variable ‘as’ i.e. stores ASCII
System.out.println(“The character = “+ans +”its ASCII code = ” + as );
Q. Write a valid statement(s) to input a character from input/keyboard using read() method.
Ans. The character can be input from the keyboard using any of the methods given below:
- Char ch= (char)System. in. read();
- BufferedReader br= new BufferedReader( new InputStreamReader( System.in);
char ch= (char) br.read();
Q. State the Output of the following statements after the expression:
- System.out.println(Character.isDigit(‘g’));
- System.out.println(Character.isUpperCase(‘t’));
- System.out.println(Character.isWhitespace(‘\t ‘));
- System.out.println(Character.isLowerCase(‘m’));
Ans.
- false
- false
- true
- true
Q. Write the output of the following:
(i) System.out.println (Character.isUpperCase(‘D’));
(ii) System.out.println(Character.toUpperCase(‘l’));
Ans.
(i) true
(ii) L
Q. State the method that:
(i) Converts a string to a primitive float data type
(ii) Determines if the specified character is an uppercase character
Ans.
(i) Float.parseFloat()
(ii) Character.isUpperCase()
Q. State the data type and the value of y after the following is executed:
char x =‘5’;
t = Character.isLetter(x);
Ans. t is of boolean type its value is false.
Q. State the data type and value of res after the following is executed:
char ch=‘n’;
res=Character.toUpperCase(ch);
Ans. res is of char type is value is ‘N’.
Q. Write a statement to perform the following task on a string:
- Extract the second last character of a word stored in the variable wd.
- Cheek if the second character of a string str is in uppercase.
Ans.
- char ch = wd.charAt(wt.length() -2);
- if (Character.isUpperCase(str.charAt(1))) OR
if (str.charAt(1)> ‘A ‘ && str.charAt(1) <= ‘z’)
Q. Write a valid Java statement to convert ch = ‘N’ into small letter and ml = ‘d’ into capital letter forms
Ans.
- To convert ch = ‘N’ into small letter the statement is:
char st= Character.toLowerCase( ch ); - To convert ml = ‘d’ into capital letter the statement is:
char st= Character.toUpperCase( m1 );
- To convert ch = ‘N’ into small letter the statement is:
Q. State the output of the following statements after the execution
- System.out.println (Character.isLowerCase (‘A’));
- System.out.println (Character.isDigit(‘b’));
- System.out.println (Character.isUpperCase(‘T’));
- System.out.println (Character.isSpaceChar(‘ ‘));
Ans.
- False
- False
- True
- True
Q. Write a valid statement(s) to input a character from input/keyboard using read() method.
Ans. The character can be input from the keyboard using any of the methods given below:
- Char ch= (char)System.in.read();
- BufferedReader br= new BufferedReader( new InputStreamReader( System.in);
char ch= (char) br.read();