Unit2B Flashcards
- What is encapsulation in object-oriented programming?
- a) The act of inheriting methods from a parent class.
- b) Grouping related ideas into a single unit referred to by one name.
- c) The ability of different classes to implement the same interface.
- d) The process of transforming an object into a string representation.
b) Grouping related ideas into a single unit referred to by one name.
- When a message is sent to an object to perform an operation, what is this action also known as?
- a) Compiling
- b) Instantiating
- c) Invoking the method
- d) Encapsulating the behavior
c) Invoking the method.
- What is the purpose of the
this
keyword in a Java class?- a) To refer to the current class’s superclass.
- b) To refer to the current instance of the class.
- c) To create a static reference to the class.
- d) To call a static method of the class.
b) To refer to the current instance of the class.
- In UML, what is the standard format for documenting an attribute?
- a)
attributeName: Type
- b)
Type attributeName
- c)
attributeName()
- d)
Type: attributeName
- a)
a) attributeName: Type
- When creating a new object in Java using a constructor with parameters, what are these parameters used for?
- a) To set static fields of the class.
- b) To initialize the object’s state with specific values.
- c) To determine the number of objects to create.
- d) To call the superclass’s constructor.
b) To initialize the object’s state with specific values.
- What is the output when you instantiate a
Dog
object with the name “Rusty”, color “red”, and then calldog.getName()
ifgetName()
is an accessor method?- a) “Rusty”
- b) “red”
- c) “Dog”
- d) There will be a compilation error.
a) “Rusty”
- What is the primary role of the
main
method in a Java class?- a) To declare variables
- b) To act as the entry point of the program
- c) To define constants
- d) To allocate memory for objects
b) To act as the entry point of the program.
- What would be a result of setting one object reference equal to another, e.g.,
object1 = object2;
?- a)
object1
will be a copy ofobject2
. - b)
object1
will refer to a new instance of the class. - c)
object1
will refer to the same memory location asobject2
. - d)
object2
will be set to null.
- a)
c) object1 will refer to the same memory location as object2.
- How does an ArrayList differ from a standard array in Java?
- a) An ArrayList can only store objects, not primitive types.
- b) An ArrayList cannot be iterated over with a for-each loop.
- c) An ArrayList’s size can dynamically change.
- d) An ArrayList requires elements to be of the same type.
c) An ArrayList’s size can dynamically change.
- If an object is perfectly designed and highly reusable, into which domain is it most likely to fit?
- a) Foundation domain
- b) Architecture domain
- c) Business domain
- d) Application domain
a) Foundation domain.
- Which of these classes would be considered more reusable?
- a) A class with attributes specific to a frequent-flyer program.
- b) A class with general attributes like name and address.
b) A class with general attributes like name and address.
- What is the correct way to declare and instantiate an
ArrayList
ofString
in Java?- a)
ArrayList<String> myList = new ArrayList<String>();
- b)
String[] myList = new ArrayList();
- c)
ArrayList myList = new String[10];
- d)
ArrayList<String> myList = new String[10];
- a)
a) ArrayList<String> myList = new ArrayList<String>();</String></String>
- Which operation on an array can potentially throw an
ArrayIndexOutOfBoundsException
in Java?- a) Accessing an element at a negative index.
- b) Accessing an element beyond the end of the array.
- c) Assigning a value to an array element within the array’s bounds.
- d) Declaring an array with a specific size.
b) Accessing an element beyond the end of the array.
- What is the main disadvantage of using arrays over
ArrayList
when it comes to adding or removing elements?- a) Arrays are immutable.
- b) Arrays do not have built-in methods to add or remove elements.
- c) Arrays can only hold primitive type
b) Arrays do not have built-in methods to add or remove elements.
- Which one of the following statements about object references in Java is true?
- a) When you assign one reference variable to another, both refer to separate objects in memory.
- b) When you assign one reference variable to another, they both refer to the same object in memory.
- c) Reference variables can be used interchangeably with primitive types.
- d) Reference variables can point to null, which means they are associated with a valid object instance.
b) When you assign one reference variable to another, they both refer to the same object in memory.