C plus plus Interview Questions with Answers Page I


From freshersonline.com

Jump to: navigation, search

Interview Question Home

The problems below are not intended to teach you how to program in C++. You should not attempt
them until you believe you have mastered all the topics on the "Checklist" in the document entitled "Computer
Science C++ Exam".
The solutions for the problems are given 



1. What is the output of the program below?
#include <iostream.h>
main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}

Ans:
The output would be as shown below. The program contains an endless loop.
9
4
1
0
-1
0
1
2
3
4
4
2
1
0
0 [endlessly]



2.What is the output of the program below?
#include <iostream.h>
main()
{
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;
return 0;
}

Ans:
The output would be as shown below.
4
1
1
0
0
1
0
0
1
1




3.What is the output of the following program?
#include <iostream.h>
main()
{
enum color_type {red, orange, yellow, green, blue, violet};
color_type shirt, pants;
shirt = red;
pants = blue;
cout << shirt << " " << pants << endl;
return 0;
}

Ans:The output would be as shown below.
0 4



4.What is the output when the following code fragment is executed?
int i = 5, j = 6, k = 7, n = 3;
cout << i + j * k - k % n << endl;
cout << i / n << endl;

Ans:The output would be as shown below.
46
1



5.What is the output when the following code fragment is executed?
int found = 0, count = 5;
if (!found || --count == 0)
cout << "danger" << endl;
cout << "count = " << count << endl;

Ans:The output would be as shown below, because when it is discovered that "!found" is true, the second
half of the OR expression is not evaluated, and thus count is not decremented.
danger
count = 5



6.What is the output when the following code fragment is executed?
char ch;
char title[] = "Titanic";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl;

Ans:The output would be as shown below. The 'a' in "Titanic" has been changed to an 'i'.
Titinic
i



7.Suppose that the following code fragment is executed.
const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the line below." << endl;
cin >> message;
cout << message << endl;
Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please go away.
What will the output of the code fragment look like?

Ans:The output would be as shown below. The line in italics would be typed by the interactive user.
Enter a sentence on the line below.
Please go away.
Please
The reason that only the word "Please" is picked up in the message array is that the extraction operator >>
ignores leading white space characters and then reads non-white space characters up to the next white space
character. That is, >> is "word oriented", not line oriented.



8.Suppose that the following code fragment is executed.
const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the line below." << endl;
cin.getline(message, LENGTH, '\n');
cout << message << endl;
a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please go away.
What will the output of the code fragment look like?
b. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please stop bothering me.
What will the output of the code fragment look like?

Ans:
a. The output would be as shown below. The line in italics would be typed by the interactive user.
Enter a sentence on the line below.
Please go away.
Please go away.

b.The output would be as shown below. The line in italics would be typed by the interactive user.
Enter a sentence on the line below.
Please stop bothering me.
Please stop botherin
The reason that the entire line typed by the user is not picked up in the message array is that the
getline instruction will read at most 20 characters from the keyboard.



9.The nested conditional statement shown below has been written by an inexperienced C/C++ programmer.
The behavior of the statement is not correctly represented by the formatting.
if (n < 10)
if (n > 0)
cout << "The number is positive." << endl;
else
cout << "The number is ______________." << endl;
a. What is the output of the statement if the variable n has the value 7 ? If n has the value 15 ?
If n has the value -3 ?
b. Correct the syntax of the statement so that the logic of the corrected statement corresponds to the
formatting of the original statement. Also, replace the blank with an appropriate word or phrase.
c. Correct the formatting of the (original) statement so that the new format reflects the logical behavior of the
original statement. Also, replace the blank with an appropriate word or phrase.

Ans: a. The original statement is formatted in such a way that it appears that the "else" clause of the statement
is the alternative to the "if (n < 10)" case, but in fact, the C or C++ compiler will treat the "else"
clause as the alternative to the "if (n > 0)" clause. If n has the value 7 , the output will be "The
number is positive". If n has the value 15 , then there will be no output. If n has the value -3
then the output will be "The number is ____________".
b. If we want the statement to behave according the logic that's suggested by the formatting of the
original statement, we can write
if (n < 10)
{
if (n > 0)
cout << "The number is positive." << endl;
}
else
cout << "The number is greater than 10." << endl;
c. If we want the statement to be formatted so as to reflect the actual logic of the original statement, we
can write
if (n < 10)
if (n > 0)
cout << "The number is positive." << endl;
else
cout << "The number is negative." << endl;



10.The loop shown below has been written by an inexperienced C/C++ programmer. The behavior of the
loop is not correctly represented by the formatting.
int n = 10;
while (n > 0)
n /= 2;
cout << n * n << endl;
a. What is the output of the loop as it is written?
b. Correct the syntax of the loop so that the logic of the corrected loop corresponds to the formatting of the
original loop. What is the output of the corrected loop?
c. Correct the formatting of the (original) loop so that the new format reflects the logical behavior of the original
loop.

Ans:
a. Since there are no braces surrounding the last two lines of the code, the compiler treats only the
statement "n /= 2;" as the body of the loop. Thus n will successively assume the values 5, 2, 1, and 0 ,
at which point the loop will exit and the "cout" statement will print 0 . The values 5, 2, and 1 will not be
printed.

b. If we want the statement to behave according the logic that's suggested by the formatting of the
original statement, we can put braces around the last two lines of code to make a compound statement as the
body of the loop:
int n = 10;
while (n > 0)
{
n /= 2;
cout << n * n << endl;
}
In this case the output of the loop will be
25
4
1
0
c. If we want the statement to be formatted so as to reflect the actual logic of the original statement, we
can write
int n = 10;
while (n > 0)
n /= 2;
cout << n * n << endl;

Personal tools