C++ Quiz on Mixed Topics Set 20

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. 20 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 will be output if you will compile and execute the following c code?
void main()
{

    printf("%s",__DATE__);

}

A
Compiler Error
B
Current system date
C
Current system date with time
D
null
Soln.
Ans: B
__DATE__ is global identifier which returns current system date.

Question 2

In the code below Line 3 has a potential error. Which is it ? [More than one options can be correct]
int main ()
{

    /* line 1 */
    int iX;

    /* line 2 */
    cout << "Enter latitude [0-180]: ";

    /* line 3 */
    cin >> iX;

    /* line 4 */
    cout << "The latitude is: " << iX << endl;

}

A
The arrows >> are in the wrong direction.
B
Bad data error if a user types a string of characters rather than an integer.
C
Logical error is possible if a user doesnot type an integer that is not in the range 0 - 180.
D
ALL of the above
Soln.
Ans: BC
Both logical as well as bad data errors are possible in this case. So we should have an eye on validation requirements whenever we write code. The direction of the arrows is correct.

Question 3

Which statement in the code below has a bug[the code below has 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
No Bug
B
Statement 3
C
Statement 2
D
Statement 4
Soln.
Ans: A
There is no bug because i is allocated on heap and it stays in scope till it is deleted

Question 4

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

    int x : 3 = 3;

    printf ("%d", x);

}

A
3
B
-3
C
5
D
Compiler Error
Soln.
Ans: D
Bit fields are not allowed outside structs. Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed as well. The address-of operator (&) cannot be applied to bit-field components.

Question 5

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

    printf("%d",sizeof(5.2));

    return 0;

}

A
sizeof(double)
B
sizeof(int)
C
sizeof(float)
D
Nothing
Soln.
Ans: A
Since 5.2 is a double, the output will be sizeof(double), which is 8 on VC++ 2010 32bit compiler.

Question 6

Consider the code below
int* MyFunction(int* x)
{

    int g = 0;

    g = x;

    return x;

}

Here the function MyFunction
A
accepts an argument by address and returns an int by value
B
accepts an argument by value and returns an int by value
C
accepts an argument by address and returns an int by address
D
accepts an argument by value and returns an int by value
Soln.
Ans: C
accepts an argument by address and returns an int by address. arguments and return types that are passed by address can be recognized by '*' after the datatype. So int* stands for 'int by address'. We can also say 'int pointer'

Question 7

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

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

    }

    return 0;

}

A
128
B
63
C
64
D
forever
Soln.
Ans: C
The loop executes 128/2 = 64 times.

Question 8

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

    int i=0;

    if( i==0 )
    {

        int j =((5,(i=3)),i=1);

        printf("%d",j);

    }

    else
    {

        printf("equal");

    }

}

A
5
B
3
C
1
D
equal
Soln.
Ans: C
The comma operator will set i = 3, then it will set i = 1 and j will get i.

Question 9

What is the output ?
extern int arr [20];

int main()
{

    printf ("%d\n", arr [1]);

    return 0;

}

int arr [20] = {20, 20};

A
Compiler Error
B
0
C
20
D
-20
Soln.
Ans: C
arr [20] has first two elements as 20 and the rest as 0. We are displaying the second one, so the output is 20.

Question 10

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

    int c=- -2;

    printf("c=%d",c);

}

A
Compiler Error
B
c = 2
C
linker error
D
c = -4
Soln.
Ans: B
Here unary minus (or negation) operator is used twice.Same math rules applies, i.e.. minus * minus= plus.

Note:However you cannot give like --2. Because -- operatorcan only be applied to variables as a decrementoperator (e.g., i--). 2 is a constant and not a variable.


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