C++ Quiz on Mixed Topics Set 22

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. 22 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
int main ()
{

    int x = -2;

    for (;x;printf ("%d\n", x++));

    return 0;

}

A
-2
-1
B
3
2
1
0
C
Infinite Loop
D
None of the above
Soln.
Ans: A
The condition in the for loop is
x;

which will be false when x++ of the printf reaches 0.

Question 2

Consider the code below and determine the output -
int main()
{

    int i = 0;

    int k = ++i;

    printf ("%d", k);

}

A
1
B
0
C
2
D
indeterminate
Soln.
Ans: A

Question 3

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

    char *str = "Hello world";

    printf("%d", printf("%s",str));

    return 0;

}

A
Hello World
B
11HelloWorld
C
Hello World11
D
None of the above
Soln.
Ans: C
Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf("Hello world") will return 11.

Question 4

What is the output of the following program ?
{

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

    int *p[ ] = {a,a+1,a+2,a+3,a+4};

    int **ptr = p;

    ptr++;

    printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);

    *ptr++;

    printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);

    *++ptr;

    printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);

    ++*ptr;

    printf("\n %d %d %d", ptr-p, *ptr-a, **ptr);

}

A
Compiler Error in both C++ and C compilers
B
Garbage value
C
linker error
D
111
222
333
344
Soln.
Ans: D
It is easy enough to rule out the first three options. The answer is D.

Question 5

What is wrong with the following switch.
int main()
{

    float i = 0;

    switch (i)
    {

    default:
    case 1:
    case 0:
        break;

    }

    return 0;

}

A
default is illegal
B
break and default cannot be together
C
case 2 is missing
D
i cannot be float
Soln.
Ans: D
The switch is illegal because only integral expression is allowed.

Question 6

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
89
C
linker error
D
77
Soln.
Ans: D
p is pointing to character '\n'. str1 is pointing to character 'a'.

++*p. "p is pointing to '\n'and that is incremented by one." the ASCII value of '\n' is 10, which is then incrementedto 11. The value of ++*p is 11.

++*str1, str1 is pointing to 'a' that is incremented by 1and it becomes 'b'. ASCII value of 'b' is 98.Now performing (11 + 98 - 32), we get 77("M");So we get the output 77 :: "M" (Ascii is 77).


Question 7

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

    int b;

    int a;

    /*Statement 1*/
    b = 10;

    {

        /*Statement 2*/
        int x = 0;

        {

            /*Statement 3*/
            int x = 0;

        }

        /*Statement 4*/
        a = x;

    }

    return 0;

}

A
The code has no error.
B
Statement 2.
C
Statement 3.
D
Statement 1.
Soln.
Ans: A
The code has no error

Question 8

Consider the code below
int PlusOne (int);

int main()
{

    PlusOne (PlusOne (4));

    return 0;

}

int PlusOne (int i)
{

    return i + 1;

}

What happens when you run it ? [More than one can be correct]
A
It doesnot compile
B
It throws a linker error
C
It doesnot display anything
D
It adds 1 to 4 and then 1 to 5 and returns
Soln.
Ans: CD
It adds 1 to 4 and then 1 to 5, returns and no display occurs.

Question 9

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

    char *str=NULL;

    strcpy(str,"cquestionbank");

    printf("%s",str);

    return 0;

}

A
cquestionbank
B
cquestionbank\0
C
it will print nothing
D
Fatal Error
Soln.
Ans: D
We cannot copy anything using strcpy function to the character pointer pointing to NULL.

Question 10

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

    int i, j;

    printf("%d",scanf("%d-%d",&i, &j));

    // value 10-10 is given as input here
}

A
Compiler Error
B
undefined
C
2
D
10-10
Soln.
Ans: C
Scanf returns number of items successfully read and not 1/0. Here 10-10 is given as inputwhich should have been scanned successfully. So number of items read is 2.

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.