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 #22 : 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, a 2 applied to abc should become cde.)

Possible Answers:

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 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 += (char)(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 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 #23 : Program Implementation

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:

8

1

6

4

3

Correct answer:

4

Explanation:

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

Example Question #23 : Programming Constructs

Fill in the blank.

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

Possible Answers:

int

constructor

empty

double

void

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 writeToConsole(boolean b, string s)

public int writeToConsole(boolean b, string s) 

public int writeToConsole()

public void 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 #1 : Method Declarations

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:

False

True

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