MOOC Week 5 Flashcards

1
Q

What is OOP?

A

Object-oriented programming is primarily about isolating concepts into their own entities or, in other words, creating abstractions.

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

What is an object and how is it created?

A

An independent entity that contains both data (instance variables) and behavior (methods). It is created using the keyword “new”, this keyword also creats a new memory location for the new object. An object is created on the basis of the class constructor.

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

How do objects interact with one another?

A

Through method calls — these method calls are used to both request information from objects and give instructions to them.

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

What is a class?

A

A blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).

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

What keyword is used for a constructor to be called from another constructor? Why is this used?

A

this() Used for constructor overloading which removes repetitive code, making it more efficient and readable.,

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

How to overload methods?

A

Have the same name but different parameters.
public void growOlder() {
this.growOlder(1);
}

public void growOlder(int years) {
this.age = this.age + years;
}

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

Declaring a primitive variable does what?

A

Causes the computer to reserve some memory where the value assigned to the variable can be stored.

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

What is a reference variable?

A

Any object instanced from a class.

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

What is the main difference between primitive and reference variables?

A

The basic difference is that primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to. Primitive variables are immutable whereas reference variables are a reference to the variable’s data, its object and doesn’t contain the object itself.

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

If you have created object variables for a class, how can you create constructors, getters, and setters automatically in NetBeans.

A

Go inside the code block of the class, but outside of all the methods, and simultaneously press ctrl and space.

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

Contruct a program that prints the current date, what premade java class would you need to import.

A

import java.time.LocalDate;

public class Example {

public static void main(String[] args) {

    LocalDate now = LocalDate.now();
    int year = now.getYear();
    int month = now.getMonthValue();
    int day = now.getDayOfMonth();

    System.out.println("today is  " + day + "." + month + "." + year);

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

What is abstraction in programming?

A

Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.

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

What is the difference between “==” and equals() method?

A

“ == “ operators is used for reference comparison (address comparison) between reference varaibles while .equals() method for content comparison.

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

What is type cast?

A

Type casting is when you assign a value of one primitive data type to another type.

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

What happens when you type cast a reference variable?

A

And casting a reference variable doesn’t touch the object it refers to but only labels this object in another way, expanding or narrowing opportunities to work with it. Upcasting narrows the list of methods and properties available to this object, and downcasting can extend it.

A reference is like a remote control to an object. The remote control has more or fewer buttons depending on its type, and the object itself is stored in a heap. When we do casting, we change the type of the remote control but don’t change the object itself.

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

What does the instanceof operator do?

A

The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

17
Q

What is the class Object?

A

The class the every java class inherits from. Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object . This is why an instance of any class can be passed as a parameter to a method that receives an Object type variable as its parameter.

18
Q

How does the compiler distinguish between overload methods?

A

signatures—the methods’ name and the number, types and order of its parameters.

19
Q

What is stack overflow and its causes?

A

When a program attempts to use more space than is available on the call stack. Usually caused by recursive methods.