Which statement is true

public class CreditCard {

 private String cardID;
 private Integer limit;
 public String ownerName;

 public void setCardInformation(String cardID,
 String ownerName,
 Integer limit) {
  this.cardID = cardID;
  this.ownerName = ownerName;
  this.limit = limit;
 }
}
  1. The class is fully encapsulated.

  2. The code demonstrates polymorphism.

  3. The ownerName variable breaks encapsulation.

  4. The cardID and limit variables break polymorphism.

  5. The setCardInformation method breaks encapsulation.


Correct Option: C
Explanation:

To solve this question, the user needs to have an understanding of object-oriented programming concepts such as encapsulation and polymorphism.

Now, let's go through each statement and evaluate its correctness:

A. The class is fully encapsulated: This statement is incorrect. Encapsulation refers to the practice of keeping the internal state and implementation details of an object hidden and accessible only through defined methods. In this case, the class has private instance variables (cardID and limit) and a public method (setCardInformation) to modify their values. However, the class also has a public instance variable (ownerName) that can be accessed directly, which breaks encapsulation.

B. The code demonstrates polymorphism: This statement is incorrect. Polymorphism refers to the ability of objects of different classes to be treated as objects of a common parent class. The code provided does not demonstrate polymorphism.

C. The ownerName variable breaks encapsulation: This statement is correct. The ownerName variable is declared as public, which means it can be accessed and modified directly from outside the class, breaking encapsulation.

D. The cardID and limit variables break polymorphism: This statement is incorrect. The cardID and limit variables have no relation to polymorphism. Polymorphism involves the ability to treat objects of different classes as objects of a common parent class.

E. The setCardInformation method breaks encapsulation: This statement is incorrect. The setCardInformation method is designed to modify the values of the private instance variables (cardID, ownerName, limit), which is a common practice in encapsulation.

The Answer is: C

Find more quizzes: