Java Classes and Methods: Definitions and Concepts - Prof. Susanna Wei, Study notes of Computer Science

Various concepts related to defining classes and methods in java, including the new operator, instance and local variables, blocks, call-by-value, this parameter, method signatures, and more. It also includes true/false and short answer questions to test understanding.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-isv
koofers-user-isv 🇺🇸

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 4
Defining Classes I
 Multiple ChoiceMultiple Choice
1) The new operator:
(a) allocates memory
(b) is used to create an object of a class
(c) associates an object with a variable that names it.
(d) All of the above.
2) A method that performs some action other than returning a value is called a __________ method.
(a) null
(b) void
(c) public
(d) private
3) The body of a method that returns a value must contain at least one _________ statement.
(a) void
(b) invocation
(c) thows
(d) return
4) A variable whose meaning is confined to an object of a class is called:
(a) instance variable
(b) local variable
1
pf3
pf4
pf5

Partial preview of the text

Download Java Classes and Methods: Definitions and Concepts - Prof. Susanna Wei and more Study notes Computer Science in PDF only on Docsity!

Chapter 4

Defining Classes I

 Multiple Choice

  1. The new operator: (a) allocates memory (b) is used to create an object of a class (c) associates an object with a variable that names it. (d) All of the above.
  2. A method that performs some action other than returning a value is called a __________ method. (a) null (b) void (c) public (d) private
  3. The body of a method that returns a value must contain at least one _________ statement. (a) void (b) invocation (c) thows (d) return
  4. A variable whose meaning is confined to an object of a class is called: (a) instance variable (b) local variable 1

(c) global variable (d) none of the above

  1. A variable whose meaning is confined to a method definition is called an/a (a) instance variable (b) local variable (c) global variable (d) none of the above
  2. In Java, a block is delimited by: (a) ( ) (b) /* */ (c) “ “ (d) { }
  3. In Java, call-by-value is only used with: (a) objects (b) primitive types (c) this (d) all of the above
  4. The parameter this refers to (a) instance variables (b) local variables (c) global variables
  1. A set method is: (a) an accessor method (b) a mutator method (c) a recursive method (d) none of the above
  2. Accessor methods: (a) return something equivalent to the value of an instance variable. (b) promotes abstraction (c) both A and B (d) none of the above
  3. A more eloquent approach in implementing mutator methods is to return a ________ value. (a) int (b) char (c) boolean (d) double
  4. A _________ states what is assumed to be true when the method is called. (a) prescript (b) postscript (c) precondition (d) postcondition
  5. The name of a method and the list of ________ types in the heading of the method definition is called the method signature.

5 (a) parameter (b) argument (c) return (d) primitive

 True/False

  1. An object of class A is an instance of class A.
  2. An invocation of a method that returns a value can be used as an expression any place that a value of the Type_Returned can be used.
  3. The Java language supports global variables.
  4. In a method invocation, there must be exactly the same number of arguments in parentheses as there are formal parameters in the method definition heading.
  5. When you give a command to run a Java program, the runtime system invokes the class constructor.
  6. Inside a Java method definition, you can use the keyword this as a name for the calling object.
  7. Boolean expressions may be used to control if-else or while statements.
  8. The modifier private means that an instance variable can be accessed by name outside of the class definition.
  9. It is considered good programming practice to validate a value passed to a mutator method before setting the instance variable.
  10. Mutator methods can return integer values indicating if the change of the instance variable was a success.
  11. Method overloading is when two or more methods of the same class have the same name but differ in number or types of parameters.
  12. Java supports operator overloading.
  13. Only the default constructor has the this parameter.

 Short Answer/Essay

Write a Java method that prints the phrase “Five purple people eaters were seen munching Martians”.

  1. What is the purpose of the new operator?

7 if((day >=1) && (day <= 31)) { this.day = day; return true; } else { return false; } }