C++ Quiz on Mixed Topics Set 16

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

What is the output ?
void main()
{

    int k=1;

    printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE");

}

A
1==1 is FALSE
B
1==1 is TRUE
C
1==0 is FALSE
D
0==1 is TRUE
Soln.
Ans: B
When two strings are placed together (or separated by white-space) they areconcatenated (this is called as "stringization" operation). So the string is as if it is givenas
"%d==1 is %s".
The conditional operator( ?: ) evaluates to "TRUE".

Question 2

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

    struct xx
    {

        int x;

        struct yy
        {

            char s;

            struct xx *p;

        };

        struct yy *q;

    };

}

A
Compiler Error
B
No output, no errors on C++ compiler. Errors on a C compiler.
C
linker error
D
Garbage
Soln.
Ans: B
The line
struct xx *p;

compiles because p is an address. NOTE: A C compiler will give an error - 'undefined struct xx', but C++ compiler compiles it because allocations are possible for an address because its size is known, 4 bytes on a 32-bit machine, for example.

Question 3

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
Make both the functions static.
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
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.
D
All of the above depending on the design will help in one way or the other.
Soln.
Ans: D
All of the above depending on the design will help in one way or the other.

Question 4

What is the output ?
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
int main()
{

    /* The powers of 10 */
    int pot[] =
    {

        0001,
        0010,
        0100,
        1000
    };

    int i;

    for(i=0;i<SIZEOF(pot);i++)
    {

        PrintInt(pot[i]);

    }

    return 0;

}

A
0001001001001000
B
0010001001001000
C
1001001001001000
D
pot[i]:1
pot[i]:8
pot[i]:64
pot[i]:1000
Soln.
Ans: D
The following
PrintInt(pot[i]);

becomes
printf("%s:%d\n","pot[i]",i)
because of the stringizing #

Question 5

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

    struct xx
    {

        int x;

        struct yy
        {

            char s;

            struct xx p;

        };

        struct yy *q;

    };

}

A
Compiler Error on both C and C++ compilers
B
No output, no errors on C++ compiler. Errors on a C compiler.
C
linker error
D
Garbage
Soln.
Ans: A
The line
struct xx p;

causes a compiler error - 'undefined struct xx' because the size of xx is not ascertainable. The precise error is 'undefined struct xx'

Question 6

How can we make the code in main() 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);

};

int main ()
{

    /* line 1 */
    string strFile;

    /* line 2 */
    cout << "Enter the name of the file to read: ";

    /* line 3 */
    getline (cin, strFile);

    CFile obj;

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

    obj.OpenAndRead (strFile);

}

A
Before line 4 add a conditional 'if' statement to check if the file exists.
B
Change the direction of all arrows >>
C
The code needs no improvements.
D
Add code to check of the file exists, and inform the user if the file doesnot exist.
Soln.
Ans: AD
Before line 4 add a conditional 'if' statement to check if the file exists, and inform the user if the file doesnot exist. This will make the code more user friendly and free from logical errors so that the user is not kept guessing as to what happened.

Question 7

Why do we code in 'C' language -
A
The C-code is human readable.
B
The C-code executes very fast.
C
C-language is a standardized language, and hence code can be made portable for use in LINUX/Windows/Mobile applications.
D
All of the above
Soln.
Ans: D

Question 8

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

    bool b = true;

    if (b = (b && false))
    {

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

    }

    if (b || true)
    {

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

    }

}

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

Question 9

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

    int i = 9;

    if (i)
    {

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

    }

    if (i - 9)
    {

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

    }

}

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

Question 10

Which statement has error in the code below
int main()
{

    int b;

    int a;

    /*Statement 1*/
    b = 10;

    {

        /*Statement 2*/
        int a = 0;

        {

            /*Statement 2*/
            int x = 0;

        }

        /*Statement 4*/
        a = a;

    }

    return 0;

}

A
The code has no error.
B
Statement 3.
C
Statement 2.
D
Statement 1.
Soln.
Ans: A
The code has no error

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