Lab 2
– Simple Class
Pre
Test
|
Lab Section: |
|
|
Name: |
|
1. Which term describes the hiding of details of an object from the other parts of a program?
a. Obfuscation
b. Data Mining
c. Compilation
d. Encapsulation
2. What is true of the returnType and the returnValue of a method?
a.
The returnValue must be exactly the same
type as the returnType.
b. The returnValue must be the same type as the returnType, or be of a type that can be converted to the returnType without loss of information.
c. The returnValue can be any type, but will automatically be converted to the returnType when the method returns to the caller.
d. If the returnType is void then the returnValue can be any type.
Answer: b
3. Which of the following statements is the best description of a class?
a. A class is a description of a kind of object.
b. A class is the part of an object that contains the variables.
c. A class is a template or mold that dictates what objects can and cannot do.
d. A class is an instance of an object.
4. Why is the main() method special in a Java program?
a. It is where the Java interpreter starts the program running.
b. Only the main() method can create objects.
c. Every class must have a main() method.
d. The main() method is the only static method in a program.
Answer: a
Comprehension:
5. What is the relationship between an object and a class?
a. An object and a class are the same thing.
b. A class is an instance of an object.
c. An object is a template for a class.
d. An object is an instance of a class.
Answer: d
6. What
is the purpose of the import statement in Java?
a. The import statement copies code from another program.
b. The import statement allows the program to refer to classes defined in the designated package without using the fully qualified class name.
c. The import statement calls methods in another program.
d. The import statement creates a subclass.
Answer: b
7. What is the effect of giving a class member private access?
a. When a member of a class is declared private it can only be used from one place in a program.
b. When a member of a class is declared private it can only be used in methods that are members of that class.
c. When a member of a class is declared private it can only be used by other private members of other classes.
d. When a member of a class is declared private there is only one instance of it, no matter how many objects are instantiated.
Answer: b
Answer: c
Application
9. The following code is invalid. Why?
public void getStudentID(){
return studentID;
}
a. The method name is not a valid identifier.
b. The method contains a return statement but the method’s declaration indicates it does not return a value.
c. The value returned by the method is undeclared.
d. The return statement is improperly formatted.
Answer: b
10. Write a single line of code that declares and creates an object of type Person and initializes the person’s name to Bob. The Person constructor is shown below.
public
Person (String pName){ // constructor definition
name = pName;
} //end Person constructor
a. personInstance = new Person(“Bob”);
b. Person personInstance = new Person();
c. Person personInstance = newPerson(“Bob”);
d. Person personInstance = new Person(“Bob”);
Answer: d