# AP Computer Science A : Method Declarations

## Example Questions

### Example Question #1 : Method Declarations

Which of the following is a method that takes a string and an integer as parameters, using the integer to increase each character value in the string before returning the new value?  (For instance, a 2 applied to abc should become cde.)

public String myMethod(String s,int i) {

String r = "";

for(int j = 0; j < s.length(); j++) {

r += (char)(s.charAt(j) + i);

}

return r;

}

public int myMethod(String s,int i) {

String r = "";

for(int j = 0; j < s.length(); j++) {

r += (char)(s.charAt(j) + i);

}

return r.length();

}

public void myMethod(String s,int i) {

String r = "";

for(int j = 0; j < s.length(); j++) {

r += (char)(s.charAt(j) + i);

}

}

None of the others

public String myMethod(String s,int i) {

String r = "";

for(int j = 0; j < s.length(); j++) {

r += (s.charAt(j) + i);

}

return r;

}

public String myMethod(String s,int i) {

String r = "";

for(int j = 0; j < s.length(); j++) {

r += (char)(s.charAt(j) + i);

}

return r;

}

Explanation:

The first thing you need to look at is the definition of the function's return value and parameters.  The only options that will pass are the ones that have:

public static String myMethod(String s,int i)

(This is based on the definition: Return a string, take a string and an integer.)

Now, in our code, the key line is:

r += (char)(s.charAt(j) + i);

The wrong version has:

r += (s.charAt(j) + i);

This does not work because s.charAt(j) + i is interpreted as a numeric value in this case.  You will end up with a String that is comprised of numbers.  However, if you cast it back to a char type, you will get the character that is defined by that particular numeric value.  This is why the correct answer has:

r += (char)(s.charAt(j) + i);

Now, on the AP Computer Science A exam, you will need to use casts for double and int.  However, the writers of the test do say that it is helpful to understand casting in general.  Therefore, apply this problem's methodology to other programming problems where you would have to type cast double values into int values and vice-versa.

### Example Question #2 : Method Declarations

1. class test

2. {

3. public:

4. test();

5. exams(int);

6. quiz(int);

7. private:

9. }

Which line represents the constructor?

6

3

1

8

4

4

Explanation:

The constructor looks like a function declaration but always has the same name of the class.

### Example Question #1 : Method Declarations

Fill in the blank.

A ______ function does not specify a return type, but may have any number of inputs.

double

empty

void

constructor

int

void

Explanation:

Remember, a void function does not need a return type. The void keyword tells the program not to expect a return statement. A void function does not necessarily mean that the inputs are void as well. A void function can still have any number of inputs of any type.

### Example Question #2 : Method Declarations

Write the stub for a public method named writeToConsole that takes in a boolean b and a string s that returns an integer.

public writeToConsole(boolean b, string s)

public int writeToConsole()

public void writeToConsole(boolean b, string s)

public int writeToConsole(boolean b, string s)

public int writeToConsole(boolean b, string s)

Explanation:

Public methods are prefixed with the word public. The second word is the return type (i.e. int). The third is the name of the method and inside the parentheses goes the inputs to the method (i.e. boolean b and string s).

### Example Question #24 : Program Implementation

True or False.

This code snippet defines a method that takes in two strings as input and will return a boolean as a result.

public void returnBoolean (String input1, String input2);

True

False