What this quiz covers
This quiz focuses on Method Signatures, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Consider a method with the signature public static double calculateArea(int length, int width, boolean includeMargin). A programmer wants to call this method but accidentally writes calculateArea(5.0, 3.0, true). What will happen when this code is compiled?
AP Computer Science a Quiz
Practice Method Signatures in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Method Signatures, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Consider a method with the signature public static double calculateArea(int length, int width, boolean includeMargin). A programmer wants to call this method but accidentally writes calculateArea(5.0, 3.0, true). What will happen when this code is compiled?
Explanation: The code will not compile because Java does not allow implicit narrowing conversions from double to int. While widening conversions (int to double) are automatic, narrowing conversions require explicit casting. The method signature specifies int parameters, but the call provides double arguments, resulting in a compilation error. Choice A is incorrect because automatic conversion only works for widening, not narrowing. Choice C is wrong because this is a compile-time issue, not runtime. Choice D is incorrect because Java does not automatically truncate without explicit casting.
Consider these method signatures in the same class: public void update(Number n), public void update(Integer i), and public void update(Object o). When the statement update(new Integer(5)) is executed, which method will be called?
Explanation: Java's method resolution follows the principle of selecting the most specific applicable method. Since Integer is passed as an argument, the compiler will choose the method that most specifically matches: update(Integer i). While Integer is also compatible with Number and Object parameters, the compiler prioritizes exact type matches over inheritance relationships. This ensures the most precise method is called. Choices A and B are incorrect because more general types are not preferred when a specific match exists. Choice D is wrong because there's no ambiguity - the most specific method is clearly determined.
Given the method signature public static <T extends Number> T findMax(T[] array), which of the following method calls will result in a compilation error?
Explanation: The generic method signature specifies that T must extend Number, which means only arrays of Number subclasses are acceptable. String does not extend Number, so passing a String array violates the type bound constraint and results in a compilation error. Integer, Double, and Float are all subclasses of Number, making choices A, B, and D valid calls. The generic type system enforces these constraints at compile time to ensure type safety.
A method signature is written as public static void process(final int[] data, int start, int end). A programmer calls this method with process(arr, 0, arr.length) where arr is an int array of length 5. Inside the method, what operations are permitted on the data parameter?
Explanation: The final keyword on a parameter means the parameter reference cannot be reassigned within the method, but it doesn't make the object immutable. Since arrays are mutable objects, the elements can still be modified through the reference. The final keyword only prevents reassigning data to point to a different array. Choice B is incorrect because final doesn't affect the mutability of the array's contents. Choice C reverses the actual behavior. Choice D is wrong because final prevents reference reassignment.
A programmer defines a method with signature public String format(Object obj, String pattern, Object... extras). Later, they attempt to call this method with format("hello", "pattern"). What is the result of this method call?
Explanation: The method call is valid and will execute successfully. Varargs parameters are optional, so when no arguments are provided for the varargs portion, Java creates an empty array. The call provides the required Object and String parameters, and the varargs parameter 'extras' becomes an empty Object array. Choice A is incorrect because the array is empty, not containing an empty String. Choice C is wrong because varargs parameters don't require any arguments. Choice D is incorrect because String is a subclass of Object and can be passed where Object is expected.
Consider these overloaded method signatures in the same class: public void method(double d, int i), public void method(int i, double d), and public void method(double d1, double d2). When the statement method(5, 3) is executed, which method will be called?
Explanation: This method call creates an ambiguous situation because both method(double d, int i) and method(int i, double d) require exactly one widening conversion from int to double. The compiler cannot determine which conversion is preferred since both require the same level of type promotion. Method resolution fails when multiple methods are equally applicable with the same conversion requirements. Choice A and B are incorrect because neither method is more specific than the other. Choice C is wrong because method(double d1, double d2) requires two conversions, making it less preferable than the others if they weren't ambiguous.
Consider a class with these method signatures: public void print(int x) and public void print(Integer x). When the code Integer num = null; print(num); is executed, what will happen?
Explanation: When you encounter method overloading with both primitive and wrapper types, the key concept being tested is Java's method resolution rules and how the compiler chooses between overloaded methods.
In this scenario, you have Integer num = null; being passed to print(num). Since num is declared as type Integer (the wrapper class), the compiler will choose the most specific match: print(Integer x). Java's method resolution prioritizes exact type matches over conversions, so the Integer parameter matches directly with the Integer method signature without requiring any unboxing conversion.
The correct answer is A because the print(Integer x) method will be called, and passing null as a parameter to a method expecting an object reference is perfectly legal in Java. The method can execute successfully with a null parameter - what happens inside the method depends on how it handles the null value, but the call itself won't fail.
Answer B is incorrect because the print(int x) method won't be called at all - the compiler chooses the Integer version first. Answer C is wrong because there's no ambiguity; the compiler has clear rules favoring exact type matches over conversions. Answer D is also incorrect since the primitive method isn't called, and even if it were, unboxing a null Integer would throw a NullPointerException, not print 0.
Study tip: Remember that Java's method resolution follows a hierarchy: exact matches beat conversions. When you see wrapper types in overloaded methods, the wrapper version will be chosen over primitive conversions.
In BankAccount, what is the return type of deposit(double amount), which increases the account balance?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. In the provided BankAccount class, the method deposit(double amount) increases the account balance. Choice A is correct because deposit methods typically return void, as they perform an action (updating the balance) rather than calculating and returning a value. Choice C is incorrect because returning double would suggest the method calculates something to return, which is not the primary purpose of a deposit operation. To help students: Emphasize that void methods perform actions without returning values. Practice identifying when methods should return values versus when they should be void.
In BankAccount, what is the return type of getBalance(), which reports the current account balance?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. In the provided BankAccount class, the method getBalance() is designed to report the current account balance. Choice B is correct because getBalance() returns double, which is appropriate for representing monetary values with decimal places. Choice C is incorrect because void would mean the method returns nothing, which wouldn't allow it to report the balance value. To help students: Emphasize the importance of choosing appropriate return types based on the method's purpose. Practice identifying return types by considering what data the method needs to provide.
In BankAccount, which parameter list would correctly overload withdraw(int dollars) without changing the method name?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, to overload withdraw(int dollars), we need a method with the same name but different parameters. Choice B is correct because withdraw(double amount) has the same method name but a different parameter type (double instead of int). Choice D is incorrect because 'withdrawal' is a different method name, which creates a new method rather than overloading. To help students: Emphasize that overloading requires keeping the same method name while changing parameters. Practice creating overloaded methods with different parameter types.
In BankAccount, which parameter list would correctly overload getBalance() while keeping the same method name?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, to overload getBalance(), we need to add parameters since the original has none. Choice A is correct because getBalance(double fee) adds a parameter, creating a valid overload. Choice C is incorrect because 'void' is not a valid parameter - it's a return type keyword that cannot be used as a parameter. To help students: Emphasize that overloading requires different parameter lists, not just adding keywords. Practice creating overloaded methods by adding meaningful parameters.
In BankAccount, in what scenario would you use withdraw(double amount) rather than withdraw(int dollars)?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, withdraw(double amount) accepts decimal values while withdraw(int dollars) accepts only whole numbers. Choice A is correct because withdraw(double amount) would be used when withdrawing an amount that includes cents (like $25.50). Choice D is incorrect because constructors have different purposes and naming conventions than regular methods. To help students: Emphasize choosing the appropriate overloaded method based on data precision needs. Practice scenarios where different parameter types serve different use cases.
In BankAccount, in what scenario would you use deposit(int dollars) instead of deposit(double amount)?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, deposit(int dollars) accepts whole dollar amounts while deposit(double amount) accepts amounts with cents. Choice A is correct because deposit(int dollars) would be used when depositing whole-dollar cash amounts without cents. Choice D is incorrect because overloading cannot be achieved by changing only the return type - the parameter lists must differ. To help students: Emphasize practical scenarios where different parameter types serve different purposes. Practice choosing the appropriate overloaded method based on the data being processed.
In BankAccount, how does deposit demonstrate overloading with deposit(int dollars) and deposit(double amount)?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, deposit demonstrates overloading with deposit(int dollars) and deposit(double amount) by using different parameter types. Choice B is correct because these methods share the same name 'deposit' but use different parameter types (int vs double). Choice A is incorrect because overloading cannot be achieved by changing only the return type - the parameter lists must differ. To help students: Emphasize that overloading is based on method name and parameter list combinations. Practice recognizing valid overloads by examining parameter differences.
In BankAccount, how does withdraw demonstrate overloading with withdraw(int dollars) and withdraw(double amount)?
Explanation: This question tests understanding of method signatures and overloading in Java, as covered in AP Computer Science A. Method signatures in Java define the method name, return type, and parameters. Overloading allows multiple methods with the same name but different parameter lists. In the provided BankAccount class, the withdraw method demonstrates overloading with withdraw(int dollars) and withdraw(double amount) by using different parameter types. Choice A is correct because these methods share the same name 'withdraw' but have different parameter types (int vs double). Choice B is incorrect because overloading cannot be achieved by changing only the return type - the parameter lists must differ. To help students: Emphasize that overloading is determined by method name and parameter list, not return type. Practice identifying overloaded methods by comparing their signatures.
A class contains these two method signatures: public void process(String data) and public void process(String[] data). When the statement process(null) is executed, what will happen?
Explanation: This creates an ambiguous method call because null can be assigned to both String and String[] references. The compiler cannot determine which overloaded method to invoke, resulting in a compilation error. Both method signatures are equally applicable to the null argument, creating ambiguity. Choice A is incorrect because there's no concept of null being 'more compatible' with single objects. Choice B is wrong because arrays don't have higher precedence in overload resolution. Choice D is incorrect because this is a compile-time ambiguity issue, not a runtime error.
A method is declared as protected final String getValue(List<String> items, int index) throws IndexOutOfBoundsException. Based on this signature, which statement about this method is correct?
Explanation: When analyzing method signatures in Java, you need to understand how access modifiers and method modifiers work together to control inheritance and method behavior.
The final keyword is the key here. When applied to a method, final prevents that method from being overridden in any subclass. This is a hard rule in Java - final methods cannot be changed by inheritance. However, final has no impact on overloading, which means creating multiple methods with the same name but different parameter lists within the same class.
The protected access modifier allows access from within the same package and by subclasses (even in different packages), but this doesn't affect the overriding restriction imposed by final.
Looking at the wrong answers: A is incorrect because final specifically prevents overriding - this is backwards. B misunderstands protected access; protected methods are accessible to subclasses regardless of package, not just to unrelated classes. C confuses the throws declaration with mandatory exception throwing - the throws IndexOutOfBoundsException simply declares that this exception might be thrown, but doesn't require it to always be thrown.
Answer D correctly identifies that final prevents overriding while allowing overloading within the same class.
Study tip: Remember that final on methods means "no overriding" but doesn't restrict overloading. The throws clause in a method signature declares possible exceptions but doesn't mandate they always occur. Focus on understanding each keyword's specific restrictions when analyzing method signatures.