Types and Method Overriding in Programming
41 Questions
100 Views

Types and Method Overriding in Programming

Created by
@WellBacklitJasmine

Questions and Answers

What is another name for declared type?

  • Static type (correct)
  • Interface type
  • Dynamic type
  • Constructor
  • What is another name for object type?

  • Primitive type
  • Dynamic type (correct)
  • Static type
  • Reference type
  • How do you override a method?

    Must be a class extending a class; use @override; keep the same method header, change method body.

    What does 'extends' mean in programming?

    <p>Relationship between two classes or two interfaces; retains method bodies for classes and method contracts for interfaces.</p> Signup and view all the answers

    A class ___________s an interface.

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

    What kind of variable is NaturalNumber?

    <p>A reference type that points to an object value.</p> Signup and view all the answers

    How do I initialize a natural number with a number bigger than the max value of an int?

    <p>Use another natural number, a string with said number, or math to get there.</p> Signup and view all the answers

    What is the default parameter mode when none is specified?

    <p>Restores Mode.</p> Signup and view all the answers

    What does restores mean?

    <p>Returns input variable to its original value at the end of the method.</p> Signup and view all the answers

    What does updates mean?

    <p>Changes the value of the input variable based on its original value.</p> Signup and view all the answers

    What does clears mean?

    <p>Returns the value of the input variable to a default value, usually zero.</p> Signup and view all the answers

    What are the results of NaturalNumber compareTo?

    <p>Positive if A &gt; B, 0 if A = B, Negative if A &lt; B.</p> Signup and view all the answers

    What does replaces mean?

    <p>Changes the value of the input variable regardless of the original value.</p> Signup and view all the answers

    What is an immutable reference?

    <p>Has a reference value and an object value; cannot be changed by a method call.</p> Signup and view all the answers

    What is a mutable reference?

    <p>Has a reference value and an object value; can be changed by methods.</p> Signup and view all the answers

    Which of the following are primitive types?

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

    Test cases should include which of the following?

    <p>All of the above</p> Signup and view all the answers

    What does overloading a method mean?

    <p>A method that appears multiple times within a class, with different argument types and/or number of arguments.</p> Signup and view all the answers

    What kind of error does infinite recursion induce?

    <p>Stack Overflow Error.</p> Signup and view all the answers

    What can a failed test case indicate?

    <p>All of the above</p> Signup and view all the answers

    What is best practice for declaring variables?

    <p>Left side should be declared type, right side should be object type.</p> Signup and view all the answers

    What does TransferFrom do?

    <p>Copies value and clears value of the one it copies from.</p> Signup and view all the answers

    What are the characteristics of NaturalNumber?

    <p>Must be positive; mutable type; unlimited upper bound; uses integer math.</p> Signup and view all the answers

    What is a string's characteristic?

    <p>Reference type, but immutable; cannot be changed by a method call.</p> Signup and view all the answers

    What is an array?

    <p>Reference type; mutable.</p> Signup and view all the answers

    What does NaturalNumber.newInstance() method do?

    <p>Creates a new natural number of the same type, set to an initial value (0); is not an alias.</p> Signup and view all the answers

    What is a string permutation?

    <p>Reordering of the same values.</p> Signup and view all the answers

    What does freelunch teach you about recursion?

    <p>To do recursion correctly, you must call the method within itself using a smaller version of the problem.</p> Signup and view all the answers

    What are static methods?

    <p>By itself, no 'this'; can return any type or nothing (void).</p> Signup and view all the answers

    What are instance methods?

    <p>Use 'this'; 'this' is a receiver.</p> Signup and view all the answers

    What is confidence building recursion argument like?

    <p>Mathematical proof by induction.</p> Signup and view all the answers

    What does DivideBy2 do?

    <p>Shave off a digit, divide it, recurse with a smaller number, and multiply that number by 10 adding remainder.</p> Signup and view all the answers

    What can JUnit do?

    <p>It can show the presence of a defect; cannot prove correctness or show the absence of defects.</p> Signup and view all the answers

    What is NaturalNumber adding itself dividing?

    <p>Do division first; add restores the argument input after adding two.</p> Signup and view all the answers

    Why isn't aliasing a problem with immutable types?

    <p>Because object values cannot be changed; changing the alias points to a new object.</p> Signup and view all the answers

    What is a reference type?

    <p>Reference points to an object; reference value = memory address of object.</p> Signup and view all the answers

    What is a substring?

    <p>Starts from initial value and goes to the value before end value.</p> Signup and view all the answers

    What is unit testing?

    <p>Testing individual units or components.</p> Signup and view all the answers

    What is integration testing?

    <p>Testing multiple components put together.</p> Signup and view all the answers

    What is system testing?

    <p>Testing a whole end-user system.</p> Signup and view all the answers

    What should the actual behavior of a method be?

    <p>A subset of allowed behaviors.</p> Signup and view all the answers

    Study Notes

    Types in Programming

    • Declared Type (Static Type)

      • Fixed type at the declaration level
      • Remains constant during program execution
    • Object Type (Dynamic Type)

      • Type that can change at runtime
      • Defined on the right side of a declaration
      • Constructors can instantiate the object type
      • Supports interface implementations and overridden methods

    Method Overriding

    • Requires a subclass to extend a superclass
    • Use the @override annotation
    • Maintain the same method header while modifying the body

    Class and Interface Relationships

    • Extends: Describes the inheritance relationship
      • Classes retain method bodies; interfaces retain contracts
    • Implements: Defines how a class uses an interface
      • A class can implement multiple interfaces

    Variable Types

    • NaturalNumber

      • Reference type that points to an object
      • Must always be positive
      • Supports unlimited upper bounds and uses integer math
    • Mutable Reference: Example includes NaturalNumber and arrays. Their values can be changed.

    • Immutable Reference: Example includes strings. Their values cannot be altered within methods.

    Method Parameter Modes

    • Restores: Returns input variable to its original state after method execution.
    • Updates: Alters the value of the input variable based on its initial state.
    • Clears: Resets the value of the input variable to a default (usually zero).
    • Replaces: Overrides the original variable value without regard for its previous state.

    Method Characteristics

    • Overloading: Same method name with different parameter types or counts within a class.
    • Static Methods: Can be invoked without a class instance; do not use this.
    • Instance Methods: Require an object instance; utilize this to refer to the current object.

    Testing Practices

    • Test Cases: Should include routine, challenging, and boundary cases, all satisfying preconditions.
    • Unit Testing: Focuses on individual components.
    • Integration Testing: Examines combined components for correctness.
    • System Testing: Evaluates the complete system from an end-user perspective.
    • Failed Test Cases: May suggest a bug in code, a faulty test case, or broken preconditions.

    Recursive Methods

    • Infinite Recursion: Leads to a stack overflow error.
    • Confidence Building Argument: Analogous to mathematical proof by induction.
    • Freelunch Principle: Requires recursive calls with smaller problem instances.

    Additional Concepts

    • TransferFrom Method: Copies value from one variable and clears the source.
    • String Permutation: Involves reordering elements of a string.
    • Substring Function: Extracts part of a string from start index to end index minus one.

    Aliasing and Reference Types

    • Immutable Types: Aliasing doesn't pose risks; any modification results in a new reference.
    • Reference Type: Consists of a memory address pointing to an object; changing the reference updates the address, not the object.

    Miscellaneous

    • Default Parameter Mode: Restores to default state when not explicitly defined.
    • Junit: A testing framework that identifies defects but does not confirm overall correctness.

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz explores concepts related to declared types, object types, and method overriding in programming. Additionally, it covers relationships between classes and interfaces, including the use of annotation in method overriding. Test your understanding of these fundamental programming principles.

    Use Quizgecko on...
    Browser
    Browser