C++ Quiz on Mixed Topics Set 28

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. 28 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 is wrong with the following switch.
int main()
{

    int i = 0;

    switch (i)
    {

    case 1:
    case 0:
        {

            return 0;

        }

    default:
        {

            switch (i + 8)
            {

            case 3:
            default:
                {

                    return 6;

                }

            }

        }

        break;

    }

    return 0;

}

A
break and default cannot be together
B
default is illegal
C
Nothing is wrong with the switch
D
switch cannot be nested
Soln.
Ans: C
The switch is OK because we are only nesting a switch inside another.

Question 2

If you do not specify an entry-point in a C executable project, but otherwise commit no syntax errors -
A
The program compiles and links correctly
B
The program doesn't compile
C
The program compiles correctly but doesnot link
D
The program neither compiles nor links
Soln.
Ans: C
- the program compiles because no syntax errors, but will not link

Question 3

What is the output ?
int i = 40;

int main ()
{

    do
    {

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

    }while (0, 1, 2, 3, 4, 5);

    return 0;

}

A
40 41 42 43 44 45
B
40
C
Error
D
Infinite Loop
Soln.
Ans: BD
The while loop has a comma operator that evaluates to 5, which is the last argument. The loop executes infinitely.

Question 4

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

    while (1)
    {

        continue;

    }

    return 0;

}

A
forever
B
1
C
2
D
never
Soln.
Ans: A
The loop executes for ever.

Question 5

What is the output
int main()
{

    short x = -(30*1000 + 2769);

    printf ("%d", x);

    return 0;

}

A
Compiler Error
B
-129
C
Undefined
D
10
Soln.
Ans: C
we cannot store -32769 in short because it is out of range. So the behaviour is undefined. Different compilers will treat it differently, VC++ gives a warning. short values must be within SHRT_MIN and SHRT_MAX

Question 6

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

    char *p="hai friends",*p1;

    p1=p;

    while(*p!='\0') ++*p++;

    printf("%s %s",p,p1);

}

A
undefined behaviour
B
ibj!gsjfoet
C
compiler error
D
linker error
Soln.
Ans: A
The above code generates a runtime fatal error because of the statement
++*p++
. DO NOT write such a code.

NOTE: Many people give wrong answer - "++*p++ will be parse in the given order*p that is value at the location currently pointed by p will be taken++*p the retrieved value will be incrementedwhen ; is encountered the location will be incremented that is p++ will be executedHence, in the while loop initial value pointed by p is 'h', which is changed to 'i' byexecuting ++*p and pointer moves to point, 'a' which is similarly changed to 'b' and soon. Similarly blank space is converted to '!'. Thus, we obtain value in p becomes "ibj!gsjfoet" and since p reaches '\0' and p1 points to p thus p1doesnot print anything."


Question 7

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

    int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };

    int *p,*q;

    p=&a[2][2][2];

    *q=***a;

    printf("%d----%d",*p,*q);

}

A
Compiler Error
B
87
C
linker error
D
Garbage
Soln.
Ans: D
p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third2D(which you have not declared) it will print garbage values. *q=***a starting address ofa is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, itwill print first element of 3D array.

Question 8

How can we make the code in main() better ? [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 code needs no improvements.
B
Add code to check of the file exists, and inform the user if the file doesnot exist.
C
Change the direction of all arrows >>
D
Before line 4 add a conditional 'if' statement to check if the file exists.
Soln.
Ans: BD
Before line 4 add a conditional 'if' statement to check if the file exists, and inform the user if the file doesnot exist. This will make the code more user friendly and free from logical errors so that the user is not kept guessing as to what happened.

Question 9

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

    static char *s[ ] =
    {

        "black", "white", "yellow", "violet"
    };

    char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;

    p = ptr;

    **++p;

    printf("%s",*--*++p + 3);

}

A
Compiler Error in both C++ and C compilers
B
whi
C
ck
D
bla
Soln.
Ans: C
In this problem we have an array of char pointers pointing to start of 4 strings. Then wehave ptr which is a pointer to a pointer of type char and a variable p which is a pointerto a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. Thenext statement increment value in p by 1 , thus now value of p = s+2. In the printfstatement the expression is evaluated *++p causes gets value s+1 then the predecrement is executed and we get s+1 - 1 = s . the indirection operator now gets thevalue from the array of s and adds 3 to the starting address. The string is printedstarting from this position. Thus, the output is 'ck'.
printf("%s",*(--(*(++p))) + 3);


Question 10

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

    float f=0.0f;

    int i;

    for(i=0;i<10;i++)
    {

        f = f + 0.1f;

    }

    if(f == 1.0f)
    printf("f is 1.0 \n");

    else
    printf("f is NOT 1.0\n");

    return 0;

}

A
f is 1.0
B
f is NOT 1.0
C
Unpredictable
D
Compiler Error
Soln.
Ans: B
float numbers can only be compared against 0.

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.