From freshersonline.com
Interview Question Home
1.Remove all the unnecessary tests from the nested conditional statement below.
float income;
cout << "Enter your monthly income: ";
cin >> income;
if (income < 0.0)
cout << "You are going farther into debt every month." << endl;
else if (income >= 0.0 && income < 1200.00)
cout << "You are living below the poverty line." << endl;
else if (income >= 1200.00 && income < 2500.00)
cout << "You are living in moderate comfort." << endl;
else if (income >= 2500.00)
cout << "You are well off." << endl;
Ans:The conditional statement should be modified as follows:
if (income < 0.0)
cout << "You are going farther into debt every month." << endl;
else if (income < 1200.00)
cout << "You are living below the poverty line." << endl;
else if (income < 2500.00)
cout << "You are living in moderate comfort." << endl;
else
cout << "You are well off." << endl;
2.Answer the questions below concerning the following fragment of code.
int n;
cout << "Enter an integer: ";
cin >> n;
if (n < 10)
cout << "less than 10" << endl;
else if (n > 5)
cout << "greater than 5" << endl;
else
cout << "not interesting" << endl;
a. What will be the output of the fragment above if the interactive user enters the integer value 0 ?
b. What will be the output of the fragment above if the interactive user enters the integer value 15 ?
c. What will be the output of the fragment above if the interactive user enters the integer value 7 ?
d. What values for n will cause the output of the fragment above to be "not interesting"?
Ans:
a. The output will be "less than 10".
b. The output will be "greater than 15".
c. The output will be "less than 10".
d. There is no value for n that will cause the output to be "not interesting". That part of the code
can never be executed!
3.Rewrite the following code fragment so that it uses a "do...while..." loop to accomplish the same
task.
int n;
cout << "Enter a non-negative integer: ";
cin >> n;
while (n < 0)
{
cout << "The integer you entered is negative." << endl;
cout << "Enter a non-negative integer: ";
cin >> n;
}
Ans:
Using do...while... in this situation makes the code a little simpler.
int n;
do
{
cout << "Enter a non-negative integer: ";
cin >> n;
if (n < 0)
cout << "The integer you entered is negative." << endl;
}
while (n < 0);
4.In the code fragment below, the programmer has almost certainly made an error in the first line of the
conditional statement.
a. What is the output of this code fragment as it is written?
b. How can it be corrected to do what is the programmer surely intended?
int n = 5;
if (n = 0) // NOTE THE OPERATOR!!!
cout << "n is zero" << ".\n";
else
cout << "n is not zero" << ".\n";
cout << "The square of n is " << n * n << ".\n";
Ans:
a. The output of the code fragment as it is written will be
n is not zero.
The square of n is 0.
The reason for this is that the "if" part assigns the value 0 to n , and the value returned by that assignment
statement is 0 , so this is treated as "false", which causes the alternative statement to be executed. But even
though the program prints "n is not zero.", in fact, n does have the value zero after the conditional
statement is finished.
b. The correction consists simply of replacing "n = 0" by "n == 0".
5.What is the output when the following code fragment is executed?
int n, k = 5;
n = (100 % k ? k + 1 : k - 1);
cout << "n = " << n << " k = " << k << endl;
18. What is the output when the following code fragment is executed?
int n;
float x = 3.8;
n = int(x);
cout << "n = " << n << endl;
Ans:The output of the code fragment is as follows:
n = 4 k = 5