Object Creation and Storage (Instantiation) - AP Computer Science A

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

Study these flashcards