C++ Quiz on Mixed Topics Set 2

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. 2 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 ?
{

    // Assume an int occupies 4-bytes
    printf("%x",-1<<4);

}

A
Compiler Error
B
fffffff0
C
linker error
D
ffff1111
Soln.
Ans: B
-1 is internally represented as all 1's. When left shiftedfour times the least significant 4 bits are filled with0's.The %x format specifier specifies that the integervalue be printed as a hexadecimal value.

Question 2

What is the output ?
int main()
{

    int a=3;

    printf (&a ["Hello"]);

    return 0;

}

A
3
B
lo
C
Hello
D
Compiler Error
Soln.
Ans: B
Let const *k = "Hello". Then, k [a] is same as *(k + a) ie, *(k + 3) ie, the second 'l' of Hello. It's address is &(*(k+3)) ie, &k[3] ie &k [a], ie &a [k], ie &a ["Hello"]. When it is passed to printf it prints the string starting at 'l', ie, it prints lo !

Question 3

With every data-type in C/C++ that requires memory allocation, you can associate
A
a value
B
an address or reference
C
Both (a) and (b)
D
None of the above
Soln.
Ans: C

Question 4

What is the output ?
#define max 5
#define int arr1[max]
void main()
{

    typedef char arr2[max];

    arr1 list={0,1,2,3,4};

    arr2 name="name";

    printf("%d %s",list[0],name);

}

A
0 name
B
name 0
C
0 n
D
compiler error
Soln.
Ans: D
This is too buggy and meaningless code. It will not compile.

Question 5

Which of the following codes is correct ?
void afunc(int x, char* c)
{

    int g = 0;

    g = x;

}

/*code 1*/
int main ()
{

    int i = 0;

    afunc (i, "c");

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    afunc ("c", "x");

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc ("x", "c");

    return 0;

}

A
Code 1 and Code 3 both are correct
B
Code 3
C
Code 2
D
Code 1
Soln.
Ans: D
Code 2 and Code 3 are wrong because they do not pass the first argument by int.

Question 6

Suppose we have a character string char cX [] = " bxza". What code best describes the logic for displaying all characters of the string one by one, but skipping all leading white space ?
/* Code 1*/
int main ()
{

    char cX [] = " abcdefgh";

    char* k = cX;

    while (' ' == *(++k));

    cout << k << endl;

}

/* Code 2*/
int main ()
{

    char cX [] = " abcdefgh";

    for (int i = 0; ; i++)
    {

        if (cX [i] == ' ')
        {

            continue;

        }

        else
        {

            cout << cX;

        }

    }

}

/* Code 3*/
int main ()
{

    char cX [] = " abcdefgh";

    for (int i = 0; ; i++)
    {

        if (cX [i] != ' ')
        {

            cout << cX;

        }

    }

}

A
Code 1
B
All will achieve the same result.
C
Code 2
D
Code 3
Soln.
Ans: A
Code 2 and Code 3 will enter an infinite loop. Code 1 is the correct answer

Question 7

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

    int i=3;

    switch(i)
    {

        default:printf("zero");

        case 1: printf("one");

        break;

        case 2:printf("two");

        break;

        case 3: printf("three");

        break;

    }

}

A
Compiler Error
B
three
C
linker error
D
zero three
Soln.
Ans: B
The default case can be placed anywhere inside theloop. It is executed only when all other cases do notmatch.

Question 8

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_int_int_char
D
afunc
Soln.
Ans: C
afunc_int_int_char, because the signature comprises of function name and the type of arguments in respective order from left to right.

Question 9

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;

    int *j = &i;

    afunc (&j);

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc (**i);

    return 0;

}

A
Code 1
B
Code 1 and Code 3 both are correct
C
Code 3
D
Code 2
Soln.
Ans: D
Code 2. Code 1 is wrong because it passes the argument by int* and not int**. Code 3 is wrong because **i is meaningless in this context.

Question 10

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

    int j = 0;

    do
    {

        switch (j)
        {

        case 5:
            {

                cout << "OX" << endl;

                break;

            }

        default:
            {

                j = 0;

            }

        }

    }while (j++);

    return 0;

}

A
0
B
C
2
D
1
Soln.
Ans: A
OX never prints. The loop ends after one 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.