C++ Quiz on Mixed Topics Set 18

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

Consider the code below
int PlusOne (int);

int PlusTwo (int);

int main()
{

    int y = PlusOne (4);

    y = PlusTwo (y);

    y = PlusOne (PlusTwo (PlusOne (y)));

    cout << y << endl;

}

int PlusOne (int i)
{

    return i + 1;

}

int PlusTwo (int i)
{

    return i + 2;

}

What is the output ?
A
11
B
9
C
10
D
Compiler error
Soln.
Ans: A
It displays 11

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 */
    char cX;

    /* line 2 */
    cout << "Enter an alphabet[A-Z]: ";

    /* line 3 */
    cin >> cX;

    /* line 4 */
    cout << "You entered: " << cX << endl;

}

A
Bad data error if a user types a string of characters rather than a single character.
B
The arrows >> are in the wrong direction.
C
Logical error is possible if a user doesnot type an alphabet that is uppercase.
D
All of the above
Soln.
Ans: AC
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

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

    int a=25;

    printf("%o %x", a, a);

}

A
31 19
B
25 25
C
25 0x25
D
12 42
Soln.
Ans: A
%o is used to print the number in octal base.%x is used to print the number in hexadecimal base.

Question 4

What is printed at statement 4 ?
/* Statement 1*/
int i = 9;

int main()
{

    {

        /* Statement 2*/
        int i = 10;

        /* Statement 3*/
        i++;

        /* Statement 4*/
        cout << (::i) << endl;

    }

    return 0;

}

A
0
B
9
C
11
D
10
Soln.
Ans: B
Statement 4 prints the value of i of Statement 1. The operator :: on i at statement 4 makes i to refer to global i of statement 1

Question 5

What is the output?
int main()
{

    int a = 1,2;

    printf("a : %d\n",a);

    return 0;

}

A
a : 1
B
a : 2
C
Compiler Error
D
Unpredictable
Soln.
Ans: C
The following has error
int a = 1,2;

Comma operator would work this way
int a = (1,2)

Question 6

What will be output if you will compile and execute the following c code?
struct SMarks
{

    int p:3;

    int c:3;

    unsigned int m:3;

};

void main()
{

    struct SMarks s = {2,-6,5};

    printf("%d %d %d", s.p, s.c, s.m);

}

A
2 2 5
B
2 2 -3
C
2 2 1
D
Compiler Error
Soln.
Ans: A
This struct is based on bit fields. The following statement
int p:3
says that the data member p will have 3 bits only. The MSB will be sign bit because of 'int'. Had it been unsigned, then no sign bit. In the above question p is 2, c is -6 and m is 5. The binary representation of p is 00000010. The lower three bits are stored in p so p is 010 or +2. Similarly -6 being 11111010, so c gets 010 or +2, and 5 being 00000101, so m gets 101 or +5[m is unsigned, so no sign bit !]. The display is 2 2 5.

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*/
    a = x;

    return 0;

}

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

Question 8

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

    int i=0;

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

    printf("%d",i);

}

A
Compiler Error
B
undefined
C
10
D
2
Soln.
Ans: D
before entering into the for loop the checking condition is "evaluated". Here it evaluatesto 0 (false) and comes out of the loop, and i is incremented (note the semicolon afterthe for loop).

Question 9

Which statement has error in the code below
int c = 9;

void fx ()
{

    int a;

    a = 8;

}

int main()
{

    /*Statement 1*/
    int b;

    /*Statement 2*/
    b = 10;

    /*Statement 3*/
    a = c++;

    /*Statement 4*/
    c = a;

    return 0;

}

A
Statement 1. and Statement 3.
B
Statement 2. and Statement 3.
C
Statement 4. and Statement 3.
D
Statement 1. and Statement 2.
Soln.
Ans: C
Statement 3 and 4 have error because they have a variable that is not in scope

Question 10

What is the output of the following code
int increment(int a)
{

    a++;

    return a;

}

int main(void)
{

    int i = 10;

    increment(increment (i));

    cout << i << endl;

    return 0;

}

A
9
B
11
C
Compiler Error
D
10
Soln.
Ans: D
It displays 10. The a++ in the function above doesn't affect i because 'a' is passed by value.

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