Method Declarations - Computer Science

Card 0 of 6

Question

#include <iostream>

#include <string>

using namespace std;

``

int main() {

string name;

cout << "Tell me your name: "<<endl;

getline(cin, name);

sayHello();

``

return 0;

}

``

void sayHello(string nameToGreet){

cout << "Hello, " << nameToGreet << "!"<< endl;

}

Why would the above program fail?

Answer

The compiler will read the program from top to bottom, and by the time it gets to the function call for sayHello in main, it will have no clue what sayHello is. Therefore an exception will be raised. To circumvent this issue, define sayHello before main, OR provide a function prototype before main to make the compiler aware of the appropriate signature for the sayHello function.

Compare your answer with the correct one above

Question

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

Answer

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.

Compare your answer with the correct one above

Question

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?

Answer

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

Compare your answer with the correct one above

Question

Fill in the blank.

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

Answer

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.

Compare your answer with the correct one above

Question

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

Answer

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

Compare your answer with the correct one above

Question

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

Answer

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.

Compare your answer with the correct one above

Tap the card to reveal the answer