All of these questions are for C++ 1. What is the output of the following C++ pr
ID: 3761781 • Letter: A
Question
All of these questions are for C++
1. What is the output of the following C++ program? (Note that the function sqrt returns the square root of
its argument. For example, sqrt(16.0) = 4.0. The specification of the function sqrt is in the header file math.h)
#include
#include
int main()
{
int counter;
for(counter = 1; counter <= 100; counter++)
if(pow(floor(sqrt(counter)),2) == counter)
cout<
cout<
return 0;
}
2. Which of the following function headings are valid? If they are invalid, explain why.
one(int a, int b)
int thisone(char x)
char another(int a, b)
double yetanother
3. Consider the following statements :
double num1, num2, num3;
int int1, int2, int3;
int value;
num1 = 5.0; num2 = 6.0; num3 = 3.0;
int1 = 4; int2 = 7; int3 = 8;
and the function prototype
double cube(double a, double b, double c);
Which of the following statements are valid? If they are invalid, explain why.
value = cube (num1, 15.0, num3);
cout<
cout<
cout<
cout<
value = cube(num1, int2, num3);
value = cube(7, 8, 9);
4. Consider the following functions:
int secret(int x)
{
int i, j;
i = 2 * x;
if (i > 10)
j = x / 2;
else
j = x / 3;
return j-1;
}
int another(int a, int b)
{
int i , j;
j = 0;
for(i = a; i <= b; i++)
j = j + i;
return j;
}
What is the output of each of the following program segments?
x = 10;
cout<
x = 5; y = 8;
cout<
x = 10; k = secret(x);
cout<
x = 5; y = 8;
cout<
5. Consider the following function prototypes:
int test(int, char, double, int);
double two(double, double);
char three(int, int, char, double);
Answer the following questions.
How many parameters does the function test have?
What is the type of the function test?
How many parameters does function two have?
What is the type of function two?
How many parameters does function three have?
What is the type of function three ?
How many actual parameters are needed to call the function test?
What is the type of each parameter, and in what order should you use these parameters in a
call to the function test?
Write a C++ statement that prints the value returned by the function test with the actual parameters 5, 5, 7.3, and 'z'.
Write a C++ statement that prints the value returned by function two with the actual parameters 17.5 and 18.3, respectively.
Write a C++ statement that prints the next character returned by function three. (Use your own actual parameters.)
6. Consider the following function:
int mystery(int x, double y, char ch)
{
int u;
if('A' <= ch && ch <= 'R')
return(2 * x + static_cast<int>(y));
else
return(static_cast<int>(2*y)-x);
}
What is the output of the following C++ statements?
cout<
cout<
cout<<2*mystery(6,3.9,'D')<
7. Consider the following function:
int secret(int one)
{
int I;
int prod = 1;
for(I = 1; I <= 3; I++)
prod = prod * one;
return prod;
}
What is the output of the following C++ statements?
cout<
cout<<2 * secret(6)<
What does the function secret do?
8. Show the output of the following program.
#include
int mystry(int);
int main()
{
int n;
for(n = 1; n <= 5; n++)
cout<
return 0;
}
int mystry(int k)
{
int x, y;
y = k;
for(x = 1; x <= (k - 1); x++)
y = y * (k - x);
return y;
}
9. Show the output of the following program.
#include
bool strange(int);
int main()
{
int num = 0;
while(num <= 29)
{
if(strange(num))
cout<<"True"<
else
cout<<"False"<
num = num + 4;
}
return 0;
}
bool strange(int n)
{
if(n % 2 == 0 && n % 3 == 0)
return true;
else
return false;
}
Explanation / Answer
1. Displays value where square root to the power 2 is equal to that number
Output Perfect squares
1,4,9,16,25,36,49,64,81,100
2.
a) not valid because return type is not specified
b) valid
c) valid
d) invalid, arguments are not specified
3.
function prototype cannot be declared inside another function
all are valid except last one
4.
10
5 8
10 2
5 8