C++ Quiz on Mixed Topics Set 29

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

Which of the following codes is correct ?
void afunc(int x, char* c)
{

    int g = 0;

    g = x;

}

/*code 1*/
int main ()
{

    int i = 0;

    afunc (i, "c");

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    afunc ("c", "x");

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc ("x", "c");

    return 0;

}

A
Code 1 and Code 3 both are correct
B
Code 3
C
Code 2
D
Code 1
Soln.
Ans: D
Code 2 and Code 3 are wrong because they do not pass the first argument by int.

Question 2

The elements of an array in C
A
are guaranteed to be contaguous.
B
it is upto the compiler where it allocates them.
C
can be accessed by the operator [].
D
Only (a) and (c) are correct.
Soln.
Ans: D

Question 3

What is the value of i [4] in the following code -
int main ()
{

    int i [20] = {20};

    ....
}

A
0
B
un-known or garbage
C
20
D
4
Soln.
Ans: A
- i [0] is 20 and all others contain zeroes

Question 4

void afunc(char* c)
{

}

/*code 1*/
int main ()
{

    char cX [] = "ABC";

    afunc (cX);

    return 0;

}

/*code 2*/
int main ()
{

    int i = 0;

    afunc (i);

    return 0;

}

/*code 3*/
int main ()
{

    int i = 0;

    afunc ('c');

    return 0;

}

Which is code is correct ?
A
Code 3
B
Code 1
C
Code 1 and Code 3 both are correct
D
Code 2
Soln.
Ans: B
Code 1. Code 2 and Code 3 are wrong because they don't pass the argument as char*.

Question 5

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

    int x;

    for(x=1; x<=5; x++);

    printf("%d",x);

    return 0;

}

A
Infinite loop
B
6
C
Compiler Error
D
4
Soln.
Ans: B
Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false.

Question 6

Examine the code and give the answer.
int main()
{

    char c;

    /*Statement 1*/
    scanf("%c",&c);

    printf("%c\n",c);

    /*Statement 2*/
    scanf(" %c",&c);

    printf("%c\n",c);

    return 0;

}

A
Statement 1 and Statement 2 both behave identically and both are correct.
B
Statement 1 is correct but Statement 2 gives a runtime error.
C
Statement 2 is correct but Statement 1 gives a runtime error.
D
None of the above.
Soln.
Ans: A
The second scanf has a leading white space which is always skipped, so both the statements are identical in behaviour.

Question 7

void increment(int a)
{

    a++;

}

int main(void)
{

    int i = 10;

    increment(i);

    cout << i << endl;

    return 0;

}

What is the output ?
A
9
B
11
C
Compiler Error
D
10
Soln.
Ans: D
It displays 10. The a++ in the function above does not affect i because a is passed by value.

Question 8

Consider the code below
void MyFunction(int x)
{

    int g = 0;

    g = x;

}

Here the function MyFunction
A
accepts two arguments and returns something
B
accepts no arguments and returns nothing
C
accepts one argument and returns nothing
D
accepts one argument and returns an int
Soln.
Ans: C
accepts one argument and returns nothing, the function accepts an int argument by value and returns void, which means it is not supposed to return anything.

Question 9

What is the output ?
int main ()
{

    int m = 1, n = 20, q = 20;

    if (q/n/m*1)
    printf ("X");

    else
    printf("Y");

    printf ("Z");

    return 0;

}

A
X
B
Y
C
YZ
D
XZ
Soln.
Ans: CD
The above code is equivalent to
int main ()
{

    int m = 1, n = 20, q = 20;

    if (1)
    {

        printf ("X");

    }

    else
    {

        printf("Y");

    }

    printf ("Z");

    return 0;

}

So the output is self-explanatory.

Question 10

What is the output ?
int i = 2, j, k = 2;

int main()
{

    int i;

    printf ("i = %d\t", i);

    printf ("j = %d\t", j);

    printf ("k = %d\t", k);

    return 0;

}

A
i = 2 j = 0 k = 2
B
i = 2 j = Garbage k = 2
C
i = 2 j = 0 k = 2
D
i = Garbage j = 0 k = 2
Soln.
Ans: D
The i that is closer in scope contains garbage.

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.