Copy Of Quiz 2 For Section L

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Rahul Dixit
R
Rahul Dixit
Community Contributor
Quizzes Created: 1 | Total Attempts: 119
Questions: 10 | Attempts: 119

SettingsSettingsSettings
Copy Of Quiz 2 For Section L - Quiz

.


Questions and Answers
  • 1. 

    What is the output of this piece of code?  #include <stdio.h>   void main() { int i;  for(i=0;i<10;i++) printf(“%d ”,i); }

    • A.

      0 1 2 3 4 5 6 7 8 9

    • B.

      0 1 2 3 4 5 6 7 8 9 10

    • C.

      10

    • D.

      Error

    Correct Answer
    C. 10
    Explanation
    The given code is a simple for loop that iterates from 0 to 9. Inside the loop, it prints the value of 'i' followed by a space. After the loop completes, it prints the value of 'i' one more time, which is 10. Therefore, the output of the code is "0 1 2 3 4 5 6 7 8 9 10".

    Rate this question:

  • 2. 

    What is the output of this C code? #include <stdio.h>  int main() {  int x = 0;   if (x++)  printf("true\n");        else if (x == 1)             printf("false\n");     }

    • A.

      True

    • B.

      False

    • C.

      Truefalse

    • D.

      0 time

    Correct Answer
    B. False
    Explanation
    The output of this C code is "false". This is because the initial value of x is 0. In the if statement, x is incremented using the post-increment operator, but the value of x before the increment is evaluated in the condition. Since the value of x before the increment is 0, the condition evaluates to false and the corresponding printf statement is executed.

    Rate this question:

  • 3. 

    What is the output of this C code? #include <stdio.h> int main()  { int n; for (n = 9; n!=0; n--)  printf("n = %d", n--);  return 0;  }

    • A.

      9 7 5 3 1

    • B.

      9 8 7 6 5 4 3 2 1

    • C.

      Infinite

    • D.

      Error

    Correct Answer
    C. Infinite
    Explanation
    The code starts with n = 9 and enters a for loop. In each iteration of the loop, it prints the value of n and then decrements n by 2. However, the decrement operator (--) is used twice in each iteration, once in the printf statement and once in the loop itself. This leads to n being decremented by 2 in each iteration instead of 1. As a result, n will never be equal to 0, causing an infinite loop. Therefore, the output of the code is infinite.

    Rate this question:

  • 4. 

    What is the output of this C code? #include <stdio.h> int main() { int i = 4, j = 7;  do  {  printf("Loop");  }while(++i < --j);  return 0; }

    • A.

      Loop

    • B.

      LoopLoop

    • C.

      Prints nothing

    • D.

      Compilation Error

    Correct Answer
    B. LoopLoop
    Explanation
    The code uses a do-while loop to repeatedly print "Loop" as long as the condition (++i < --j) is true. Initially, i is incremented to 5 and j is decremented to 6, so the condition is true. "Loop" is printed once. Then, i is incremented to 6 and j is decremented to 5, still making the condition true. "Loop" is printed again. After this, i is incremented to 7 and j is decremented to 4, making the condition false. The loop terminates and the program ends, resulting in the output "LoopLoop".

    Rate this question:

  • 5. 

    How many times is a do while loop guaranteed to loop?

    • A.

      1

    • B.

      0

    • C.

      Infinite

    • D.

      Depend on Variable

    Correct Answer
    A. 1
    Explanation
    A do while loop is guaranteed to loop at least once because the condition is checked at the end of the loop. This means that the loop will always execute the code block once before checking if the condition is true or false. Therefore, even if the condition is false from the beginning, the loop will still run once.

    Rate this question:

  • 6. 

    What is the output of this C code? #include <stdio.h>     int main()     {         do             printf("In while loop ");         while (0);             printf("After loop\n");     }

    • A.

      In while loop

    • B.

      In while loop After loop

    • C.

      After loop

    • D.

      For (i = n-1; i>-1; i–)

    Correct Answer
    B. In while loop After loop
    Explanation
    The code uses a do-while loop with a condition of 0, which means the loop will execute at least once. Inside the loop, it prints "In while loop ". After the loop, it prints "After loop". Therefore, the output of the code will be "In while loop After loop".

    Rate this question:

  • 7. 

    What is the output of this C code? int main ()  { int i=0; for (i=0; i<20; i++)  { switch (i) { case 0 : i += 5; case 1 : i += 2; case 5 : i += 5; default : i += 4; break;  } printf("%d, ", i);  } }

    • A.

      0,5,9,13,17

    • B.

      5,9,13,17

    • C.

      12,17,22

    • D.

      16,21

    Correct Answer
    D. 16,21
    Explanation
    The code starts with initializing the variable i to 0. Then, it enters a for loop that iterates until i is less than 20. Inside the loop, there is a switch statement that checks the value of i.

    When i is 0, it adds 5 to i and continues to the next case. When i is 1, it adds 2 to i and continues to the next case. When i is 5, it adds 5 to i and continues to the next case.

    In the default case, it adds 4 to i and then breaks out of the switch statement. Finally, the value of i is printed using printf.

    The output of the code is 16,21 because after the loop iterates 16 times, the value of i becomes 16 and then it adds 4 in the default case before printing it.

    Rate this question:

  • 8. 

    What is the output of the given code: #include <stdio.h>   void main()   {        int i = 0;        while (++i)  {   printf("H");      }  }

    • A.

      H

    • B.

      Error

    • C.

      H is printed infinite times

    • D.

      None

    Correct Answer
    C. H is printed infinite times
    Explanation
    The code contains an infinite loop. The condition in the while loop is "++i", which means that the value of "i" will be incremented by 1 before each iteration of the loop. Since the condition is always true (non-zero), the loop will continue indefinitely. Inside the loop, the code prints "H" repeatedly, resulting in "H" being printed infinite times.

    Rate this question:

  • 9. 

    What is the output of this C code? #include     int main()     {         int x = 1;         if (x > 0)             printf("inside if\n");         else if (x > 0)             printf("inside elseif\n");     }

    • A.

      Inside if

    • B.

      Inside elseif

    • C.

      Inside if inside elseif

    • D.

      Syntax error

    Correct Answer
    A. Inside if
    Explanation
    The output of this C code is "inside if". This is because the variable x is equal to 1, which is greater than 0. Therefore, the condition in the if statement is true and the code inside the if block is executed. The else if statement is not evaluated because the condition x > 0 is already satisfied.

    Rate this question:

  • 10. 

    What is the output of the given code: int main() { int i; int arr[5] = {1}; for (i = 0; i < 5; i++) printf("%d ", arr[i]); return 0; }

    • A.

      1 followed by four garbage values:

    • B.

      10000

    • C.

      11111

    • D.

      00000

    Correct Answer
    D. 00000
    Explanation
    The given code initializes an integer array `arr` with size 5 and assigns the first element as 1 while leaving the rest of the elements uninitialized. The code then enters a for loop that iterates 5 times. Each iteration, it prints the value of `arr[i]`, which in this case will be 1 for the first iteration and garbage values for the rest of the iterations since the rest of the elements in the array are uninitialized. Therefore, the output will be "1 followed by four garbage values".

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 30, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 12, 2018
    Quiz Created by
    Rahul Dixit
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.