C++ Functions

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Raphord
R
Raphord
Community Contributor
Quizzes Created: 2 | Total Attempts: 12,294
| Attempts: 5,039 | Questions: 19
Please wait...
Question 1 / 19
0 %
0/100
Score 0/100
1. 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);
}

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.

Submit
Please wait...
About This Quiz
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... see morea different way of learning the terms so that you can read the questions that you will have on the test and final exam. see less

2. 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";
}

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

Submit
3. 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";
}

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

Submit
4. 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;
}

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.

Submit
5. 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?

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.

Submit
6. In the following declaration, what is the return type?

int myMethod(int count, double value)
{
    return 4;
}

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.

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

Explanation

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

Submit
8. What is the value that is returned in the following function?

int getVolume() {
   int v;

   v = 23;
   if (v < 50) {
      return 50;
   }
   return v;
}

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.

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

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.

Submit
10. 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
}

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.

Submit
11. The following code in the main is valid:


void createGameCharacter() {
}

int main(int argc, char *argv[]) {
    int value;
    value = createGameCharacter();
    // rest of program
}

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.

Submit
12. 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"; }

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

Submit
13. 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";     }

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

Submit
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;
}

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.

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

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.

Submit
16. 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;
}

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.

Submit
17. 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);
}

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

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

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.

Submit
19. 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 integer
2. the studentName which must be a String
3. the testScore which must be a double.

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.

Submit
View My Results

Quiz Review Timeline (Updated): Aug 10, 2024 +

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
Cancel
  • All
    All (19)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
When the changeChannel() method is called below, what is the value of...
What is the output of this main() method?  Put the letters into...
What is the output of this program?  Put the letters into the...
What word is missing below (fill in the __________).   Write...
In C++, what is the correct syntax for declaring and defining a...
In the following declaration, what is the return type?int myMethod(int...
In the following functiondeclaration, how many formal parameters are...
What is the value that is returned in the following function?int...
In the following function declaration, what is the name of the...
The following code in the main is valid:int depositMoney()...
The following code in the main is valid:void createGameCharacter()...
What is the output of this main() method?  Put the letters into...
What is the output of this main() method?  Put the letters into...
What is the output of this main() method?  Put the letters into...
In the following function how many values are returned? ...
What is the output of this main() method?  Put the letters into...
What is the output of this main() method?  Put the letters into...
In C++, a function that does not return any value is declared with the...
Write a function declaration for the following description (just the...
Alert!

Advertisement