wujr5 / c-and-cpp-language-learning

C和C++编程语言学习 - 2015级
68 stars 34 forks source link

软件:week6 理论题(2015-11-2 实验课结束提交) #16

Open ghostbody opened 9 years ago

ghostbody commented 9 years ago

1. What's the output of the following program:

#include <stdio.h>
int main() {
    int n = 1, m = 4;
    while(n = 1) {
        m--;
        n--;
    }
    printf("%d %d\n", n, m);
    return 0;
}

A. 1 -1
B. 1 0
C. Dead Loop
D. 0 0

2.What's the output of the following program:

#include<stdio.h>
int main() {
    int a=4,b=8,c=5;
    int d1,d2,d3,d4;
    d1=(a<b)||(++a==5)||(c>b--);
    printf("d1=%d,a=%d,b=%d,c=%d\n",d1,a,b,c);
    d2=(a>b)&&(++a==5)||(c>b--);
    printf("d2=%d,a=%d,b=%d,c=%d\n",d2,a,b,c);
    d3=(a<b)||(++a==5)&&(c>b--);
    printf("d3=%d,a=%d,b=%d,c=%d\n",d3,a,b,c);
    d4=(a>b)&&(++a==5)&&(c>b--);
    printf("d4=%d,a=%d,b=%d,c=%d\n",d4,a,b,c);
    return 0;
}

A. d1=1,a=5,b=7,c=5 d2=0,a=6,b=6,c=5 d3=0,a=7,b=5,c=5 d4=1,a=8,b=6,c=5
B. d1=1,a=5,b=7,c=5 d2=1,a=6,b=6,c=5 d3=1,a=7,b=5,c=5 d4=0,a=8,b=6,c=5
C. d1=1,a=4,b=8,c=5 d2=0,a=4,b=7,c=5 d3=1,a=4,b=7,c=5 d4=0,a=4,b=7,c=5
D. Compile error

3.What's the output of the following program:

#include<stdio.h>
int main() {
    int a = 2, b = 3, c = 4;
    printf("%d %d",c > b > a, c < b < a);
    return 0;
}

A. 0 0
B. 0 1
C. 1 0
D. 1 1

4.What's the output of the following program:

#include<stdio.h>
int main() {
    int a = 3, b = 4, c = 5;
    int d = (a + b) + c - 1 && b + c / 2;
    printf("%d\n", d);
    return 0;
}

A. 13
B. 1
C. 3
D. Compile error

5.What's the output of the following program:

#include<stdio.h>
int main() {
    float x = 2.5, y = 4.7;
    int a = 7;
    printf("%.1f\n", x+a%3*(int)(x+y)%2/4);
    return 0;
}

A. 2.5
B. error: invalid operands to binary %
C. 0.0
D. 1.2

6.Expression !x||a==b is Equivalent to A. !((x||a)==b)
B. !(x||y)==b
C.!(x||(a==b))
D.(!x)||(a==b)

7.What's the output of the following program:

#include<stdio.h>
int main() {
    int i = -2;
    int n = ++i == 0 ? 99 : (i == -1 ? 11 : 22);
    printf("%d\n",  n);
    return 0;
}

A.22
B.Compile error
C.0
D.11

8.What's the output of the following program:

#include<stdio.h>
int main() {
    int x = 0, y = 0;
    switch(x) {
        x++;
      y = 2;
      case(0):
        printf("%d\n", y--);
        break;
      case(1):
        printf("%d\n", ++y);
        break;
   }
}

A. 0
B. 2
C. 3
D. -1

9.What's the output of the following program:

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

A. 10 10 11 9
B. 10 11 10 10
C. 9 8 9 9
D. 10 11 10 9

10.What's the output of the following program:

int main() {
    int a = 21, b = 11;
    printf("%d\n", (--a+b, --b+a));
}

A.30
B.31
C.32
D.33

ghostbody commented 9 years ago

答案,请大家搞懂为什么

1. What's the output of the following program:

#include <stdio.h>
int main() {
    int n = 1, m = 4;
    while(n = 1) {
        m--;
        n--;
    }
    printf("%d %d\n", n, m);
    return 0;
}

A. 1 -1
B. 1 0
C. Dead Loop
D. 0 0

answer: C

2.What's the output of the following program:

#include<stdio.h>
int main() {
    int a=4,b=8,c=5;
    int d1,d2,d3,d4;
    d1=(a<b)||(++a==5)||(c>b--);
    printf("d1=%d,a=%d,b=%d,c=%d\n",d1,a,b,c);
    d2=(a>b)&&(++a==5)||(c>b--);
    printf("d2=%d,a=%d,b=%d,c=%d\n",d2,a,b,c);
    d3=(a<b)||(++a==5)&&(c>b--);
    printf("d3=%d,a=%d,b=%d,c=%d\n",d3,a,b,c);
    d4=(a>b)&&(++a==5)&&(c>b--);
    printf("d4=%d,a=%d,b=%d,c=%d\n",d4,a,b,c);
    return 0;
}

A. d1=1,a=5,b=7,c=5 d2=0,a=6,b=6,c=5 d3=0,a=7,b=5,c=5 d4=1,a=8,b=6,c=5
B. d1=1,a=5,b=7,c=5 d2=1,a=6,b=6,c=5 d3=1,a=7,b=5,c=5 d4=0,a=8,b=6,c=5
C. d1=1,a=4,b=8,c=5 d2=0,a=4,b=7,c=5 d3=1,a=4,b=7,c=5 d4=0,a=4,b=7,c=5
D. Compile error
answer: C
3.What's the output of the following program:

#include<stdio.h>
int main() {
    int a = 2, b = 3, c = 4;
    printf("%d %d",c > b > a, c < b < a);
    return 0;
}

A. 0 0
B. 0 1
C. 1 0
D. 1 1

answer: B

4.What's the output of the following program:

#include<stdio.h>
int main() {
    int a = 3, b = 4, c = 5;
    int d = (a + b) + c - 1 && b + c / 2;
    printf("%d\n", d);
    return 0;
}

A. 13
B. 1
C. 3
D. Compile error

answer: B

5.What's the output of the following program:

#include<stdio.h>
int main() {
    float x = 2.5, y = 4.7;
    int a = 7;
    printf("%.1f\n", x+a%3*(int)(x+y)%2/4);
    return 0;
}

A. 2.5
B. error: invalid operands to binary %
C. 0.0
D. 1.2

*answer:** A

6.Expression !x||a==b is Equivalent to A. !((x||a)==b)
B. !(x||y)==b
C.!(x||(a==b))
D.(!x)||(a==b)

answer: D

7.What's the output of the following program:

#include<stdio.h>
int main() {
    int i = -2;
    int n = ++i == 0 ? 99 : (i == -1 ? 11 : 22);
    printf("%d\n",  n);
    return 0;
}

A.22
B.Compile error
C.0
D.11

answer: D

8.What's the output of the following program:

#include<stdio.h>
int main() {
    int x = 0, y = 0;
    switch(x) {
        x++;
      y = 2;
      case(0):
        printf("%d\n", y--);
        break;
      case(1):
        printf("%d\n", ++y);
        break;
   }
}

A. 0
B. 2
C. 3
D. -1

answer: A
9.What's the output of the following program:

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

A. 10 10 11 9
B. 10 11 10 10
C. 9 8 9 9
D. 10 11 10 9

answer: C

10.What's the output of the following program:

int main() {
    int a = 21, b = 11;
    printf("%d\n", (--a+b, --b+a));
}

A.30
B.31
C.32
D.33

answer: A

有问题请大家在下面留言讨论

fengyh3 commented 9 years ago

师兄,那个第10题能讲解一下吗?

smallGum commented 9 years ago

师兄可以讲解下第九题吗?我想了好久没想明白……

ghostbody commented 9 years ago

@15331080 @smallGum 第九题第十题,涉及到printf执行的时候是从左往右进栈,然后从右往左依次出栈。第十题,逗号运算符。

smallGum commented 9 years ago

表示不懂入栈出栈是怎么回事啊……又要自己百度吗……5555

fengyh3 commented 9 years ago

啊啊啊懂了懂了,谢谢师兄。

8652 commented 9 years ago

师兄,我查了下……貌似printf计算从左向右进栈,从右向左出栈; 见:http://blog.csdn.net/swliao/article/details/5335363 输出时从右向左进栈,从左向右出栈? 见:http://www.cnblogs.com/Robotke1/archive/2013/05/15/3079057.html

ghostbody commented 9 years ago

@8652 不是,只有一次进栈和出栈。你先搞懂什么是栈。printf是先将后面所有表达式压栈,然后依次出栈进行进算处理。

8652 commented 9 years ago

因缺斯汀,谢谢师兄。

inkblack commented 9 years ago

仍然不懂第九题【躺倒,不过第十题按照逗号运算符好像是得30,但是我并不知道栈……这个算是 蒙的对吗

ghostbody commented 9 years ago

@inkblack 从右往左计算。

zichang06 commented 9 years ago

求解释一下第八题~多谢!!

ghostbody commented 9 years ago

@rui01 请自行理解switch语句。

inkblack commented 9 years ago

从右往左计算……那num--不是也应该先输入后减吗,那不就还是……9 9 9 9…