C++ Quiz on Mixed Topics Set 7

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. 7 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 output of the following program ?
void main()
{

    char s[ ]="man";

    int i;

    for(i=0;s[ i ];i++)
    {

        printf("\n%c%c%c%c",s[i ],*(s+i),*(i+s),i[s]);

    }

}

A
Compiler Error
B
Linker Error
C
mmmm
aaaa
nnnn
D
mmaannnn
Soln.
Ans: C
s[i], *(i+s), *(s+i), i[s] are all different ways ofexpressing the same idea. Generally array name is thebase address for that array. Here s is the base address. iis the index number/displacement from the base

Question 2

Consider the code below
int PlusOne (int);

int main()
{

    int y = PlusOne (PlusOne (4));

    cout << y << endl;

    return 0;

}

int PlusOne (int i)
{

    return i + 1;

}

What is the output ?
A
Compiler error
B
4
C
5
D
6
Soln.
Ans: D
It displays 6

Question 3

In the code below Line 3 has a potential error. Which is it ? [More than one options can be correct]
int main ()
{

    /* line 1 */
    char cX;

    /* line 2 */
    cout << "Enter an alphabet[A-Z]: ";

    /* line 3 */
    cin >> cX;

    /* line 4 */
    cout << "You entered: " << cX << endl;

}

A
Bad data error if a user types a string of characters rather than a single character.
B
The arrows >> are in the wrong direction.
C
Logical error is possible if a user doesnot type an alphabet that is uppercase.
D
All of the above.
Soln.
Ans: AC
Both logical as well as bad data errors are possible in this case. So we should have an eye on validation requirements whenever we write code. The direction of the arrows is correct.

Question 4

What is the output of the following program ?
enum colors {BLACK,BLUE,GREEN};

int main()
{

    printf("%d..%d..%d",BLACK,BLUE,GREEN);

    return(1);

}

A
BLACK..BLUE..GREEN
B
0..1..2
C
2..1..0
D
linker error
Soln.
Ans: B
enum assigns numbers starting from 0, if not explicitly defined.

Question 5

What is the output ?
int main()
{

    int a=3;

    printf ("%c", a ["Hello"]);

    return 0;

}

A
3
B
l
C
Hello
D
Compiler Error
Soln.
Ans: B
Let const *k = "Hello". Then, a [k] is same as *(a + k) ie, *(k + a) ie, k [a] ie the second 'l' of Hello.

Question 6

What will be output if you will compile and execute the following c code?
#define x 5+2
int main()
{

    int i;

    i=x*x*x;

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

}

A
343
B
27
C
133
D
Compiler Error
Soln.
Ans: B
Following
i = x*x*x
becomes
i = 5+2*5+2*5+2;

which is 27.

Question 7

Consider the code below:
int main()
{

    {

        int b;

        int a;

        /*Statement 1*/
        b = 10;

        {

            /*Statement 2*/
            int a = 0;

            {

                /*Statement 4*/
                int x = 0;

            }

        }

    }

    /*Statement 3*/
    a = b;

    return 0;

}

Which statement has error in the code above ?
A
The code has no error.
B
Statement 1.
C
Statement 2.
D
Statement 3.
Soln.
Ans: D
Statement 3 has error a and b are not in scope

Question 8

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

    int j = 0;

    do
    {

        switch (j)
        {

        case 5:
            {

                cout << "OX" << endl;

                break;

            }

        default:
            {

                j++;

            }

        }

    }while (true);

    return 0;

}

A
3
B
5
C
4
D
Soln.
Ans: D
OX prints infinite times.

Question 9

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

    printf("\nab");

    printf("\bsi");

}

A
Compiler Error on both C and C++ compilers
B
abbsi
C
absi
D
asi
Soln.
Ans: D
The first line prints 'ab' but the second line has \b which is backspace, so it erases the b.

Question 10

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

    int c[ ]={2.8,3.4,4,6.7,5};

    int j,*p=c,*q=c;

    for(j=0;j<5;j++)
    {

        printf(" %d ",*c);

        ++q;

    }

    for(j=0;j<5;j++)
    {

        printf(" %d ",*p);

        ++p;

    }

}

A
Compiler Error
B
4 2 5 2 2 2 3 4 6 5
C
5 4 3 2 1
D
2 2 2 2 2 2 3 4 6 5
Soln.
Ans: D
Initially pointer c is assigned to both p and q. In the firstloop, since only q is incremented and not c , the value 2will be printed 5 times. In second loop p itself isincremented. So the values 2 3 4 6 5 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.