C++ Quiz on Mixed Topics Set 5

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. 5 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 is the output of the following code ?
#define Isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
int main()
{

    char c;

    scanf ("%c", &c);

    if (Isupper(c++))
    {

        printf ("True");

    }else

    {

        printf ("False");

    }

    return 0;

}

A
True
B
False
C
Unexpected side-effects
D
Compiler Error
Soln.
Ans: C
If c is > A, the expression 'Isupper(c++)' will cause two increments and not one, which is an unexpected side-effect.

Question 2

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

    char *str="c-pointer";

    printf("%*.*s",10,7,str);

}

A
c-pointer
B
   c-point
C
cpointer null null
D
Runtime error
Soln.
Ans: B
Meaning of %*.*s in the printf function: First * indicates the width i.e. how many spaces will take to print the string and second * indicates how many characters will print of any string. The first star and second star are respectively obtained from 10 and 7. The printf becomes "%10.7s". Following figure illustrates output of above code:

Question 3

Consider the code below.
class CMyClass
{

public:
    void MyFunc(int& i)
    {

        i = i*2;

        return;

    }

};

int main ()
{

    int x = 100;

    CMyClass cmc;

    cmc.MyFunc (x);

    cmc.MyFunc (x);

    cout << x << endl;

}

Which of the following is correct ?
A
MyFunc accepts i by reference, and cout will display 400
B
MyFunc accepts i by value, and cout will display 400
C
MyFunc accepts i by reference, and cout will display 100
D
MyFunc accepts i by value, and cout will display 100
Soln.
Ans: A
MyFunc accepts i by reference, so when x is passed as an argument it goes as its alias and any changes that occur inside MyFunc actually occur on x. MyFunc is called twice above, so cout will display 400.

Question 4

What could go wrong in the following code ? [More than one options can be correct]
// class definition
class CFile
{

    // returns true if a file strFileName exists
public:
    bool FileExists (const string& strFileName);

    // opens and reads the file strFileName
public:
    void OpenAndRead (const string& strFileName);

};

int main ()
{

    /* line 1 */
    string strFile;

    /* line 2 */
    cout << "Enter the name of the file to read: ";

    /* line 3 */
    getline (cin, strFile);

    CFile obj;

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

    obj.OpenAndRead (strFile);

}

A
The function OpenAndRead can fail if the user enters a file name that doesnot exist.
B
The arrows < are="" in="" the="" wrong="">
C
The code doesnot have enough provisions for giving feedback to the user regarding failure conditions.
D
The code will never fail.
Soln.
Ans: AC
The code doesnot have enough provisions for giving feedback to the user regarding failure conditions because the function OpenAndRead has been called on the assumption that the user will type the correct file path.

Question 5

What is the output ?
void main()
{

    char not;

    not = !2;

    printf("%d", not);

}

A
0
B
1
C
2
D
!2
Soln.
Ans: A
! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, andany non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zerovalue so TRUE. !TRUE is FALSE (0) so it prints 0.

Question 6

What is the output
int main()
{

    int var1 = 10, var2 = 12, var3 = 12;

    var1 = var2 == var3 == var1;

    printf ("%d", var1);

    return 0;

}

A
Compiler Error
B
1
C
0
D
10
Soln.
Ans: C
The expression
var1 = var2 == var3 == var1;

is same as
var1 = (var2 == (var3 == var1));

which is same as
var1 = (var2 == 0);

which is same as
var1 = 0;

because var2 == 0 is logical false.

Question 7

Consider the code below
void afunc(int x)
{

    int g = 0;

    g = x;

}

What is the signature of afunc ?
A
void_int
B
afunc_int
C
void_afunc_int
D
void
Soln.
Ans: B
afunc_int, because the signature comprises of function name and the type of arguments in respective order from left to right.

Question 8

How many times does OX print ?
int main()
{

    int i = 0;

    do
    {

        switch (i++)
        {

        case 0:
        case 1:
        case 2:
            {

                cout << "OX" << endl;

                continue;

            }

        default:
            {

                return 0;

            }

        }

    }while (true);

    return 0;

}

A
4
B
5
C
3
D
2
Soln.
Ans: C
Three times. As soon as i becomes 3, the default label operates, which causes executes return 0.

Question 9

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 value, and cout will display 200
B
MyFunc accepts i by reference, and cout will display 200
C
MyFunc accepts i by reference, and cout will display 100
D
MyFunc accepts i by value, and cout will display 100
Soln.
Ans: B
MyFunc accepts i by reference, so when x is passed as an argument it goes as its alias and any changes that occur inside MyFunc actually occur on x. So cout will display 200.

Question 10

What is the output ?
int main()
{

    char dummy[80];

    printf("Enter a string:");

    scanf("%[^a]",dummy);

    // user enters: I am OK
    printf("%s\n",dummy);

    return 0;

}

A
I am OK
B
I
C
m OK
D
am
Soln.
Ans: B
The following
scanf("%[^a]",dummy);

keeps reading the input till an 'a' is encountered, then on pressing the enter key, it displays the input excluding 'a'

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