Java Objects Flashcards

1
Q

What is an object in Java?

A

An object is an instance of a class that holds data (fields) and behavior (methods).

class Dog {
    String name; // field to store the dog's name
    void bark() { // method to make the dog bark
        System.out.println(name + " says woof!");
    }
}
Dog myDog = new Dog(); // create a new Dog object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create an object in Java?

A

Use the new keyword followed by the constructor of a class.

Dog myDog = new Dog(); // create a Dog object using the default constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you access fields and methods of an object?

A

Use the dot (.) operator to access members of an object.

myDog.name = "Millie"; // assign value to the field
myDog.bark(); // call the method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a constructor in Java?

A

A constructor is a special method that initializes an object when it’s created.

class Person {
    String name; // field
    Person(String n) { // constructor
        name = n; // initialize field
    }
}
Person p = new Person("Owen"); // create a new Person object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you overload constructors in Java?

A

Yes, you can define multiple constructors with different parameter lists.

class Cat {
    String name;
    int age;
    Cat(String n) { name = n; } // constructor with one parameter
    Cat(String n, int a) { name = n; age = a; } // constructor with two parameters
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the this keyword used for in Java?

A

this refers to the current object and helps distinguish instance variables from parameters.

class Book {
    String title;
    Book(String title) {
        this.title = title; // 'this.title' refers to the instance variable
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if an object is declared but not initialized?

A

Its reference is null, and accessing it will cause a NullPointerException.

Car car; // declared but not initialized
car.drive(); // NullPointerException when calling a method on null reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can objects be passed to methods in Java?

A

Yes, Java passes the object reference by value.

void paint(Car c) {
    c.color = "red"; // modify the object's field
}
Car myCar = new Car();
paint(myCar); // myCar.color is now "red"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you compare two objects in Java?

A

Use .equals() for content comparison and == for reference comparison.

String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);       // false - compares references
System.out.println(s1.equals(s2));  // true - compares content
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default value of an object reference in Java?

A

The default value of an object reference is null.

String name; // not initialized
System.out.println(name); // prints null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you define a class that can create objects?

A

Define fields and methods inside a class.

class Student {
    String name; // field
    void study() { // method
        System.out.println(name + " is studying.");
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can objects be stored in arrays in Java?

A

Yes, you can store object references in arrays.

Student[] students = new Student[3]; // array of Student references
students[0] = new Student(); // assign a Student object to index 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between a reference and an object?

A

A reference points to an object in memory, while the object holds the actual data.

Dog a = new Dog(); // 'a' is a reference to the Dog object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is object encapsulation?

A

Encapsulation means keeping fields private and providing public methods to access them.

class BankAccount {
    private double balance; // private field
    public double getBalance() { return balance; } // public getter
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the null keyword used for in Java?

A

null represents the absence of a value for an object reference.

String s = null; // no object assigned to s
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the Declaration, Instantiation, and Initialization steps in Java object creation?

A

All three steps involved in creating an object in Java:

1. Declaration – Declare a reference variable:

Car myCar;

2. Instantiation – Use new to create the object in memory:

new

3. Initialization – The constructor initializes the object:

Car();  // Constructor call

Full example combining all three:

Car myCar = new Car();
  • Car myCar is the declaration.
  • new is the instantiation.
  • Car() (constructor) is the initialization.