C++ Functions

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Raphord
R
Raphord
Community Contributor
Quizzes Created: 2 | Total Attempts: 11,944
Questions: 19 | Attempts: 4,941

SettingsSettingsSettings
C++ Functions - Quiz

In order to learn how to use and write functrions in C++ we must fist start by knowing some of the words.

This quiz provides you with a different way of learning the terms so that you can read the questions that you will have on the test and final exam.


Questions and Answers
  • 1. 

    In the following declaration, what is the return type?int myMethod(int count, double value) {    return 4;}

    • A.

      Int

    • B.

      MyMethod

    • C.

      4

    • D.

      Double

    • E.

      Count

    Correct Answer
    A. Int
    Explanation
    The return type of the given method is "int" because the method is declared with the "int" keyword before the method name, indicating that it will return an integer value. Additionally, the method body includes a return statement with the value 4, which is an integer.

    Rate this question:

  • 2. 

    In the following function declaration, what is the name of the method? void showMenu(string category) { }

    • A.

      Void

    • B.

      ShowMenu

    • C.

      String

    • D.

      Category

    Correct Answer
    B. ShowMenu
    Explanation
    The name of the method in the given function declaration is "showMenu". This is evident from the function declaration itself, where "showMenu" is followed by the parameter list in parentheses.

    Rate this question:

  • 3. 

    In the following functiondeclaration, how many formal parameters are there? double squareRoot(double value) {   return 2.3; }

    • A.

      Double

    • B.

      Value

    • C.

      1

    • D.

      2

    Correct Answer
    C. 1
    Explanation
    The correct answer is 1 because there is only one formal parameter in the function declaration, which is "double value".

    Rate this question:

  • 4. 

    In the following function how many values are returned? void syncPhone(int idNumber) { }

    • A.

      0

    • B.

      1

    • C.

      2

    Correct Answer
    A. 0
    Explanation
    The function syncPhone(int idNumber) is declared as void, which means it does not return any value. In C++, void is a special type that represents the absence of value or no value. So, this function does not return any values.

    Rate this question:

  • 5. 

    What word is missing below (fill in the __________).   Write a function declaration called increaseTemperature which takes 2 integers as a formal parameters.  The two parameters represent the upper and lower targets.  The function returns the different between the upper and lower targets as an integer._________  increaseTemperature(int upper, int lower) {    return upper - lower;}

    Correct Answer
    int
    Explanation
    The given correct answer is "int". In the given code snippet, "int" is used to specify the data type of the return value of the function increaseTemperature. It indicates that the function will return an integer value.

    Rate this question:

  • 6. 

    Write a function declaration for the following description (just the first line).  The wording must be exact and correct!The method must be called calculateFinalExam.  The method must return a double.  The method accepts 3 parameters:1. the studentId which must be an integer2. the studentName which must be a String3. the testScore which must be a double.

    Correct Answer
    double calculateFinalExam(int studentId, string studentName, double testScore)
    Explanation
    The given answer is the correct function declaration for the description provided. It declares a function named "calculateFinalExam" that takes in three parameters: "studentId" of type integer, "studentName" of type string, and "testScore" of type double. The function returns a double.

    Rate this question:

  • 7. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)void showMenu() {    cout << "Q";}void eraseFile() {   cout << "T";}void processRequest() {  cout << "2";} int main(int argc char *argv[]) {     cout << "A";     processRequest();     cout << "B";     cout << "C";     showMenu();     cout << "D";}

    Correct Answer
    A2BCQD
    Explanation
    The main() method starts by printing "A". Then it calls the processRequest() function, which prints "2". After that, it prints "B" and "C". Next, it calls the showMenu() function, which prints "Q". Finally, it prints "D". Therefore, the output of the main() method is "A2BCQD".

    Rate this question:

  • 8. 

    What is the output of this program?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)void showMenu() {    cout << "Q";}void eraseFile() {   cout << "T";}public static void processRequest() {  cout << "2";} int main(int argc, char *argv[]) {     cout << "A";     processRequest();     processRequest();     showMenu();     eraseFile();     cout << "B";}

    Correct Answer
    A22QTB
    Explanation
    The program starts by printing "A". Then, the function processRequest() is called twice, which prints "2" twice. After that, the function showMenu() is called, which prints "Q". Finally, the function eraseFile() is called, which prints "T". The program ends by printing "B". Therefore, the output of the program is "A22QTB".

    Rate this question:

  • 9. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG) int main(int argc, char *argv[]) {      showMenu();      eraseFile();      cout << "B";      showMenu(); } void showMenu() {     cout << "Q";     eraseFile(); } void eraseFile() {    cout << "T"; } void processRequest() {   cout << "2"; }

    Correct Answer
    QTBTQT
    Explanation
    main() starts and calls showMenu():
    showMenu() executes cout << "Q";, so "Q" is printed.
    showMenu() then calls eraseFile().
    eraseFile() executes cout << "T";, so "T" is printed.
    showMenu() completes, returning to main().
    main() then calls eraseFile():
    eraseFile() executes cout << "T";, so "T" is printed.
    main() then executes cout << "B";:
    "B" is printed.
    main() calls showMenu() again:
    showMenu() executes cout << "Q";, so "Q" is printed.
    showMenu() then calls eraseFile().
    eraseFile() executes cout << "T";, so "T" is printed.
    showMenu() completes, returning to main().

    Rate this question:

  • 10. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG) int getRemoteId() {    return 3; } int getMonitorCount() {   return 2; } int getStudentCount() {   return 6; } int main(int argc, char *argv[]) {     int value;     cout << "@";     v = getRemoteId();     cout << v;     cout << "E";     v = getStudentCount();     cout << v;     cout << "F";     v = getMonitorCount();     cout << "W";     }

    Correct Answer
    @3E6FW
    Explanation
    The main() method starts by printing "@" to the console. Then, it calls the getRemoteId() function and assigns the returned value (which is 3) to the variable "v". It prints the value of "v" (which is 3) to the console. Next, it prints "E" to the console. It then calls the getStudentCount() function and assigns the returned value (which is 6) to the variable "v". It prints the value of "v" (which is 6) to the console. After that, it prints "F" to the console. Finally, it calls the getMonitorCount() function and prints "W" to the console. Therefore, the output of the main() method is "@3E6FW".

    Rate this question:

  • 11. 

    The following code in the main is valid:void createGameCharacter() {}int main(int argc, char *argv[]) {    int value;    value = createGameCharacter();    // rest of program}

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given code is not valid because the function `createGameCharacter()` has a return type of `void`, which means it does not return any value. However, in the `main()` function, the variable `value` is assigned the return value of `createGameCharacter()`, which is not possible since the function does not return anything. Therefore, the code is invalid.

    Rate this question:

  • 12. 

    The following code in the main is valid:int depositMoney() {   return 4;}int main(int argc, char *argv[]) {    int value;    value = depositMoney(amount);    // rest of program}

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The code in the main is not valid because it is calling a function called "depositMoney" with a parameter "amount" that has not been declared or defined anywhere in the code. This will result in a compilation error.

    Rate this question:

  • 13. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)void loadSong(int id) {   cout << id;}int checkLength(int id) {  cout << id;  id = id + 4;  cout << id;  return 9;}int getStudentCount() {  return 6;}int main(int argc, char *argv[]) {    int value;    loadSong(5);    cout << "A";    value = checkLength(1);    cout << "B";    cout << value;    value = checkLength(4);}

    Correct Answer
    5A15B948
    Explanation
    The main() method starts by calling the loadSong() function with an argument of 5, which prints "5" to the console. Then, "A" is printed to the console. Next, the checkLength() function is called with an argument of 1. Inside the function, "1" is printed to the console, then the value of "id" is incremented by 4 and printed again as "5". The function then returns 9. Back in the main() method, "B" is printed to the console, followed by the value returned by the previous call to checkLength(), which is 9. Finally, the checkLength() function is called again with an argument of 4, which prints "4" and "8" to the console. Therefore, the output of the main() method is "5A15B948".

    Rate this question:

  • 14. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)void loadSong(int id) {   cout << id;}int checkLength(int id) {  cout << id;  id = id + 4;  cout << id;  return 9;}int getStudentCount() {  return 6;}int main(int argc, char *argv[]) {    int value;    int x;    x = 3;    cout << "A";    value = checkLength(x);    cout << "B";    cout << x;    cout << value;}

    Correct Answer
    A37B39
    Explanation
    The main() method initializes the variable x with the value 3. It then prints "A" to the console. The method checkLength(x) is called, which prints the value of x (3) and then adds 4 to it, resulting in 7. The method returns the value 9. The main() method then prints "B" to the console, followed by the value of x (which is still 3) and the value returned by checkLength(x) (which is 9). Therefore, the output of the main() method is A37B39.

    Rate this question:

  • 15. 

    What is the output of this main() method?  Put the letters into the answer box provided wih no spaces between (i.e. ABCEDFG)void showValues(int x, int y) {   cout << x;   cout << y;   x = 4;}int checkLength(int id) {  cout << id;  id = id + 4;  cout << id;  return 9;}int getStudentCount() {  return 6;}int main(int argc, char *argv[]) {    int x;    int y;    x = 1;    y =  2;    showValues(y, x);    cout << x;    cout << y;}

    Correct Answer
    2112
    Explanation
    The main() method starts by declaring two variables, x and y, and assigns them the values 1 and 2 respectively. Then, the showValues() function is called with the arguments y and x. Inside the showValues() function, the value of x is printed, which is 2, and then the value of y is printed, which is 1. After that, the value of x is changed to 4. Back in the main() method, the value of x is printed again, which is still 1, and then the value of y is printed again, which is still 2. Therefore, the output of the main() method is 2112.

    Rate this question:

  • 16. 

    When the changeChannel() method is called below, what is the value of the parameter number?void changeChannel(int number) {   // code is hidden} int main(int argc, char *argv[]) {    int ch;   ch = 240;   changeChannel(ch + 10);}

    Correct Answer
    250
    Explanation
    The value of the parameter number is 250 because in the main function, the variable ch is assigned the value of 240 and then passed to the changeChannel() method as ch + 10, which is 250.

    Rate this question:

  • 17. 

    In C++, a function that does not return any value is declared with the return type ______________.

    Correct Answer
    void
    Explanation
    In C++, a function that does not return any value is declared with the return type void. This keyword indicates that the function performs its operations without providing a result. When defining such a function, you use void in place of a data type, such as int or char, to signify that no value will be returned to the calling code after the function executes.

    Rate this question:

  • 18. 

    What is the value that is returned in the following function?int getVolume() {   int v;   v = 23;   if (v < 50) {      return 50;   }   return v;}

    • A.

      50

    • B.

      23

    • C.

      50 and 100

    • D.

      150

    Correct Answer
    A. 50
    Explanation
    The function getVolume() initializes a variable v to 23. It then checks if v is less than 50. Since 23 is indeed less than 50, the function returns 50. Therefore, the value that is returned in this function is 50.

    Rate this question:

  • 19. 

    In C++, what is the correct syntax for declaring and defining a function pointer that points to a function named "calculate" taking two integers as parameters and returning a double?

    • A.

      Double (*calculate)(int, int);

    • B.

      Int (*calculate)(double, double);

    • C.

      Void (*calculate)(int, int); 

    • D.

      Double (*calculate)(double, double);

    Correct Answer
    A. Double (*calculate)(int, int);
    Explanation
    In C++, declaring and defining a function pointer involves specifying the return type and parameter types of the function it points to, enclosed in parentheses. In this case, we want a function pointer "calculate" that points to a function taking two integers as parameters and returning a double, which is correctly represented by option A. This is a more advanced concept in C++ programming.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Aug 10, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 14, 2011
    Quiz Created by
    Raphord
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.