C++ questions for Computer Science and MCA students

What are escape sequence characters? Explain with example.

Escape sequences are used in almost all programming languages such as C, C++, Java, and C#. An escape sequence always begins with backslash and is followed by a character.

For example: "\n" is an escape sequence that denotes a newline character.
Sr. No.Escape SequencesMeaning
1'\a'Bell (alert)
2'\n'New line
3'\t'Horizontal tab
4'\r'Carrige return
5'\b'Backspace
6'\v'Vertical tab

Example
void main()
{
       cout<<"Welcome at \n careerRide"<<endl;
       cout<<"Horijontal tab"<<endl;
       cout<<"Welcome at \t careerRide"<<endl;
       cout<<"Carriage return"<<endl;
       cout<<"Welcome at \r careerRide"<<endl;
       cout<<"Backspace"<<endl;
       cout<<"Welcome at \b careerRide"<<endl;
       cout<<"Vertical tab"<<endl;
       cout<<"Welcome at \v careerRide"<<endl;
}

Describe enum with example?

Enum is a list of named integer constant. The value of first enumeration constant, by default starts with 0. You can change this value according to program need.

The standard way of declaring an enum in C++ is like given below:
enum <identifier> { <list_of_elements> };
enum Colors{Red, Green, Blue};

In this example by default enum list value are as follows:
Red=0, Green=1, Blue=2, but you can change this default behavior.
enum Colors{Red=2, Green=5, Blue=10};
Here enum is a c++ keyword, is name of enum and is a list of names for integer constant.

Example

int main()
{
enum Colors{Red, Green, Blue};
int i;
    cout <<"Please enter the color of your choice(0 to 2)::"<<endl;
    cin >> i;
switch(i)
{
    case Red:
            cout <<"Your favorite color is red"<<endl;
    break;
    case Green:
            cout <<"Your favorite color is green"<<endl;
    break;
    case Blue:
            cout <<"Your favorite color is blue"<<endl;
    break;
}
return 0;
}

Output
Please enter the color of your choice (0 to 2):
1
Your favorite color is green.
Description: In this program I have entered input 1. So case green: is converted as case 1: that’s why output is Your favorite color is green.

Write output with explanation.

void main ( )
{
     bool b1, b2;
     b2 = true;
     int a = 10, b = 20;
     b1 = a = b;
     bool b3 = b1 || b2;
     if (b3)
          cout<<"good";
     else
          cout<<"very good";
}

Output:good
Description:
  • Variable b1, b2, and b3 are Boolean type. In the above program, it is given that int a = 10, b = 20; and b1 = a = b;. Now b1 = 20 > 0, that’s why its value is true.
  • Variable b1 and b2 has value true, so statement bool b3 = b1 || b2; will produce b3=true. So the output is good.

Write output with explanation.

class outer
{
class inner
     {
          int data i;
     }
public:
     void create()
     {
          i.data=100;
     }
     void display()
     {
          cout<<i.data;
     }
};
void main()
{
     outerobj;
     obj.create();
     obj.display();
}

Description:

In the above example, int data is private member of class inner, so you cannot access it outside the class. This program will not compile and give the error.

Trace the output.

void main (void)
{
     int i = 20.5;
     float f = 10.25;
     double d = i + f;
     cout<<d<<endl;
     getch();
}

Output: 30.25
Description: In the above program, 'i' is an integer type variable. It will store only integer part of 20.5, so compiler will consider 'i' as i = 20.
Therefore output will be 20 + 10.25 = 30.25

What will be the output of following program?

class Test
{
     staticint count;
public:
     staticvoid showCount()
     {
          cout<<"count =:"<<++count;
     }
};
int Test::count;
void main()
{
     Test t1,t2,t3;
     t1.showCount();
     t2.showCount();
     t3.showCount();
}

Output:
count =:1 count =:2 count =:3