Java OCA OCP Practice Question 242

Question

Which of the following correctly assigns animal to both variables?

  • I. String cat = "animal", dog = "animal";
  • II. String cat = "animal"; dog = "animal";
  • III. String cat, dog = "animal";
  • IV. String cat, String dog = "animal";
  • A. I
  • B. I, II
  • C. I, III
  • D. I, II, III, IV


A.

Note

Option A (I) correctly assigns the value to both variables.

II does not compile as dog does not have a type.

Notice the semicolon in that line, which starts a new statement.

III compiles but only assigns the value to dog since a declaration only assigns to one variable rather than everything in the declaration.

IV does not compile because the type should only be specified once per declaration.




PreviousNext

Related