C++ Quiz on Mixed Topics Set 13

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

Last Reviewed and Updated on May 17, 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

int a = 0;

int increment()
{

    a++;

    return a;

}

int main(void)
{

    increment ();

    cout << a << endl;

    return 0;

}

What is the output of the above program ?
A
2
B
Compiler error
C
1
D
0
Soln.
Ans: C
It displays 1. The a++ in the function above affects a which is in global scope.

Question 2

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

    do
    {

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

            cout << "OX" << endl;

            if (2 == i) break;

        }

    }while (false);

    return 0;

}

A
1
B
3
C
never
D
forever
Soln.
Ans: B
Thrice.

Question 3

Consider the code below
int MyFunction(int x)
{

    int g = 0;

    g = x;

}

Here the function MyFunction
A
accepts one argument and returns an int
B
accepts one argument and returns nothing
C
accepts no arguments and returns nothing
D
accepts two arguments and returns something
Soln.
Ans: A
accepts one argument and returns an int, the function accepts an int argument by value and returns an int by value.

Question 4

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

    extern int i;

    i=20;

    printf("%d",i);

}

A
Compiler Error
B
Linker Error
C
20
D
-20
Soln.
Ans: B
Linker Error : Undefined symbol '_i'

extern storage class in the followingdeclaration,

extern int i;

specifies to the compiler that the memory for i isallocated in some other program and that address willbe given to the current program at the time of linking.But linker finds that no other variable of name i isavailable in any other program with memory spaceallocated for it. Hence a linker error has occurred .

Question 5

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

What is the output?
int main ()
{

    switch (printf ("AB"))
    {

    case 1:
        printf ("A");

        break;

    case 2:
        printf ("B");

        break;

    case 3:
        printf ("C");

        break;

    default:
        printf ("D");

    }

    return 0;

}

A
ABB
B
ABA
C
AAB
D
Error
Soln.
Ans: A
printf of switch prints AB, returns the number of characters printed which is 2, then the case 2 prints its B.

Question 7

Which line below should need a pre-validation if this code should never fail ?
int main ()
{

    /* line 1 */
    int iAge;

    /* line 2 */
    cout << "Enter you age ?";

    /* line 3 */
    cin >> iAge;

    /* line 4 */
    cout << "Your age is: " << iAge << endl;

}

A
Line 4
B
Line 1
C
Line 2
D
Line 3
Soln.
Ans: D
Line 3 can be a source of error because if the user enters a very large number, or doesnot enter a number at all.

Question 8

What is the output in the following C++ program?
class X
{

public:
    X()
    {

        cout << "C-tor Called \n";

    }

};

void main()
{

}

X x;

A
No Output
B
C-tor Called
C
Compiler Error
D
Linker Error
Soln.
Ans: B
The variable x in the global namespace causes the c-tor to display the message.

Question 9

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

    static char names[5][20]=
    {

        "pascal",
        "ada",
        "cobol",
        "fortran",
        "perl"
    };

    int i;

    char *t=names[3];

    names[3]=names[4];

    names[4]=t;

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

        printf("%s",names[i]);

    }

}

A
Compiler Error
B
pascal ada ada cobol perl fortran
C
pascal ada fortran cobol perl
D
garbage
Soln.
Ans: A
Compiler error: names [3] is not an l-value.

EXPLANATION: An l-value can be assigned. The following is an error because 3 is not an l-value -

int u;

3 = u;

In the above question names [3] being "fortran" cannot be assigned to.

Question 10

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

    clrscr();

}

clrscr();

A
undefined behaviour
B
No output, and the behaviour would be perfectly OK
C
compiler error
D
linker error
Soln.
Ans: B
The first clrscr() occurs inside a function. So it becomes a function call. In the secondclrscr(); is a function declaration (because it is not inside any function).

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