Object Creation and Storage (Instantiation) - AP Computer Science A
- What is the syntax to create a new object of class Car?
- Car myCar = new Car();. Standard object creation: declare variable, use new, call constructor.
- How do you invoke a method on an object?
- objectName.methodName();. Dot notation connects object reference to its methods.
- What is the result of 'Car[] cars = new Car[10];'?
- Array of 10 Car references. Array holds references, objects created separately.
- What is stored in a variable that holds an object reference?
- Memory address of the object. References are pointers to objects in heap memory.
- Identify the type of 'Car myCar;' where Car is a class.
- Object reference declaration. Declares variable to hold reference to Car object.
- How can you declare an object reference without instantiation?
- Car myCar;. Creates reference variable without allocating object memory.
- What happens if you attempt to use an uninitialized object reference?
- NullPointerException. Thrown when accessing methods on null references.
- What is the significance of 'null' in object references?
- Indicates no object is referenced. Default value for uninitialized object references.
- Identify the return type of a constructor.
- Constructors have no return type. Constructors initialize but don't return values.
- What does 'new' keyword do in object creation?
- Allocates memory for the object. Reserves space in heap for object's instance variables.
- Which operator is used to access an object's methods?
- Dot operator (.). Accesses methods and variables of an object instance.
- How do you create an array of objects?
- ClassName[] arrayName = new ClassName[size];. Creates array to hold object references, not objects.
- What is the purpose of the 'new' keyword?
- To create new objects. Allocates memory and initializes objects dynamically.
- Identify the error: 'Car myCar = new Car()'
- Missing semicolon. Statement terminator required after constructor call.
- How do you call a constructor when creating an object?
- Using new keyword with class name. Constructor follows class name after new keyword.
- What happens if no constructor is defined in a class?
- A default constructor is provided by Java. Java automatically provides a parameterless constructor.
- What is initialized when an object is instantiated?
- Object's fields. Constructor sets initial state of instance variables.
- What is the result of 'Car myCar = null;'?
- myCar points to no object. Reference is explicitly set to point nowhere.
- Correct the error: 'Car myCar = Car();'
- Car myCar = new Car();. Missing 'new' keyword required for object creation.
- What is the result of using 'new' keyword on a class?
- A new instance of the class is created. Each use of new creates a separate object instance.
- What is the difference between class and object?
- Class defines; object is instance. Class is template; object is concrete implementation.
- What does 'myCar = new Car();' do?
- Instantiates a Car object. Allocates memory and initializes new Car object.
- State the relationship between class and object.
- Class is a blueprint; object is an instance. Class defines structure; objects are actual instances.
- What is the role of the 'this' keyword?
- Refers to the current object instance. References the object whose method is being called.
- Identify the keyword used in Java for object instantiation.
- new. Creates objects by allocating memory and calling constructor.
- What is the purpose of a constructor?
- To initialize new objects. Special method that sets up new object instances.
- Which method is called automatically when an object is created?
- Constructor. Runs immediately when new keyword creates the object.
- How do you instantiate an object with a parameterized constructor?
- new ClassName(parameters). Pass arguments to constructor for custom initialization.
- Find the error: 'new Car myCar = new Car();'
- Remove 'new' before 'Car'. 'new' should only appear before constructor call.
- What is the consequence of not using 'new' keyword?
- No object is instantiated. Objects must be created with new keyword.
- Identify the part of 'Car myCar = new Car()' that is a constructor call.
- new Car(). Constructor call invokes the Car() constructor method.
- In Java, what is a constructor used for?
- Initializing a new object. Sets initial values for object's instance variables.
- What does an object reference variable store?
- Address of the object. Holds memory location where object data is stored.
- Identify the error: 'Car myCar = new Car;'
- Missing parentheses: new Car();. Constructor call requires parentheses after class name.
- What does 'null' signify for an object reference?
- No object is referenced. Special value indicating absence of object reference.
- What is the default constructor?
- A no-argument constructor provided by Java. Takes no parameters and provides basic initialization.