AP Computer Science A : Method Declarations

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #25 :Program Implementation

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, a2applied toabcshould becomecde.)

Possible Answers:

None of the others

public void myMethod(String s,int i) {

String r = "";

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

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

}

}

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);

}

返回r.length ();

}

public String myMethod(String s,int i) {

String r = "";

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

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

}

return r;

}

Correct answer:

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 becauses.charAt(j) + iis interpreted as a numeric value in this case. You will end up with a String that is comprised ofnumbers. However, if you cast it back to achartype, 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 fordoubleandint. 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 castdoublevalues intointvalues and vice-versa.

Example Question #1 :Method Declarations

1. class test

2. {

3. public:

4. test();

5. exams(int);

6. quiz(int);

7. private:

8. int grade;

9. }

Which line represents the constructor?

Possible Answers:

3

4

6

8

1

Correct answer:

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.

Possible Answers:

int

constructor

empty

void

double

Correct answer:

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 #1 :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.

Possible Answers:

public int writeToConsole()

public int writeToConsole(boolean b, string s)

public void writeToConsole(boolean b, string s)

public writeToConsole(boolean b, string s)

Correct answer:

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 #22 :Programming Constructs

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);

Possible Answers:

True

False

Correct answer:

False

Explanation:

The prompt specifies that the return type of the method is a boolean. The return type of the method in the code snippet is void. Therefore, it will not return anything. The two input types however are correct.

Learning Tools by Varsity Tutors