If/else , switch( ) case , goto

1. What gets printed?

void main()
{
    int i = 3;
    if (!i)
       i++;
    i++;

    if (i == 3)
    i += 2;
    i += 2;
    printf("%d\n", i);
    getch();
}

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

Đáp án
C
Giải thích: !i = 0 nên câu lệnh i++ đầu tiên không được thực hiện, câu lệnh i++ tiếp theo được thực hiện (i = 4). Gặp lệnh if tiếp theo không thỏa mãn, câu lệnh i+=2 thứ 2 được thực hiện.

2. What gets printed?

void main()
{
    int x;
    if(x = 0)
        printf ("Value of x is 0");
    else
        printf ("Value of x is not 0");
    getch();
}

A. Value of x is 0
B. Value of x is not 0
C. Error

Đáp án
B
Giải thích: biểu thức x=0 của lệnh if(x=0) là phép gán 0 cho biến x mà ko phải biểu thức so sánh. Biểu thức gán này có giá trị là 0 nên câu lệnh của else sẽ được thực hiện.

 

3. What gets printed?

void main()
{
    int i;
    for(i = 0; i < 20; i++)
    {
        switch(i)
        {
        case 0:i+=5;
        case 1:i+=2;
        case 5:i+=5;
        default: i+=4;
        break;
        }
        printf("%d,", i);
    }
    getch();
}

A. 14,18,
B. 16,20,
C. 16,21,

Đáp án
C
Giải thích: i=0. Do không có lệnh break giữa các case nên tất cả các case được thực hiện i = 16, gặp lệnh break thoát ra khỏi switch case, in ra : 16, . Vòng lặp mới i++ (i = 17), nhảy vào case default i = 17+4 = 21, gặp lệnh break thoát khỏi switch case và in ra: 16,21. Vì i = 21 > 20, vòng for kết thúc.

 

4. What gets printed ?

void main()
{
    static int i;
    while(i <= 10&&i >= 0)
        (i > 2 ? i++ : i--);
    printf("%d", i);
    getch();
}

A. -1
B. 0
C. Complier error

Đáp án
A
Giải thích: biến i là biến static nên complier sẽ tự động khởi tạo cho i = 0. Vì i < 2, nên i-- được thực hiện i = -1.

 

5. What is output ?

void main()
{
    int i = 10, j = 20;
    if(i = 20)
        printf(" Hello");
    else
        printf(" Hi");
 getch();
}

A.Hello
B. Hi
C. Complier error

Đáp án
A
Giải thích: i = 20 là biểu thức gán (không phải biểu thức so sánh). Giá trị của biểu thức này trả về là #0.Nên lệnh printf(" Hello"); được thực hiện

 

6. What is output?

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

A. x=0 y=1
B. x=0 y=0
C. Error syntax

Đáp án
B
Giải thích: Biểu thức so sánh x == 0 là TRUE nên biểu thức ++y sẽ không được thực hiện.

 

7. What is output?

void main()
{
    int i = 5, k;
    if (i == 0)
        goto label;
label: printf("%d", i);
    printf("Hey");
    getch();
}

A. Hey
B. 5
C. 5Hey

D. Complie error
Đáp án

C
Giải thích: Lệnh goto label không được thực hiện, nhưng lệnh label: printf("%d", i); vẫn được thực hiện.

 

8. What is output?

void main()
{
    printf("%d ", 1);
    goto l1;
    printf("%d ", 2);
l1:goto l2;
    printf("%d ", 3);
l2:printf("%d ", 4);
    getch();
}

A. 1 4
B. 1 2 3 4
C. Syntax error
D. Another

Đáp án
A

 

9. What is output?

#include <conio.h>
#include <stdio.h>
void foo();
int main()
{
    printf("%d ", 1);
    goto l1;
    printf("%d ", 2);
}
void foo()
{ 
l1: printf("3 ");
}

A. Complie error
B. 3
C. 1
D. 1 3

Đáp án
A
Giải thích: lệnh goto chỉ có thể nhảy tới label trong cùng 1 hàm. Chương trình sẽ báo lỗi label l1 chưa được khai báo.

 

10. What is output?

#include <conio.h>
#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    while (i < 2)
    {
l1: i++;
        while (j < 3)
        {
            printf("loop\n");
            goto l1;
        }
    }
    getch();
}

A. loop loop loop
B. Infinite loop
C. Complie error

Đáp án
B
Giải thích: biểu thức j < 3 luôn TRUE nên vòng while(j < 3) sẽ là vòng lặp vô hạn.

 

11. What is output?

void main()
{
    int i = 0, j = 0;
    while (l1: i < 2)
    {
        i++;
        while (j < 3)
        {
            printf("loop\n");
            goto l1;
        }
    }
    getch();
}

A. loop loop
B. loop loop loop loop
C. Complie error
D. Another

Đáp án
C
Giải thích: label của lệnh goto không được đặt trong biểu thức điều kiện của vòng lặp.

 

12. What is output?

void main()
{
    int a = 15, b = 10, c = 5;
    if(a > b > c)
        printf("True");
    else
        printf("False"); 
    getch();
}

A. True
B. False
C. Complier Error
D. Run time error

Đáp án
B
Giải thích: biểu thức a>b>c sẽ được thực hiện từ trái sang phải. a>b trả về giá trị 1, 1>c (c=5) trả về giá trị là 0. Do vậy, lệnh printf("False"); sẽ được thực hiện.

 

13. What is output?

#include <stdio.h>
#include <conio.h>
void main()
{
    int i = 0;
    switch (i)
    {
    case '0': printf("A");
        break;
    case '1': printf("B");
        break;
    default: printf("ABC");
    }
    getch();
}

A.  A
B. B
C. ABC

Đáp án
ABC
Giải thích: trong lệnh switch(<biểu thức điều kiện>), biểu thức điều kiện phải là số nguyên. giá trị của kí tự '0' trong bảng mã ASCII là 48, của kí tự '1' là 49. Do vậy, chương trình sẽ nhảy vào case default.

 

14. What is output?

#include <stdio.h>
#include "conio.h"
void main()
{
    int i = 3;
    switch (i)
    {
    case 0+1: printf("A");
        break;
    case 1+2: printf("B");
        break;
    default: printf("ABC");
    }
    getch();
}

A. A
B. B
C. ABC

Đáp án
B

 

15. What is output?

#include <stdio.h>
#include <conio.h>
#define A 0
#define B 1
int main()
{
    int i = 3;
    switch (i & 1)
    {
    case A: printf("FALSE");
        break;
    case B: printf("TRUE");
        break;
    default: printf("Default");
    }
    getch();
}

A. FALSE
B. TRUE
C. Default

Đáp án
B
Giải thích: i&1 = 101&1 = 1.

 

16. What is output?

#include <stdio.h>
#include <conio.h>
int main()
{
    int i;
    if (printf("0"))
        i = 3;
    else
        i = 5;
    printf("%d", i);
    getch();
}

 

A. 3
B. 5
C. 03
D. 05

Đáp án
C
Giải thích: hàm printf() trả về số lượng kí tự in ra màn hình. printf("0") trả về giá trị 1.

 

17. What is output?

#include<stdio.h>
#include <conio.h>
int main()
{
    int a = 5;
    switch(a)
    {
    default:
        a = 4;
    case 6:
        a--;
    case 5:
        a = a + 1;
    case 1:
        a = a - 1;
    }
    printf("%d \n", a);
    getch();
}

A. 5
B. 4
C. 3

Đáp án
A
Giải thích: chương trình sẽ nhảy vào case 5 và thực hiện lệnh a = a+1 = 6. Nhưng vì không có lệnh break nên a = a - 1 = 5 sẽ được thực hiện.

 

18. What is output?

#include <stdio.h>
#include <conio.h>
int main()
{
    int x = 3;
    if (x == 2); x = 0;
    if (x == 3) x++;
    else x += 2;

    printf("x = %d", x);
    getch();
}

A. x = 2
B. x = 6
C. x = 0

Đáp án
A
Giải thích: x = 3, x = 0, x += 2 được thực hiện.

 

19. What is output?

#include <stdio.h>
#include <conio.h>
int main()
{
    int check = 20, arr[] = {10, 20, 30};
    switch (check)
    {
    case arr[0]: printf("A ");
    case arr[1]: printf("B");
    case arr[2]: printf("C");
    }
    getch();
}

A. ABC
B. BC
C. B
D. Complier Error

Đáp án
D
Giải thích: case <biểu thức>, biểu thức phải là hằng số nguyên. arr[0], arr[1], arr[2] không phải là hằng số nguyên.

 

20. What is output of code?

void main() 
{
    if ((1 || 0) && (0 || 1)) 
    {
        printf("ABC");
    } 
    else 
    {
        printf("DEF");
    }
    getch();
}

A. ABC
B. DEF
C. Syntax error

Đáp án
A
Giải thích: biểu thức logic ((1 || 0) && (0 || 1)) = (1 && 1) = 1.

 

21. What is output of code?

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

void main()
{
    char str1[] = "vncoding";
    char str2[] = "vncoding";
    if (strcmp(str1, str2))
        printf("Equal");
    else
        printf("Unequal");

    getch();
}

A. Equal
B. Unequal
C. nothing is printed
Đáp án

B

 

22. What is output of code?

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

void main()
{
    int i;
    i = 10;
    if(i == 20 || 30)
        printf("True");
    else
        printf("False");

    getch();
}

A. True
B. False
C. Complier error
Đáp án

A

 

23. What is output of code?

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

void main()
{
    if(1,0)
        printf("True");
    else
        printf("False");

    getch();
}

A. True
B. False
C. Complier error
Đáp án

B

 

24. What is output of code?

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

void main()
{
    int i, j, *ptr, *ptr1;
    i = 10;
    j = 10;
    ptr = &i;
    ptr1 = &j;

    if(ptr == ptr1)
        printf("True");
    else
        printf("False");

    getch();
}

A. True
B. False
C. Complier error
Đáp án

B

 

25. What is output of code?

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

void main()
{
    int i;
    i = 2;
DES:
    printf("%d", i);
    i = i + 2;
    if(i <= 20)
        goto DES;

    getch();
}

A. 2468101214161820
B. 468101214161820
C. nothing is printed
Đáp án

A

 

26. What is output of code?

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

void main()
{
    int i;
    i = 0;
    if(i = 15, 10, 5)
        printf("C/C++ %d", i);
    else
        printf("Java %d", i);

    getch();
}

A. C/C++ 15
B. Java 15
C. Java 5
D. Complier error
Đáp án

A

 

27. What is output of code?

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

void main()
{
    int a = 80;
    if(a++ > 80)
        printf("C/C++ %d", a);
    else
        printf("Java %d", a);

    getch();
}

A. C/C++ 80
B. C/C++ 81
C. Java 80
D. Java 81
Đáp án

B

 

28. What is output of code?

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

void main()
{
    int a;
    a = 1;
    while(a <= 1)
        if(a%2)
            printf("%d ", a++);
        else
            printf("%d ", ++a);
    printf("%d ", a+10);

    getch();
}

A. 1 12
B. 2 12
C. 2 11
Đáp án

A

1 Comment on If/else , switch( ) case , goto

Leave a Reply