C++ Quiz on Mixed Topics Set 33

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. 33 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 statement has error in the code below
int fx(int a)
{

    /*Statement 1*/
    int b;

    /*Statement 2*/
    b = 10;

    /*Statement 3*/
    a = 20;

    /*Statement 4*/
    c = 30;

}

A
Statement 4.
B
Statement 3.
C
Statement 1.
D
Statement 2.
Soln.
Ans: A
Statement 4 has error because it is an undeclared variable that is not in scope

Question 2

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

    printf("%d", out);

}

int out=100;

A
Compiler Error in both C++ and C compilers
B
undefined
C
linker error
D
no error, output is 100 in a C++ compiler and compiler error in a C compiler.
Soln.
Ans: A
Compiler error: undeclared symbol 'out'. The rule is that a variable is available for use from the point of declaration. Even thougha is a global variable, it is not available for main. Hence an error.

Question 3

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

    printf("%d%d",sizeof("string"),strlen("string"));

}

A
6 6
B
7 7
C
6 7
D
7 6
Soln.
Ans: D
sizeof operator returns the size of string including null character[ie the total bytes] while strlen function returns length of a string excluding null character.

Question 4

What is the output of the following program ?
#define f(g,g2) g##g2
void main()
{

    int var12=100;

    printf("%d",f(var,12));

}

A
Compiler Error
B
undefined
C
100
D
12
Soln.
Ans: C
g##g2 is a concatenation. So f(var, 12) becomes var12 which is 100, and is printed.

Question 5

What is the output ?
int main()
{

    int i=1;

    do
    {

        printf("%d\n",i);

        i++;

        if(i < 15)
        {

            continue;

        }

    }while(false);

    return 0;

}

A
1 - 15 are printed
B
1 - 14 are printed
C
1
D
No Output
Soln.
Ans: C
After 1 is printed the while terminates because of while(false)

Question 6

How many times does the following loop execute ?
int main()
{

    for(int i = 0; i < 128; i++)
    {

    }

    return 0;

}

A
128
B
127
C
129
D
126
Soln.
Ans: A
The loop executes 128 times.

Question 7

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)
    {

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

    }

}

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

Question 8

Suppose we have a character string char cX [] = "abcxyz". What best describes the logic for displaying the string on your monitor, one character at a time ?
A
Read each character and display it on the screen.
B
Before reading a character check if that character exists, then read it and display it.
C
Read each character, display it on the screen and repeat this if more characters exist in the string,
D
First count the number of characters in the string, then if there are any more characters yet to be displayed, read the next character, display it, keep doing it till no more characters are yet to be processed.
Soln.
Ans: D
First count the number of characters in the string, then if there are any more characters yet to be displayed, read the next character, display it, keep doing it till no more characters are yet to be processed.

Question 9

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

    int i=1;

    while (i<=5)
    {

        printf("%d",i);

        if (i>2)
        goto here;

        i++;

    }

}

fun()
{

here:
    printf("PP");

}

A
Compiler Error
B
PP
C
PPPP
D
garbage
Soln.
Ans: C
Compiler error: undefined label 'here'

Question 10

What will be output if you will compile and execute the following c code?
struct SMarks
{

    int p:3;

    int c:3;

    int m:2;

};

void main()
{

    struct SMarks s = {2,-6,5};

    printf("%d %d %d", s.p, s.c, s.m);

}

A
2 -6 5
B
2 -6 1
C
2 2 1
D
Compiler Error
Soln.
Ans: C
This struct is based on bit fields. The following statement
int p:3
says that the data member p will have 3 bits only. The MSB will be sign bit because of 'int'. Had it been unsigned, then no sign bit. In the above question p is 2, c is -6 and m is 5. The binary representation of p is 00000010. The lower three bits are stored in p so p is 010 or +2. Similarly -6 being 11111010, so c gets 010 or +2, and 5 being 00000101, so m gets 01 or +1. The display is 2 2 1.

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.