Biến,toán tử và biểu thức toán học

1. What will be output when you will execute following c code?

#include <stdio.h>
#include <conio.h>

int main()
{
    printf("%d\t", sizeof(6.5));
    printf("%d\t", sizeof(90000));
    printf("%d", sizeof('A'));
    getch();
}

Biết kích thước kiểu char : 1 byte, float : 4 byte, int : 4 byte, double : 8 byte, long : 4 byte.
A. 8 4 1
B. 8 2 1
C. 4 4 1
D. Depend on complier

Đáp án
A
Toán tử sizeof() trả về kích thước của biến (đơn vị là byte). 6.5 ở đây chương trình hiểu là kiểu double, 90000 là kiểu int, 'A' là hằng kí tự kiểu char.

 

2. What will be output when you will execute following c code?

#include <stdio.h>

int main()
{
    double num = 5.2;
    int var = 5;
    printf("%d\t", sizeof(!num));
    printf("%d\t", sizeof(var=15/2));
    printf("%d", var);
    return 0;
}

A. 1 4 5
B. 1 4 7
C. 8 4 7
D. Another

Đáp án
A
Giải thích: toán tử ! trả về giá trị kiểu bool (0, 1), mà sizeof(kiểu bool) = 1.
var = 15/2 = 7, sizeof(var) = 4

 

3. What value gets printed by the program below?

#include <stdio.h>

int main()
{
    int w = 3;
    int x = 31;
    int y = 10;
    double z = x / y % w;
    printf("%f\n", z);

    return 0;
}

A. 1
B. 0
C. 0.1

Đáp án
B
Toán tử / lấy thương, còn toán tử % lấy phần dư. Cả 2 toán tử này có độ ưu tiên toán tử như nhau. Do vậy, x/y%w = (x/y)%w = (31/10)%3 = 0

 

4. What will be output when you will execute following c code?

#include <stdio.h>

int main()
{
    char a = 250;
    int expr;
    expr= a + !a + ~a + ++a;
    printf("%d", expr);
    return 0;
}

A. – 6
B. 4
C. 5
D. Another

Đáp án
A
Biến char a = 250 vượt ra khỏi dải (-128 đến 127). Nên a = 250%256 = -6 (xem thêm: )
Trong các toán tử trong phép toán. Toán tử ! ~ ++ có cùng độ ưu tiên, nhưng trình tự kết hợp từ phải sang trái. Do vậy, ++a = -5 được thực hiện trước. expr = -5 + !(-5) + ~(-5) + (-5) = -5 + 0 + 4 -5 = -6

 

5. What will be output when you will execute following c code?

#include <stdio.h>

int main()
{
    int a = -5;
    unsigned int b = -5u; // (*)
    if(a == b)
        printf("Avatar");
    else
        printf("Alien");
    return 0;
}

A. Avatar
B. Alien
C. Error at (*)
D. Another

Đáp án
A

 

6. What will be output when you will execute following c code?

#include <stdio.h>
#include <conio.h>

void main()
{
    int x = 3;
    printf("%d", x++ + ++x);
    getch();
}

A. 7
B. 8
C. 9
D. Another

Đáp án
B
Phép toán ++x sẽ được thực hiện trước tiên (x = 4), sau đó x++ + x = 4++ + 4 = 8. Sau phép toán,giá trị củax = 5

 

7. What is output ?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 5, j = 6, k;
    k = i & j;
    printf("%d", k);
    getch();
}

A. 4
B. 0
C. 1
D. 5

Đáp án
A
i = 5 = 101 (hệ nhị phân), j = 6 = 110 (hệ nhị phân). Phép toán & là phép toán AND bit. k = i&j = 100 = 4.

 

8. What is output ?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 5,j = 6;
    printf("%d", i | j); 
    getch();
}

A. 7
B. 6
C. 5
D. 1

Đáp án
A
i = 5 = 101 (hệ nhị phân), j = 6 = 110 (hệ nhị phân). Phép toán | là phép toán OR bit. i|j = 111 = 7.

 

9. What is output ?

#include <stdio.h>
#include <conio.h>

extern int x = 0; 

void main()
{
    x++;
    printf("%d", x); 
    getch();
}

A. 0
B. Error
C. 1
D. x isn’t defined

Đáp án
C
Biến extern int x có thể thay đổi giá trị của nó.

 

10. What is output ?

#include <stdio.h>
#include <conio.h>

extern int x = 0;

void main()
{
    {
        int x = 1;
    }
    printf("%d", x);
    getch();
}

A. 0
B. 1
C. Error Comlier

Đáp án
A
Khai báo int x = 1, x ở đây là biến local và chỉ có giá trị trong {}. Hàm printf() ở ngoài {} sẽ dùng giá trị của biến x = 0.

 

11. What is output ?

#include <stdio.h>
#include <conio.h>

int y = 0;
void main()
{
    {
        int x = 0;
        x++;
        ++y;
    }
    printf("%d\t%d", x, y);
    getch();
}

A. 1 1
B. 1 0
C. ‘x’ undeclared identifier

Đáp án
C
Biến x là biến cục bộ, nên trình biên dịch sẽ báo lỗi tại lệnh printf().

 

12. Output of following code?

void main()
{
    int x;
    {
        x++;
    }
    printf("%d", x);
    getch();
}

A. 1
B. 0
C. Error

Đáp án

C
Giải thích: Lỗi biến x chưa được khởi tạo

 

13. Output of following code?

void main()
{
    int x=0;
    {
        int x = 0, y = 0;
        y++;
        x++;
    }
    printf("%d", x);
    getch();
}

A. 1
B. Error
C. 0

Đáp án

C
Giải thích: biến x được khai báo trong ngoặc {} là biến local, hàm printf() sẽ lấy giá trị x = 0.

 

14. Output of following code?

void count()
{
    static int page = 0;
    printf("%d", page);
    page++;
}

void main()
{
    int i;
    for(i = 0; i < 10; i++)
    {
        count();
    }
    getch();
}

A. 0123456789
B. 0000000000
C. 0101010101

Đáp án

A
Giải thích: biến page là biến static. Biến static được khai báo trong 1 hàm có đặc điểm giữ nguyên trạng thái (giá trị) giữa các lần gọi hàm.

 

15. Output of following code?

const int x = 5;

void main()
{
    int x[x];
    int y = sizeof(x) / sizeof(int);
    printf("%d", y);
    getch();
}

A. 1
B. 5
C. 20
D. ‘x’ isn’t defined

Đáp án

B
Giải thích: khai báo biến const x tương đương với việc sử dụng #define, chương trình biên dịch sẽ không cho phép người lập trình thay đổi biến này.
Theo msdn: việc sử dụng biến const để khai báo mảng trong C ko được phép.

 

16. What is output?

#include <stdio.h>

int main()
{
    int x = 5, y = 10, z = 15;
    printf("%d %d %d");
    return 0;
}

A. Garbage Garbage Garbage
B. 5 10 15
C. 15 10 5
D. Run time error

Đáp án

A

 

17. What is output?

#include <stdio.h>

int main()
{
asm
{
    mov bx, 8;
    mov cx,10
    add bx, cx;
}
    printf("%d", _BX);
    return 0;
}

A. 18
B. 8
C. 0
D. Complie error18.

 

 

18. What is output?

#include <stdio.h>

int main()
{
    char *url="c:\tc\bin\rw.c";
    printf("%s", url);
    return 0;
}

A. c:\tc\bin\rw.c
B. c:/tc/bin/rw.c
C. c: c inw.c
D. c:cinw.c
E. w.c in

Đáp án

E
Giải thích: trong chuỗi url, có các kí tự đặc biệt: ‘\t’ : cách 1 tab, ‘\b’ : đưa con trỏ trên màn hình lùi về trước 1 kí tự, ‘\r’ : quay về đầu dòng.
Chương trình sẽ in: c: c, gặp kí tự ‘\b’ đưa con trỏ lùi về 1 kí tự và kết quả tiếp theo: c: in. Khi gặp kí tự ‘\r’, con trỏ sẽ trỏ về vị trí đầu dòng và in đè lên kết quả đã có. Cuối cùng: w.c in

 

19. What is output ?

#include <stdio.h>

int main()
{
    const int i = 5;
    i++;
    printf("%d", i);
    return 0;
}

A. 5
B. 6
C. 0
D. Complier error

Đáp án

D
Giải thích: chương trình biên dịch sẽ không cho phép thay đổi giá trị của biến const.

 

20. What is output?

#include <stdio.h>
#include <conio.h>

void main()
{
    char c = 125;
    c = c + 10;
    printf("%d", c);
    getch();
}

A. 135
B. 8
C. -121
D. 121

Đáp án

C
Giải thích: biến char a = 135 vượt ra khỏi dải (-128 đến 127). Nên a = 135%256 = -121

 

21. What is output?

#include <stdio.h>
#include <conio.h>

int main()
{
    char c = 48;
    int i, mask = 01;
    for(i = 1; i <= 5; i++)
    {
        printf("%c", c|mask);
        mask = mask << 1;
    }
    getch();
}

A. 12480
B. 1248@
C. 12500
D. 12522

Đáp án

A
Giải thích:
c|mask = 49 -> in ra 1, mask = mask<<1 = 2
c|mask = 50 -> in ra 2, mask = mask<<1 = 4
c|mask = 52 -> in ra 4, mask = mask<<1 = 8
c|mask = 56 -> in ra 8, mask = mask<<1 = 16
c|mask = 48 -> in ra 0, end for()

 

22. What is output?

#include <stdio.h>
#include <conio.h>

int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    getch();
}

A. Hi
B. Hello
C. None of above

Đáp án

A

 

23. What is output of program?

#include <stdio.h>
#include <conio.h>
void main()
{
    int x = 10, y = 20, z = 5, i;
    i = x < y < z;
    printf("i = %d", i);
    getch();
}

A. i = 0
B. i = 1
C. Error
D. None of these

Đáp án
B
Toán tử so sánh có thứ tự ưu tiên từ trái sang phải. x < y <=> 10 < 20 trả về true (1). (x < y) < z <=> 1 < z trả về true (1)

 

24. Which of the declaration is correct?
A. int length;
B. char int;
C. int long;
D. float double;

Đáp án
A

 

25. What is output of program?

#include <stdio.h>
#include <conio.h>

int N = 10;
void main()
{
    int N = 20;
    printf("N = %d", N);
    getch();
}

A. N = 20
B. N = 10
C. Error
D. No Output

Đáp án
A

 

26. What is output of program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int a[5] = {1, 2};
    printf("%d %d %d", a[2], a[3], a[4]);
    getch();
}

A. 0 0 0
B. 1 2 2
C. 1 1 1
D. Error

Đáp án
A
Đối với mảng tĩnh, nếu khởi tạo thiếu cho các phần tử thì mặc định các phần tử đó được gán bằng 0.
Hay nói cách khác, câu lệnh int a[5] = {1,2}; <=> int a[5] = {1, 2, 0, 0, 0};

 

27. What is output of program?

#include <stdio.h>
#include <conio.h>

void main()
{
    extern int func(float);
    int a;
    a = func(3.14);
    printf("%d", a);
    getch();
}

int func(float a)
{
    return (int)++a;
}

A. 3
B. 4
C. Complier Error
D. 3.14

Đáp án
B
Toán tử ++ dùng cho cả số nguyên và số thập phân.

 

28. Which of the following operations are INCORRECT?
A. int i = 35; i = i%5
B. short int j = 5; j = j;
C. long int k = 123L; k = k;
D. float a = 3.14; a = a%3;

Đáp án
D
Toán tử % chỉ dùng cho số nguyên

 

29. Point out a error in the following program

#include <stdio.h>
#include <conio.h>

void main()
{
    void v = 0;
    printf("%d", v);
    getch();
}

A. Error: Declaration syntax error v (or) ‘v’: illegal use of type ‘void’
B. Program terminate abnormally
C. No error
D. None of these

Đáp án
A
Dùng kiểu con trỏ void* để trỏ đến kiểu dữ liệu khác.

 

30. In the following program how long will the for loop get executed?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 5;
    for(;scanf("%d", &i); printf("%d", i));
    getch();
}

A. The for loop would not get executed at all
B. The for loop would get executed only once
C. The for loop would get executed 5 times
D. The for loop would get executed infinite times

Đáp án
D
Hàm scanf() đợi người dùng nhập giá trị vào từ bàn phím. Đồng thời trong đoạn code này, hàm scanf() luôn trả về 1 (vòng lặp vô hạn), hàm printf() in ra giá trị nhập vào.

 

31. Point out the error in the following program (if it is compiled with Turbo C complier)

#include <stdio.h>
#include <conio.h>

void main()
{
    display();   
    getch();
}

void display()
{
    printf("vncoding.net");
}

A. No error
B. display( ) is not declared
C. None of these

Đáp án
B

 

32. Which of the following are unary operators in C?
1. !
2. sizeof
3. ~
4. &&

A. 1, 2
B. 1, 3
C. 2, 4
D. 1, 2, 3

Đáp án
D

 

33. In the expression a = b = 5 the order of Assignment is NOT decided by Associativity of operators.
A. True
B. False

Đáp án
B
Toán tử ‘=’ có trình tự kết hợp từ phải sang trái, b = 5 sau đó a = b.

 

34. Which of the following is the correct order if calling functions in the below code?

a = f1(11, 16) * f2(12/8)  +  f3();

A. f1, f2, f3
B. f3, f2, f1
C. Order may vary from complier to complier
D. None of above

Đáp án
C

 

35. Which of the following is the correct usage of condition operators used in C?
A. a>b? c = 20 : c = 21;
B. a>b? c = 20;
C. max = a>b? a>c?a:c:b>c?b:c;
D. return (a>b)?(a:b);

Đáp án
C
Câu lệnh max = a>b? a>c?a:c:b>c?b:c; <=> max = a>b? (a>c?a:c):(b>c?b:c);

 

36. The expression of the right hand side of || operators does not get evaluated if the left hand side determines the outcome.
A. True
B. False

Đáp án
A
Ví dụ: if(a || b) nếu a là khác 0, b sẽ không được đánh giá.

 

37. Which of the following correctly shows the hierarchy of arithmetic operator in C?
A. / + * –
B. * – / +
C. + – / *
D. / * + –

Đáp án
D

 

38. Which of the following is the correct order of evaluation for the below expression?
z = x + y * z / 4 % 2 – 1;
A. * / % + – =
B. = * / % + –
C. / * % – + =
D. * % / – + =

Đáp án

 

39. What will be the output of the program?

#include<stdio.h>
#include<conio.h>

int main()
{
    int i = 4, j = -1, k = 0, w, x, y, z;
    w = i||j||k;
    x = i&&j&&k;
    y = i||j&&k;
    z = i&&j||k;
    printf("%d  %d  %d  %d", w, x, y, z);
    getch();
}

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

Đáp án

 

40. Two different operators would always have different Associativity.
A. Yes
B. No

Đáp án

 

41. What will be the output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    static int a[20];
    int i = 0;
    a[i] = i;
    printf("%d  %d  %d", a[0], a[1], i);
    getch();
} 

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

Đáp án
C
Mảng static được tự động khởi tạo với giá trị 0

 

42. What will be the output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int x = 12, y = 7, z = 2;
    z = x != 4 || y == 2;
    printf("z = %d", z);
    getch();
}

A. z = 0
B. z = 1
C. z = 4
D. z = 3

Đáp án
B
z = x != 4 || y == 2 = (x != 4) || (y == 2) = 1 || 0 = 1

 

43. Every operators has an Associativity
A. Yes
B. No

Đáp án
B
Trình tự kết hợp của mỗi toán tử phụ thuộc vào nó đi kèm với toán tử nào.

 

44. Will the expression *p = p be disallowed by the complier?
A. Yes
B. No

Đáp án
A
Error: cannot convert from ‘int *’ to ‘int’

 

45. What will be the output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 3, j = 2, k = -1, m;
    m = ++i||++j&&++k;
    printf("%d  %d  %d  %d", i, j, k, m);
    getch();
}

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

Đáp án
A
m = ++i||++j&&++k = (++i) || (++j&&++k). Vì ++i = 4 != 0 nên (++j&&++k) không được thực hiện.

 

46. What is output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 3, j = 2, k = -1, m;
    m = ++i&&++j&&++k;
    printf("%d  %d  %d  %d", i, j, k, m);
    getch();
}

A. 4 3 0 0
B. 3 3 0 1
C. 4 2 0 0
D. None of these

Đáp án
A
m = ++i&&++j&&++k = (++i)&&(++j)&&(++k) = 4 && 3 && 0 = 0

 

47. Are the following two statement same?

1. a <= 20 ? (b = 30) : (c = 30);
2. (a <= 20) ? b : (c = 30);

A. Yes
B. No

Đáp án
A
a <= 20 ? (b = 30) : (c = 30) luôn trả về 30
Giá trị của “(a <= 20) ? b : (c = 30)” phụ thuộc vào giá trị của b

 

48. What will be the output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 2;
    int j = i + (1, 2, 3, 4);
    printf("j = %d", j);
    getch();
}

A. 6
B. 3
C. 12
D. Complier error

Đáp án
A
j = i + (1, 2, 3, 4) = i + 4 = 6

 

49. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int n = 2;
    printf("%d  %d   %d", n <= 3, n = 4, n >= 2);
    getch();
}

A. 0 4 1
B. 1 4 1
C. 1 4 1
D. None of these

Đáp án
A
Thứ tự thực hiện các phép toán trong printf từ phải sang trái.
(n >= 2) = (2 >= 2) = 1
n = 4
n <= 3 = (4 <= 3) = 0

 

50. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int n = 2;
    printf("%d  %d", ++n, ++n);
    getch();
}

A. 4 4
B. 3 4
C. 2 2
D. 2 3

Đáp án
A

51. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int a = 10, b = 19;
    int c;
    c = (a == 10 || b < 20);
    printf("c = %d", c);
    getch();
}

A. c = 1
B. c = 29
C. c = 10
D. c = 19

Đáp án
A

 

52. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int x = 4, y, z;
    y = --x;
    z = x--;
    printf("%d %d %d", x, y, z);
    getch();
}

A. 4 3 2
B. 4 3 3
C. 2 3 2
D. 2 3 3

Đáp án
D

 

53. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int k, num = 20;
    k = (num>5 ? (num <= 10 ? 10 : 30): 40);
    printf("%d", k);
    getch();
}

A. 30
B. 40
C. 20
D. 10

Đáp án
A

 

54. What is output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int i = 3;
    i = i++;
    printf("%d", i);
    getch();
}

A. 4
B. 3
C. Complier error
D. None of above

Đáp án
A

 

55. What will be output of the program?

#include <stdio.h>
#include <conio.h>

void main()
{
    int arr[3] = {3};
    int i;
    for(i = 0; i <= 2; i++)
    printf("%d, ", arr[i]);
    getch();
}

A. 3, 0, 0,
B. 3, 3, 3,
C. 3, garbage, garbage
D. Another

Đáp án
A

Be the first to comment

Leave a Reply