C++ Quiz on Mixed Topics Set 19

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. 19 in this series.

Last Reviewed and Updated on October 26, 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 will the last printf display?
int main()
{

    int i;

    i = 10;

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

    printf("sizeof(i++) is: %d\n",sizeof(i++));

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

    return 0;

}

A
11
B
10
C
Compiler Error
D
No Display
Soln.
Ans: B
The sizeof (i++) is evaluated at design time and is never executed at run time, so i stays 10.

Question 2

What is the output?
int main()
{

    int var1 = 10, var2 = 12, var3 = 12;

    var1 = var2 == var3;

    printf ("%d", var1);

    return 0;

}

A
Compiler Error
B
1
C
0
D
12
Soln.
Ans: B
The expression
var1 = var2 == var3;

is same as
var1 = (var2 == var3);

which is same as
var1 = 1;

because var2 == var3 is logical true.

Question 3

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

    int arr [] = {0, 1, 2, 3, 4};

    cout << arr [0, 1, 2, 3, 4] << endl;

}

A
0, 1, 2, 3, 4
B
0
C
4
D
Compiler Error
Soln.
Ans: C
The comma operator causes
arr [0, 1, 2, 3, 4]
to evaluate to
arr [4]

Question 4

What is printed at statement 4 ?
int main()
{

    /* Statement 1*/
    int i = 9;

    {

        /* Statement 2*/
        int i = 10;

        /* Statement 3*/
        i++;

    }

    /* Statement 4*/
    cout << i << endl;

    return 0;

}

A
10
B
9
C
0
D
11
Soln.
Ans: B
Statement 4 prints the value of i of Statement 1. The i of Statement 2 is incremented at Statement 3, but that i is not in scope any more.

Question 5

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

    int i [20] = {0};

}

A
0
B
un-known or garbage
C
20
D
4
Soln.
Ans: A
- zeros in all locations

Question 6

What is the output?
int main()
{

    int i = 6;

    if(((++i < 7) && ( i++/6)) || (++i <= 9))
    {

    }

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

    return 0;

}

A
8
B
7
C
9
D
0
Soln.
Ans: A
Only the following are executed
++i < 7="" and="" ++i=""><=>

Question 7

Consider the code below
int main()
{

    PlusOne (4);

    return 0;

}

int PlusOne (int i)
{

    return i + 1;

}

What happens when you run it ?
A
It doesnot compile
B
It adds 1 to 4 and returns
C
It throws a linker error
D
It doesnot display anything
Soln.
Ans: A
The program will not compile because PlusOne is used in main without declaring it first.

Question 8

int a = 0;

int increment()
{

    a++;

    return a;

}

int main(void)
{

    cout << increment () << endl;

    return 0;

}

What is the output of the above program ?
A
1
B
0
C
Compiler error
D
2
Soln.
Ans: A
It displays the return value 1.

Question 9

What is the output?
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{

    int d;

    for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    {

        printf("%d\n",array[d+1]);

    }

    return 0;

}

A
Nothing is printed
B
All elements of the array are printed
C
Garbage value is printed
D
Compiler Error
Soln.
Ans: A
This is a tricky question. The TOTAL_ELEMENTS is unsigned type. When it is compared to d, which is signed, then d is promoted to unsigned. so
d <= (TOTAL_ELEMENTS - 2)
becomes
(unsigned)d <= 5
which becomes
(unsigned)-1 <= 5
But since (unsigned)-1 is same as UINT_MAX, the above condition is always false. So the loop never runs.

Question 10

The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)?
int main()
{

    int n = 0;

    printf("Enter a number:\n");

    scanf("%d\n",n);

    printf("You entered %d \n",n);

    return 0;

}

A
scanf should have &n as the second argument.
B
printf should have &n as the second argument.
C
scanf should come before printf.
D
both printf should come before scanf.
Soln.
Ans: A
Self-Explanatory.

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