You are on page 1of 10

Test: Creating Classes, Objects, and Methods: Quiz

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.
Section 1
(Answer all questions in this section)
1. Which of the following creates a class named Diver with one constructor, and 2
instance variables maxDepth and certified?

Mark for
Review
(1) Points

(*)

2. The basic unit of encapsulation in Java is the:

Mark for
Review
(1) Points

class (*)
classpath
method
package

3. What is wrong with the following class declaration?

Mark for
Review
(1) Points

Classes cannot include strings.


Classes cannot include mixed data types.
There is no constructor method and you have to make a constructor method.
There is nothing wrong. (*)

4. Which of the following is true?

Mark for
Review
(1) Points

In Java, a method declared public generates a compilation error.


int is the name of a class available in the package java.lang.
Instance variable names may only contain letters and digits.
A class always has a constructor (possibly automatically supplied by the java
compiler). (*)
The more comments in a program, the faster the program runs.

5. Which of the following may be part of a class definition?

Mark for
Review

(1) Points
instance variables
instance methods
constructors
comments
all of the above (*)

6. Which of the following creates a Object from the Animal class listed below?

Mark for
Review
(1) Points

Animal dog=new Animal();


Animal dog=new Animal(50,30); (*)
Animal dog=Animal(50,30);
Animal dog=new Animal(50);

7. The following code creates an object of type Animal:


Animal a;

Mark for
Review
(1) Points

True
False (*)

8. Which of the following creates an object from the Car class listed below?

Mark for
Review
(1) Points

Car c = new Car(3000, "Toyota"); (*)


Car c=new Car;
Car c=Car();
Car c;
Car c =new Car();

9. The following statement compiles and executes. What can you say for sure?
submarine.dive(depth);

Mark for
Review
(1) Points

The variable depth must be an int.


dive must be a method. (*)
dive must be the name of an instance field.
submarine must be the name of a class.
submarine must be a method.

10Which of the following creates a method that compiles with no errors in the class?
.

Mark for
Review
(1) Points

(*)

All of the above.


None of the above.

11What value will be returned when the setValue method is called?


.

Mark for
Review
(1) Points

35
36
37 (*)
38

12The return value of a method can only be a primitive type and not an object. True
. or false?

Mark for
Review
(1) Points

True
False (*)

13Which of the following calls the method moveUp in the class below:
.

Mark for
Review
(1) Points

Puzzle p=new Puzzle(); p.moveUp(3,4);


PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4); (*)
PuzzlePiece p=new PuzzlePiece(); p.moveUp(3,4,5);
Puzzle p=new Puzzle(); p.moveUp(3,4,5);

14A class can have multiple constructors. True or false?


.

Mark for
Review
(1) Points

True (*)
False

15The constructor of a class has the same name as the class. True or false?
.

Mark for
Review
(1) Points

True (*)
False

16Complete the sentence. A constructor...


.

Mark for
Review
(1) Points

must have the same name as the class it is declared within.


is used to create objects.
may be declared public.
is all of the above. (*)

17What operator do you use to call an object's constructor method and create a new
. object?

Mark for
Review
(1) Points

new (*)
class
instanceOf

18Consider:
.
public class MyClass{ public MyClass(){/*code*/} // more code...}
To instantiate MyClass, what would you write?

Mark for
Review
(1) Points

MyClass m = new MyClass(); (*)


MyClass m = new MyClass;
MyClass m = MyClass;
MyClass m = MyClass();

19What is garbage collection in the context of Java?


.

Mark for
Review
(1) Points

The operating system periodically deletes all of the Java files available on the
system.

Any package imported in a program and not used is automatically deleted.


When all references to an object are gone, the memory used by the object is
automatically reclaimed. (*)
The JVM checks the output of any Java program and deletes anything that
does not make sense.

20Which of the following keywords are used to access the instance variables of an
. object from within the class code for that object?

Mark for
Review
(1) Points

public
private
protected
this (*)

21Which of the following adds a constructor to the class below?


.

Mark for
Review
(1) Points

(*)

22Which constructor code populates the instance variables of the class correctly?
.

Mark for
Review
(1) Points

(*)

You might also like