AP CSA Chapter 9 MCQs
32 Questions
100 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the result of the following code: Android a = new Android(x); a.setServoCount(y); System.out.println(a.getServoCount());

The value of y

Which of the following can be used in the printInfo method in the Artwork class to produce the intended output?

  • System.out.print(super.printInfo() + ' by ' + artist);
  • super.printInfo(); System.out.print(' by ' + artist); (correct)
  • super.printInfo(artist);
  • System.out.print(title + ' (' + year + ') by ' + artist); (correct)
  • super(); System.out.print(' by ' + artist);
  • What happens when executing the code: EBike eB = new EBike(4);?

  • The code segment will not execute because the constructor of the EBike class is missing a second parameter to use to initialize the numWheels instance variable.
  • An implicit call to the one-parameter Bike constructor with the parameter passed to the EBike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries.
  • Because super is not explicitly called from the EBike constructor, the instance variable numWheels is not initialized. The instance variable numBatteries is initialized using the value of the parameter batteries.
  • An implicit call to the zero-parameter Bike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries. (correct)
  • The code segment will not execute because the Bike class is a superclass and must have a constructor.
  • Which of the following can be used to compare if hotChocolate and coffee have the same temperature?

    <p>hotChocolate.equals(coffee)</p> Signup and view all the answers

    What is the output of the code segment: Hero j = new SuperHero('JavaHero', 50); j.powerUp(10); System.out.println(j.showPower());

    <p>70</p> Signup and view all the answers

    Under which condition will the statement myPet.speak(); compile and run without error?

    <p>I, II, and III</p> Signup and view all the answers

    Which of the following best explains why the code segment involving Book and AudioBook does not compile?

    <p>Line 4 will not compile because variables of type Book may only call methods in the Book class.</p> Signup and view all the answers

    Is it true that the statement b1.equals(b2) checks if b1 and b2 have the same lengths and widths?

    <p>True</p> Signup and view all the answers

    Which of the following can be used to correctly initialize a variable of type B?

    <p>e2.doNothing(e2, e2);</p> Signup and view all the answers

    What will be printed by the code segment: Dog fido = new UnderDog();? What is the result of the call fido.act()?

    <p>run eat bark sleep</p> Signup and view all the answers

    What error occurs in the code segment involving the Road and Highway classes?

    <p>Line 3 will cause an error because a Highway variable cannot be instantiated as an object of type Road.</p> Signup and view all the answers

    What happens when the message method is removed from class A in the code segment?

    <p>The statement in line 3 will cause a compiler error because the message method for obj1 cannot be found.</p> Signup and view all the answers

    What is true about the setPrice method in the context of Vehicle and Car classes?

    <p>The code v.setPrice(1000.0); will cause the setPrice method of the Car class to be called.</p> Signup and view all the answers

    Which of the following best describes the effect of executing the statement Bird b = new Hawk(5, 8);?

    <p>The Bird variable b is instantiated as a Hawk. The call super(beak) invokes the Bird constructor and initializes the instance variable beakStrength with the value from the parameter beak. The instance variable talonStrength is then initialized with the value from the parameter talon.</p> Signup and view all the answers

    What, if anything, is printed when the code segment ClassA obj = new ClassB(); obj.showValue(); is executed?

    <p>B</p> Signup and view all the answers

    Which of the following statements will NOT compile?

    <p>Sub s3 = new Sub(5);</p> Signup and view all the answers

    What code segment can replace super() so that the Square class constructor initializes the Rectangle class instance variables height and width to x?

    <p>super(x);</p> Signup and view all the answers

    The following code segment appears in a method in a class other than Book or TextBook. What best describes the effect of executing the code segment Book b = new TextBook("Psychology");?

    <p>There is an implicit call to the zero-parameter Book constructor. The instance variable bookTitle is then initialized to &quot;&quot;. Then, the instance variable subject is initialized with the value of the parameter theSubject.</p> Signup and view all the answers

    Which of the following should be used to replace so that all instance variables are initialized with parameters in the Boot class?

    <p>super(brand, model); heelHeight = height;</p> Signup and view all the answers

    Under which of the following conditions will the statement someApple.printColor(); print "Red"?

    <p>When someApple is an object of type Jonagold.</p> Signup and view all the answers

    Which of the following best explains why the code segment does not produce the intended output?

    <p>Method m2 is executed from the subclass instead of the superclass because obj1 is instantiated as a C2 object.</p> Signup and view all the answers

    Which of the following best explains why the code segment does not compile: Game g1 = new BoardGame("checkers"); BoardGame g2 = new Game("chess");?

    <p>A Game object cannot be assigned to the BoardGame reference g2.</p> Signup and view all the answers

    Which of the following replacements will cause a compile-time error?

    <p>two.methodB();</p> Signup and view all the answers

    Which of the following can be used so that the TextBook constructor compiles without error?

    <p>super(the_author, the_title); subject = the_subject;</p> Signup and view all the answers

    Which of the following must be true so that the code segment will compile without error: Coffee myCup = new Coffee(); myCup.setSize("large");?

    <p>The Drink class must have a public method named setSize that takes a String value as its parameter.</p> Signup and view all the answers

    Which of the following proposed constructors for the NamedPoint class would be legal?

    <p>I and III only</p> Signup and view all the answers

    Which of the following best explains why the code segment does not compile: Bike b = new EBike(250); System.out.println(b.getNumOfWatts());?

    <p>The getNumOfWatts method is not found in the Bike class.</p> Signup and view all the answers

    Which of the following explanations best describes why the code segment does not compile: FamilyMembership m1 = new Membership("123");?

    <p>In line 1, m1 cannot be declared as type FamilyMembership and instantiated as a Membership object.</p> Signup and view all the answers

    What is printed as a result of executing the following code segment?

    <p>B</p> Signup and view all the answers

    Which of the following declarations will compile without error?

    <p>Food tacos = new Snack();</p> Signup and view all the answers

    What is printed as a result of executing the statement Derived d1 = new Derived();?

    <p>Base Derived</p> Signup and view all the answers

    Which code segment can be used to ensure the value 20 will be printed?

    <p>setServoCount(y);</p> Signup and view all the answers

    Study Notes

    Class Definitions and Inheritance

    • Bird Class: Contains a private variable beakStrength. The constructor initializes this variable with the provided input.
    • Hawk Class: Inherits from Bird and initializes both beakStrength (through the superclass constructor) and its own variable talonStrength.
    • Binding a Bird type variable to a Hawk object instantiates a Hawk and initializes both strength attributes via their constructors.

    Method Overriding and Polymorphism

    • ClassA and ClassB: ClassB overrides getValue from ClassA. A ClassA reference can point to a ClassB object, executing showValue prints "B" due to method overriding.
    • In Thing1 and Thing2, method calls illustrate how superclass methods can also be called from subclass overrides, affecting output.

    Access Control and Constructor Conflicts

    • Constructors that don't invoke the superclass constructors properly will lead to compilation errors. For instance, Sub s3 = new Sub(5); will not compile because it doesn't match any constructor defined.
    • Properly calling superclass constructors is essential for initializing inherited instance variables.

    Object Types and ArrayLists

    • Java's type system requires that the declared variable type matches the object it references. For example, a Game object cannot be referenced as a BoardGame type.

    Compilation Errors

    • Using a method defined in one class on an instance of an unrelated class will lead to a compile-time error (e.g., two.methodB()).
    • Field access restrictions due to visibility can also result in compilation errors.

    Constructors in Subclasses

    • When creating subclass constructors, ensure superclass constructors are called correctly to initialize inherited fields. For example, super(the_author, the_title); is required when initializing Textbook.

    Printing Output and Method Execution

    • Methods from subclasses can override those in the superclass, affecting what gets printed during execution.
    • Compiling methods referenced on variables depends on the actual object being instantiated, not just the declared type.

    Object Instantiation and Implicit Calls

    • Creating an instance of a subclass automatically calls the superclass's no-argument constructor unless specified otherwise, allowing for inherited field initialization without explicit calls.

    Advanced Object Interaction

    • Multiple variables and their interactions can lead to various expected outputs when printed. Each method's presence and visibility play a crucial role in what gets executed.

    Method Interaction and Visibility

    • When invoking methods, the context (whether accessing via a superclass or subclass) affects the outcome, especially when dealing with method overriding.

    Key Output Expectations

    • Statement outputs can be predicted based on which methods are overridden, and variables which hold values help understand behavior across method calls in inheritance hierarchies.### Class Initialization and Constructor Behavior
    • Instance Variable Initialization: In an EBike constructor, the instance variable numBatteries is initialized with the parameter batteries. If not explicitly referenced, it may lead to uninitialized variables.
    • Superclass Constructor: Failure to call the super constructor in subclasses like EBike can result in uninitialized instance variables from the superclass, leading to compile-time errors.
    • Constructor Requirements: Superclasses must have constructors defined, a missing constructor in the superclass can cause execution failures.

    Object Equality and Method Overriding

    • Method Overriding Using equals: The equals method in the Beverage class compares temperature values, returning true if they match. Using hotChocolate.equals(coffee) will set same to true if both beverages have the same temperature.
    • Behavior of showPower Method: When invoking j.powerUp(10) on a SuperHero type object, the overridden method doubles the power increment, printing the result as 70.

    Polymorphism and Dynamic Binding

    • Dynamic Method Invocation: The speak method in the Pet class displays behavior determined at runtime. For instance, when myPet.speak() is called, it can run without errors for Dog and Cat types due to polymorphism.
    • Correct Method Calls: For methods like doNothing, valid calls depend on the class type. Example: Using e2.doNothing(e2, e2); is a proper instantiation for an object of type Example2.

    Inheritance and Method Invocation

    • Super Calls in Constructor: In class B, a constructor must explicitly call the superclass constructor if it's designed to initialize superclass variables.
    • Ambiguous Calls: Compiler errors may arise due to ambiguous method calls, especially when multiple classes share method names with similar signatures.

    Object Behavior and Access Modifiers

    • Private Variables Access: Private instance variables in classes cannot be accessed directly outside the class; methods like getLength() must be utilized.
    • Runtime vs Compile-time Errors: Understanding when an error is compile-time (due to method definitions and signatures) versus runtime (due to logic flaws) is crucial in debugging.

    Object Instantiation and Type Compatibility

    • Instantiation Mismatches: Attempting to instantiate subclasses without proper references leads to compilation errors. For example, Highway r3 = new Road("Sullivan Street"); will fail as Highway cannot hold a Road reference directly.
    • Overridden Methods in Subclasses: The behavior of methods can change based on references; for instance, a Ticket reference to a DiscountTicket will invoke the overridden getPrice method.

    Overriding and Method Behavior

    • Super Keyword Usage: super.getPrice() calls the parent class method, allowing the subclass to modify its return value while still accessing the original logic.

    Summary of Expected Outputs

    • Output Results: Correct understanding of printed outputs is key. For example, a DiscountTicket will print "Price is 5.0" due to method overriding that alters how the price is calculated.

    Important Definitions

    • Method Overloading and Overriding: Distinction between methods with the same name but different signatures (overloading) versus the same name in different contexts/classes (overriding).
    • Object Equality and Hashing: Custom implementations of equals and hashCode are crucial for collections and are often overridden to ensure proper functioning.

    Error Investigation

    • Referencing Non-Existing Methods: Always check if the methods you intend to use on an object are defined within its class or inherited from its ancestors; undefined methods result in compile-time errors.

    Note on Code Compilation

    • Understanding when a code segment will compile without error is essential for efficient object-oriented programming; different types of references can result in valid or invalid operations based on class definitions.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge with these multiple-choice questions from AP Computer Science A Chapter 9. Explore object-oriented programming concepts, including class definitions, inheritance, and method overriding. Prepare effectively for your exam and reinforce your understanding of essential programming principles.

    More Like This

    Object-Oriented Programming Concepts
    30 questions
    AP CSA CodeHS 2.8 Flashcards
    6 questions
    APCSA Chapter 7 Test Flashcards
    24 questions

    APCSA Chapter 7 Test Flashcards

    SustainableAntigorite1088 avatar
    SustainableAntigorite1088
    Object-Oriented Programming Concepts
    13 questions
    Use Quizgecko on...
    Browser
    Browser