C++ Quiz on Mixed Topics Set 15

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. 15 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 lines of the following program will be printed ?
int main ()
{

    bool b = true;

    if (b = (b && false))
    {

        /* Line 1 */
        cout << "Inside first 'if'" << endl;

    }

    if (b || true)
    {

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

    }

}

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

Question 2

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
B
C
D
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.

Question 3

Which lines will give an error?[MORE THAN ONE OPTIONS CAN BE CORRECT]
int main()
{

    int * ptr1,ptr2;

    /*Statement 1*/
    ptr1 = (int*)malloc(sizeof(int));

    /*Statement 2*/
    ptr2 = ptr1;

    /*Statement 3*/
    *ptr2 = 10;

    return 0;

}

A
Statement 1
B
Statement 2
C
Statement 3
D
None
Soln.
Ans: BC
The declaration
int * ptr1,ptr2;

is same as
int * ptr1;

int ptr2;

ptr1 is a pointer, whereas ptr2 is an int.

Question 4

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

}

/*code 1*/
int main ()
{

    int i = 0;

    afunc ("c");

    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 2
B
Code 1 and Code 3 both are correct
C
Code 3
D
Code 1
Soln.
Ans: D
Code 2 and Code 3 are wrong because they don't pass the argument as char*.

Question 5

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 !

Question 6

What is the output of the following program ?
struct xx
{

    int x=3;

    char name[]="hello";

};

void main()
{

    struct xx *s;

    printf("%d",s->x);

    printf("%s",s->name);

}

A
Compiler Error
B
hello
C
linker error
D
Garbage
Soln.
Ans: A
You cannot initialize variables in declaration. only static int can be.

Question 7

void afunc(int x)
{

    int g = 0;

    g = x;

}

Which of the following is the correct function call for afunc ?
/*code 1*/
int main ()
{

    int i = 0;

    afunc (i);

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    afunc (&i);

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc ("i");

    return 0;

}

A
Code 1
B
Code 3
C
Code 2
D
Code 1 and Code 3 both are correct
Soln.
Ans: A
Code 2 is wrong because it passes the argument by address, i.e., int*. Code 3 is wrong because "i" is a character string. So if your intention was to pass i, then "i" is illegal.

Question 8

Which statement has error in the code below
int b, a;

int main()
{

    {

        int b;

        int a;

        /*Statement 1*/
        b = 10;

        {

            /*Statement 2*/
            int a = 0;

            {

                /*Statement 3*/
                int x = 0;

            }

        }

    }

    /*Statement 4*/
    a = b;

    return 0;

}

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

Question 9

Consider the code below.
class CMyClass
{

public:
    void MyFunc(int& i)
    {

        i = i*2;

        return;

    }

};

int main ()
{

    int x = 100;

    CMyClass cmc;

    cmc.MyFunc (x);

    cout << x << endl;

}

Which of the following is correct ?
A
MyFunc accepts i by reference, and cout will display 200
B
MyFunc accepts i by value, and cout will display 200
C
MyFunc accepts i by value, and cout will display 100
D
MyFunc accepts i by reference, and cout will display 100
Soln.
Ans: A
MyFunc accepts i by reference, so when x is passed as an argument it goes as its alias and any changes that occur inside MyFunc actually occur on x. So cout will display 200.

Question 10

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

    int i;

    double a=5.2;

    char *ptr;

    ptr=(char *)&a;

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

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

    }

}

It is given that the memory representation of 5.2f is
A
-51 -52 -52 -52 -52 -52 20 64
B
51 52 52 52 52 52 20 64
C
Eight garbage values.
D
Compiler Error
Soln.
Ans: A
In c double data type is eight byte data type while char pointer ptr can point one byte of memory at a time. ptr pointer will point first eighth byte then seventh byte then sixth byte then fifth byte then fourth byte then third byte then second byte then first byte as shown in above figure. Content of eighth byte:Binary value=11001101Decimal value= -128+64+8+4+1=-51Content of seventh byte:Binary value=11001100Decimal value= -128+64+8+4=-52Content of sixth byte:Binary value=11001100Decimal value= -128+64+8+4=-52Content of fifth byte:Binary value=11001100Decimal value= -128+64+8+4=-52Content of fourth byte:Binary value=11001100Decimal value= -128+64+8+4=-52Content of third byte:Binary value=11001100Decimal value= -128+64+8+4=-52Content of second byte:Binary value=000010100Decimal value=16+4=20Content of first byte:Binary value=01000000Decimal value=64

Note: Character pointer treats MSB bit of each byte i.e. left most bit of above figure as sign bit.


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.