C++ Quiz on Mixed Topics Set 30

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

Which lines of the following program will be printed ?
int main ()
{

    int x = 9;

    if ( 1 + (x % 9))
    {

        /* Line 1 */
        cout << "Inside first 'if'" << endl;

    }

    if ((x + 1) % 9)
    {

        /* Line 2 */
        cout << "Inside second 'if'" << endl;

    }

}

A
Line 1 as well as Line 2 will be printed
B
Line 1 only will be printed
C
None will be printed
D
Line 2 only will be printed
Soln.
Ans: A
Line 1 as well as Line 2 only will be printed. 1 + (x % 9) is same as 1 + (0) which is same as 1, ie, non-zero. (x + 1) % 9 is same as 10%9 which is 1, ie, non-zero. Hence both the lines will be printed.

Question 2

What is the value of 4 [i] in the following code -
int main ()
{

    int i [20] = {20};

    ...
}

A
0
B
un-known or garbage
C
20
D
4
Soln.
Ans: A
4 [i] is same as i [4] because i [4] = *(i + 4), which is same as *(4 + i) i.e., 4 [i]

Question 3

What is the output here?
int main()
{

    enum week {sun, mon=10, tue=sun+mon+1};

    printf ("%d ...%d ...%d ...", sun, mon, tue);

    return 0;

}

A
0 ... 10 ...11 ...
B
10 ... 10 ... 10...
C
10, 11, 12
D
None of the above
Soln.
Ans: A
sun is 0, mon is 10 and tue is 0 + 10 + 1 = 11

Question 4

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

    int i=5;

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

}

A
Compiler Error
B
undefined
C
12
D
11
Soln.
Ans: B
Undefined behaviour because of sequence points.

Question 5

Which compiler would you use to write a scaleable C/C++ project that targets the Windows OS ?
A
Microsoft Visual Basic
B
Microsoft Office 2011
C
Microsoft Visual C++
D
Turbo Compiler
Soln.
Ans: C

Question 6

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

    int a=2;

    if(a==2)
    {

        a=~a+2<<1;

        printf("%d",a);

    }

    else
    {

        break;

    } return 0;

}

A
break; is not possible here.
B
23
C
-3
D
19
Soln.
Ans: A
break is orphan here.

Question 7

What is the output
int main()
{

    char x = -129;

    printf ("%d", x);

    return 0;

}

A
Compiler Error
B
-129
C
Undefined
D
10
Soln.
Ans: C
Consider the chart
+127 --> 01111111
-127 --> 11111111
-126 --> 11111110
-125 --> 11111101
we cannot store -129 in char because it is out of range. So the behaviour is undefined. Different compilers will treat it differently, VC++ gives a warning.

Question 8

What is the value of c [0] in the following ?
char c [] = "XABCDE";

A
A
B
X
C
indeterminate because c has no size
D
the statement doesnot compile
Soln.
Ans: B

Question 9

What is '&' doing in the following code ?
int main()
{

    int i = 9, j = 10, *k = 0;

    k = i * (& j);

    return 0;

}

A
Multiplying value of i by address of j.
B
Performing a bitwise logical AND between i and j.
C
Performing a logical AND between i and j.
D
Multiplying j by address of i.
Soln.
Ans: A

Question 10

Which lines of the following program will be printed ?
int main ()
{

    int i = 9;

    if (i - 9)
    {

        /* Line 1 */
        cout << "Inside first 'if'" << endl;

    }

    if (i + 9)
    {

        /* Line 2 */
        cout << "Inside second 'if'" << endl;

    }

}

A
None will be printed
B
Line 2 only will be printed
C
Line 1 only will be printed
D
Line 1 as well as Line 2 will be printed
Soln.
Ans: B
Line 2 only will be printed

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.