人生有度,好在适度_C循环(片刻,片刻,片刻)-适度性问题与解答

人生有度,好在适度

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.

C编程循环能力问题和解答:在本节中,您将在各种循环语句(如while,do dhile,嵌套循环等)中找到C能力倾向问题和解答。

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

Answer
Correct Answer - 1

0 1 2 ... 255
The range of unsigned char is 0 to 255 and when the value of var will cross over 255, value will be 0 and again same process will happen.

1)以下程序的输出是什么?
  1. 0 1 2 ...无限次

  2. 0 1 2 ... 255

回答
正确答案-1

0 1 2 ... 255
无符号char的范围是0到255,并且当var的值超过255时,value将是0,并且再次发生相同的过程。

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

Answer
Correct Answer - 4

1
Before entering into the for loop the CHECK CONDITION is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).

2)以下程序的输出是什么?
  1. 0 1 2 ...无限次

  2. 0 1 2 ... 127

  3. 0

  4. 1个

回答
正确答案-4

1个
在进入for循环之前,将对“ 检查条件 ”进行“评估”。 在这里,它的值为0(假)并退出循环,并且i递增(请注意for循环后的分号)。

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

Answer
Correct Answer - 1

Compiler error: Undefined label ‘print’ in function main.
Labels have functions scope, in other words the scope of the labels is limited to functions. The label ‘print’ is available in function fun() Hence it is not visible in function main.

3)以下程序的输出是什么?
  1. 错误

  2. 12345includehelp.com

  3. 1234includehelp.com

  4. 1includehelp.com 2includehelp.com 3includehelp.com 4includehelp.com 5includehelp.com

回答
正确答案-1

编译器错误:函数main中的未定义标签'print'。
标签具有功能范围 ,换句话说,标签的范围仅限于功能。 标签'print'在fun()函数中可用,因此在main函数中不可见。

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

Answer
Correct Answer - 3

1 2 3 4 5 6 7 8 9 10
for(; ;) it is possible in c, there is no need to place condition with in the for(), you can place condition with in the body of the loop.

4)以下程序的输出是什么?
  1. 0 1 2 3 4 5 6 7 8 9 10

  2. 0 1 2 3 ...无限次

  3. 1 2 3 4 5 6 7 8 9 10

  4. 1 2 3 4 5 6 7 8 9

回答
正确答案-3

1 2 3 4 5 6 7 8 9 10
for(;;)可能在c中,不需要在for()中放置条件,您可以在循环体中放置条件。

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#

Answer
Correct Answer - 1

#0#1#2#3#4#5#6###

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
    A B C D
    A B C D
    A B C D
            
    

  2. A B C D E
    A B C D
    A B C
    A B
    A
            
    

Answer
Correct Answer - 4
6)以下程序的输出是什么?
#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
    A B C D
    A B C D
    A B C D
            
    
  2. A B C D E
    A B C D
    A B C
    A B
    A
            
    
回答
正确答案-4
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

Answer
Correct Answer - 2

1,
After loop cnt=2

do while is an exit controlled loop, here loop body executed first, then condition will be checked.

7)以下程序的输出是什么?
  1. 循环后cnt = 1

  2. 1,
    循环后cnt = 2

  3. 循环后cnt = 2

回答
正确答案-2

1,
循环后cnt = 2
do while是一个退出控制的循环,此处首先执行循环主体,然后将检查条件。

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

Answer
Correct Answer - 1

1,
After loop cnt=1

Here condition will be checked first.

8)以下程序的输出是什么?
  1. 循环后cnt = 1

  2. 1,
    循环后cnt = 2

  3. 循环后cnt = 2

回答
正确答案-1

1,
循环后cnt = 1
这里将首先检查条件。

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)

Answer
Correct Answer - 4

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello (11 times)

9)以下程序的输出是什么?
  1. 你好

  2. 你好你好你好...(无限次)

  3. 你好你好你好你好你好你好你好你好你好你好(10次)

  4. 你好你好你好你好你好你好你好你好你好你好你好你好(11次)

回答
正确答案-4

你好你好你好你好你好你好你好你好你好你好你好你好(11次)

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

Answer
Correct Answer - 1

Error: lvalue required as increment operand.

10)以下程序的输出是什么?
  1. 错误:需要左值作为增量操作数

  2. 你好你好... 10次

  3. 你好你好... 11次

  4. 你好

回答
正确答案-1

错误:需要左值作为增量操作数。

翻译自: https://www.includehelp.com/c-programs/c-looping-aptitude-questions-and-answers.aspx

人生有度,好在适度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值