-
Which of the following is a correct declaration for the C library
square root function?
- double sqrt(int x);
- double sqrt(float x);
- float sqrt(int);
- float sqrt(float);
- double sqrt(double);
next prev start
-
What is the return value and type if the C math library function,
pow is called with these arguments:
pow(2,3)
- 0 (int)
- 8 (int)
- 9 (int)
- 8.0 (double)
- 9.0 (double)
next prev start
-
The function main calls function
compoundInterest. Both functions are in the same
file. Which one of the following has a incorrect placement of the
declaration/definition of compoundInterest?
-
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 int main()
6 {
7 double amount;
8 int mnths;
9 double rate;
10
11 cout << "Enter the initial amount: ";
12 cin >> amount;
13 cout << "Enter the number of months: ";
14 cin >> mnths;
15 cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
16 cin >> rate;
17
18 cout << "In " << mnths << " months, the value will be $";
19 cout.setf(ios::fixed);
20 cout.setf(ios::showpoint);
21 cout.precision(2);
22 cout << compoundInterest(mnths, rate, amount) << endl;
23
24 return 0;
25 }
26
27 double compoundInterest(int months, double annualRate, double initialAmt)
28 {
29 double monthlyRate = annualRate / 1200.0;
30 double result;
31
32 result = pow(1.0 + monthlyRate, months) * initialAmt;
33
34 return result;
35 }
-
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 double compoundInterest(int months, double annualRate, double initialAmt)
6 {
7 double monthlyRate = annualRate / 1200.0;
8 double result;
9
10 result = pow(1.0 + monthlyRate, months) * initialAmt;
11
12 return result;
13 }
14
15 int main()
16 {
17 double amount;
18 int mnths;
19 double rate;
20
21 cout << "Enter the initial amount: ";
22 cin >> amount;
23 cout << "Enter the number of months: ";
24 cin >> mnths;
25 cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
26 cin >> rate;
27
28 cout << "In " << mnths << " months, the value will be $";
29 cout.setf(ios::fixed);
30 cout.setf(ios::showpoint);
31 cout.precision(2);
32 cout << compoundInterest(mnths, rate, amount) << endl;
33
34 return 0;
35 }
-
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 double compoundInterest(int, double, double);
6
7
8 int main()
9 {
10 double amount;
11 int mnths;
12 double rate;
13
14 cout << "Enter the initial amount: ";
15 cin >> amount;
16 cout << "Enter the number of months: ";
17 cin >> mnths;
18 cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
19 cin >> rate;
20
21 cout << "In " << mnths << " months, the value will be $";
22 cout.setf(ios::fixed);
23 cout.setf(ios::showpoint);
24 cout.precision(2);
25 cout << compoundInterest(mnths, rate, amount) << endl;
26
27 return 0;
28 }
29
30 double compoundInterest(int months, double annualRate, double initialAmt)
31 {
32 double monthlyRate = annualRate / 1200.0;
33 double result;
34
35 result = pow(1.0 + monthlyRate, months) * initialAmt;
36
37 return result;
38 }
-
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 int main()
6 {
7 double compoundInterest(int months, double annualRate, double initialAmt);
8 double amount;
9 int mnths;
10 double rate;
11
12 cout << "Enter the initial amount: ";
13 cin >> amount;
14 cout << "Enter the number of months: ";
15 cin >> mnths;
16 cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
17 cin >> rate;
18
19 cout << "In " << mnths << " months, the value will be $";
20 cout.setf(ios::fixed);
21 cout.setf(ios::showpoint);
22 cout.precision(2);
23 cout << compoundInterest(mnths, rate, amount) << endl;
24
25 return 0;
26 }
27
28 double compoundInterest(int months, double annualRate, double initialAmt)
29 {
30 double monthlyRate = annualRate / 1200.0;
31 double result;
32
33 result = pow(1.0 + monthlyRate, months) * initialAmt;
34
35 return result;
36 }
next prev start
-
What is the error in this function which is supposed to
calculate the average of the three float values passed in as
paramters?
1 float avg(float a, float b, float c)
2 {
3 return a + b + c/3;
4 }
-
The divisor should be 3.0 instead of 3.
- Line 3 should be: return(a+b+c)/3;
- Line 3 should be: return(a+b+c/3.0);
- The value to return in a return statement can only be a single
variable or a constant. It cannot be any other expression.
next prev start
-
Which of the following is not a correct
declaration of the function avg from the
previous problem?
- float avg(float a, float b, float c);
- float avg(float x, float y, float z);
- float avg(float& a, float& b, float& c);
- float avg(float, float, float);
next prev start
-
A function named sort3 has three double
parameters. If it is called with three variables:
sort3(a,b,c), then after the call, it is
required that a <= b and b <= c even if this was not the case before
the call.
Which one of the following is false?
- sort3 should use call by value and be declared like this: void sort3(double, double, double);
- sort3 should use call by reference and be declared like this: void sort3(double, double, double);
- sort3 should use call by value and be declared like this: void sort3(double&, double&, double&);
- sort3 should use call by reference and be declared like this: void sort3(double&, double&, double&);
next prev start
-
The function should return a value that is 1 more than the
value passed to it. However, it contains an error. What is the error?
1 int incr(int x)
2 {
3 int x;
4 x++;
5 return x;
6 }
7
8
- Line 1 should be int f(x)
- Line 3 should be deleted.
- Line 4 should be ++x
- The parameter should be passed by reference.
next prev start
-
The rand function returns a pseudo
random integer between 0 and RAND_MAX. Assume the value of RAND_MAX is
32767. Which one of the following is a possible output of the program below?
1 #include <iostream>
2 #include <cstdlib>
3 using namespace std;
4
5 int next(unsigned int n)
6 {
7 int r;
8 r = rand() % n;
9 return r;
10 }
11
12 int main()
13 {
14 for(int i = 0; i < 5; i++) {
15 cout << next(10) << " ";
16 }
17 cout << endl;
18 return 0;
19 }
- 8 8 3 5 10
- 10 10 10 10 10
- 9 4 7 5 3
- 32767 32767 32767 32767 32767
- 38 4753 7 550 6475
next prev start
- If the program in the previous problem is run several times,
- the output for any two runs will be the same.
- the output for two runs will most likely not be the same, but could
occasionally be the same.
- the output for any two runs will most likely be the same, but could
occasionally be different.
- the output for two runs will never be the same.
next prev start
- Line 14 has been added to the code below.
1 #include <iostream>
2 #include <cstdlib>
3 using namespace std;
4
5 int next(unsigned int n)
6 {
7 int r;
8 r = rand() % n;
9 return r;
10 }
11
12 int main()
13 {
14 srand(735);
15 for(int i = 0; i < 5; i++) {
16 cout << next(10) << " ";
17 }
18 cout << endl;
19 return 0;
20 }
If this modified program is run several times,
- the output for any two runs will be the same.
- the output for two runs will most likely not be the same, but could
occasionally be the same.
- the output for any two runs will most likely be the same, but could
occasionally be different.
- the output for two runs will never be the same.
next prev start
- Which is the correct statement about the following program?
1 #include <iostream>
2 using namespace std;
3
4 void f(int& x, int& y)
5 {
6 int tmp;
7 if ( x < y ) {
8 tmp = x;
9 x = y;
10 y = tmp;
11 }
12 }
13
14 int main()
15 {
16 int a = 10;
17 int b = 5;
18 f(b,a);
19 cout << "a = " << a << ", b = " << b << endl;
20 return 0;
21 }
- Compiler error, function f should have a return
statement.
- Compiler error, the header of function f should be void
f(int x, int y).
- The output is: a = 5, b = 10
- The output is: a = 10, b = 5
next prev start
-
What is the output of the following program?
1 #include <iostream>
2 using namespace std;
3
4 int f(int n, int k);
5
6 int main()
7 {
8 cout << f(8, 0) << endl;
9
10 return 0;
11 }
12
13 int f(int n, int k)
14 {
15 if ( n > 1 ) {
16 return f(n/2, k+1);
17 } else {
18 return k;
19 }
20 }
- 8
- 4
- 3
- 2
- 1
next prev start
-
What is the output of the following program?
1 #include <iostream>
2 using namespace std;
3
4 int f(int n);
5
6 int main()
7 {
8 cout << f(8) << endl;
9
10 return 0;
11 }
12
13 int f(int n)
14 {
15 int k = 0;
16 while(n > 1) {
17 n /= 2;
18 k++;
19 }
20 return k;
21 }
- 8
- 4
- 3
- 2
- 1
next prev start
-
A header file named myutils.h to declare a
pair of user defined functions is shown below. What should be changed?
1 #ifdef MYUTILS_H
2 #define MYUTILS_H
3
4 int next(int);
5 void init();
6
7 #endif
- Line 4 should provide a name for next's formal parameter.
- Line 5 is missing the parameter list for function init.
- Lines 1, 2, and 7 should each be terminated with a semicolon.
- Line 1 #ifdef should be
#ifndef.
- No changes are needed.
next prev start
-
What line causes a compiler error in the following
program?
1 #include <iostream>
2 using namespace std;
3
4 void swap(const int& x, const int& y);
5
6 int main()
7 {
8 int a = 5, b = 10;
9 swap(a,b);
10 cout << a << endl;
11 cout << b << endl;
12 return 0;
13 }
14 void swap(const int& x, const int& y)
15 {
16 int tmp = x;
17 x = y;
18 y = tmp;
19 }
- Lines 4 and 14
- Line 8
- Line 9
- Line 16
- Lines 17 and 18
next prev start
- For the C library function int abs(int); which
one of the following statements is correct?
-
The precondition is: The argument passed to
abs should be any int value.
The postcondition is: The return value will be a
non-negative int value which is equal to the
parameter value passed in if that is already non-negative or else the
negative of the parameter value.
-
The precondition is: The return value will be a
non-negative int value which is equal to the
parameter value passed in if that is already non-negative or else the
negative of the parameter value.
The postcondition is: The argument passed to
abs should be any int value.
-
The precondition is: The argument passed to
abs should be any int value except for
the most negative int value.
The postcondition is: The return value will be a
non-negative int value which is equal to the
parameter value passed in if that is already non-negative or else the
negative of the parameter value.
-
The precondition is: The return value will be a
non-negative int value which is equal to the
parameter value passed in if that is already non-negative or else the
negative of the parameter value.
The postcondition is: The argument passed to
abs should be any int value except for
the most negative int value.
next prev start