Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Questions 1-Consider the function shown below. What should be the last state

ID: 643583 • Letter: C

Question

C++ Questions

1-Consider the function shown below. What should be the last statement of this function?

int do_it(int x, int y)

{

   int val = 0;

   if (x < y)

   {

      val = x + 1;

   }

   else

{

      val = y - 1;

   }

   // the last statement goes here

}

a-An assignment statement

b-A return statement

c-An increment statement

d-A comparison statement

2-Which of the following statements about variables is true?

a)The same variable name can be used in two different functions.

b)The same variable name can be used for two different variables in a single function.

c)You should use global variables whenever possible.

d)A variable is visible from the point at which it is defined until the end of the program.

3-Which of the following is not legal in a function definition?

a)Parameter variable names

b)Parameter variable data types

c)One return value

d)Multiple return values

4)Which of the following is the correct first line of a function definition named calc_sum that accepts four int parameters and returns a double?

a)double calc_sum()

b)calc_sum(int a, int b, int c, int d)

c)double calc_sum(int a, int b, int c, int d)

d)int calc_sum(double a, double b, double c, double d)

5-Which of the following is the correct first line of a function definition named calc_average that accepts three int parameters and returns a double?

a)double calc_average()

b)calc_average(int a, int b, int c)

c)double calc_average(int a, int b, int c)

d)int calc_average(double a, double b, double c)

6)You need to write a function that calculates the volume for a shape, which depends on the shape's length, width, and height. What should be the inputs and their data types for this function?

a)double length, double height

b)double length

c)double length, double height, string depth

d)double width, double length, double height

7)In the following code snippet, what is the scope of variable b?

void func1()

{

   int i = 0;

   double b = 0;

}

void func2()

{

}

int main()

{

   func1();

   func2();

   return 0;

}

a)It can be used only in func1().

b)It can be used in user-defined functions, func1() and func2().

c)It can be used anywhere in this program.

d)It can be used in many programs.

8-Which of the following code snippets can be used for defining a function that does not return a value? The function should accept a string and then display the string followed by "And that's all folks!" on a separate line.

a) string display_message(string str)

{

   cout << str << endl;

   cout << "And that's all folks!" << endl;

}

b)void display_message(string str)

{

   cout << str << endl;

   cout << "And that's all folks!" << endl;

}

c)void display_message(string str)

{

   cout << str << endl;

   cout << "And that's all folks!" << endl;

   return str;

}

d)void display_message()

{

   cout << str << endl;

   cout << "And that's all folks!" << endl;

       }

9- What is the output of the following code snippet?

int pow(int base, int power)

{

   int result = 1;

   for (int i = 0; i < power; i++)

   {

      result = result * base;

   }

   return result;

}

int main()

{

   cout << pow(pow(2, 2), 2) << endl;

   return 0;

}

a)4

b)8

c)16

d)32

10- Suppose you need to write a function that calculates the volume of rectangular boxes. Which of the following is the best choice for the declaration of this function?

a)void volume(int a)

b)double volume(double w, double h, double l)

c)void volume(double w, double h, double l)

d)double volume(double w)

Explanation / Answer

1. A return statement, the function must return a value of type int

2.The same variable name can be used in two different functions. The scope of a variable is only limited within the parenthesis in which it is declared

3.Multiple return values. A function can have only one return type.

4.double calc_sum(int a, int b, int c, int d)

5.double calc_average(int a, int b, int c)

6.double width, double length, double height. The function need to have all the values of type double only to compute the volume.

7.It can be used only in func1(). As it is declared in the parenthesis of func1().

8.

b)void display_message(string str)

{

   cout << str << endl;

   cout << "And that's all folks!" << endl;

}

9. 16, pow(2,2)=4 pow(4,2)=16

10.double volume(double w, double h, double l), the function takes in length,breadth ,height and returns volume

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote