c++输入/输出 i/o_C基本输入/输出-能力问题与解答

c++输入/输出 i/o

C programming Basic Aptitude Questions and Answers: In this section, you will find C Aptitude Questions and Answers on Basic Input, Output, Data Type, printf, scanf etc.

C编程基本能力问答:在本节中,您将找到有关基本输入,输出,数据类型,printf,scanf等的C能力问答。

C编程基本输入,输出,声明,数据类型列表适度性问答 (List of C programming basic input, output, declaration, data types Aptitude Questions and Answers )

1) What will be the output of following program ?
#include <stdio.h>
void main()
{
    printf("includehelp.com\rOK\n");
    printf("includehelp.com\b\b\bOk\n");
}

  1. OK
    includehelp.ok

  2. OK
    includehelp.okm

  3. includehelp.com
    includehelp.okm

  4. OKcludehelp.com
    includehelp.okm

Answer & Explanation

Correct Answer - 4
OKcludehelp.com
includehelp.Okm

/r is an escape sequence which means carriage return. Carriage return takes back the cursor to the leftmost side in a line.

Thus in the statement printf("includehelp.com\rOK\n");

First "includehelp" is printed ( not still displayed) then cursor moves to leftmost position ("i" here) and starts printing "OK" which results in overwriting of first two characters of "includehelp". Thus the final output is "OKcludehelp.com" and then cursor moves to next line due to character feed \n.

In the second statement /b escape character is used which is equivalent to backspacing the cursor. Overwrite also took place here due to three backspaces.

N.B: The output is platform dependent.

1)以下程序的输出是什么?

  1. includehelp.ok


  2. includehelp.okm

  3. includehelp.com
    includehelp.okm

  4. OKcludehelp.com
    includehelp.okm

答案与解释

正确答案-4
OKcludehelp.com
includehelp.Okm

/ r是转义序列,表示回车。 回车将光标移到一行的最左侧。

因此,在语句中printf(“ includehelp.com \ rOK \ n”);

首先打印(未显示) “ includehelp”,然后光标移至最左侧位置(此处为“ i” )并开始打印“ OK” ,这将覆盖“ includehelp”的前两个字符。 因此,最终输出为“ OKcludehelp.com” ,然后由于输入字符\ n,光标移至下一行。

在第二个语句中,使用/ b转义字符,这等效于对光标进行退格。 由于三个退格键,此处也发生了覆盖。

注意:输出取决于平台

2) What will be the output of following program ?
#include <stdio.h>
void main(){
    unsigned char c=290;
    printf("%d",c);
}

  1. 34

  2. 290

  3. Garbage

  4. ERROR

Answer & Explanation

Correct Answer - 1
34

290 is beyond the range of unsigned char. Its corresponding value printed is: (290 % (UINT_MAX +1) where UINT_MAX represents highest (maximum) value of UNIT type of variable.

Here it's character type and thus UINT_MAX=255

Thus it prints 290 % (UINT_MAX +1)=34

2)以下程序的输出是什么?
  1. 34

  2. 290

  3. 垃圾

  4. 错误

答案与解释

正确答案-1
34

290超出了unsigned char的范围。 其对应的打印值是:( 290%(UINT_MAX +1) ,其中UINT_MAX表示变量的UNIT类型的最高(最大值)值。

这是字符类型,因此UINT_MAX = 255

因此它打印290%(UINT_MAX +1)= 34

3) What will be the output of following program ?
#include <stdio.h>
void main(){
    int a=0;
    a=5||2|1;
    printf("%d",a);
}

  1. 1

  2. 7

  3. 0

  4. 8

Answer & Explanation

Correct Answer - 1
1

Bitwise OR operator has precedence over logical OR operator

Thus the expression 5 || 2 | 1 is actually 5 || (2 |1)

Now

2= 0000 0010

1= 0000 0001

2|1= 0000 0011=3 (refer to bitwise operator)

5 || 3 returns true as both are nonzero

Thus a=1

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

  2. 7

  3. 0

  4. 8

答案与解释

正确答案-1
1个

按位或运算符的优先级高于逻辑或运算符

因此,表达式5 || 2 | 1实际上是5 || (2 | 1)

现在

2 = 0000 0010

1 = 0000 0001

2 | 1 = 0000 0011 = 3 (请参阅按位运算符 )

5 || 3都返回true,因为两者都不为零

因此a = 1

4) What will be the output of following program (on 32 bit compiler)?
#include <stdio.h>
int main(){
    float a=125.50;
    int   b=125.50;
    char  c='A';
 
    printf("%d,%d,%d\n",sizeof(a),sizeof(b),sizeof(125.50));
    printf("%d,%d\n",sizeof(c),sizeof(65));
    return 0;
}

  1. 4,4,4
    1,4

  2. 4,4,8
    1,1

  3. 4,4,4
    1,1

  4. 4,4,8
    1,4

Answer & Explanation

Correct Answer - 4
4,4,8
1,4

sizeof(a)= 4 bytes (float), sizeof(b)=4 bytes(int...in Visual studion, 2 in turboc), sizeof(125.50)=8 bytes (125.50 will consider as double constant), sizeof(c)= 1 byte (character), sizeof(65) = 4 bytes ( 65 is an integer ).

4)以下程序(在32位编译器上)的输出是什么?
  1. 4,4,4
    1,4

  2. 4,4,8
    1,1

  3. 4,4,4
    1,1

  4. 4,4,8
    1,4

答案与解释

正确答案-4
4,4,8
1,4

sizeof(a) = 4个字节(浮点数), sizeof(b) = 4个字节( int ...在Visual Studion中为2,在turboc中), sizeof(125.50) = 8个字节(125.50将被视为双常量), sizeof( c) = 1个字节(字符), sizeof(65) = 4个字节(65是整数)。

5) What will be the output of following program ?
#include <stdio.h>
int main(){
    static char a;
    static long b;
    int c;
 
    printf("%d,%d,%d",a,b,c);
    return 0;
}

  1. 0,0,0

  2. Garbage,Garbage,Garbage

  3. Garbage,Garbage,0

  4. 0,0,Gargabe

Answer & Explanation

Correct Answer - 4
0,0,Garbage Value

Static variable always initialize with 0, and other one by garbage value.

N.B: output is compiler dependent

5)以下程序的输出是什么?
  1. 0,0,0

  2. 垃圾,垃圾,垃圾

  3. 垃圾0

  4. 0,0,Gargabe

答案与解释

正确答案-4
0,0,垃圾价值

静态变量始终以0初始化,而其他变量始终以垃圾值初始化。

注意:输出取决于编译器

6) What will be the output of following program ?
#include <stdio.h>
int main()
{
    int ok=-100;
    -100;
    printf("%d",ok);
    return 0;
}

  1. 100

  2. ERROR

  3. -100

  4. 0

Answer & Explanation

Correct Answer - 3
-100

-100 is evaluated and this does not effect the value of ok.

6)以下程序的输出是什么?
  1. 100

  2. 错误

  3. -100

  4. 0

答案与解释

正确答案-3
-100

评估-100 ,这不会影响ok的值。

7) What will be the output of following program ?
#include <stdio.h>
enum numbers
{
    zero, one, two, three , four=3,five,six,seven=0,eight
};
void main()
{
    printf("%d,%d,%d,%d,%d,%d,%d,%d,%d",zero,one,two,three,four,five,six,seven,eight);
}

  1. 0,1,2,3,3,4,5,0,1

  2. 0,1,2,4,5,6,7,8,9

  3. 0,1,2,3,3,1,2,3,4

  4. 0,1,2,3,3,4,5,0,9

Answer & Explanation

Correct Answer - 1
0,1,2,3,3,4,5,0,1

We have discussed that enum values starts with 0 (if we do not provide any value) and increase one by one.

We are assigning 3 to four, then four will be 3, five will be 4 ... again we are assigning 0 to seven then seven will be 0 and eight will be 1.

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

  2. 0,1,2,4,5,6,7,8,9

  3. 0,1,2,3,3,1,2,3,4

  4. 0,1,2,3,3,4,5,0,9

答案与解释

正确答案-1
0,1,2,3,3,4,5,0,1

我们已经讨论了枚举值以0(如果我们不提供任何值)开头并以1递增。

我们将3分配给4,然后将4分配为3,将5分配为4 ...再次将0分配给7,然后将7分配为0,将8分配为1。

8) What will be the output of following program ?
#include <stdio.h>
int main(){
    int a,b,c;
    a=0x10; b=010;
    c=a+b;
    printf("\nAddition is= %d",c);
    return 0;
}

  1. Addition is = 20

  2. Addition is = 24

  3. Addition is = Garbage

  4. ERROR

Answer & Explanation

Correct Answer - 2
Addition is = 24

0x10 is hex value it's decimal value is 16 and 010 is an octal value it's decimal value is 8, hence answer will be 24.

8)以下程序的输出是什么?
  1. 加法是= 20

  2. 加法是= 24

  3. 加法是=垃圾

  4. 错误

答案与解释

正确答案-2
加法是= 24

0x10是十六进制值,其十进制值为16,而010是八进制值,其十进制值为8,因此答案为24。

9) What will be the output of following program ?
#include <stdio.h>
int main()
{
    int var=250;
    printf("value of var = %d\n",var);
    200+50;
    "includehelp.com";
    printf("%s\n","includehelp");
    return 0;
}

  1. value of var = 250
    includehelp.com

  2. value of var = 250
    includehelp

  3. ERROR

  4. value of var = 250
    Garbage

Answer & Explanation

Correct Answer - 2
value of var = 250
includehelp

200+50; and "includehelp.com"; are executable statements, will not give any error because there is no action is performed by these statements.

9)以下程序的输出是什么?
  1. 值var = 250
    includehelp.com

  2. 值var = 250
    包括帮助

  3. 错误

  4. 值var = 250
    垃圾

答案与解释

正确答案-2
值var = 250
包括帮助

200 + 50; 和“ includehelp.com”; 是可执行语句,不会出现任何错误,因为这些语句没有执行任何操作。

10) What will be the output of following program ?
#include <stdio.h>
int main()
{
    int a=100;
    printf("%d\n"+1,a);
    printf("Value is = %d"+3,a);
    return 0;
}

  1. ERROR

  2. 101,
    Value is = 103

  3. d
    ue is = 100

  4. 100
    100

Answer & Explanation

Correct Answer - 3
d
ue is = 100

The "+" moves the string pointer passed to printf().
"%d\n"+1 ... Prints "d"
Value is = %d"+3 ... Prints "ue is = 100"

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

  2. 101,
    值= 103

  3. d
    ue = 100

  4. 100
    100

答案与解释

正确答案-3
d
ue = 100

“ +”将传递给printf()的字符串指针移动。
“%d \ n” +1 ...打印“ d”
值是=%d“ +3 ...打印” ue is = 100“

11) What will be the output of following program ?
#include <stdio.h>
int main()
{
    extern int ok;
    printf("value of ok = %d",ok);
    return 0;
}
extern int ok=1000;

  1. Declaration error

  2. value of ok = 1000.

  3. Linking error

  4. value of ok = 0.

Answer & Explanation

Correct Answer - 2
value of ok = 1000.

extern keyword is used to extend the visibility of variables thus ok is declared and defined memory though the statement extern int ok=1000; is outside main function.

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

  2. ok的值= 1000。

  3. 链接错误

  4. ok的值= 0。

答案与解释

正确答案-2
ok的值= 1000。

extern关键字被用于扩展从而确定被声明变量尽管声明的extern INT确定可见性和定义的存储器= 1000; 在主要功能之外。

12) What will be the output of following program ?
#include <stdio.h>
int main()
{
    int a=23;
    ;
    ;printf("%d",a);
    ;
    return 0;
}

  1. 23

  2. Error

  3. ;23;

  4. ;23

Answer & Explanation

Correct Answer - 1
23

; (statement terminator) and no expression/statement is available here, so this is a null statement has no side effect, hence no error is occurred.

12)以下程序的输出是什么?
  1. 23

  2. 错误

  3. ; 23;

  4. ; 23

答案与解释

正确答案-1
23

; (语句终止符)并且此处没有表达式/语句可用,因此这是一个空语句,没有副作用,因此不会发生错误。

13) Find the output of this program,(program name is : static_ex.c)?
#include <stdio.h>
int main()
{
    int intVar=24;
    static int x=intVar;
    printf("%d,%d",intVar,x);
    return 0;
}

  1. 24,24

  2. 24,0

  3. ERROR: Illegal Initialization

  4. Run time error.

Answer & Explanation

Correct Answer - 3
ERROR: Illegal Initialization.

You can not assign a variable into static variable at the time of declaration.

13)找到该程序的输出,(程序名是: static_ex.c )?
  1. 24,24

  2. 24,0

  3. 错误:非法初始化

  4. 运行时错误。

答案与解释

正确答案-3
错误:非法初始化。

声明时不能将变量分配给静态变量。

14) Find the output of this program ?
#include <stdio.h>
int main()
{
    int a=15;
    float b=1.234;
    printf("%*f",a,b);
    return 0;
}

  1. 1.234

  2. 1.234000

  3.        1.234000

  4. ERROR

Answer & Explanation

Correct Answer - 3
       1.234000

You can define width formatting at run time using %*, This is known as Indirect width precision .

printf("%*f",a,b); is considered as "%15f", hence value of b is printed with left padding by 15.

14)找到该程序的输出?
  1. 1.234

  2. 1.234000

  3. 1.234000

  4. 错误

答案与解释

正确答案-3
1.234000

您可以在运行时使用%*定义宽度格式,这称为间接宽度精度

printf(“%* f”,a,b); 被视为“%15f” ,因此b的值以15的左填充打印。

15) Find the output of this program ?
#include <stdio.h>
int main()
{
    float a,b;
    a=3.0f;
    b=4.0f;
    printf("%.0f,%.1f,%.2f",a/b,a/b,a/b);
    return 0;
}

  1. 1,0.8,0.75

  2. 0,0.7,0.75

  3. 0,0.8,0.75

  4. ERROR: Invalid format specifier

Answer & Explanation

Correct Answer - 1
1,0.8,0.75 (Using rounding off technique)

15)找到该程序的输出?
  1. 1,0.8,0.75

  2. 0,0.7,0.75

  3. 0,0.8,0.75

  4. 错误:格式说明符无效

答案与解释

正确答案-1
1,0.8,0.75 (使用四舍五入技术)

16) Which is the valid identifier (variable name) to store student’s age?
  1. int student-age;

  2. int student_age;

  3. int -age;

  4. int _age;

Answer & Explanation

Correct Answer - 2 and 4

For more details check the variable/identifier naming conventions.

16)用来存储学生年龄的有效标识符(变量名)是什么?
  1. 学生年龄

  2. int student_age;

  3. 年龄

  4. int _age;

答案与解释

正确答案-2和4

有关更多详细信息,请检查变量/标识符命名约定

17) What type of declaration is it?
    unsigned num;

  1. num is an unsigned integer.

  2. num is an unsigned character.

  3. num is an unsigned float.

  4. It’s an invalid declaration.

Answer & Explanation

Correct Answer - 1
num is an unsigned integer.

17)这是什么类型的声明?
  1. num是一个无符号整数。

  2. num是一个无符号字符。

  3. num是一个无符号的浮点数。

  4. 这是无效的声明。

答案与解释

正确答案-1
num是一个无符号整数。

18) Which statement does not require semicolon?
  1. goto xyz

  2. int x=20

  3. #define MAX 1000

  4. do{ ... }while(count<=100)

Answer & Explanation

Correct Answer - 3

#define MAX 1000 - does not require semicolon.

18)哪个语句不需要分号?
  1. 转到xyz

  2. 整数x = 20

  3. #定义MAX 1000

  4. 做{...} while(count <= 100)

答案与解释

正确答案-3

#define MAX 1000-不需要分号。

翻译自: https://www.includehelp.com/c/basic-input-output-aptitude-questions-and-answers.aspx

c++输入/输出 i/o

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值