C++ Quiz on Mixed Topics Set 34

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. 34 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 of the following program ?
void main()
{

    int i=-1,j=-1,k=0,l=2,m;

    m=i++&&j++&&k++||l++;

    printf("%d %d %d %d %d",i,j,k,l,m);

}

A
Compiler Error
B
Linker Error
C
1 0 1 3 0
D
0 0 1 3 1
Soln.
Ans: D
Logical operations always give a result of 1 or 0 . Andalso the logical AND (&&) operator has higher priorityover the logical OR (||) operator. So the expression 'i++&& j++ && k++' is executed first. The result of thisexpression is 0 (-1 && -1 && 0 = 0). Now theexpression is 0 || 2 which evaluates to 1 (because ORoperator always gives 1 except for '0 || 0' combination forwhich it gives 0). So the value of m is 1. The valuesof other variables are also incremented by 1.

Question 2

What is the value of i in the following code
int i = (234 % 10) / 10;

A
23
B
4
C
3
D
0
Soln.
Ans: D
234 % 10 = 4 and 4 / 10 = 0

Question 3

What is the value of i in the following code
int i = 20 % 5
A
4
B
0
C
5
D
100
Soln.
Ans: B

Question 4

Which statement in the code below has a bug ? [There is no compiler error]
int main()
{

    int b;

    int* p;

    /*Statement 1*/
    b = 10;

    {

        /*Statement 2*/
        int a = 0;

        {

            int x = 0;

            /*Statement 3*/
            p = &x;

        }

    }

    /*Statement 4*/
    *p = 3;

    return 0;

}

A
Statement 1
B
Statement 2
C
Statement 4
D
Statement 3
Soln.
Ans: C
In Statement 4 even though 'p' is in scope but it stores the address of x which is not in scope.

Question 5

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

    float me = 1.1;

    double you = 1.1;

    if(me==you)
    printf("I love U");

    else
    printf("I hate U");

}

A
Compiler Error
B
Linker Error
C
I hate U
D
I love U
Soln.
Ans: C
For floating point numbers (float, double, long double)the values cannot be compared like int.

Rule of Thumb:Never compare or at-least be cautious when usingfloating point numbers with relational operators (== , >,<, <=, >=,!= ) .


Question 6

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

Question 7

Consider the code below.
void MyFunc(int i)
{

    i = i*2;

    return;

}

int main ()
{

    int x = 100;

    MyFunc (x);

    cout << x << endl;

}

Which of the following is correct ?
A
MyFunc accepts i by reference, and cout will display 200
B
MyFunc accepts i by value, and cout will display 100
C
MyFunc accepts i by reference, and cout will display 100
D
MyFunc accepts i by value, and cout will display 200
Soln.
Ans: B
MyFunc accepts i by value, so when x is passed as an argument it goes as its exact copy and any changes that occur inside MyFunc take place on that copy and not on x. So cout will display 100.

Question 8

What is the output ?
int main()
{

    int d = -1;

    printf ("%u", d);

    return 0;

}

A
-1
B
1
C
0
D
UINT_MAX
Soln.
Ans: D
d = -1 has binary representation of all bits = 1. The MSB is reserved for sign and has value 1.The %u in printf causes d to be treated as unsigned so the MSB also is treated as value bit. The same binary number with all its bits 1 becomes UINT_MAX.

Question 9

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 10

What is the output ?
void main()
{

    register int i=5;

    char j[]= "hello";

    printf("%s %d",j,i);

}

A
hello 5
B
hello hello
C
Garbage
D
Doesnot compile
Soln.
Ans: C
if you declare i as register compiler will treat it as ordinary integer and it will takeinteger value. i value may be stored either in register or in memory.

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