C++ Quiz on Mixed Topics Set 6

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. 6 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 '&' doing in the following code ?
int main()
{

    int i = 9, j = 10, k = 0;

    k = i & j;

    return 0;

}

A
Multiplying value of i by address of j.
B
Performing a bitwise logical AND between i and j.
C
Performing a logical AND between i and j.
D
Multiplying j by address of i.
Soln.
Ans: B

Question 2

Which statement in the code below has a bug ?[There is no compiler error]
/* Statement 1*/
int* fx ()
{

    int *i = new int;

    *i = 99;

    return i;

}

int main()
{

    /* Statement 2*/
    int* p = fx ();

    /* Statement 3*/
    *p = 8;

    /* Statement 4*/
    delete p;

    return 0;

}

A
Statement 2
B
No Bug
C
Statement 4
D
Statement 3
Soln.
Ans: B
There is no bug because i is allocated on heap and it stays in scope till it is deleted.

Question 3

Which statement has error in the code below
int main()
{

    int b;

    int a;

    /*Statement 1*/
    b = 10;

    do
    {

        /*Statement 2*/
        int x = 0;

    }while (b-- > 0);

    /*Statement 3*/
    a = x;

    return 0;

}

A
Statement 1.
B
The code has no error.
C
Statement 3.
D
Statement 2.
Soln.
Ans: C
Statement 3 has an error because variable x is not in scope

Question 4

What is the output?
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int max(int x, int y)
{

    (x > y) ? return x : return y;

}

int main()
{

    int a = 10, b = 20;

    PrintInt(a);

    PrintInt(b);

    PrintInt(max(a,b));

    return 0;

}

A
Compiler Error
B
10
20
20
C
a : 10
b : 20
max(a,b) : 10
D
a : 10
b : 20
max(a,b) : 20
Soln.
Ans: A
The statement
(x > y) ? return x : return y;

doesnot compile. It can be corrected like this
return (x > y) ? x : y;

The answer would be D then.

Question 5

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

    char s[]={'a','b','c','\n','c','\0'};

    char *p,*str,*str1;

    p=&s[3];

    str=p;

    str1=s;

    printf("%d",++*p + ++*str1-32);

}

A
Compiler Error
B
undefined
C
77
D
32
Soln.
Ans: C
p is pointing to character '\n'.str1 is pointing to character 'a'

++*p means:"p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it isincremented to 11. the value of ++*p is 11.

++*str1 means: "str1 is pointing to 'a'that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 isadded and result is subtracted from 32.i.e. (11+98-32)=77;


Question 6

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 = 0
C
i is = 0, j = 0
D
i is = 1, j = 1
Soln.
Ans: A
i is = 0, j = 1. The if prevents the increment of i.

Question 7

Which line below should need a pre-validation if this code should never fail ?
int main ()
{

    /* line 1 */
    int iAge;

    /* line 2 */
    cout << "Enter you age ?";

    /* line 3 */
    cin >> iAge;

    /* line 4 */
    cout << "Your age is: " << iAge << endl;

}

A
Line 4
B
Line 3
C
Line 1
D
Line 2
Soln.
Ans: B
Line 3 can be a source of error because if the user enters a very large number, or doesnot enter a number at all.

Question 8

What is the output?
int GetBitCount(unsigned int x)
{

    int count=0;

    while(x)
    {

        count++;

        x = x&(x-1);

    }

    return count;

}

int main()
{

    int i = 3;

    printf ("%u", GetBitCount (i));

    return 0;

}

A
3
B
2
C
1
D
0
Soln.
Ans: B
The function GetBitCount returns the number of 1's in the binary representation of x. So for x = 3 we have 3 = 101 ⇒ 2

Question 9

What is the output?
#define SIZE 10
void size(int arr[SIZE])
{

    printf("size of array is:%d\n",sizeof(arr));

}

int main()
{

    int arr[SIZE];

    size(arr);

    return 0;

}

NOTE: ASSUME THE SIZE OF AN int IS 32-bits,
and the address is also 32-bits
A
40
B
80
C
4
D
10
Soln.
Ans: C
arr is the starting address of the array 'arr' and such has a sizeof equal to the size of an address which is 32-bits or 4 bytes. So the display is 4.

Question 10

What is the output ?
int main()
{

    int a=3;

    const char* k = "Hello";

    printf (&(k [a]));

    return 0;

}

A
3
B
lo
C
Hello
D
Compiler Error
Soln.
Ans: B
k [a] is same as *(k + a) ie, *(k + 3) ie, the second 'l' of Hello. It's address is &(*(k+3)) ie, &k[3] ie &k [a]. When &k [a] is passed to printf it prints the string starting at 'l', ie, it prints lo !

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