C++ Quiz on Mixed Topics Set 23

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

The C-String c in the following code
char c [ ] = "4";

A
is not NULL-terminated.
B
is NULL-terminated by the compiler.
C
the statement doesnot compile.
D
indeterminate or contains garbage.
Soln.
Ans: B
NULL terminated by compiler

Question 2

What is the output of the following program ?
#define a 10
void main()
{

    #define a 50
    printf("%d",a);

}

A
10
B
50
C
compiler error
D
linker error
Soln.
Ans: B
The preprocessor directives can be redefined anywhere in the program. So the mostrecently assigned value will be taken.

Question 3

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

    int i;

    printf("%d",scanf("%d",&i));

    // value 10 is given as input here
}

A
Compiler Error
B
undefined
C
1
D
10
Soln.
Ans: C
Scanf returns number of items successfully read and not 1/0. Here 10 is given as inputwhich should have been scanned successfully. So number of items read is 1.

Question 4

What is the output ?
int main()
{

    int j = 1;

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

    return 0;

}

A
7
B
6
C
8
D
No Compiler Error but unpredictable.
Soln.
Ans: D
The statement
printf ("%d", j+(++j)+++j);

is same as
printf ("%d", j+ (++j) + ++j);

but sequence points of C will cause the behaviour to be undefined because j is being modified and read more than once in the same statement. Different compilers will give different output. So such a code is not reliable, even though it gets compiled.

Question 5

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

    int x = 9;

    if (x % 9)
    {

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

    }

    if ((x + 1) % 9)
    {

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

    }

}

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

Question 6

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

    int const * p=5;

    printf("%d",++(*p));

}

A
Compiler Error
B
Linker Error
C
6
D
5
Soln.
Ans: A
p is a pointer to a "constant integer". But we tried tochange the value of the "constant integer".

Question 7

Which of the following is true -
A
A function that doesn't return void must accept arguments.
B
A function that returns void must accept arguments.
C
A function that returns void may or may not accept any arguments.
D
A function that returns void cannot accept arguments.
Soln.
Ans: C
A function may or may not accept arguments and can return void or any valid data type.

Question 8

The following code
int main()
{

    char str[80];

    printf("Enter the string:");

    scanf("%s",str);

    printf("You entered:%s\n",str);

    return 0;

}

A
Will never fail
B
Fail if the user enters too long a string
C
It will not compile
D
Linker Error
Soln.
Ans: B
The above code will fail with unexpected results if the user enters a string longer than 79 characters.

Question 9

What is the output ?
int main()
{

    int j = 5;

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

    return 0;

}

A
6
B
7
C
Compiler Error
D
Compiles but behaviour is undefined
Soln.
Ans: C
The statement
printf ("%d", ++j++);

is same as
printf ("%d", ++j + +);

which is meaningless.

Question 10

Suppose we have a character string char cX [] = " bxza". What best describes the logic for displaying all characters of the string one by one, but skipping all leading white space ?
A
Read each character, donot display it only if it is not white space
B
Store the starting address of this array in a variable, say, 'x', keep incrementing 'x' till a white space character is read. Finally display the string starting at 'x'.
C
Before reading a character check if that character exists, then read it and display it only if it is not white space.
D
Read each character, display it on the screen if it is not white space and repeat this if more characters exist in the string.
Soln.
Ans: B
Store the starting address of this array in a variable, say, 'x', keep incrementing 'x' till a white space character is read. Finally display the string starting at 'x'.

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.