C++ Quiz on Mixed Topics Set 31

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. 31 in this series.

Last Reviewed and Updated on October 26, 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 is a good programming practice ?
A
Always make a provision for exit from a loop.
B
Use a lot many variables without worrying about memory allocation.
C
Type a complete program, howsoever long, and compile only after the typing has ended.
D
All of the above.
Soln.
Ans: A

Question 2

What is the output ?
int main ()
{

    int x = 011, i;

    for (i = 0; i < x; i += 3)
    {

        printf ("A");

        continue;

        printf ("B");

    }

    return 0;

}

A
A
B
B
C
AABB
D
AAA
Soln.
Ans: D
x = 011; is same as x = 9; Since there is a continue in the loop the remaining statements never execute. So the above code becomes
int main ()
{

    int x = 011, i;

    for (i = 0; i < 9; i += 3)
    {

        printf ("A");

    }

    return 0;

}

Which prints AAA

Question 3

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

void main()
{

    // assume 32-bit addresses and char to be 8 bits
    char far, *farther,*farthest;

    printf("%d..%d",sizeof(farther),sizeof(farthest));

}

A
Compiler Error
B
1..4
C
4..4
D
1..4
Soln.
Ans: C
farther and farthest are char*, so they are addresses and 4 bytes long.

Question 4

Consider the code below and determine the output -
int main()
{

    int i = 0;

    int k = ++i;

    printf ("%d-%d", k++, ++k);

    return 0;

}

A
0-2
B
1-2
C
1-3
D
indeterminate
Soln.
Ans: D
- sequence point violations

Question 5

What is the output ?
extern int *arr;

int main()
{

    printf ("%d\n", arr [1]);

    return 0;

}

int arr [20] = {20, 20};

A
Compiler Error
B
0
C
20
D
-20
Soln.
Ans: AC
extern int* arr
and
int arr [20] = {20, 20};

donot match so compiler gives the error 'arr' : redefinition; different types of indirection

Question 6

Consider the code below and determine the output -
int main()
{

    int i = 0;

    int k = ++i + i++;

    printf ("%d", k);

    return 0;

}

A
1
B
0
C
2
D
indeterminate
Soln.
Ans: D
- sequence point violations

Question 7

What will be the output ?
void main()
{

    char *cptr,c;

    void *vptr,v;

    c=10; v=0;

    cptr=&c; vptr=&v;

    printf("%c%v",c,v);

}

A
Compiler error (at line number 4): size of v is Unknown.
B
No errors
C
Linker Error
D
Garbage output
Soln.
Ans: A
You can create a variable of type void * but not of type void, since void is an emptytype. In the second line you are creating variable vptr of type void * and v of type voidhence an error.

Question 8

Which is the correct code ?
void afunc(char** c)
{

}

/*code 1*/
int main ()
{

    char* cX [] = {"ABC"};

    afunc (cX);

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    afunc (i);

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc ('c');

    return 0;

}

A
Code 1
B
Code 2
C
Code 3
D
Code 1 and Code 3 both are correct
Soln.
Ans: A
Code 2 and Code 3 are wrong because they don't pass the argument as char**, ie, as array of char*. Following would be equivalent for /*Code 1*/
int main ()
{

    char cX [] = "ABC";

    char* k = cX;

    char** y = &k;

    afunc (y);

    return 0;

}

and also the following
int main ()
{

    char cX [] = "ABC";

    char* k = cX;

    afunc (&k);

    return 0;

}


Question 9

What will be output if you will compile and execute the following c code?
void main()
{

    int i;

    float a = 5.2f;

    char *ptr;

    ptr=(char *)&a;

    for(i=0;i<=3;i++)
    {

        printf("%d ",*(ptr++));

    }

}

It is given that the memory representation of 5.2f is
A
5 6 7
B
7 6 5
C
0 0 0 0
D
102 102 -90 64
Soln.
Ans: D
In c float data type is four byte data type while char pointer ptr can point one byte of memory at a time. On each loop ptr pointer will point first fourth byte then third byte then second byte then first byte. Content of fourth byte:Binary value=01100110Decimal value= 64+32+4+2=102Content of third byte:Binary value=01100110Decimal value=64+32+4+2=102Content of second byte:Binary value=10100110Decimal value=-128+32+4+2=-90Content of first byte:Binary value=01000000Decimal value=64. Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.

An 8086 machine stores LSB first.


Question 10

What is the output?
int main()
{

    int i = 0;

    switch (i)
    {

        case 0: do
        {

            printf ("%u", i);

            case 2: printf ("%u", i);

            case 1: printf ("%u", i);

        }while (false);

    }

    return 0;

}

A
000
B
Compiler Error: Wrong use of switch and do-while
C
Unpredicatble
D
Nothing is printed
Soln.
Ans: A
The code is correct. We can intersperse do-while inside a switch like shown above. Try and run !

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