C++ Quiz on Mixed Topics Set 3

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. 3 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 is the output ?
void main()
{

    int *j;

    {

        int i=10;

        j=&i;

    }

    printf("%d",*j);

}

A
10
B
Garbage value
C
Compiler Error
D
Linker Error
Soln.
Ans: B
The variable i is a block level variable and the visibility is inside that block only. But thelifetime of i is lifetime of the function so it lives upto the exit of main function. Since thei is still allocated space, *j prints the value stored in i since j points i.

Question 2

What happens ?
int main()
{

    int *a, i;

    a = (int*)malloc(SIZE*sizeof(int));

    /*Statement 1*/
    for (i=0; i<SIZE; i++)
    {

        *(a + i) = i * i;

    }

    /*Statement 2*/
    for (i=0; i<SIZE; i++)
    {

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

    }

    /*Statement 3*/
    free(a);

    return 0;

}

A
Statement 1 causes a runtime error
B
Statement 2 causes a runtime error
C
Statement 3 causes a runtime error
D
No Error occurs at runtime
Soln.
Ans: C
free(a) causes error because 'a' has changed to a location[in Statement 2] that was not allocated with malloc. so 'free' causes a runtime error.

Question 3

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

    int i=320;

    char *ptr=(char *)&i;

    printf("%d",*ptr);

    return 0;

}

A
320
B
1
C
64
D
Compiler Error
Soln.
Ans: C
As we know size of int data type is two byte while char pointer can pointer one byte at time.Memory representation of int i=320

So char pointer ptr is pointing to only first byte as shown above figure.*ptr i.e. content of first byte is 01000000 and its decimal value is 64.

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 += 9)
    {

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

    }

}

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

Question 5

What is the output
int main ()
{

    int x = 1;

    if (x--)
    printf ("X");

    --x;

    else
    {

        printf ("Y");

    }

    return 0;

}

A
X
B
Y
C
XY
D
Error
Soln.
Ans: D
The statement
--x;

comes just before 'else' which is illegal. Since no braces have been used, the above code is same as
int main ()
{

    int x = 1;

    if (x--)
    {

        printf ("X");

    }

    ERROR → --x;

    else
    {

        printf ("Y");

    }

    return 0;

}


Question 6

What is the output?
int main()
{

    int cnt = 5, a;

    do {
    a /= cnt;

} while (cnt --);

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

return 0;

}

A
Runtime error or Garbage
B
1
C
5
D
None of the above
Soln.
Ans: A
Self-Explanatory

Question 7

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 8

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

    char c='\08';

    printf("%d",c);

    return 0;

}

A
8
B
'8'
C
9
D
undefined
Soln.
Ans: D
There is no octal number 8.

Question 9

void afunc(int x, int y)
{

    int g = 0;

    g = x;

}

Which of the following is/are correct ?
A
x is passed by reference and y is also passed by reference
B
x is passed by reference and y is passed by value
C
x is passed by value and y is also passed by value
D
x is passed by value and y is passed by reference
Soln.
Ans: C
x is passed by value and y is also passed by value

Question 10

What is the output?
int main()
{

    int i, j = 2;

    printf ("%d\n", i);

    printf ("%d\n", j);

    return 0;

}

A
Garbage
2
B
Garbage
Garbage
C
0
2
D
0
Garbage
Soln.
Ans: A
Self Explanantory

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.