C++ Quiz on Mixed Topics Set 27

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. 27 in this series.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

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 value of i in the following code
int i = (234 / 10) % 10;

A
23
B
4
C
3
D
0
Soln.
Ans: C

Question 2

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

public:
    X()
    {

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

    }

};

void main()
{

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

}

X x;

A
In main()
B
In main()
C-tor Called
C
C-tor Called
In 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 3

int g = 10;

void afunc(int x)
{

    g = x;

}

int main(void)
{

    g = 10;

    afunc(20);

    cout << g << endl;

    return 0;

}

What is the output of the above program ?
A
Compiler error
B
10
C
20
D
21
Soln.
Ans: C
It displays 20.

Question 4

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

    printf("%p",main);

}

A
undefined behaviour
B
some valid hexadecimal address will be printed, and the behaviour would be perfectly OK
C
compiler error
D
linker error
Soln.
Ans: B
Function names are just addresses (just like array names are addresses).main() is also a function. So the address of function main will be printed. %p in printfspecifies that the argument is an address. They are printed as hexadecimal numbers.

Question 5

What will be output if you will compile and execute the following c code?
#define max 5;

int main()
{

    int i=0;

    i=max++;

    printf("%d",i++); return 0;

}

A
Compiler Error
B
6
C
5
D
7
Soln.
Ans: A
It is clear 'max' has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.

Question 6

What is the output ?
int i=10;

void main()
{

    extern int i;

    {

        int i = 20;

        {

            const volatile unsigned i = 30;

            printf("%d",i);

        }

        printf("%d",i);

    }

    printf("%d",i);

}

A
301020
B
302010
C
102030
D
201030
Soln.
Ans: B
'{' introduces new block and thus new scope. In the innermost block i is declared as,const volatile unsignedwhich is a valid declaration. i is assumed of type int. So printf prints 30. In the nextblock, i has value 20 and so printf prints 20. In the outermost block, i is declared asextern, so no storage space is allocated for it. After compilation is over the linkerresolves it to global variable i (since it is the only variable visible there). So it prints i'svalue as 10.

Question 7

void afunc(int** x)
{

    int g = 0;

    g = **x;

}

Which of the following is the correct function call for afunc ?
/*code 1*/
int main ()
{

    int i = 0;

    afunc (&i);

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    int *j = &i;

    afunc (&j);

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc (**i);

    return 0;

}

A
Code 1
B
Code 1 and Code 3 both are correct
C
Code 3
D
Code 2
Soln.
Ans: D
Code 2. Code 1 is wrong because it passes the argument by int* and not int**. Code 3 is wrong because **i is meaningless in this context.

Question 8

The following code prints minus signs indefinitely. Can we correct it by just changing one character so that it prints 20 minus signs ?
int main()
{

    int i;

    int n = 20;

    for( i = 0; i < n; i-- )
    {

        printf("-");

    }

    return 0;

}

A
for( n = 0; i < n;="" i--="">
B
for( i = 0; i < i;="" i--="">
C
for( i = 0; i < n;="" n--="">
D
None of the above
Soln.
Ans: C
Self-Explanatory. But it is known that this problem has two more solutions. What could they be?

Question 9

Which statement in the code below has a bug ?[There is no compiler error]
/* Statement 1*/
int* fx ()
{

    int i = 0;

    return &i;

}

int main()
{

    /* Statement 2*/
    int* p = fx ();

    /* Statement 3*/
    *p = 8;

    return 0;

}

A
Statement 1
B
Statement 2
C
Statement 3
D
Statement 4
Soln.
Ans: C
In Statement 3 even though 'p' is in scope but it stores the address of i contained in fx. But i is not in scope at statement 3.

Question 10

What is the output ?
void main()
{

    int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };

    int *p,*q;

    p=&a[2][2][2];

    *q=***a;

    printf("%d..%d",*p,*q);

}

A
10.. 10
B
2.. 10
C
Garbage
D
Doesnot compile
Soln.
Ans: C
This code is meaningless. It uses q without initializing its address. So the output is unpredictable.

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.