C++ Quiz on Mixed Topics Set 32

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. 32 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 will be output if you will compile and execute the following c code?
#define message "union is\
power of c"
void main()
{

    printf("%s",message);

}

A
Error
B
union is power of c
C
union is
D
power of c
Soln.
Ans: B
The symbol 'message' is defined as "union is power of c" because the slash \ is used to continue in new line for readability. Following is equivalent
#define message "union is\
power of c"
to
#define message "union is power of c"

Question 2

What is the output?
int main()
{

    int a=1;

    int b = 9;

    switch(a)
    {

        b=20;

    case 1:
        {

            printf("b is %d\n",b);

        }

        break;

    default:
        {

            printf("b is %d\n",b);

        }

        break;

    }

    return 0;

}

A
b is 20
B
b is 9
C
Compiler Error
D
Linker Error
Soln.
Ans: B
The program compiles succesfully and case 1 operates. b never gets 20.

Question 3

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

    for(int i = 0, j = 0; (i < 128) || (0 == j); i+=2)
    {

    }

    return 0;

}

A
64
B
63
C
128
D
forever
Soln.
Ans: D
The loop executes for ever.

Question 4

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

    int i=10;

    i=!i>14;

    printf ("i=%d",i);

}

A
Compiler Error
B
i = 1
C
linker error
D
i = 0
Soln.
Ans: B
In the expression !i>14 , NOT (!) operator has moreprecedence than ' >' symbol. ! is a unary logicaloperator. !i (!10) is 0 (not of true is false). 0>14 is false(zero).

Question 5

What will be output if you will compile and execute the following c code?
struct SMarks
{

    unsigned int p:3;

    unsigned int c:3;

    unsigned int m:3;

};

void main()
{

    struct SMarks s = {2,-6,5};

    printf("%d %d %d", s.p, s.c, s.m);

}

A
2 2 5
B
2 2 -3
C
2 2 1
D
Compiler Error
Soln.
Ans: A
This struct is based on bit fields. The following statement
unsigned int p:3
says that the data member p will have 3 bits only. There is no sign bit because of 'unsigned int'. In the above question p is 2, c is -6 and m is 5. The binary representation of p is 00000010. The lower three bits are stored in p so p is 010 or +2. Similarly -6 being 11111010, so c gets 010 or +2, and 5 being 00000101, so m gets 101 or +5[m is unsigned, so no sign bit !]. The display is 2 2 5.

Question 6

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
Logical error is possible if a user doesnot type an alphabet that is uppercase.
B
The arrows >> are in the wrong direction.
C
Bad data error if a user types a string of characters rather than a single character.
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 7

What is the output ?
int i = 40;

int main ()
{

    do
    {

        printf ("%d", i++);

    }while (5, 4, 3, 2, 1, 0);

    return 0;

}

A
40 41 42 43 44 45
B
40
C
Error
D
Infinite Loop
Soln.
Ans: B
The while loop has a comma operator that evaluates to 0, which is the last argument. The loop executes only once, so the output is 40.

Question 8

Consider the code below
int* MyFunction(int* x)
{

    int g = 0;

    g = x;

    return x;

}

Here the function MyFunction
A
accepts an argument by address and returns an int by value
B
accepts an argument by value and returns an int by value
C
accepts an argument by address and returns an int by address
D
accepts an argument by value and returns an int by value
Soln.
Ans: C
accepts an argument by address and returns an int by address. arguments and return types that are passed by address can be recognized by '*' after the datatype. So int* stands for 'int by address'. We can also say 'int pointer'

Question 9

What is the output of the following?
int main()
{

    int i = 4, x;

    x = (++i + ++i + ++i);

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

}

A
Undefined
B
21
C
15
D
12
Soln.
Ans: A
The sequence points of C-Language cause the statement
x = (++i + ++i + ++i);

to have an undefined behaviour. In some places the answer is given as 21, which is not correct. If you run it on your compiler, you might get 21, but the behaviour is not standard, it is compiler depemdent. Such a thing is called indeterminate or undefined behaviour.

Question 10

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 2
B
Code 3
C
Code 1
D
Code 1 and Code 3 both are correct
Soln.
Ans: A
Code 1 is wrong because it doesn't pass the argument by int*. Code 3 is wrong because i* is meaningless in this context.

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.