week02 Foundational OOP Flashcards
What is the role of the main
method in Java?
It’s the starting point of the program. It’s where objects are created and the program begins execution.
What are the requirements for the main
method in Java?
It must be public and static, with a single String array argument(String[] args
).
What are two key characteristics of the main
method?
The main
method must exist for the program to run and should initiate the program’s operations by creating objects and calling methods.
What is a constructor in Java?
A constructor is a special method to create and initialize an object. It has the same name as the class and sets up initial values for object attributes.
What are the key characteristics of a constructor?
A constructor does not have a return type, not even void. It initializes all instance variables with values.
How is a constructor different from a regular method?
Unlike methods, a constructor is called automatically when a new object is created and does not need a return type.
How does a constructor use parameters in Java
It uses parameters passed to it to assign values to the object’s attributes using this
keyword.
What is the equivalent of a constructor in Python?
The \_\_init\_\_
method in Python is like a constructor in Java. It initializes object’s properties.
The instance variables are declared in the constructor.
True or false?
False.
The instance variables are declared in the class.
What are instance variable in Java?
Instance variables are data elements of a class. Each object has its own copy.
Where do you declare instance variables?
Declare them at the top of a class. They should be outside any method or constructor.
How do you assign initial values to instance variables?
Set initial values in the constructor, which is called when creating an object.
What is the scope of instance variables?
The entire class. All methods and constructors in the class can use them.
What else can instance variables be called?
Fields, attributes, or data members. They store information about an object’s state.
Do instance variables have a data type?
Yes, each instance variable must be declared with a data type.
What is the purpose of instance variables?
They represent the state of an object of instance and hold values specific to each object.
What happens to instance variables that are not initialized?
They get a default value from Java: 0 for numbers, false for boolean, null for objects.
How are local variables different from instance variables regarding initialization?
Local variables do not get a default value and must be initialized before use.
What does Java do if you don’t write a constructor for a class?
Java creates a default constructor that sets all instance variables to default values.
What happens to the default constructor when you write your own constructor(s)?
If you write any constructors, Java does not create a default one.
Java class can only have one constructor.
True or false?
False. You can have multiple constructors and they can call each other with this
.
What are the default values for data types in Java?
・byte, short, int, long: 0(or 0L for long)
・float, double: 0.0(or 0.0f for float)
・char: ‘\u0000’(null character)
・boolean: false
・object: null
What is a Java object?
It’s an instance of a class that has its own space in memory.
What determines the state of a Java object?
Its state is determined by the values stored in its instance variables.