Unit2B Flashcards

1
Q
  1. 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.
A

b) Grouping related ideas into a single unit referred to by one name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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
A

c) Invoking the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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.
A

b) To refer to the current instance of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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.
A

b) To initialize the object’s state with specific values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is the output when you instantiate a Dog object with the name “Rusty”, color “red”, and then call dog.getName() if getName() is an accessor method?
    • a) “Rusty”
    • b) “red”
    • c) “Dog”
    • d) There will be a compilation error.
A

a) “Rusty”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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
A

b) To act as the entry point of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What would be a result of setting one object reference equal to another, e.g., object1 = object2;?
    • a) object1 will be a copy of object2.
    • b) object1 will refer to a new instance of the class.
    • c) object1 will refer to the same memory location as object2.
    • d) object2 will be set to null.
A

c) object1 will refer to the same memory location as object2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. 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.
A

c) An ArrayList’s size can dynamically change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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

a) Foundation domain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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.
A

b) A class with general attributes like name and address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What is the correct way to declare and instantiate an ArrayList of String 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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.
A

b) Accessing an element beyond the end of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. 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
A

b) Arrays do not have built-in methods to add or remove elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • 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.
A

b) When you assign one reference variable to another, they both refer to the same object in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  • If a method in Java is named as printStudentDetails and it prints out the student’s name and ID, what kind of method is it?
    • a) Accessor
    • b) Mutator
    • c) Constructor
    • d) None of the above
A

d) None of the above - It’s likely a method for displaying or printing information rather than getting or setting it.

17
Q
  • What does the ArrayList method remove(int index) do?
    • a) It adds an element at the specified index.
    • b) It returns the element at the specified index without removing it.
    • c) It removes the element at the specified index.
    • d) It checks if an element exists at the specified index.
A

c) It removes the element at the specified index.

18
Q
  • What is the purpose of a constructor in a Java class?
    • a) To destroy an object when it’s no longer needed.
    • b) To initialize a new object with default or given values.
    • c) To set static fields of the class.
    • d) To act as a getter method for private fields.
A

b) To initialize a new object with default or given values.

19
Q
  • What principle allows a single variable name to refer to objects of multiple types that are related by some class hierarchy?
    • a) Encapsulation
    • b) Inheritance
    • c) Polymorphism
    • d) Composition
A

c) Polymorphism

20
Q
  • In the Dog class example from the slides, what will be the output of calling dog321.chase("cat", 5, 20); assuming chase method is properly implemented to print the chase action?
    • a) “Dog is chasing cat at speed 5 for 20 meters.”
    • b) “Dog is not chasing.”
    • c) No output, as the method does not print anything.
    • d) The actual output depends on the implementation of the chase method in the Dog class.
A

d) The actual output depends on the implementation of the chase method in the Dog class.