AP CSA 1.9 — Method Signatures: Homework

Complete all parts. Submit this executed notebook on GitHub Pages and answer MCQs on the Google Form.

MCQs

1) Which pair is a valid overload?

  • A) int f(int a) and int f(int x)
  • B) int f(int a) and double f(int a)
  • C) int f(int a) and double f(double a)
  • D) int f(int a, int b) and int f(int a, int b)

2) Which is the AP CSA notion of a method signature?

  • A) public static int max(int a, int b)
  • B) max(int, int)
  • C) max(int a, int b) throws Exception
  • D) max(int, double)

3) With the overloads void p(int x) and void p(double x), what prints?

p(5);
p(5.0);
  • A) int, double
  • B) double, double
  • C) int, int
  • D) Compile error

4) Which call is ambiguous with only these two methods?

void h(long x) {}
void h(float x) {}
  • A) h(5)
  • B) h(5.0)
  • C) h(5f)
  • D) None are ambiguous

5) Which statement is TRUE?

  • A) Parameter names affect the method signature.
  • B) Return type affects the method signature.
  • C) Access modifiers affect the method signature.
  • D) Overloading requires different parameter lists.

Short answer

  • Explain why int sum(int a, int b) and double sum(int a, int b) cannot both exist.
  • In one sentence, distinguish parameters vs arguments.

Coding Tasks (write Java in code blocks; pseudo-Java acceptable)

1) Write three overloads of abs: int abs(int x), double abs(double x), and long abs(long x). 2) Implement two overloads of concat:

  • String concat(String a, String b) returns concatenation.
  • String concat(String a, int n) returns a repeated n times. 3) Determine output: ```java static void show(int x) { System.out.println(“int”); } static void show(double x) { System.out.println(“double”); } static void show(long x) { System.out.println(“long”); }

show(7); show(7L); show(7.0); ``` Write the expected output and a one-line explanation for each.

FRQ-style

  • FRQ 1. indexOf(char target, String s): return the index of first target in s or -1 if not found. Overload with indexOf(String target, String s) for first substring occurrence (without using library indexOf). State assumptions and constraints.
  • FRQ 2. Overload clamp:
    • int clamp(int value, int low, int high) returns value confined to [low, high].
    • double clamp(double value, double low, double high) similarly for doubles. Handle the case low > high by swapping.

Submission checklist

  • MCQs completed on the Google Form.
  • This notebook executes top-to-bottom with outputs visible.
  • Answers are clear and concise.