116k views
1 vote
Which of the following statements contains an error?

I. char gender = genderString.charAt(0);
II. boolean isFemale = gender == "F";
III. int age = Integer.parseInt(ageString);

A. I only
B. II only
C. III only
D. I and II only
E. II and III only

1 Answer

3 votes
I. is syntactically correct if genderString exists. if genderString, for example, is "Male", then char gender would be the character at index 0 (the first character), meaning 'M'.

II. is incorrect. It is using the comparison operator (==) instead of the assignment operator (=). It is also setting a boolean variable to a String value of 'F'. Boolean values cannot hold string values, and can only hold true & false.

III. is correct if ageString only contains numbers (presumably, it does, as it's called ageString). Integer.parseInt is a function that converts String values to integer values if the string values only contain numerical characters.

The answer in this case should be B. II only.
User Aatishk
by
6.9k points