week04 inheritande&exceptions Flashcards

1
Q

What is an array in Java?

A

An array is a fixed-size collection that can store multiple items of the same type. The size is set when the array is created and cannot change. Arrays are zero-indexed, which means the first element is at index 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or false?
“You can change the size of an array after it is created.”

A

False. Once created, the size of a Java array cannot change. You must create a new array if you need a different size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you find the number of elements in an array?

A

Use the length property of the array. if array is an array, array.length gives you the number of elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should you be careful about with arrays of objects?

A

An array of objects is initialized with null for each element. You must assign an object to each position before using it. Accessing null elements will cause a NullPointerException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or false:
“System.arraycopy() creates a deep copy of an array.”

A

False. It creates a shallow copy, meaning it copies the references, not the actual objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a multi-dimensional array in Java?

A

It’s an array of arrays. You can imagine it is like a table with rows and columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an ArrayList in Java?

A

An ArrayList is a list that backed by an array. It can grow and shrink. It’s part of Java’s collections Framework and provides more functions than a simple array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or false:
“An ArrayList has a fixed size”

A

False. Unlike arrays, an ArrayList can change its size dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are key differences between an ArrayList and an array?

A

ArrayList can change size, only stores objects, and has many methods. Arrays have fixed size, can store primitives, have no methods, and can be multi-dimensional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When is it better to use an ArrayList?

A

Use ArrayList when you need a dynamic collection that chagnes size, when you’re storing objects, and when you want useful methods like add, remove, and contains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should you use an array instead of an ArrayList?

A

Use an array when you know the collection size won’t change, need to store primitives, or don’t need the extra features of ArrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you find the size of an array in Java?

A

Use the length property. Example: arrayName.length returns the size as an int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can an array contain in Java?

A

A single kind of ‘thing’ - primitives, object of a class, but all must be the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is composition in OOP?

A

A ‘has-a’ relationship where a class includes object references as instance variables, indicating ownership

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is aggregation in OOP?

A

A specific ‘has-a’ relationship where a class contains a collection of objects, which can exist independently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between composition and aggregation in OOP?

A

Composition implies ownership and a strong relationship; without the containing object, the contained objects do not make sense. Aggregation allows contained objects to exist independently.

17
Q

What does the “is-a” relationship signify in Java, and how is it implemented?

A

It signifies a class inheriting from another class, indicating a parent-child relationship. Implemented with the extends keyword, where the subclass “is a” specialized version of the superclass.

18
Q

What are the pillars of Object-Oriented Programming (OOP) in Java?

A

Encapsulation, information hiding, inheritance, polymorphism, and abstraction.

19
Q

How does Java’s inheritance relate to the Object class?

A

Every class in Java inherits from the Object class, gaining access to methods like toString(), equals(), and hashCode().

20
Q

What are the rules of inheritance in Java regarding superclass and subclass?

A

Java supports only single inheritance; a subclass can have only one superclass, ensuring a clear “is-a” relationship.

21
Q

Can a superclass have multiple subclasses? What must each subclass represent?

A

Yes, a superclass can have any number of subclasses. Each subclass must be a specialized version of the superclass.

22
Q

How should common attributes be managed in an inheritance hierarchy?

A

Common attributes should be declared in the superclass. Subclass constructors must pass these attributes to the superclass constructor using super().

23
Q

Should instance variables in the superclass be duplicated in the subclass?

A

No, instance variables should not be duplicated. Subclasses inherit them, including private ones, but access private variables through public or protected methods.

24
Q

How does inheritance support encapsulation in Java?

A

By grouping related instance variables and methods in a superclass, subclasses inherit and can use these, promoting independence and reducing coupling.

25
Q

What happens if the super() constructor call is not explicitly made in a subclass constructor?

A

Java attempts to call the superclass’s default (no-argument) constructor automatically. If not available, the subclass must explicitly call a different superclass constructor.

26
Q

How can a subclass access non-private and private superclass members?

A

Non-private members can be accessed directly. Private members are accessed indirectly through public or protected methods of the superclass.

27
Q

What is method overriding and the role of the protected visibility modifier?

A

Overriding allows subclass methods to implement superclass method signatures. Protected makes variables or methods accessible to subclasses and package classes.

28
Q

What is the difference between overriding and hiding methods in Java?

A

Overriding changes a superclass method’s behavior in the subclass. Hiding occurs when a subclass defines a static method with the same signature as a static superclass method.

29
Q

What are final classes and methods in Java?

A

Final classes cannot be extended, and final methods cannot be overridden. This is useful for locking down behavior to prevent further modification.

30
Q

What is the base class of all exceptions in Java?

A

Throwable. Exceptions in Java extend Throwable, meaning they can be thrown and caught.

31
Q

How does Java handle exceptions compared to Python?

A

Java uses try and catch blocks, similar to Python, but it adopts a Look Before You Leap (LBYL) approach, using exceptions only for exceptional situations, not for control flow.

32
Q

What are the components of Java’s exception handling structure?

A

Wrap risky code in a try block, catch exceptions with catch blocks, and optionally use a finally block for code that runs regardless of exceptions.

33
Q

How do you document a method that throws an exception in Java?

A

Add throws ExceptionName to the method signature and use an @throws tag in the Javadoc.

34
Q

What are the two types of exceptions in Java?

A

Checked exceptions (must be caught or declared to be thrown) and unchecked exceptions (runtime errors, often due to programming mistakes).

35
Q

How should you handle unchecked exceptions in Java?

A

Unchecked exceptions should not be caught routinely; they indicate programming errors that need to be fixed.

36
Q

What must you do with checked exceptions in Java?

A

Must be handled or declared, as they represent recoverable conditions. The compiler enforces this rule.

37
Q

What is exception chaining in Java?

A

When an exception causes another exception, both can be seen in the stack trace. Be cautious with exceptions thrown from finally blocks, as they can obscure the original exception.

38
Q

How do you create custom exceptions in Java?

A

Extend Exception for checked exceptions, or extend RuntimeException for unchecked exceptions.

39
Q

What happens if super() is not called explicitly in a subclass constructor?

A

Java tries to call the superclass’s no-argument constructor automatically. If unavailable, you must call a different superclass constructor explicitly.