Calling Instance Methods - AP Computer Science A
- What keyword is used to refer to the current object in an instance method?
- this. References the current object instance within methods.
- What is an instance method in Java?
- A method that operates on an instance of a class. Operates on specific object instances and can access instance variables.
- What is the access level of a method if no modifier is provided?
- Package-private (default). No modifier means package-private visibility within same package.
- What does 'public' signify in a method declaration?
- The method is accessible from any other class. Highest visibility level allowing access from any class.
- What happens if a method's return type is void and it returns a value?
- A compile-time error occurs. Void methods cannot return values; causes compilation failure.
- How do you call a method 'update' on 'obj' with 'newValue'?
- obj.update(newValue);. Standard method call with single argument parameter.
- Identify the error: 'object.method();' when 'method' requires an argument.
- Provide an argument: 'object.method(arg);'. Missing argument causes compile error; provide required parameter.
- Can 'this' keyword be used in static methods?
- No, 'this' is not available in static methods. 'this' refers to instance; static methods have no instance.
- What does 'return' do in a method?
- Exits the method and optionally returns a value. Terminates method execution and sends result to caller.
- When is an instance method appropriate over a static method?
- When the method needs to access instance fields. Instance methods can access and modify object state.
- How do you call the method 'add' with integer arguments 4 and 5 on 'calc'?
- calc.add(4, 5);. Pass arguments in parentheses matching parameter types.
- Identify the error: 'obj.method(1, 2)' when 'method' is protected.
- Cannot access from outside package or subclass. Protected access requires package membership or inheritance relationship.
- What does 'protected' mean for a method's access level?
- Accessible within package and subclasses. Visible to package members and subclasses only.
- What does the return type 'void' signify in a method declaration?
- The method does not return a value. Method completes without returning any data to caller.
- What distinguishes an instance method from a static method?
- Instance methods require an object to be called; static do not. Static methods belong to class; instance methods belong to objects.
- What is an overloaded method?
- A method with the same name but different parameters. Same name but different parameter list creates overload.
- What must match when calling an instance method with arguments?
- The number, type, and order of parameters. Method signature must exactly match call arguments.
- How do you call method 'run' with 'speed' argument on object 'vehicle'?
- vehicle.run(speed);. Argument passes data to method parameter.
- What is required for method overloading?
- Different parameter lists. Overloading requires distinct parameter signatures for each version.
- What does overloading a method allow?
- Multiple methods with the same name but different parameters. Provides multiple method implementations for different parameter types.
- Can an instance method be called without an object in Java?
- No, it requires an object. Instance methods need object context to access instance data.
- How do you call a method 'print' with no arguments on 'printer'?
- printer.print();. Empty parentheses indicate no parameters required.
- What is the default return type if none is specified?
- There is no default; return type must be specified. Java requires explicit return type specification for all methods.
- How do you call a method 'display' of class 'Screen' on an instance 'screenObj'?
- screenObj.display();. Standard object.method() syntax for instance method calls.
- What is method chaining?
- Calling multiple methods on the same object in a single statement. Links method calls together for fluent programming style.
- How do you call method 'display' with 'message' on object 'screen'?
- screen.display(message);. Passes string argument to display method parameter.
- How do you call an instance method named 'calculate' on an object 'obj'?
- obj.calculate();. Use dot notation: objectName.methodName();
- What is the syntax to define an instance method in Java?
- accessModifier returnType methodName(parameters) { body }. Standard format for declaring methods with optional parameters.
- What happens if a method is called with incorrect argument types?
- A compile-time error occurs. Type mismatch prevents compilation; arguments must match parameters.
- What is the purpose of method overloading?
- To define methods with the same name but different parameters. Enables multiple method versions with different parameter signatures.
- Identify the error: 'obj.method(4, 5)' when 'method' takes no parameters.
- Remove the arguments: 'obj.method();'. Too many arguments causes compile error; remove extras.
- Identify the return type: 'public int getValue() { return x; }'
- int. Return type appears before method name in declaration.
- When is the 'this' keyword used?
- To refer to the current instance of a class. Disambiguates between instance variables and parameters with same name.
- How do you refer to the current class's method from another method?
- Use 'this.methodName()'. Optional 'this.' prefix clarifies current object method call.
- What is required to call an instance method?
- An instance of the class. Instance methods must be called on specific object instances.
- Why must a void method not have a return statement with a value?
- It causes a compile-time error. Void methods cannot return values; only execution control.
- What is the effect of calling a method with no return statement?
- If non-void, causes a compile-time error. Non-void methods must return value; missing return fails compilation.
- Identify the error in 'obj.calculate()' when 'calculate' is private.
- Cannot access private method from outside class. Private methods only accessible within same class.
- What is the role of parameters in an instance method?
- They allow input to be passed to the method. Parameters accept data from caller for method processing.
- What is the correct way to call a method 'compute' on instance 'obj'?
- obj.compute();. Standard dot notation for calling methods on objects.