Expected output of the program - c program, C/C++ Programming

Expected output of the program:

1. Consider the following programs. For each, indicate whether the program is correct. If yes, what is the expected output? If not, what is the error? (Hint: remember the discussion about scope and how curly braces { } form a new scope.)

a)

            #include

            int main()

            {

                        int my_var(19);

                        my_var += 2;

                        char my_var('a');

                        std::cout << my_var << std::endl;

                        return 0;

            }

b)

            #include

            int main()

            {

                        int my_var(19);

                        my_var += 2;

                        {

                                    char my_var('a');

                                    std::cout << my_var << std::endl;

                        }

                        return 0;

            }

c)

            #include

            int my_var = 23;

            int main()

            {

                        int my_var(19);

                        my_var += 2;

                        {

                                    char my_var('a');

                                    std::cout << my_var << std::endl;

                                    std::cout << ::my_var << std::endl;

                        }

                        return 0;

            }

2. What is the expected output of the following code snippet if the user enters the following values for "number":

            a) 0

            b) 2

            c) 10

#include

int main()

{

            int number;

            std::cout << "Enter a number : ";

            std::cin >> number;

            switch(number) {

                        case 0:

                                    std::cout << "Zero\n";

                                    break;

                        case 1:

                                    std::cout << "One\n";

                        case 2:

                                    std::cout << "Two\n";

                        case 3:

                                    std::cout << "Three\n";

                                    break;

                        case 4:

                                    std::cout << "Four\n";

                                    break;

                        default:

                                    std::cout << "Less than zero or greater than four\n";           

            }

            return 0;

}

3. Consider the following code snippet:

            int *p;

            int i;

            int k;

            i = 42;

            k = i;

            p = &i;

            (i) Assume that the above code snippet is followed by the following statements (each of them separately). For the statements that you believe are correct and will compile, what would the expected values of i and k be in each case? If you believe there is a compile-time error in any of the cases, please indicate this and briefly explain your answer.

1.*p = 10;

2.   *k = 100;

3.   p = 4532;

4.   k = 75;

 

            (ii) If the statement "p = &i;" is moved and placed right before "k = i;", which of the above answers would change (indicate A, B, C or D)?

Posted Date: 2/11/2013 3:00:22 AM | Location : United States







Related Discussions:- Expected output of the program - c program, Assignment Help, Ask Question on Expected output of the program - c program, Get Answer, Expert's Help, Expected output of the program - c program Discussions

Write discussion on Expected output of the program - c program
Your posts are moderated
Related Questions
Explain zero based addressing. - Array subscripts always start at zero. - These subscript values are used to identify elements in the array. - As subscripts start at 0, a

a) Define an Asset base class that provides the following method: class Asset { public:      virtual double getValue()=0; };   This will be the base class for both stock an

Program is to store first n natural no in a file: Write a program to  store first n natural no in a file void main()     {     fstream file;     file.open("studen

find area under the curve y=f(x) between x=a &y=b integrate y=f(x)

Explain about the Expressions in c Language? An expression is the combination of constants, variables and operators arranged as per the syntax of the language. Some of the illu

I started preparing an alternative cryptocurrency and I am way over my head. I would like help in making modifications to the open source files and then help with deployment. I wil

Explain structured programming The principal idea behind structured programming was as easy as the idea of "divide and conquer." A computer program could be regarded as having

minimum of seven number

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write a function that has two int parameters num and n, and returns TRUE when the nth bit in num is 1, otherwise FALSE. This function should use the function in the above problem.