C++ Quiz on Mixed Topics Set 26

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. 26 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

How can we make the following class better ? [More than one options can be correct]
// class definition
class CFile
{

    // returns true if a file strFileName exists
public:
    bool FileExists (const string& strFileName);

    // opens and reads the file strFileName
public:
    void OpenAndRead (const string& strFileName);

};

A
Add a private const data-member to save the name of the file, and also add a parametrized constructor and remove the arguments to both the functions.
B
Add a private data-member to save the name of the file, and also add a parametrized constructor. Remove the arguments to both the functions.
C
Make both the functions static.
D
ALL of the above
Soln.
Ans: D
All of the above depending on the design will help in one way or the other.

Question 2

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

    int i=1,j=2;

    switch(i)
    {

        case 1: printf("GOOD");

        break;

        case j: printf("BAD");

        break;

    }

}

A
Compiler Error
B
undefined
C
GOOD
D
BAD
Soln.
Ans: A
Compiler Error: Constant expression required in function main.

The case statement can have only constant expressions (this implies that we cannotuse variable names directly so an error).Note:Enumerated types can be used in case statements.


Question 3

What will be output if you will compile and execute the following c code?
int * call();

int main()
{

    int *ptr;

    ptr=call();

    printf("%d",*ptr);

    return 0;

}

int * call()
{

    int a=25;

    a++;

    return &a;

}

A
25
B
26
C
Garbage Value
D
Compiler Error
Soln.
Ans: C
In this question variable a is a local variable and its scope and visibility is within the function call. After returning the address of a by function call variable a became dead while pointer ptr is still pointing to address of variable a. This problem is known as dangling pointer problem. If you compile and run this program you are likely to see 26 as the answer, but this code has a logical error or bug.

Question 4

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

    int i = 9;

    if (i -= 9)
    {

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

    }

    if (i)
    {

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

    }

}

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

Question 5

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

    break;

}

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

Question 6

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

    int j = 0;

    do
    {

        switch (j)
        {

        case 5:
            {

                cout << "OX" << endl;

                return 0;

            }

        default:
            {

                j++;

            }

        }

    }while (true);

    return 0;

}

A
4
B
1
C
3
D
5
Soln.
Ans: B
OX prints once.

Question 7

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

    extern int out;

    printf("%d", out);

}

int out=100;

A
Compiler Error in both C++ and C compilers
B
no error, output is 100 in both C and C++ compilers.
C
linker error
D
no error, output is 100 in a C++ compiler and compiler error in a C compiler.
Soln.
Ans: B
extern int out is linked as 100. So display is 100.

Question 8

What is '*' doing in the following code ?
int main()
{

    int i = 9, j = 10;

    int *k;

    return 0;

}

A
Multiplying value of k with int.
B
The usage is incorrect as per C-Standard.
C
declaring an int* variable identifier k.
D
Bitwise XOR operation.
Soln.
Ans: C

Question 9

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

    int i = 0;

    switch (i)
    {

    case 0:
    default:
        break;

    }

    return 0;

}

A
case 1 is missing
B
default is illegal
C
Nothing is wrong
D
break and default cannot be together
Soln.
Ans: C
The switch is OK

Question 10

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

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