C++ Quiz on Mixed Topics Set 1

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. 1 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 in the code below has a bug ?[There is no compiler error]
int main()
{

    int b;

    int a;

    /*Statement 1*/
    b = 10;

    for (a = 0; a < b; a++)
    {

        /*Statement 2*/
        int x = 0;

    }

    /*Statement 3*/
    a = x;

    return 0;

}

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

Question 2

What is the value of c [5] in the following ?
char c [ ] = "ABCDE";

A
E
B
'\0', ie, NULL
C
indeterminate because c has no size
D
the statement doesnot compile
Soln.
Ans: B

Question 3

Consider the code below
int PlusOne (int);

int main()
{

    int y = PlusOne (4);

    y *= PlusOne (y);

    cout << y << endl;

    return 0;

}

int PlusOne (int i)
{

    return i + 1;

}

What is the output ?
A
60
B
20
C
Compiler Error
D
30
Soln.
Ans: D
It displays 30, y = y * PlusOne (y) => y = 5 * 6

Question 4

Which of these is the correct main for the entry-point function as per the C-Standard ?
A
void main (){}
B
int main (){}
C
int main (){return 0;}
D
int Main (){return 0;}
Soln.
Ans: C

Question 5

What is error in following declaration?
struct outer
{

    int a;

    struct inner
    {

        char c;

    };

};

A
Nesting of a struct
B
Inner struct must have a name
C
Outer struct must have a name
D
No Error
Soln.
Ans: D
No Error compiles successfully in VC++

Question 6

Which is the correct input-output usage ?
A
scanf ("%u", i); printf("%u", &i);
B
scanf("%u", &i);printf(%u", i);
C
scanf("%u", &i);printf("%u",&i);
D
scanf("%u", i);printf("%u",i);
Soln.
Ans: B

Question 7

What is the output?
int main (int& main)
{

    main++;

    return main;

}

void main()
{

    int x = 5;

    main (x);

    cout << x << endl;

}

A
5
B
6
C
Linker Error
D
Compiler Error
Soln.
Ans: D
main function cannot be overloaded. It is a special function.

Question 8

Consider the code below
int PlusOne (int);

int main()
{

    PlusOne (4);

    return 0;

}

int PlusOne (int i)
{

    return i + 1;

}

What happens when you run it ? [More than one can be correct]
A
It doesn't display anything
B
It adds 1 to 4 and returns
C
It throws a linker error
D
It doesn't compile
Soln.
Ans: AB
It adds 1 to 4, returns and no display occurs.

Question 9

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

    int a = 3;

    printf ("%d", a--);

    return 0;

}

A
3
B
2
C
1
D
0
Soln.
Ans: A
a-- is postfixed so the display will be 3.

Question 10

Which statement in the code below has a bug ? [There is no compiler error]
int main()
{

    int* p;

    // array of 10 int
    int b [10];

    {

        int i = 0;

        /*Statement 1*/
        for (i = 0; i < 10; i++)
        {

            b [i] = i;

        }

        /*Statement 2*/
        for (i = 0; i < 10; i++)
        {

            p = (b + i);

        }

        /*Statement 3*/
        p = &i;

    }

    /*Statement 4*/
    *p = 3;

    return 0;

}

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

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.