C++ Quiz on Mixed Topics Set 25

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. 25 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?
struct SMarks
{

    int p:3;

    int c:3;

    int m:3;

};

void main()
{

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

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

}

A
2 -6 5
B
2 2 -3
C
2 2 1
D
Compiler Error
Soln.
Ans: B
This struct is based on bit fields. The following statement
int p:3
says that the data member p will have 3 bits only. The MSB will be sign bit because of 'int'. Had it been unsigned, then no sign bit. 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 -3. The display is 2 2 -3.

Question 2

What could go wrong in the following code ? [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
The function OpenAndRead can fail if the user enters a file name that doesnot exist.
B
The code will never fail.
C
The code doesnot have enough provisions for giving feedback to the user regarding failure conditions.
D
The arrows < are="" in="" the="" wrong="">
Soln.
Ans: AC
The code doesnot have enough provisions for giving feedback to the user regarding failure conditions because the function OpenAndRead has been called on the assumption that the user will type the correct file path.

Question 3

What is the output ?
void main()
{

    const int i=4;

    float j = 0;

    j = ++i;

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

}

A
4 0
B
4 0.0
C
4 5
D
Doesn't compile
Soln.
Ans: D
Doesn't compile because i is a constant. you cannot change the value of constant.

Question 4

What is the output of the following program?
int call(int address)
{

    address++;

    return address;

}

void main()
{

    static int main;

    int x;

    x=call(main);

    printf("%d ",x);

}

A
0
B
1
C
Garbage
D
Compiler error
Soln.
Ans: B
'main' can be name variable in the main and other functions. Following is also OK
int Increment (int& Increment)
{

    Increment++;

    return Increment;

}

void main()
{

    int x = 5;

    Increment (x);

}


Question 5

What is the output?
int main()
{

    int y = 100;

    int *p;

    p = (int*)malloc(sizeof(int));

    *p = 10;

    y = y/*p; /*dividing y by *p */;

    printf ("%d", y);

    return 0;

}

A
100
B
10
C
Error
D
Unpredicatble
Soln.
Ans: A
The line
y = y/*p; /*dividing y by *p */;

is actually a comment /**/ so effectively it is
y = y;


Question 6

Consider the code below:
int main()
{

    int b;

    int* p;

    /*Statement 1*/
    b = 10;

    {

        /*Statement 2*/
        int a = 0;

        {

            int x = 0;

            /*Statement 3*/
            p = &b;

        }

    }

    /*Statement 4*/
    *p = 3;

    return 0;

}

Which statement has error in the code above ?
A
The code has no error.
B
Statement 1.
C
Statement 2.
D
Statement 3.
Soln.
Ans: A
There is no bug because p stores the address of b and b is very much in scope throughout

Question 7

What will be output if you will compile and execute the following c code?
int main()
{

    int array[]={10,20,30,40};

    printf("%d",-2[array]);

    return 0;

}

A
Compiler Error
B
Garbage
C
-30
D
10
Soln.
Ans: C
In c, array[2]= *(array+2) = *(2+array) = 2[array] = 30. So -2[array] is -30.

Question 8

The C-String c in the following code
char *c = "4";

A
is not NULL-terminated.
B
is NULL-terminated by the compiler.
C
the statement doesnot compile.
D
indeterminate or contains garbage.
Soln.
Ans: B
NULL terminated by compiler

Question 9

What will be the output of?
int main()
{

    printf ("%d", printf ("Hello"));

    return 0;

}

A
Hello
B
0
C
Hello5
D
Compiler Error
Soln.
Ans: C
The inner printf first prints Hello, then it returns the number of characters printed which is 5. The outer printf then prints 5.

Question 10

What will be output if you will compile and execute the following c code?
void main()
{

    int a,b;

    a=1,3,15;

    b=(2,4,6);

    printf("%d ",a+b);

}

A
3
B
21
C
17
D
7
Soln.
Ans: D
In c comma behaves as separator as well as operator.
a=1, 3, 15;

b= (2, 4, 6);

In the above two statements comma is working as operator. Comma enjoys least precedence and associative is left to right. Assigning the priority of each operator in the first statement:

Assigning the priority of each operator in the second statement:

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.