C++ Quiz on Mixed Topics Set 4

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. 4 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

int a = 0;

int increment()
{

    a++;

    return a;

}

int main(void)
{

    a = increment ();

    cout << a << endl;

    return 0;

}

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

Question 2

What is the output of the following program ?
#define int char
void main()
{

    int i=65;

    // assume the size of an int = 4 and size of a char = 1
    printf("sizeof(i)=%d",sizeof(i));

}

A
Compiler Error
B
sizeof(i)=1
C
linker error
D
sizeof(i)=4
Soln.
Ans: B
the #define replaces the string int by the macrochar

Question 3

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

    int a=10;

    printf("%d %d %d", a, a++, ++a); return 0;

}

A
12 11 11
B
11 11 12
C
12 10 10
D
Indeterminate
Soln.
Ans: D
The behavior is undefined because of c-sequence points. The variable 'a' cannot be altered twice during a function call. NOTE: some people say the answer is obtained by evaluating the arguments from left to right in printf. They are wrong in this particular case because you are altering the same variable. Different compilers will give different answers. VC++ 2010 gives 12 11 12.

Question 4

How many times does the following loop execute ?
for (i = 0; i < 10; i++)
{

}

A
10 times
B
for ever
C
9 times
D
11 times
Soln.
Ans: A

Question 5

What is the output?
#define F abc
#define B def
#define FB(arg) #arg
#define FB1(arg) FB(arg)
int main()
{

    printf ("%s\n", FB(F B));

    printf ("%s\n",FB1(F B));

    return 0;

}

A
FB
FB1
B
F B
abc def
C
FC
abc
D
None of the above
Soln.
Ans: A
The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal.The following is the track
printf ("%s\n", FB(F B));

printf ("%s\n",FB1(F B));

Change to
printf ("%s\n", "F B");

printf ("%s\n",FB(F B));

the second printf further changes as follows
===> printf ("%s\n",FB(abc def));

===> printf ("%s\n","abc def");


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

Question 7

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

    char string[]="Hello World";

    display(string);

}

void display(char *string)
{

    printf("%s",string);

}

A
Compiler Error
B
Hello World
C
linker error
D
World
Soln.
Ans: A
Compiler Error : Type mismatch in re-declaration offunction display

Explanation :In third line, when the function display is encountered,a C-compiler doesn't know anything about the functiondisplay. It assumes the arguments and return types tobe integers, (which is the default type). When it sees theactual function display, the arguments and typecontradicts with what it has assumed previously. Hencea compile time error occurs. A C++ compiler also gives error: Undefined identifier


Question 8

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

    int a = -12;

    a= a >> 3;

    printf("%d", a);

}

A
-4
B
-3
C
-2
D
Unpredictable
Soln.
Ans: D
Right shifting of a negative number is implementation dependent, so the behaviour is unpredictable.As per the C Standard
The result of E1 >> E2 is
E1 right-shifted E2 bit positions.
If E1 has an unsigned type or
if E1 has a signed type and a nonnegative value,
the value of the result is the integral part of the
quotient of E1 / 2E2. If E1 has a signed type and a
negative value, the resulting value is
implementation-defined.

Question 9

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

    int i=5;

    printf("%d",i+++++i);

}

A
Compiler Error
B
undefined
C
12
D
11
Soln.
Ans: A
Compiler error because the The expression i+++++i is parsed as
i++ ++ + i
which is an illegal combination ofoperators.

NOTE: The following would be OK syntax wise -

i+++i;

Is parsed as: i++ + i;

But please be cautious because it would be undefined behavior because of sequence points.

Question 10

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

    printf("\nab");

    printf("\bsi");

    printf("\rha");

}

A
ha
B
abbsi
C
absi
D
asi
Soln.
Ans: A
As tested on VC++ compiler: The third line has a line feed so the console displays 'ha'.

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