AP Computer Science a Quiz: Method Signatures
17 questions · exam conditions
0:00
Method SignaturesQuestion 1 of 17

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?

The code will compile successfully because double values can be automatically converted to int parameters
The code will not compile because the method expects int parameters but receives double arguments
The code will compile but will produce a runtime error when the method executes
The code will compile successfully because Java automatically truncates double values to integers
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Method Signatures

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.

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.

How to use this quiz

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.

All questions

Question 1

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?

  1. The code will compile successfully because double values can be automatically converted to int parameters
  2. The code will not compile because the method expects int parameters but receives double arguments (correct answer)
  3. The code will compile but will produce a runtime error when the method executes
  4. The code will compile successfully because Java automatically truncates double values to integers

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.

Question 2

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?

  1. The method with Object parameter because it's the most general and can accept any argument
  2. The method with Number parameter because Integer extends Number and this provides better type safety
  3. The method with Integer parameter because it provides the most specific match for the argument type (correct answer)
  4. A compilation error will occur because multiple methods can accept the Integer argument

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.

Question 3

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?

  1. findMax(new Integer[]{1, 2, 3})
  2. findMax(new Double[]{1.0, 2.0, 3.0})
  3. findMax(new String[]{"a", "b", "c"}) (correct answer)
  4. findMax(new Float[]{1.0f, 2.0f, 3.0f})

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.

Question 4

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?

  1. The elements of data can be modified, but data cannot be reassigned to point to a different array (correct answer)
  2. Neither the elements of data nor the reference itself can be modified due to the final keyword
  3. The data reference can be reassigned, but individual elements cannot be modified for safety reasons
  4. Both the data reference and its elements can be freely modified since arrays are mutable objects

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.

Question 5

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?

  1. The method executes successfully with extras containing one empty String element
  2. The method executes successfully with extras being an empty array of Objects (correct answer)
  3. A compilation error occurs because not enough arguments are provided for the varargs parameter
  4. A compilation error occurs because String cannot be passed as an Object parameter

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.

Question 6

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?

  1. The method with signature (double d, int i) because the first parameter matches exactly
  2. The method with signature (int i, double d) because the second parameter requires less conversion
  3. The method with signature (double d1, double d2) because it provides the most general parameter types
  4. A compilation error will occur because the call is ambiguous between multiple applicable methods (correct answer)

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.

Question 7

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?

  1. The print(Integer x) method will be called and will execute successfully with a null parameter (correct answer)
  2. The print(int x) method will be called and a NullPointerException will be thrown during unboxing
  3. A compilation error will occur because the compiler cannot choose between the two methods
  4. The print(int x) method will be called and will successfully print 0 as the default value

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.

Question 8

In BankAccount, what is the return type of deposit(double amount), which increases the account balance?

  1. void (correct answer)
  2. int
  3. double
  4. boolean

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.

Question 9

In BankAccount, what is the return type of getBalance(), which reports the current account balance?

  1. int
  2. double (correct answer)
  3. void
  4. String

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.

Question 10

In BankAccount, which parameter list would correctly overload withdraw(int dollars) without changing the method name?

  1. withdraw(int dollars)
  2. withdraw(double amount) (correct answer)
  3. withdraw()
  4. withdrawal(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, 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.

Question 11

In BankAccount, which parameter list would correctly overload getBalance() while keeping the same method name?

  1. getBalance(double fee) (correct answer)
  2. getBalance()
  3. getBalance(void)
  4. getbalance(int fee)

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.

Question 12

In BankAccount, in what scenario would you use withdraw(double amount) rather than withdraw(int dollars)?

  1. When withdrawing an amount with cents included. (correct answer)
  2. When you need to change the method name.
  3. When the return type must become int.
  4. When you want a constructor to run.

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.

Question 13

In BankAccount, in what scenario would you use deposit(int dollars) instead of deposit(double amount)?

  1. When depositing a whole-dollar cash amount. (correct answer)
  2. When you need the account number returned.
  3. When you must change the method's visibility.
  4. When you want to overload by return type.

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.

Question 14

In BankAccount, how does deposit demonstrate overloading with deposit(int dollars) and deposit(double amount)?

  1. They overload by changing only the return type.
  2. They share a name but use different parameter types. (correct answer)
  3. They overload because one method is static.
  4. They overload by placing methods in different classes.

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.

Question 15

In BankAccount, how does withdraw demonstrate overloading with withdraw(int dollars) and withdraw(double amount)?

  1. They share a name but change parameter types. (correct answer)
  2. They overload by changing only the return type.
  3. They overload because one is public and one is private.
  4. They overload by using different method names.

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.

Question 16

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?

  1. The method with String parameter will be called because null references are more compatible with single objects
  2. The method with String[] parameter will be called because arrays have higher precedence in overload resolution
  3. A compilation error will occur because the compiler cannot determine which method to call with null (correct answer)
  4. A runtime error will occur because null cannot be passed to either method without explicit casting

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.

Question 17

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?

  1. The method can be overridden in subclasses but cannot be overloaded in the same class
  2. The method can only be called by classes within the same package or unrelated classes
  3. The method must always throw an IndexOutOfBoundsException when called from outside the package
  4. The method cannot be overridden in subclasses but can be overloaded in the same class (correct answer)

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.