Top Banner
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 (*)
11
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1. Creating Classes, Objects, And Methods

Test: Creating Classes, Objects, and Methods: QuizReview 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

Page 2: 1. Creating Classes, Objects, And Methods

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

Page 3: 1. Creating Classes, Objects, And Methods

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

Page 4: 1. Creating Classes, Objects, And Methods

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.

10. 

Which of the following creates a method that compiles with no errors in the class? Mark for

Review (1) Points

Page 5: 1. Creating Classes, Objects, And Methods

(*)

All of the above.

None of the above.

11. 

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

Review (1) Points

35

36

37 (*)

38

Page 6: 1. Creating Classes, Objects, And Methods

12. 

The 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 (*)

13. 

Which 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);

14. 

A class can have multiple constructors. True or false? Mark for

Review (1) Points

True (*)

False

15. 

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

Review (1) Points

True (*)

False

Page 7: 1. Creating Classes, Objects, And Methods

16. 

Complete 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. (*)

17. 

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

Review (1) Points

new (*)

class

instanceOf

18. 

Consider: 

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();

19. 

What 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

Page 8: 1. Creating Classes, Objects, And Methods

automatically reclaimed. (*)The JVM checks the output of any Java program and deletes anything that does not make sense.

20. 

Which 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 (*)

21. 

Which of the following adds a constructor to the class below?  Mark for

Review (1) Points

Page 9: 1. Creating Classes, Objects, And Methods

(*)

22. 

Which constructor code populates the instance variables of the class correctly? Mark for

Review (1) Points

Page 10: 1. Creating Classes, Objects, And Methods

(*)