C++ Quiz on Mixed Topics Set 12

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

What best describes a computer executable program in C ?
A
Set of function declarations and defintions only.
B
Set of variable declarations and definitions only.
C
main function as well as declarations and definitions of other helper variables and functions as per requirement.
D
Set of variable and function declarations and definitions.
Soln.
Ans: C
main function as well as declarations and definitions of other helper variables and functions as per requirement

Question 2

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

    show();

}

void show()
{

    printf("I'm the greatest");

}

A
Compiler Error in both C++ and C compilers
B
no error, output is I'm the greatest in both C and C++ compilers.
C
linker error
D
no error, output is I'm the greatest in a C++ compiler and compiler error in a C compiler.
Soln.
Ans: B
Compiler error: undeclared function 'show'. The rule is that a name is available for use from the point of declaration. But show is not available.

Question 3

What is the difference bewteen the following prototypes ?
int fx ();

and
int fx(void);

A
One and the same thing in C++ but different in C
B
fx(void) is a compiler error in C++
C
fx () is a compiler error in C
D
One and the same thing in C but different in C++
Soln.
Ans: A
In C, the declaration int f(void) means a function returning int that takes no parameters. The declaration int f() means a function returning int that takes any number of parameters. Thus, if you have a function that takes no parameters in C, the former is the correct prototype.

In C++, int f(void) is deprecated, and int f() is preferred, as it specifically means a function that takes no parameters


Question 4

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

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

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

}

A
Line 1 only will be printed
B
Line 1 as well as Line 2 will be printed
C
None will be printed
D
Line 2 only will be printed
Soln.
Ans: B
Line 1 will be printed because it is a part of the 'if' condition and Line 2 will be printed because it is the next statement of this code.

Question 5

What is pointer arithmetic -
A
Operations on two addresses
B
Operations on two values
C
Operations on an address and an integer
D
All of the above are correct
Soln.
Ans: C

Question 6

What is the value of i in the following code
int i = 20 / 3;

A
6
B
6.33
C
60
D
203
Soln.
Ans: A

Question 7

What is the output?
int main ()
{

    int num = 1;

    if(*(char *)&num == 1)
    {

        printf("\nLittle-Endian\n");

    }

    else
    {

        printf("Big-Endian\n");

    }

    return 0;

}

A
Little-Endian
B
Big-Endian
C
Compiler Error
D
Depends on Machine
Soln.
Ans: D
The code is used to determine whether the machine is little-endian or big-endian. So it depends on the machine. Intel is Little Endian and Motorala, Solaris are Big Endian. "Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.

Question 8

What is the output ?
#define FALSE -1
#define TRUE 1
#define NULL 0
void main()
{

    if(NULL)
    puts("NULL");

    else if(FALSE)
    puts("TRUE");

    else
    puts("FALSE");

}

A
NULL
B
FALSE
C
TRUE
D
Doesnot compile
Soln.
Ans: C
The input program to the compiler after processing by the preprocessor is,
main()
{

    if(0)
    puts("NULL");

    else if(-1)
    puts("TRUE");

    else
    puts("FALSE");

}

Preprocessor doesn't replace the values given inside the double quotes. The check by ifcondition is boolean value false so it goes to else. In second if -1 is boolean value truehence "TRUE" is printed.

Question 9

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

    int i=400,j=300;

    printf("%d..%d");

}

A
Compiler Error
B
400.. 300
C
linker error
D
garbage
Soln.
Ans: D
farther and farthest are char*, so they are addresses and 4 bytes long. There are no arguments following the format specifier, so the behavior is un-defined.

Question 10

What is the output?
int main()
{

    int a = 12, b = 25;

    while (b)
    {

        int carry = a & b;

        a = a ^ b;

        b = carry << 1;

    }

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

    return 0;

}

A
37
B
12
C
25
D
13
Soln.
Ans: A
The above is a full-adder implementation to add two numbers without using + or ++ signs.

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.