Copy Of Quiz 2 For Section L

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Rahul Dixit
R
Rahul Dixit
Community Contributor
Quizzes Created: 1 | Total Attempts: 128
| Attempts: 128 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. What is the output of this C code?
  #include <stdio.h>
    int main()
    {
        do
            printf("In while loop ");
        while (0);
            printf("After loop\n");
    }

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

Submit
Please wait...
About This Quiz
Copy Of Quiz 2 For Section L - Quiz

This quiz titled 'Copy of Quiz 2 for Section L' assesses understanding of C programming through various code snippets. It tests knowledge on loops, conditionals, and output prediction,... see moreenhancing coding proficiency and logical thinking in learners. see less

2. 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");     }

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.

Submit
3.
What is the output of the given code:

#include <stdio.h>
  void main()
  {
       int i = 0;
       while (++i)
 {
  printf("H");
     }
 }

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.

Submit
4. How many times is a do while loop guaranteed to loop?

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.

Submit
5. 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; }

Explanation

The code uses a do-while loop to repeatedly print "Loop" as long as the condition (++i

Submit
6.
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;
}

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

Submit
7. 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;  }

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.

Submit
8.
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");
    }

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.

Submit
9. 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);  } }

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.

Submit
10. 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); }

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

Submit
View My Results

Quiz Review Timeline (Updated): Mar 30, 2023 +

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
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the output of this C code?...
What is the output of this C code?...
What is the output of the given code:...
How many times is a do while loop guaranteed to loop?
What is the output of this C code?...
What is the output of the given code:...
What is the output of this C code?...
What is the output of this C code?...
What is the output of this C code?...
What is the output of this piece of code?...
Alert!

Advertisement