C++ Quiz on Mixed Topics Set 14

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. 14 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 ?
void main()
{

    extern int i;

    i=20;

    // assume addresses are 32-bits
    printf("%d",sizeof(i));

}

A
Compiler Error in both C++ and C compilers
B
undefined
C
linker error
D
no error, output is 4 in a C++ compiler and compiler error in a C compiler.
Soln.
Ans: C
Linker error: undefined symbol 'i'. extern declaration specifies that the variable i is defined somewhere else. The compilerpasses the external variable to be resolved by the linker. So compiler doesn't find anerror. During linking the linker searches for the definition of i. Since it is not found thelinker flags an error.

Question 2

What is the output here?
int main()
{

    int j = 1;

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

    return 0;

}

A
No Compiler Error but Unpredictable
B
6
C
3
D
Compiler Error
Soln.
Ans: A
The statement
printf ("%d", j+++j+++j);

is same as
printf ("%d", j++ + j++ +j);

but sequence points of C will cause the behaviour to be undefined because j is being modified and read more than once in the same statement. Different compilers will give different output. So such a code is not reliable, even though it gets compiled.

Question 3

Which lines of the following program will be printed ?
int main ()
{

    if (true)
    {

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

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

    }

}

A
Line 2 only will be printed
B
Line 1 only will be printed
C
None will be printed
D
Line 1 as well as Line 2 will be printed
Soln.
Ans: D
Line 1 as well as Line 2 will be printed because both them are in the common 'if' braces.

Question 4

What happens ?
void main()
{

    main();

}

A
Will execute endlessly
B
Will not compile
C
Will not link
D
Will stop after some time
Soln.
Ans: D
Stack overflow. main function calls itself again and again. Each time the function is called its returnaddress is stored in the call stack. Since there is no condition to terminate the functioncall, the call stack overflows at runtime. So it terminates the program and results in anerror.

Question 5

How many times does the following loop execute ?
for (i = 0; i < 10; i)
{

}

A
10 times
B
for ever
C
9 times
D
11 times
Soln.
Ans: B

Question 6

What is the output ?
int main ()
{

    int i = 1;

    for (i = 0; i = -1; i = 1)
    {

        printf ("%d", i);

        if (i != 1)
        {

            break;

        }

    }

    return 0;

}

A
-1
B
∞ loop
C
Error
D
1
Soln.
Ans: A
The for loop causes i to be 0, then checks the condition i = -1 which is true, so the loop executes, but the if condition causes a break and loop terminates. So output is -1.

Question 7

What is wrong with the following switch.
int main()
{

    int i = 0;

    switch (i)
    {

    default:
    case 1:
    case 0:
        break;

    }

    return 0;

}

A
break and default cannot be together
B
Nothing is wrong
C
default is illegal
D
case 2 is missing
Soln.
Ans: B
The switch is OK. You can put default anywhere, not necessarily at the end.

Question 8

What is the output?
int main()
{

    int i=43;

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

    return 0;

}

A
43
B
1234
C
4321
D
This is not a valid pogram
Soln.
Ans: C
printf returns the number of characters printed. So starting with the innermost printf we can see that the output is 4321

Question 9

What is the output of?
int main()
{

    int a = 15;

    printf ("%d", scanf ("%d", &a));

    return 0;

}

A
Prompt the user for a value then prints the value
B
Prompt the user for a value then prints 0
C
Prompt the user for a value then prints 1 if the user enters an input that can be stored in a.
D
None of the above
Soln.
Ans: C
scanf prompts the user to type something. If the user types a value that is successfully stored in 'a' then it returns 1 which is printed. If the value cannot be converted, scanf returns 0.

Question 10

In the code below Line 3 has a potential error. Which is it ? [More than one options can be correct]
int main ()
{

    /* line 1 */
    int iX;

    /* line 2 */
    cout << "Enter latitude [0-180]: ";

    /* line 3 */
    cin >> iX;

    /* line 4 */
    cout << "The latitude is: " << iX << endl;

}

A
The arrows >> are in the wrong direction.
B
Bad data error if a user types a string of characters rather than an integer.
C
Logical error is possible if a user doesnot type an integer that is not in the range 0 - 180.
D
All of the above.
Soln.
Ans: BC
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.

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.