Quiz Chapter 1 Answers


Quiz Chapter 1
Explain how interface is like a contract
Class implementing interface must define all it’s methods.
Write method verifyPin for class ATMbankAmerica assuming this class has a data field pin (type String)
Public Boolean verifyPin(String pin){
return this.pin.equals(pin);
}
Can you add the following constructor to class Notebook? If so, what would you need to do to class Computer?
public Notebook() {}
Yes we can. But need to provide default constructor to class Computer.
What are two important differences between abstract class and actual class? What are the similarities?
Abstract class should have abstract methods. It can’t be instantiated. They both may have actual datafields and methods.
Write class Vegetable. Assume that vegetable has three double constants: VEG_PROTEIN_CAL, VEG_FAT_CAL and VEG_CARBO_CAL. Compute the fat percentage as VEG_FAT_CAL divided by the sum of all the constants.
Public class Vegetable{
Private double VEG_PROTEIN_CAL, VEG_FAT_CAL,VEG_CARBO_CAL;
Public double fatPercentage(){
Return VEG_FAT_CAL/(PROTEIN_CAL+ VEG_FAT_CAL+VEG_CARBO_CAL);
}
}
Write both equals methods for classes Computer and Notebook. Assuming that Notebook extends Computer. Data are: {manufacturer, processor, ramSize};
Public Boolean equals(Object obj){
if(this == obj){return true;}
if(obj == null){return false;}
if(this.getClass() == obj.getClass()){
Computer c = (Computer) o;
Return this.manufacture.equals(c.manufacturer)&& this. processor.equals(c. processor)&& this. ramSize==c. ramSize;
}
Retur false;
}
Public Boolean equals(Object o){
Return Super.equals(o);
}
List four subclasses of RuntimeException
IndexOutOfBounds, Arithmetic, IlegalArgument, InputMismatch
How protected visibility can be equivalent to public?
If two related classes are in the same package.
Explain why Shape can not be an actual class
Because it has abstract methods.
What does polymorphism mean, and how it is used in JAVA? What is method overriding and overloading?
Polymorpism = many forms (ability of the object of one class to behave like object of another). Overriding – the same method realized differently in a child class. Overloading – when number of parameters changed then it will be another method.
Explain the effect of method instanceOf? Give any example.
If(I instanceOf Number){…}
Check whether the object belong to some class or not.
Describe the use of the keywords super. and super(…)? Give an example of each.
If you want to call some method of parent class then use super.
Super.equals() – will call equals method of parent class;
If you want to call constructor of parent class;
What can you say about class Object?
Object is parent of all classes in JAVA.
Describe the difference between is-a and has-a relationships?
Is-a – parent child relationship
Has-a – when one class has another inside itself.
What is the advantage of specifying an abstract data type as an interface instead of just going ahead and implementing it as a class?
Any class can implement many interface and can not extend more than one class.
Quiz Chapter 1
What are two different uses of a term interface in programming
Interface is a java keyword.
Relationship between user and computer (HCI).
Explain the effect of each valid statement in the following fragment. Indicate any invalid statements
System.out.println(c2.manufacturer + “, ”+ c4.manufacturer);
Invalid, because private data taken directly
System.out.println(c2.getDiskSize() + “, ”+ c4.getRamSize());
Displays ramSize of object of Computer
System.out.println(c2.toString() + “, ”+ c4.toString());
Displays information about Computer objects.
Write constructors for classes Computer and Notebook that allow you to specify only the processor, Ram size and disk size
Public Computer(String p, double ram, double disk){
This.processor = p;
This.ramSize = ram;
This.diskSize = disk;
}
Public Notebook(String p, double ram, double disk){
Super(p, ram, disk);
}
Write the following accessor and modifier methods for class Computer:
Public String getManufacturer(){return manufacturer;}
Public void setManufacturer(String manufacturer){this.manufacturer=manufacturer;}
What do abstract methods and interfaces have in common? How do they differ?
In interface methods are not defined. But in abstract classes the abstract methods can be defined to show its default behaviour.
Indicate the effect of each of the following statements. Rewrite the invalid statements to remove errors.
Object o = new String (“HELLO”); / +
String s = o;/ (String) o;
Object p = 25;/ +
int k = p; / (Integer)p;
Number n = k; /+
Explain the difference between checked and unchecked exceptions. Give an example of each.
Checked – due to the system or computer
Unchecked – due to programmer
List two subclasses of IOException
EOFException, FileNotFoundException
Explain why Shape can not be an interface
Because it has actual methods
What does encapsulation mean, and how it is used in JAVA? What is method overriding and overloading?
Encapsulation – limiting access to datafields.
Describe the use of the keywords this. and this(…)? Give an example of each.
This. – used to get datafields of current class
This() – used to call the constructor of current class.
Explain the relationship between classes Number, Integer and Short.
Number is parent of Integer and Short.
Which can have more data fields and methods: superclass or the subclass?
subclass
The four kinds of visibility in order of decreasing visibility are:
public, package, protected, private,
What is default package visibility?
Classes in the same package are visible to each other without import.

Приложенные файлы

  • docx 24016794
    Размер файла: 18 kB Загрузок: 0

Добавить комментарий