C++ Quiz on Mixed Topics Set 11

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. 11 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
int main ()
{

    int a = 5, b = 10;

    if (++a || ++b)
    {

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

    }

    else
    {

        printf ("hello");

    }

    return 0;

}

A
6 10
B
5 10
C
6 11
D
Hello
Soln.
Ans: A
Since ++a is true, ++b is never executed. So the output is 6 10

Question 2

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

    int a[2][3][2] =
    {

        {

            {

                2,4
            },

            {

                7,8
            },

            {

                3,4
            }

        },

        {

            {

                2,2
            },

            {

                2,3
            },

            {

                3,4
            }

        }

    };

    printf("%u %u %u %d \n",a,*a,**a,***a);

    printf("%u %u %u %d \n", a+1,*a+1,**a+1,***a+1);

}

A
Compiler Error in both C++ and C compilers
B
Garbage value
C
linker error
D
Compiler error only in C++
Soln.
Ans: B
Garbage value.

Question 3

What is the output?
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{

    printf("%s\n",h(f(1,2)));

    printf("%s\n",g(f(1,2)));

    return 0;

}

A
12
12
B
f(1, 2)
f(1, 2)
C
f(1, 2)
12
D
12
f(1, 2)
Soln.
Ans: C
The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal.The following is the track
printf("%s\n",g(f(1,2)));

printf("%s\n",h(f(1,2)));

changes to
printf("%s\n","f(1,2)");

printf("%s\n",g(f(1,2)));

the second printf further changes as follows
====> printf("%s\n",g(12));

====> printf("%s\n","12");


Question 4

What is displayed in the following program ?
int main ()
{

    int i = 0, j = 0;

    if (false)
    {

        i++;

        j++;

    }

    cout << "i is = " << i << ", j = " << j << endl;

}

A
i is = 0, j = 1
B
i is = 1, j = 1
C
i is = 1, j = 0
D
i is = 0, j = 0
Soln.
Ans: D
i is = 0, j = 0. The 'if' prevents the increment of both i and j.

Question 5

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 1 only will be printed
B
Line 2 only will be printed
C
None will be printed
D
Line 1 as well as Line 2 will be printed
Soln.
Ans: B
Line 2 only will be printed

Question 6

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

    struct xx
    {

        int x=3;

        char name[]="hello";

    };

    struct xx *s=malloc(sizeof(struct xx));

    printf("%d",s->x);

    printf("%s",s->name);

}

A
Compiler Error
B
undefined
C
hello
D
hellohello
Soln.
Ans: A
Initialization should not be done for non-integral and non-static structure members inside the structure declaration

Question 7

Consider the code below
void afunc(int& x, int& y, char & z)
{

    int g = 0;

    g = x;

}

What is the signature of afunc ?
A
void
B
void_int*_char_int
C
afunc_int*_int_char&
D
afunc_int&_int&_char&
Soln.
Ans: D
afunc_int&_int&_char&, because the signature comprises of function name and the type of arguments in respective order from left to right.

Question 8

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

    printf("%s", call(c/c++));

}

A
c/c++
B
c
C
Error
D
c++
Soln.
Ans: A
#define call(x) #x causes stringization of the argument. So
call (1)
becomes
"1"
and
call(c/c++)
becomes
"c/c++"

Question 9

What will be the position of the file marker?
a: fseek(ptr,0,SEEK_SET);

b: fseek(ptr,0,SEEK_CUR);

A
a: end, b: start
B
a: current, b: current
C
a: start, b: current
D
None of the above
Soln.
Ans: C
a: The SEEK_SET sets the file position marker to the starting of the file.

b: The SEEK_CUR sets the file position marker to the current position


Question 10

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

    int i=0;

    for(;i<=2;)
    {

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

        return 0;

    }

}

A
12
B
123
C
1
D
234
Soln.
Ans: C
The return statement causes the program to quit. NOTE: In for loop each part is optional.

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