midterm_package Flashcards

1
Q

What is the difference between a class and an object?

A

Object is a class, and all other classes are subclasses of the Object class (implicitly inherit). So we can say that any instance of a class IS AN object.

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

What does “Java is a strongly typed language” mean?

A

Java is a strongly typed language because every ‘thing’ has a type, and certain operations can only be performed for certain types.

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

What are the four visibility modifiers used in Java, and which of the four access levels(class, package, subclass, everyone) are permitted by each modifier?

A

private: can be accessed within the class only.
default: can be accessed by other classes in the same packages. can’t be accessed by subclasses in different packages
protected: can be accessed by other classes in the same package, and subclasses in different packages
public: can be accessed anywhere

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

How must we use visibility modifiers to hide information and enforce encapsulation?

A

To hide information and enforce encapsulation, we use ‘private’ for members we want to hide from other classes, and provide public getter and setter methods to access them. we use ‘protected’ when we want to share it with subclasses and within the same package. Default(no modifier) access is limited to the same package.

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

Data conversion in Java can be wodening or narrowing. What does that mean?

A

Widening occurs when data is converted from a smaller to a larger data type, and it happens automatically.
Narrowing occurs when data is converted from a larger to a smaller data type. This requires explicit casting by a programmer since data loss could occur.

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

Java makes a distinction between identity and equality. what does this mean?

A

Identity referes to the uniqueness of an object instance at a memory location. Two references have the same identity if they point to the same memory location, meaning they references to the same object == operator is used to check identity.
Equality is a logical condition that checks if two objects have the same value or state but can reside at different memory location. .equals() methods is used to check equality.

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

What is pass by value in Java?

A
  1. if the argument is a primitive, a copy of the value is passed to the method.
  2. if the argument is an object, a copy of the reference to the object is passed.

As a result, the original value or the object that the reference points to is not affected by what happens within the method.

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

What is the difference between overriding and overloading?

A

Overriding is when a subclass redefines a method from its superclass with the same signature to provide a specific implementation. It enables runtime polymorphism, allowing Java to determine which method to invoke based on the object type at runtime.

Overloading involves defining multiple methods with the same name but different parameter lists (different types or numbers of parameters) within the same class. It enables compile-time(static) polymorphism, allowing multiple methods to do similar tasks with different types of inputs.

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

what are four ways we can use the final keyword?

A
  1. When applied to a variable, final makes the variable unchangeable after it has been assigned
  2. When applied to a method, final prevents from being overridden in subclasses
  3. When applied to a class, final makes the class cannot have subclasses
  4. When applied to a parameter of a method, final prevents from being modified within the method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Programmers like to make a distinction between an API’s ”interface” and an API’s “implementation”. What do they mean?

A

An API’s “interface” refers to the exposed methods and properties that define how to use it, while “implementation” is the underlying code that actually performs the tasks, hidden from the user.

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

What is the difference between an abstract class and an interface? when should you use an abstract class, and when should you use an interface?

A

You “extends” an abstract class while you “implements” an interface. Any variables in an interface are public, static, and final. Methods in an interface can only be private or public.

Use an abstract class to define behaviours and properties that are closely related to other classes. Use an interface when you don’t care who implements it.

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

what is a static type?

A

Static type refers to the declared type of a variable. It is determined at compile time based on the variable’s declaration. The static type dictates what methods and properties are accessible through the variable in your code.

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

What is a dynamic type?

A

Dynamic type refers to the actual type of the object that a variable points to at runtime, which can be an instance of the declared type or any of its subclasses. The dynamic type can differ from the static type if the variable points to an instance of a subclass.

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

What is polymorphic method dispatch?

A

When a class’s method is overriden by some subclass, the subclass’s version is invoked instead. Human bob = new Baby(), the dynamic type (Baby) determines which version of the method to invoke during runtime, given Baby has overriden a method in the Human class

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

What is the difference between encapsulation and information hiding?

A

Encapsulation is grouping related data together in one place. Data is only accessible through setter and getter.
Information hiding is keeping data as private as possible by using visibility modifiers(e.g. private)

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

How does Java use visibility modifier to reveal the interface and hide the implementation?

A

Private modifiers hide the implementation by concealing internal elements not meant for user knowledge, while public modifiers reveal the interface by exposing methods for user interaction.

17
Q

What is underflow?

A

Underflow occurs when a calculation’s result falls below the smallest value that can be represented by the data type. leading to inaccuracies or a zero value.

18
Q

What is a hash code and how is it related to the equals method?

A

A hash code is an integer generated by a hash function from an object’s data. If two objects are equal according to equals method, they must have the same hash code. However, different objects can also have the same hash code, due to hash collisions.

19
Q

What are exceptions and when should they be used?

A

We throw exceptions to handle errors and exceptional events, typically when encountering situations that the program cannot resolve using its regular flow. This allows for structured error management and recovery.

20
Q

What is the difference between a checked exception and an unchecked exception in Java?

A

A checked exception in Java must be caught or declared in the method’s signature, enforcing error handling. An unchecked exception, extending RuntimeException, indicates runtime errors such as logic flaws or API misuse and doesn’t require declaration or explicit handling in the method’s signature.

21
Q

What is the difference between the Java inheritance mechanism and the Java interface mechanism? Why do we need both?

A

A class can extend only one superclass, allowing single inheritance to inherit properties and behaviour. However, a class can implement multiple interfaces, enabling it to adhere to multiple contracts for behaviour. We need both to combine code reuse and hierarchy with the ability to support multiple behaviours and flexibility.

22
Q

What does polymorphism mean? How does it work in Java?

A

Polymorphism in Java lets us use a single interface to represent different underlying forms (data types). It means we can call the same method on different objects and each object can respond in its own way. Java handles these details at runtime, choosing the right method for the object.

23
Q

what does “program to an interface” mean?

A

The “program to an interface” means use abstract and interface types for method parameters and the static type when declaring variables instead of concrete class types. This follows the solid principles and helps make code more maintainable, flexible, and re-useable.

24
Q

what is a primitive value and how is it different from a reference type?

A

Primitive type: cannot be broken down further, stored where it is used(heap or stack), each contains a wrapper class, which is a reference type
Reference type: is stored in the heap, reference type objects belong to a class that contains methods that can be invoked on it.

25
Q

How do we follow the Liskov Substitution Principle?

A

To follow the Liskov Substitution Principle, ensure that subclasses can stand in for their parent classes without changing the program’s behaviour. This means a subclass should not override parent methods in a way that alters expected behaviour, and it should fulfill all the contracts and invariants defined by the superclass.

26
Q

What is the Law of Demeter?

A

“You should only talk to your friends.”
If class A and B are friends, and C is B’s friend, class A should ask B to talk to C, instead of A directly interacting with C.
Moreover, a class should never directly reach into another class’ contents, but ask through getter methods.

27
Q

What are two differences between the java array and the java ArrayList?

A
  1. Array size is fixed, cannot grow and shrink.
    ArrayList’s size can dynamically grow and shrink.
  2. Array can store primitive types.
    ArrayList only can store references.
28
Q

What are exceptions and when should they be used?

A

Expection in Java is an object contains nothing but information:
1. Exception type
2. The location where the Exception occurred
3. Access to a stack trace
4. Short descriptive message

We try some code that may contain errors, and if error happens we throw an exception.