C++ Quiz on Mixed Topics Set 10

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. 10 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

Consider the code below
int PlusOne (int);

int PlusTwo (int);

int main()
{

    int y = PlusOne (4);

    y = PlusTwo (y);

    y = PlusTwo (y) * PlusOne (y) * y;

    cout << y << endl;

}

int PlusOne (int i)
{

    return i + 1;

}

int PlusTwo (int i)
{

    return i + 2;

}

What is the output ?
A
504
B
160
C
Compiler Error
D
220
Soln.
Ans: A
It displays 504

Question 2

What is the output ?
void main()
{

    int i=5,j=6,z;

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

}

A
11
B
There is no +++ operator
C
Garbage
D
Doesnot link
Soln.
Ans: C
the expression i+++j is treated as
(i++ + j)

Question 3

What is the output
int main ()
{

    int a = 5, b = 10, c = 1;

    if (a && b > c)
    {

        printf ("X");

    }else

    {

        break;

    }

    return 0;

}

A
No output
B
X
C
Error
D
None of the above
Soln.
Ans: C
break statement cannot be used outside a switch or a loop.

Question 4

What are the files which are automatically opened when a C file isexecuted?
A
stdin, stdout and stderr
B
cout, cin and cerr
C
stdin, stdout, stderr, cout, cin, cerr
D
stdio
Soln.
Ans: A
Following are opened in C - stdin, stdout, stderr (standard input,standard output,standard error).

Question 5

What is the output?
int main()
{

    printf ("%c", 2["Punjab"]);

    return 0;

}

A
n
B
Compiler Error: Conversion failure
C
Pu
D
NULL
Soln.
Ans: A
The code can be understood like this
int main()
{

    char *k = "Punjab";

    printf ("%c", 2[k]);

    return 0;

}

In the above code 2 [k] ≡ *(2 + k) ≡ *(k + 2) ≡ k [2] ≡ n

Question 6

What will be output if you will compile and execute the following c code?
#define call(x,y) x##y
int main()
{

    int x=5, y=10, xy=20;

    printf("%d",xy + call(x,y));

    return 0;

}

A
40
B
15
C
510
D
None
Soln.
Ans: A
## is concatenation c preprocessor operator. It only concatenates the operands i.e.a##b=ab. So
printf("%d",xy + call(x,y));

becomes
printf("%d",xy + xy);


Question 7

Consider the code below
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 passed by value
B
x is passed by reference and y is also passed by reference
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: D
x is passed by value and y is passed by reference.

Question 8

How many times does OX print ?
int main()
{

    do
    {

        int i = 0;

        switch (i++)
        {

        case 0:
        case 1:
        case 2:
            {

                cout << "OX" << endl;

                continue;

            }

        default:
            {

                return 0;

            }

        }

    }while (true);

    return 0;

}

A
4
B
5
C
D
3
Soln.
Ans: C
OX prints infinite times. Because everytime we reach the top of the do loop, a new 'i' is defined equal to 0.

Question 9

Consider the code below
int& MyFunction(int& x)
{

    int g = 0;

    g = x;

    return x;

}

Here the function MyFunction
A
accepts an argument by reference and returns an int by reference
B
accepts an argument by reference and returns an int by value
C
accepts an argument by value and returns an int by value
D
accepts an argument by value and returns an int by value
Soln.
Ans: A
accepts an argument by reference and returns an int by reference. arguments and return types that are passed by reference can be recognized by '&' after the data type. So int& stands for 'int by Reference'

Question 10

What is the output of the following code?
int main()
{

    int i = 2, j = 3, k = 4;

    int a = (i, j, k);

    printf ("%d", a);

    return 0;

}

A
2
B
3
C
4
D
0
Soln.
Ans: C
The comma operator returns k here
(i, j, k)

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