Skip to content

Module 4 Mastery Check

wolvesled edited this page Aug 31, 2013 · 1 revision
  1. What is the difference between a class and an object? Answer: a class is the definition of an object, it define the instance variable and method code in which an object will be use, but a class will not take any form of memory and actual value until an object of the class is initialized or instantiated, to be simple, class is the code of an object, and object is the instance of the class.
  2. How is a class defined? Answer: class ClassName {code block of variables and methods}
  3. What does each object have its own copy of? Answer: instance variables.
  4. Using two separate statements, show how to declare an object called counter of a class called MyCounter. Answer: MyCounter counter; counter = new MyCounter(); MyCounter counter = new MyCounter();
  5. Show how a method called myMeth() is declared if it has a return type of double and has two int parameters called a and b. double myMeth(int a, int b) { code; }
  6. How must a method return if it returns a value? Answer: put the return var; statement at the end of method block.
  7. What name does a constructor have? Answer: the same name of the class it belongs to
  8. What does new do? Answer: create an object and allocate memory of a specified class and return the reference to this object.
  9. What is garbage collection, and how does it work? What is finalize()? Answer: is a way in java to release unused memory happened when two factors are met: unreferenced objects, and not enough memory when creating new object asked to. finalize() is a method defined in every class for the java garbage collector to call it before destroying the object.
  10. What is this? Answer: the self reference of an object when its method is called.
  11. Can a constructor have one or more parameters? Answer: yes.
  12. If a method returns no value, what must its return type be? Answer: void

Clone this wiki locally