Chapter 11: 1. When you instantiate an object, the automatically created method
ID: 3564076 • Letter: C
Question
Chapter 11:
1. When you instantiate an object, the automatically created method that is called is a(n) .
a. creator
b. initiator
c. architect
d. constructor
2. Every class has .
a. exactly one constructor
b. at least one constructor
c. at least two constructors
d. a default constructor and a programmer-written constructor
3. Which of the following can be overloaded?
a. constructors
b. instance methods
c. both of the above
d. none of the above
4. Every default constructor .
a. is automatically created
b. sets a default value for every field in a class
c. requires no parameters
d. is the only constructor that is explicitly written in a class
5. When you write a constructor that receives a parameter, .
a. the automatically created default constructor no longer exists
b. the parameter must be used to set a data field
c. it becomes the default constructor
d. the constructor body must be empty
6. When you write a constructor that receives no parameters, .
a. the automatically created constructor no longer exists
b. it becomes known as the default constructor
c. both of the above
d. none of the above
7. Most often, a destructor is called when .
a. an explicit call is made to it
b. an object goes out of scope
c. an object is created
d. a value is returned from a class method
8. Which of the following is not a similarity between constructors and destructors?
a. Both can be called automatically.
b. Both have the same name as the class.
c. Both have no return type.
d. Both can be overloaded.
9. Advantages of creating a class that inherits from another include all of the following except:
a. You save time because subclasses are created automatically from those that come built in as part of a programming language.
b. You save time because you need not re-create the fields and methods in the original class.
c. You reduce the chance of errors because the original classs methods have already been used and tested.
d. You make it easier for anyone who has used the original class to understand the new class.
10. Employing inheritance reduces errors because .
a. the new classes have access to fewer data fields
b. the new classes have access to fewer methods
c. you can copy and paste methods that you already created
d. many of the methods you need have already been used and tested
11. A class that is used as a basis for inheritance is called a .
a. derived class
b. base class
c. child class
d. subclass
12. Which of the following is another name for a derived class?
a. base class
b. child class
c. superclass
d. parent class
13. Which of the following is not another name for a derived class?
a. extended class
b. subclass
c. child class
d. superclass
14. Which of the following is true?
a. A base class usually has more fields than its descendent.
b. A child class can also be a parent class.
c. A classs ancestors consist of its entire list of children.
d. To be considered object oriented, a class must have a child.
15. A derived class inherits data and methods of its ancestors.
a. all
b. only the public
c. only the private
d. no
16. Which of the following is true?
a. A classs data fields usually are public.
b. A classs methods usually are public.
c. both of the above
d. none of the above
17. A is a collection of predefined, built-in classes that you can use when writing programs.
a. vault
b. black box
c. library
d. store
18. An environment in which you can develop GUI programs by dragging components to their desired positions is a(n) .
a. visual development environment
b. integrated compiler
c. text-based editor
d. GUI formatter
19. In object-oriented programs, errors are known as .
a. faults
b. gaffes
c. exceptions
d. omissions
20. The general principle of exception handling in object-oriented programming is that a method that uses data should .
a. be able to detect errors, but not be required to handle them
b. be able to handle errors, but not detect them
c. be able to handle and detect errors
d. not be able to detect or handle errors
21. Which of these does not belong with the others? (Not sure 100%)
a. instance variable
b. attribute
c. field
d. object
22. Assume that a working program contains the following statement: myDog.setName("Bowser")
Which of the following do you know?
a. setName() is a public method.
b. setName() accepts a string parameter.
c. both of the above
d. none of the above
23. Assume that a working program contains the following statement: name = myDog.getName()
Which of the following do you know?
a. getName() returns a string.
b. getName() returns a value that is the same data type as name.
c. both of the above
d. none of the above
23. Assume that you have created a class named Dog that contains a data field named weight and an instance method named setWeight(). Further assume that the setWeight() method accepts a numeric parameter named weight. Which of the following statements correctly sets a Dogs weight within the setWeight() method?
a. weight = weight
b. this.weight = this.weight
c. weight = this.weight
d. this.weight = weight
24. Assume that you have created an object named myObject and that a working program contains the following statement: output myObject.getSize() Which of the following do you know?
a. getSize() is a static method.
b. getSize() returns a number.
c. getSize() receives a this reference.
d. all of the above
25. Assume that you have created a class that contains a private field named myField and a nonstatic public method named myMethod(). Which of the following is true?
a. myMethod() has access to myField and can use it.
b. myMethod() does not have access to myField and cannot use it.
c. myMethod() can use myField but cannot pass it to other methods.
d. myMethod() can use myField only if it is passed to myMethod() as a parameter.
Programming Exercises
1. Complete the following tasks:
a. Design a class named Circle with fields named radius, area, and diameter. Include a constructor that sets the radius to 1. Include get methods for each field, but include a set method only for the radius. When the radius is set, do not allow it to be zero or a negative number. When the radius is set, calculate the diameter (twice the radius) and the area (the radius squared times pi, which is approximately 3.14). Create the class diagram and write the pseudocode that defines the class.
b. Design an application that declares two Circles. Set the radius of one manually, but allow the other to use the default value supplied by the constructor. Then, display each Circles values.
Explanation / Answer
1)
When a class is created, a default constructor is created automatically. A constructor is called when an object is instantiated.
Therefore the correct option is “constructor”
2)
When a class is created, a default constructor is created automatically. In addition to default constructor, programmer can overload or add parameterized constructors.
Hence, every class has at least one constructor.
3)
All methods of instance, including constructors can be overloaded.
Hence, the correct option is “both of the above”.
4)
Default constructors may not have parameters.
Hence the correct option is “requires no parameters.”
5)
A constructor is used to initialize the data fields of the class. User can write parameterized constructor that can be used to initialize the class variables.
Hence, the correct option is “the parameter must be used to set a data field”
6)
If a constructor is written without parameters, then the constructor is considered as a default constructor.
Hence the correct option is “it becomes known as the default constructor”
7)
In destructor method , code is written to free the memory and free resources utilized by the object, when the object id dead(out of scope). But in java , there is a garbage collection mechanism, which frees the memory. However, most times, destructors are invoked automatically.
Hence the correct option is “b. an object goes out of scope”
8)
Unlike constructors, destructors cannot be overloaded.
Hence the correct option is “d. Both can be overloaded.”
9)
Sub classes are not created automatically.
Hence the correct option is “a. You save time because subclasses are created automatically from those that come built in as part of a programming language.”
10)
If a class is inherited from another existing class, the tested methods and fields of base class are automatically provided for sub class. Since the methods already tested, errors will be reduced.
Hence the correct option is “d. many of the methods you need have already been used and tested”.
11)
In the inheritance, if a class inherits (acquires properties) from another class, the class that is inherited is called base class. Base class is the basis and it provides its methods to the sub class.
Hence the correct option is “b. base class”
12)
In the inheritance, if a class inherits (acquires properties) from another class, the class that is derived is called sub class. Sub class or derived class is also called as child class.
Hence the correct option is “b. child class ”
13)
In the inheritance, if a class inherits (acquires properties) from another class, the class that is derived is called sub class. Sub class or derived class is also called as child class. Base class is also called as super class or parent class.
Hence, the correct option is “d. super class”
14)
A child class can also be extended further, that is a child class also can be a parent class. For example, if class B inherits class C and class A inherits class B, then C is the parent class for class B and Class B is the parent class for the class A.
Hence the correct option is “b. A child class can also be a parent class.”
15)
Data hiding concept enforces a child class not to inherit private data from base class. That is , only non private data can be inherited from the base class.
Hence the correct option is “b. only the public ”.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.