SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Self-test Exercises
Question:16 | ISBN:9780321531346 | Edition: 7

Question

What is the output of the following program lines, when embedded in a correct program that declares all variables to be of type char?

a = 'b';

b = 'c';

c = a;

cout << a << b << c << 'c';

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The output of the given program lines:

bcbc

Explanation:

  • a='b' means the character 'b' is stored in the variable, a. So, the initialization  of the variable a is 'b'.
  • b = 'c' means the character 'c' is stored in the variable, b. So, the initialization of the variable b is 'c'.
  • c=a means the initilization value of the 'a' (that is is 'b') is stored in the variable, c.  So, the the value of c = 'b'.
  • In observed, the cout statement represents 'c'. It is print the character 'c' on the screen at end.
  • Finally, the cout statement is used to print the result is 'bcbc'

Complete executable program code:

#include <iostream>
using namespace std;

int main ()
{
    //Declares all variables to be 
    //of data type char
    char a,b,c;
    
    //Initialize the variables
    a = 'b';
    b = 'c';
    c = a;
    
    //Print output
    cout << a << b << c << 'c';
    return 0;
}

Output of the program code:

bcbc 

 

0 0

Discussions

Post the discussion to improve the above solution.