# Looptrics

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.
| By Kirubha66
K
Kirubha66
Community Contributor
Quizzes Created: 1 | Total Attempts: 790
Questions: 30 | Attempts: 791

Settings

Working with LOOPS

• 1.

### What will be output of following c code? #includeextern int x;int main(){    do{        do{             printf("%o",x);         }         while(!-2);    }    while(0);    return 0;}int x=8;

• A.

8

• B.

10

• C.

18 8

• D.

Run time error

B. 10
Explanation
The code will output "10".

The code first declares an external variable "x" and initializes it to 8. Then, it enters a nested do-while loop. The inner do-while loop prints the value of "x" in octal format using the "%o" format specifier, which is 10 in octal representation. The inner loop will continue until the condition "!-2" is false, which is never as -2 is always true. The outer do-while loop will execute only once since the condition "while(0)" is false. Finally, the main function returns 0.

Rate this question:

• 2.

### What will be output of following c code?        #include<stdio.h>int main(){    int i=2,j=2;    while(i+1?--i:j++)         printf("%d",i);    return 0;}

• A.

2 1

• B.

1 0

• C.

1

• D.

0

C. 1
Explanation
The code starts with i=2 and j=2. In the while loop condition, the expression i+1?--i:j++ is evaluated. Since i+1 is true (non-zero), --i is executed which decrements i by 1. The value of i is then printed as 1. The loop continues and the condition i+1?--i:j++ is evaluated again. This time, i+1 is false (zero), so j++ is executed which increments j by 1. However, the value of j is not printed. The loop ends and the output is 1.

Rate this question:

• 3.

### What will be output of following c code? #include<stdio.h>int main(){    int x=011,i;    for(i=0;i<x;i+=3){         printf("Start ");         continue;         printf("End");    }    return 0;}

• A.

Start Start Start End

• B.

Start end Start End Start End

• C.

Start Start Start

• D.

Start End Start

C. Start Start Start
Explanation
The code declares an integer variable x and initializes it with the octal value 011, which is equivalent to decimal 9. The code then enters a for loop that starts from i=0 and increments i by 3 in each iteration. Inside the loop, it first prints "Start" and then continues to the next iteration without executing the remaining code. Therefore, the loop will run three times (i=0, i=3, and i=6), and "Start" will be printed three times. Hence, the output will be "Start Start Start".

Rate this question:

• 4.

### What will be output of following c code? #include<stdio.h>int main(){    int i,j;    i=j=2,3;    while(--i&&j++)         printf("%d %d",i,j);    return 0;}

• A.

1 2

• B.

1 3

• C.

1 4

• D.

Error

B. 1 3
Explanation
The code initializes variables i and j to the values 2 and 3 respectively. Inside the while loop, the condition --i && j++ is evaluated. Since i is decremented before the evaluation and it is initially 2, the condition becomes true. j is then incremented by 1, making it 4. The printf statement inside the loop prints the values of i and j, which are 1 and 4 respectively. Therefore, the output will be "1 4".

Rate this question:

• 5.

### What will be output of following c code? #include<stdio.h>int main(){    static int i;    for(++i;++i;++i) {         printf("%d ",i);         if(i==4) break;    }    return 0;}

• A.

Infinite loop

• B.

3 6

• C.

2 4

• D.

3 5

C. 2 4
Explanation
The code declares a static integer variable "i" without initializing it. It then enters a for loop where "i" is incremented twice before each iteration. The loop continues until "i" is equal to 4, at which point it breaks out of the loop.

In the first iteration, "i" is incremented to 1 and then to 2, so the first number printed is 2. In the second iteration, "i" is incremented to 3 and then to 4, so the second number printed is 4. After that, the loop breaks and the program ends. Therefore, the output will be "2 4".

Rate this question:

• 6.

### What will be output of following c code? #include<stdio.h>int main(){    int i=1;    for(i=0;i=-1;i=1) {         printf("%d ",i);         if(i!=1) break;    }    return 0;}

• A.

-1

• B.

Conditional error

• C.

1

• D.

0

A. -1
Explanation
The output of the given code will be -1.

In the for loop, the condition is set as "i=-1", which means that the loop will continue as long as i is equal to -1. However, since i is initially set to 1, the loop will not execute. Therefore, the program will directly move to the next line which is "return 0;". Hence, the output will be -1.

Rate this question:

• 7.

### What will be output of following c code? #include<stdio.h>int main(){    for(;;) {         printf("%d ",10);    }    return 0;}

• A.

10

• B.

Infinite loops

• C.

Loops initialization required

• D.

Runtime error

B. Infinite loops
Explanation
The given C code contains an infinite loop because the for loop has no condition specified in the middle section. In C, if the middle section of the for loop is left empty, it is considered as true and the loop will continue indefinitely. Therefore, the output of this code will be an infinite loop printing the number 10 repeatedly.

Rate this question:

• 8.

### What will be output of following c code?        #include<stdio.h>int r();int main(){    for(r();r();r()) {         printf("%d ",r());    }    return 0;}int r(){    int static num=7;    return num--;}

• A.

7 6 5 4 3 2 1

• B.

Infinite loops

• C.

5 2

• D.

6 6

C. 5 2
Explanation
The code includes a function called "r" which returns a static variable "num" that starts at 7 and decrements by 1 each time it is called. In the main function, the for loop calls the "r" function three times: once as the initialization expression, once as the condition expression, and once as the increment expression. Inside the loop, the "r" function is called again and its return value is printed using printf. The loop continues as long as the "r" function returns a non-zero value. Therefore, the output of the code will be 5 2, as the "r" function will return 5 and 2 in consecutive iterations.

Rate this question:

• 9.

### Output of following C program?#include<stdio.h>int main(){    int i = 0;    for (printf("1st\n"); i < 2 && printf("2nd\n"); ++i && printf("3rd\n"))    {        printf("*\n");    }    return 0;}

• A.

1st 2nd * 3rd 2nd *

• B.

1st 2nd * 3rd 2nd * 3rd

• C.

1st 2nd 3rd * 2nd 3rd

• D.

1st 2nd 3rd * 1st 2nd 3rd

B. 1st 2nd * 3rd 2nd * 3rd
Explanation
The program starts with the "1st" being printed. Then, it enters the for loop where it checks the condition i < 2. Since i is initially 0, the condition is true and it proceeds to the next part of the condition which is printf("2nd"). After that, it prints "*", and then "3rd". Since the condition is still true, it goes back to the beginning of the loop and repeats the process. This time, it prints "2nd", "*", and "3rd" again. After this iteration, the condition i < 2 becomes false and the loop terminates. Therefore, the output is "1st 2nd * 3rd 2nd * 3rd".

Rate this question:

• 10.

### #includeint main(){    int i;    for(i=0;i<=5;i++);    printf("%d",i);    return 0;}

• A.

0 1 2 3 4 5

• B.

6

• C.

Error

• D.

5

B. 6
Explanation
The correct answer is 6 because the for loop runs from 0 to 5 (inclusive) and increments the variable i by 1 each time. After the loop finishes, the value of i is 6. So when the printf statement is executed, it prints the value of i, which is 6.

Rate this question:

• 11.

### What will be output of following c code? #include<stdio.h>int i=40;extern int i;int main(){    do{         printf("%d",i++);    }    while(5,4,3,2,1,0);    return 0;}

• A.

Runtime error

• B.

40

• C.

40 40 40 40 40

• D.

40 41 42 43 44

B. 40
Explanation
The code will output "40" because the variable "i" is declared as an external variable and is initialized to 40. The do-while loop will execute once because the condition "5,4,3,2,1,0" evaluates to 0, which is false. Therefore, the printf statement will only be executed once, printing the value of "i" which is 40.

Rate this question:

• 12.

### Output?#include <stdio.h>int main(){    int c = 5, no = 10;    do {        no /= c;    } while(c--);      printf ("%d\n", no);    return 0;}

• A.

1

• B.

Runtime Error

• C.

0

• D.

Compile Time Error

B. Runtime Error
Explanation
The code is using a post-decrement operator (c--) in the while loop condition. This means that the value of c is first evaluated, and then decremented. In the first iteration of the loop, c is still 5, so the loop runs. However, after the first iteration, c becomes 4. Since the condition is evaluated after the decrement, the loop condition becomes false and the loop stops. However, the code tries to divide no by c, which results in a division by zero error, causing a runtime error.

Rate this question:

• 13.

### What will be output of following c code? #include<stdio.h>int main(){    int i;    for(i=10;i<=15;i++){         while(i){             do{                 printf("%d ",1);                 if(i>>1)                      continue;             }while(0);             break;         }    }    return 0;}

• A.

1 1 1 1 1 1

• B.

1 1 1 1 1

• C.

Infinite Loops

• D.

1 1 1 1 1 1 1 1

A. 1 1 1 1 1 1
Explanation
The given code snippet contains a nested loop structure. The outer loop runs from i=10 to i>1 is always true, causing an infinite loop. Therefore, the output of the code will be "Infinite Loops".

Rate this question:

• 14.

### How many times this loop will execute? #include<stdio.h>int main(){    char c=125;    do         printf("%d ",c);    while(c++);    return 0;}

• A.

Finite Times

• B.

Infinite Times

• C.

One time

• D.

Three times

A. Finite Times
Explanation
The loop will execute a finite number of times because the condition for the loop to continue is "while(c++)", which means it will keep executing as long as the value of c is not zero. Initially, c is assigned the value of 125, so the loop will execute until c reaches zero. Since c is incremented by 1 in each iteration, it will take a finite number of iterations for c to reach zero. Therefore, the loop will execute a finite number of times.

Rate this question:

• 15.

### What will be output of following c code?        #include<stdio.h>int main(){    int x=123;    int i={         printf("c" "++")    };    for(x=0;x<=i;x++){         printf("%x ",x);    }    return 0;}

• A.

C ++ 0 1 2 3

• B.

Improper initialization of integer variable

• C.

123

• D.

C++ 0 1 2 3

D. C++ 0 1 2 3
Explanation
The code snippet initializes the variable 'x' with the value 123. The variable 'i' is declared with an improper initialization syntax, as it includes a printf statement within the curly braces. However, this does not affect the output of the code. The for loop runs from x=0 to i, which is the value of the uninitialized variable 'i' (which is undefined behavior). The loop prints the values of x in hexadecimal format, resulting in the output "c++ 0 1 2 3".

Rate this question:

• 16.

### How many times will Hello be printed in the below program?#include <stdio.h> int main(){    int i = 1024;    for (; i; i >>= 1)        printf("Hello");    return 0;}

• A.

10

• B.

11

• C.

1024

• D.

2

B. 11
Explanation
The program uses a for loop to print "Hello" repeatedly. The loop condition is "i", which starts with a value of 1024. In each iteration of the loop, the value of "i" is right-shifted by 1. This means that the loop will continue as long as "i" is non-zero. Since the loop condition is not explicitly specified in the for loop, it will continue until "i" becomes zero. Therefore, the loop will iterate 11 times before "i" becomes zero, resulting in "Hello" being printed 11 times.

Rate this question:

• 17.

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

• A.

Infinite Loops

• B.

N = 9n = 8n = 7n= 6n = 5n = 4n = 3n = 2n = 1

• C.

N = 9n= 7n = 5n = 3n = 1

• D.

N = 9n= 6n = 3n =1

A. Infinite Loops
Explanation
The given code will result in an infinite loop. This is because the condition in the for loop, "n!=0", will always be true since the value of n is decremented twice in each iteration (once in the loop itself and once in the printf statement). Therefore, the loop will continue indefinitely, printing "n = " followed by the current value of n, which starts at 9 and keeps decreasing by 2 in each iteration.

Rate this question:

• 18.

### What will be the output of the program, if a short int is 2 bytes wide?#include<stdio.h>int main(){    short int i = 0;    for(i<=5 && i>=-1; ++i; i>0)        printf("%u,", i);    return 0;}

• A.

1 ... 65535

• B.

Expression syntax error

• C.

No Output

• D.

0, 1, 2, 3, 4, 5

A. 1 ... 65535
Explanation
The program includes a for loop that initializes the short int variable i to 0. The loop condition checks if i is less than or equal to 5 and greater than or equal to -1. Since these conditions are always true, the loop will continue indefinitely. Inside the loop, the printf statement prints the value of i as an unsigned integer followed by a comma. The value of i will keep incrementing by 1 in each iteration. Since a short int is 2 bytes wide, it can store values from 0 to 65535. Therefore, the program will output the numbers 0, 1, 2, 3, 4, 5, and so on until it reaches the maximum value of 65535.

Rate this question:

• 19.

### Point out the error, if any in the for loop.#include<stdio.h>int main(){    int i=1;    for(;;)    {        printf("%d\n", i++);        if(i>10)           break;    }    return 0}

• A.

There should be a condition in the for loop

• B.

The two semicolons should be dropped

• C.

The for loop should be replaced with while loop

• D.

No Error

D. No Error
Explanation
The given for loop does not have any error. It is a valid infinite loop that will keep printing the value of 'i' until it reaches a value greater than 10, at which point the loop will break. The use of ';;' in the for loop is allowed and indicates that there is no initialization or increment/decrement statement present.

Rate this question:

• 20.

### What will be the output of the program? #include<stdio.h>int main(){    int x=1, y=1;    for(; y; printf("%d %d\n", x, y))    {        y = x++ <= 5;    }    printf("\n");    return 0;}

• A.

2 1 3 1 4 1 5 1 6 1 7 0

• B.

2 1 3 1 4 1 5 1 6 1

• C.

2 1 3 1 4 1 5 1

• D.

2 2 3 3 4 4 5 5

A. 2 1 3 1 4 1 5 1 6 1 7 0
Explanation
The program uses a for loop to print the values of x and y. Initially, x and y are both 1. The loop condition is y, which means the loop will continue as long as y is non-zero. Inside the loop, y is updated to the result of the comparison x++

Rate this question:

• 21.

### Which of the following statement creates infinite loop?

• A.

For ( ; ; )

• B.

While ( ; ; )

• C.

When ( ; ; )

• D.

if( ; ; )

A. For ( ; ; )
Explanation
The correct answer is "for ( ; ; )". This statement creates an infinite loop because all three sections of the for loop (initialization, condition, and increment) are empty, which means the condition is always true and the loop will continue indefinitely.

Rate this question:

• 22.

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

• A.

In while loop after loop

• B.

After Loop

• C.

Compile Time Error

• D.

Infinite loop

C. Compile Time Error
Explanation
The code will result in a compile time error because the while loop condition is missing. The while loop requires a condition to be specified in parentheses after the keyword "while". Since there is no condition provided, the code will not compile.

Rate this question:

• 23.

### How many times i value is checked in the below code?     #include <stdio.h>    int main()    {        int i = 0;        do {            i++;            printf("in while loop\n");        } while (i < 3);    }

• A.

2

• B.

3

• C.

4

• D.

1

B. 3
Explanation
In the given code, the value of i is checked 3 times. The do-while loop is executed at least once, and then the condition i < 3 is checked. Since the initial value of i is 0, the loop is executed 3 times before the condition becomes false. Therefore, the value of i is checked 3 times in the code.

Rate this question:

• 24.

### What is the output of this C code?     #include <stdio.h>    void main()    {        int i = 2;        do        {            printf("Hi");        } while (i < 2)    }

• A.

Compile Time Error

• B.

Hi Hi

• C.

Hi

• D.

Varies

A. Compile Time Error
Explanation
The output of this C code is "Compile Time Error". This is because the condition in the do-while loop is i < 2, and since i is initialized to 2, the condition is false from the beginning. Therefore, the code inside the do block will never be executed. However, according to the C language syntax, the main function should have a return type of int, not void. Hence, there is a compile-time error in the code.

Rate this question:

• 25.

### How many times the while loop will get executed if a short int is 2 byte wide?#include<stdio.h>int main(){    int j=1;    while(j <= 255)    {        printf("%c %d\n", j, j);        j++;    }    return 0;}

• A.

Infinite Times

• B.

255 Times

• C.

256 Times

• D.

254 Times

B. 255 Times
Explanation
The while loop will get executed 255 times because a short int is 2 bytes wide, which means it can store values from -32768 to 32767. In this code, the variable j starts at 1 and increments by 1 in each iteration of the while loop. The loop will continue executing until j becomes greater than 255. Therefore, the loop will run for j = 1, 2, 3, ..., 255, which is a total of 255 times.

Rate this question:

• 26.

### Point out the error, if any in the while loop.#include<stdio.h>int main(){    int i=1;    while()    {        printf("%d\n", i++);        if(i>10)           break;    }    return 0;}

• A.

There should be a condition in the while loop

• B.

There should be at least a semicolon in the while

• C.

The while loop should be replaced with for loop.

• D.

No error

A. There should be a condition in the while loop
Explanation
The error in the while loop is that there is no condition specified. In order for the loop to execute, there needs to be a condition that evaluates to either true or false. Without a condition, the loop will continue indefinitely, resulting in an infinite loop.

Rate this question:

• 27.

### #include<stdio.h>int main(){       for(int i=0;i<=10;i++){         printf("%d ",i);    }   return 0;        }

• A.

0 1 2 3 4 5 6 7 8 10

• B.

Error: cannot declare variable at expression

• C.

1 2 3 4 5 6 7 8 9 10

• D.

Infinite Loops

B. Error: cannot declare variable at expression
Explanation
The given code is a simple for loop that prints the numbers from 0 to 10. However, there is a mistake in the code where the variable 'i' is declared inside the for loop instead of before it. This is not allowed in C programming, hence the error message "error: cannot declare variable at expression".

Rate this question:

• 28.

### #include<stdio.h> int main(){ int i,j,k;     do          while(0)              for(;0;)                  printf("cbyexample"); while(0); return 0;    }

• A.

Infinite looping

• B.

Cbyexample

• C.

No ouput

• D.

Internal error

D. Internal error
Explanation
The given code will result in an "internal error" because of the syntax error in the code. The do-while loop and the for loop both have a condition of 0, which means they will never execute. Additionally, the semicolon after the while loop will cause a syntax error. Therefore, the code will not produce any output and will result in an internal error.

Rate this question:

• 29.

### Int x = 0; for (x=1; x<4; x++); printf("x=%d\n", x);   What will be printed when the sample code above is executed?

• A.

X=0

• B.

X=1

• C.

X=3

• D.

X=4

D. X=4
Explanation
The code initializes the variable x to 0. Then, it enters a for loop where x is incremented by 1 each time until x is less than 4. However, the loop body is empty due to the semicolon immediately after the closing parenthesis of the for loop. After the loop, the printf statement is executed, printing the value of x, which is 4.

Rate this question:

• 30.

### What would be printed from the following C++ code segment?for(x=1;x<=5;x++){     for(y=1;y<=x;y++)         cout<<x<<"\t";  cout<<"\n";}

• A.

1 2 3 4 5

• B.

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

• C.

Infinite Loops

• D.

Error

B. 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Explanation
The given code segment contains two nested for loops. The outer loop iterates from 1 to 5, and the inner loop iterates from 1 to the value of the outer loop variable. Inside the inner loop, the value of the outer loop variable is printed followed by a tab character. After the inner loop completes, a newline character is printed.

Therefore, the code will print the numbers 1 to 5, with each number repeated as many times as its value. For example, 1 will be printed once, 2 will be printed twice, 3 will be printed three times, and so on. The final output will be: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5.

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 22, 2023
Quiz Edited by
ProProfs Editorial Team
• Jul 31, 2016
Quiz Created by
Kirubha66

Related Topics