C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Aptitude Que. & Ans. » C Aptitude Que. & Ans.

C Looping (for, while, do while) - Aptitude Questions & Answers

C programming Looping Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on various looping statements like while, do dhile, for nested looping etc.

1) What will be the output of following program ?
        #include < stdio.h >
        void main()
        {	unsigned char var=0;
	        for(var=0;var<=255;var++)
	        {
		        printf("%d ",var);
	        }
        }
        
  1. 0 1 2 ... infinite times
  2. 0 1 2 ... 255

2) What will be the output of following program ?
#include <stdio.h>
void main()
{
char cnt=0;
for(;cnt++;printf("%d",cnt)) ;
printf("%d",cnt);
}  
  1. 0 1 2 ... infinite times
  2. 0 1 2 ... 127
  3. 0
  4. 1

3) What will be the output of following program ?
#include <stdio.h>
void main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i==5)
              goto print;
       i++;
    }
}
fun()
{
   print:
     printf("includehelp.com");
}  
    
  1. Error
  2. 12345includehelp.com
  3. 1234includehelp.com
  4. 1includehelp.com 2includehelp.com 3includehelp.com 4includehelp.com 5includehelp.com

4) What will be the output of following program ?
#include < stdio.h >
int main()
{
	int tally=0;
	for(;;)
	{
		if(tally==10)
			break;
		printf("%d ",++tally);
	}
	return 0;
}
  1. 0 1 2 3 4 5 6 7 8 9 10
  2. 0 1 2 3 ... infinite times
  3. 1 2 3 4 5 6 7 8 9 10
  4. 1 2 3 4 5 6 7 8 9

5) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int tally;
	for(tally=0;tally<10;++tally)
	{
		printf("#");
		if(tally>6)
			continue;
		printf("%d",tally);
	}
}    
    
  1. #0#1#2#3#4#5#6###
  2. #0#1#2#3#4#5#6#7#8#9#10
  3. #0#1#2#3#4#5##7#8#9#10
  4. #0#1#2#3#4#5#

6) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int i,j,charVal='A';

	for(i=5;i>=1;i--)
	{
		for(j=0;j< i;j++)
			printf("%c ",(charVal+j));
		printf("\n");
	}
}     
  1. A B C D E
    A B C D E
    A B C D E
    A B C D E
    A B C D E
            
  2. A B C D
    A B C D
    A B C D
    A B C D
            
  3. A B C D
    A B C
    A B
    A
            
  4. A B C D E
    A B C D
    A B C
    A B
    A
            

7) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int cnt=1;
	do
	{
		printf("%d,",cnt);
		cnt+=1;
	}while(cnt>=10);
	printf("\nAfter loop cnt=%d",cnt);
	printf("\n");
}
        
  1. After loop cnt=1
  2. 1,
    After loop cnt=2
  3. After loop cnt=2

8) What will be the output of following program ?
#include <stdio.h>
void main()
{
	int cnt=1;
	while(cnt>=10)
	{
		printf("%d,",cnt);
		cnt+=1;
	}
	printf("\nAfter loop cnt=%d",cnt);
	printf("\n");
}
        
  1. After loop cnt=1
  2. 1,
    After loop cnt=2
  3. After loop cnt=2

9) What will be the output of following program ?
#include <stdio.h>
#define TRUE 1
int main()
{
	int loop=10;
	while(printf("Hello ") && loop--);
}
        
  1. Hello
  2. Hello Hello Hello Hello ... (Infinite times)
  3. Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (10 times)
  4. Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)

10) What will be the output of following program ?
#include <stdio.h>
int main()
{
	int i=1;
	while(i<=10 && 1++)
		printf("Hello");
}
        
  1. Error: lvalue required as increment operand
  2. HelloHello ... 10 times
  3. HelloHello ... 11 times
  4. Hello









COMMENTS