C++ Quiz on Mixed Topics Set 17

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

Which statement has error in the code below
int c = 9;

int main()
{

    /*Statement 1*/
    int b;

    /*Statement 2*/
    b = 10;

    /*Statement 3*/
    a = 20;

    /*Statement 4*/
    c = 30;

    return 0;

}

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

Question 2

What is the output of the following program ?
#define clrscr() 100
void main()
{

    clrscr();

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

}

A
100
B
No display
C
compiler error
D
linker error
Soln.
Ans: A
Preprocessor executes as a seperate pass before the execution of the compiler. Sotextual replacement of clrscr() to 100 occurs.The input program to compiler looks likethis :
main()
{

    100;

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

}

Note:
100;

is an executable statement but with no action. So it doesn't give any problem

Question 3

What is the output ?
void main()
{

    int i=-1;

    -i;

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

}

A
i = -1, -i = 1
B
i = -1, -i = -1
C
-i = -1, i = -1
D
+i = +1, -i = -1
Soln.
Ans: A
-i is executed and this execution doesn't affect the value of i. In printf first you just printthe value of i. After that the value of the expression -i = -(-1) is printed.

Question 4

Consider the code below
void afunc(int* x, int y, char & z)
{

    int g = 0;

    g = *x;

}

What is the signature of afunc ?
A
void
B
void_int*_char_int
C
afunc_
D
afunc_int*_int_char&
Soln.
Ans: D
afunc_int*_int_char&, because the signature comprises of function name and the type of arguments in respective order from left to right.

Question 5

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*/
    b[0] = *p;

    return 0;

}

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

Question 6

How will scanf execute ?
void main()
{

    char s[12];

    scanf(" \"%[^\"]\"",s);

}

A
First it checks for the leading white space and discards it.Then it matches with aquotation mark and then it reads all character upto another quotation mark.
B
First it matches with aquotation mark and then it reads all character upto another quotation mark. Then it checks for the leading white space and discards it.
C
Normal way, ignores special characters
D
None of the above
Soln.
Ans: A
First it checks for the leading white space and discards it. Then it matches with aquotation mark and then it reads all character up to another quotation mark.

Question 7

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

    while (0)
    {

    }

    return 0;

}

A
never
B
1
C
forever
D
2
Soln.
Ans: A
The loop never executes.

Question 8

Which statement has error in the code below
int c = 9;

int a = 9;

void fx ()
{

    int a;

    a = 8;

}

int main()
{

    /*Statement 1*/
    int b;

    /*Statement 2*/
    b = 10;

    /*Statement 3*/
    a = c++;

    /*Statement 4*/
    c = a;

    return 0;

}

A
Statement 1. and Statement 3.
B
Statement 1. and Statement 2.
C
The code has no error.
D
Statement 4. and Statement 3.
Soln.
Ans: C
The code has no error

Question 9

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

    int i=-1;

    +i;

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

}

A
Compiler Error in both C++ and C compilers
B
i = -1, +i = -1
C
i = -1, i = +1
D
i = +1, - i = +1
Soln.
Ans: B
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore itjust because it has no effect in the expressions (hence the name dummy operator).

Question 10

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

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