C++ Quiz on Mixed Topics Set 8

This quiz series contains mixed questions on C++. It includes questions from various topics like predict ouput, functions, loops, etc., Solutions to all the questions have been provided. This is quiz no. 8 in this series.

Last Reviewed and Updated on May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

Quiz Questions

Each question has four choices. More than one options can be correct. When you have finished the quiz, click the button at the end of the questions to view the result, and the solutions and answers.


Your Score
Correct Answers:
Wrong Answers:
Unattempted:

Question 1

What is the output?
int main ()
{

    char x;

    for (x = 0; x < 128; x++)
    {

        printf ("%d", x);

    }

    return 0;

}

A
Infinite Loop
B
print 0 to 127
C
print 0 to 128
D
Error
Soln.
Ans: A
The maximum value of char is CHAR_MAX, which is 127 and hence x can never exceed 128, and the loop continues to infinity.

Question 2

What is the output of the following program ?
void main()
{

    struct xx
    {

        int x;

        struct yy
        {

            char s;

            struct xx *p;

        };

        struct yy *q;

    };

}

A
Compiler Error in both C++ and C compilers
B
undefined
C
linker error
D
no error, no output in a C++ compiler and compiler error in a C compiler.
Soln.
Ans: D
Following is perfectly legal in a C++ compiler because p is an address of struct xx[which itself is not yet visible although]
struct xx *p;

Compiler can allocate memory for an address because its size is always known. C compiler will give an error.

Question 3

What is the output ?
int main ()
{

    int a = 100;

    if (a > 10)
    {

        printf ("X");

    }else if (a > 20)

    {

        printf ("Y");

    }else if (a > 30)

    {

        printf ("Z");

    }

    return 0;

}

A
X
B
Y
C
Z
D
XYZ
Soln.
Ans: A
Self-Explanatory

Question 4

What is the output of the following program ?
#define square(x) x*x
int main()
{

    int i;

    i = 64/square(4);

    printf("%d",i);

}

A
undefined behaviour
B
64
C
compiler error
D
linker error
Soln.
Ans: B
the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 .Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 =64

Question 5

What is the output in the following C++ program?
class X
{

public:
    X()
    {

        cout << "C-tor Called \n";

    }

};

X x;

void main()
{

    cout << "In main()" << endl;

}

A
In main()
B
In main()C-tor Called
C
C-tor CalledIn main()
D
Linker Error
Soln.
Ans: C
The variable x in the global namespace causes the c-tor to display the message first. Then the main is entered.

Question 6

What is the output ?
void Error(char* s)
{

    printf(s);

    return;

}

int main()
{

    int *p;

    p = (int*)malloc(sizeof(int));

    if(p == NULL)
    {

        Error("Could not allocate the memory\n");

        Error("Quitting....\n");

        exit(1);

    }

    else
    {

        /*some stuff to use p*/
    }

    return 0;

}

A
Display depends on the success of malloc, the code has a memory leak because there is no matching free.
B
No display
C
There is a compiler error
D
Linker Error
Soln.
Ans: A
Self Explanatory

Question 7

What is the output of the following program ?
void main()
{

    int i=5;

    printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);

}

A
undefined behaviour
B
45545
C
54421
D
455551206641329
Soln.
Ans: A
You cannot change a value more than once in a single statement, even when you are passing arguments to a function. If you do this, it results in undefined behavior because of sequence points.

NOTE: Some people give wrong answer to this - "The arguments in a function call are pushed into the stack from left to right. Theevaluation is by popping out from the stack. and the evaluation is from right to left,hence the result."


Question 8

Consider the code below:
int main()
{

    int b;

    int a;

    /*Statement 1*/
    b = 10;

    {

        /*Statement 2*/
        b = 0;

        {

            /*Statement 3*/
            int a = 0;

        }

        /*Statement 4*/
        a = b;

    }

    return 0;

}

Which statement has error in the code above
A
The code has no error.
B
Statement 2.
C
Statement 1.
D
Statement 3.
Soln.
Ans: A
The code has no error

Question 9

void afunc(int x, int y)
{

    int g = 0;

    g = x;

}

Which of the following is/are correct ?
A
x is passed by reference and y is also passed by reference
B
x is passed by reference and y is passed by value
C
x is passed by value and y is also passed by value
D
x is passed by value and y is passed by reference
Soln.
Ans: C
x is passed by value and y is also passed by value

Question 10

What is the value of i in the following code
int i = 20 % 15
A
4
B
0
C
5
D
100
Soln.
Ans: C

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.




Advertisement