C++ Quiz on Mixed Topics Set 21

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. 21 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 ()
{

    switch (printf ("Hello" + 1))
    {

    case 1:
        printf ("A");

        break;

    case 2:
        printf ("B");

        break;

    case 3:
        printf ("C");

        break;

    default:
        printf ("D");

    }

    return 0;

}

A
A
B
elloD
C
AB
D
Error
Soln.
Ans: B
"Hello" + 1
is equivalent to
char* k = "Hello";

k + 1 points to ello
so
printf("Hello" + 1)
prints ello and returns 4, so the default prints D.

Question 2

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

    char c=125;

    c=c+10;

    printf("%d",c);

    return 0;

}

A
135
B
+INF
C
-121
D
-8
Soln.
Ans: C
Consider the following table
125 --> 125
126 --> 126
127 --> 127
128 --> 0
129 --> -127
130 --> -126
131 --> -125
132 --> -124
133 --> -123
134 --> -122
135 --> -121

Question 3

Which lines of the following program will be printed ?
int main ()
{

    int i = 9;

    if (i)
    {

        /* Line 1 */
        cout << "Inside first 'if'" << endl;

    }

    if (i - 9)
    {

        /* Line 2 */
        cout << "Inside second 'if'" << endl;

    }

}

A
Line 1 only will be printed
B
None will be printed
C
Line 2 only will be printed
D
Line 1 as well as Line 2 will be printed
Soln.
Ans: A
Line 1 only will be printed

Question 4

What is the output of?
int main()
{

    printf ("%c", printf ("abc"));

    return 0;

}

A
abc
B
a
C
D
None of the above
Soln.
Ans: C
The inner printf first prints abc, then it returns the number of characters printed which is 3. The outer printf then prints character equivalent of 3.

Question 5

Suppose we have a character string char cX [] = "abcxyz". What best describes the logic for displaying the string on your monitor, one character at a time ?
A
B
C
D
Soln.
Ans: C
First count the number of characters in the string, then if there are any more characters yet to be displayed, read the next character, display it keep doing it till no more characters are yet to be processed.

Question 6

How many times does the following loop execute ?
int main()
{

     for(int i = 0; i <= 128; i++)
     {
     }
     return 0;

}

A
129
B
126
C
128
D
127
Soln.
Ans: A
The loop executes 129 times.

Question 7

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

    printf("%s","c" "question" "bank");

}

A
c question bank
B
c
C
bank
D
cquestionbank
Soln.
Ans: D
In c string constant "xy" is same as "x" "y". It is also same as "x""y".

Question 8

What will be output if you will compile and execute the following c code?
int extern x;

void main()
{

    printf("%d",x);

    x=2;

}

int x=23;

A
2
B
3
C
23
D
Compiler error
Soln.
Ans: C
'extern' declares the existence of x as an int. The linker then links it to the global variable x
int x = 23;


Question 9

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

    int i,j = 0;

    for(i=0;i<=10;i++)
    {

        j+=5;

        assert(i<5);

    }

}

A
Compiler Error in both C++ and C compilers
B
No display, no error
C
assertion failure
D
linker error
Soln.
Ans: C
Assertion will fail when i becomes greater than 5 in the for loop.

Question 10

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.

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.