Getting to Know C Language--Training Questions

Through the previous study, we have a certain understanding of the basic knowledge of C language, so let's practice and consolidate the knowledge

1. Multiple choice questions

1. Which of the following is not a built-in data type in C language ( )

A.char

B.double

C.struct Stu

D.short

2. The scope of local variables is ( )

A. Inside the main function

B. The whole program

Before C.main function

D. Local scope where local variables are located

3. The end of the string is ( )

A. is '0'

B. is EOF

C. is '\0'

D. is a space

4. Which of the following is not an escape character ( )

A.'\n'

B.'\060'

C.'\q'

D.'\b'

5. The wrong description about the array is ( )

A. An array is a collection of elements of the same type

B. The subscript of the array starts from 1

C. The subscript of the array starts from 0

D. If the array is initialized, the size of the array may not be specified

6. Which of the following arrays is incorrectly created in C language ( )

A.int arr[10] = {0}

B.int n = 10; int arr[n] ={0}

C.int arr[] = {1,2,3,4,5,6,7,8,9,0}

D.char ch[10] = "hello bit"

7. The correct statement about C language keywords is ( )

A. Keywords can be created by yourself

B. Keywords cannot be created by themselves

C. Keywords can be used as variable names

D.typedef is not a keyword

8. The statement about static is incorrect ( )

A.static can modify local variables

B.static can modify global variables

C.static modified variables cannot be changed

D.static can modify functions

9. The result of the following code is ( )

#include<stdio.h>#include<string.h>intmain()
{
    char arr[] = {'b', 'i', 't'};
    printf("%d\n", strlen(arr));
    return0;
}

A.3

B.4

C. Random value

D.5

10. The output of the following code is ( )

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

A. There is a problem with the program and cannot be compiled

B. Output 1

C. output 10

D. output 0

11. The result of the following program is ( )

#include <stdio.h>
#include <string.h>
int main()
{
    printf("%d\n", strlen("c:\test\121"));
    return 0;
}

A.7

B.8

C.9

D.10

12. What is the result of the following code ( )

#include <stdio.h>
int sum(int a)
{
    int c = 0;
    static int b = 3;
    c += 1;
    b += 2;
    return (a + b + c);
}
int main()
{
    int i;
    int a = 2;
    for (i = 0; i < 5; i++) 
    { 
        printf("%d,", sum(a)); 
    } 
} 

A. 6,8,10,12,14

B.8,10,12,14,16

C.10,12,14,16,18

D.12,14,16,18,20

13. The correct statement about the pointer is ( )

A.sizeof(char*) must be 1

B. A pointer variable is a variable used to store addresses

C. The size of the pointer variable is 4 bytes

D. Pointers are not variables

2. Programming questions

Note '=' and '=='

1. The program realizes the input of a number, if the number is less than 0, it will output -1, if it is greater than 0, it will output 1, if it is equal to 0, it will output 0

Example: Enter: -2

Output: -1

Input: 2

output: 1

Input: 0

Output: 0

2. Write a program to print a small plane on the screen

For example:

3. Write a program to realize the IQ test, if the IQ is greater than 140, print Genius

Example: Enter: 158

Output: Genius

4. Write a program to calculate the result of the expression (-8+22)*a-10+c/2 when a=40 and c=212

5. Write a program to compare the size of two numbers

Example: Enter: 5 8

Output: 8>5

6. Write a program to calculate division with remainder

Example: Enter: 5 2

Output: 2 1

7. Input a number, if the number is divisible by 5, then output YES, if not, output NO

Example: Enter: 25

Output: YES

8. Judge the parity of an integer, if it is even, output Even, if it is odd, output Odd

Example: Enter: 4

Output: Even

9. Enter 5 numbers, and calculate their average grades, the result retains one decimal place

Example: Enter: 56 89 78 63 98

Output: 76.8

Note: There are decimals here, pay attention when setting variables

10. Use functions to compare the size of two numbers

11. Conversion of percentile grades: The conversion rules are: 90-100 is 'A', 80-89 is 'B', 70-79 is 'C', 60-69 is 'D', and below 59 is 'E'. If the input grade is not between 0-100, output 'F'

For example: Enter: 91

Output: A

12. Convert the following ASCII codes to corresponding characters and output them (one line outputs the characters corresponding to all ASCII codes given in the conversion title, without separating them by spaces)

73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33

13. Input 10 integers and count the number of output positive and negative numbers respectively

Example: Enter: -1 2 3 -6 7 8 -1 6 8 10

Output: positive:7

negative:3

3. Analysis

Multiple choice questions:

1.C

The struct keyword is a user-defined structure type, which does not belong to the built-in type of C language

2.D

In C language, variables are divided into local variables and global variables.

Local variables: Generally, variables defined in functions are called local variables, which can only be used inside the function.

Global variables: Variables defined in the global scope, that is, variables outside functions, are called global variables. The life cycle of global variables is born when the program starts, and dies when the program ends, and can be used in any function.

Note: Global variables are convenient to use, but in order to prevent conflicts and security, try to avoid defining global variables.

A: The scope of the local variables defined inside the main function is in the main function, but the local variables in other functions are not, so option A is wrong.

B: The local variable scope is inside the function, and the global variable is the entire program, so option B is wrong

C: Before the main function, there are some things that the system does, so it is not correct

D: Correct, that is, in the function body

3.C

C language regulations: use '\0' as the end mark of a valid string

4.C

A: '\n' escape character, representing a newline

B: '\060' escape character, 060 octal data, 48 in decimal, means '0' with ASCII code 48

C: '\q' is nothing

D: '\b' escape character, means backspace

5.B

Array subscripts start at 0

It should be noted that D: int a[] = {1,2,3}, the array can be initialized to determine the size

6.B

The size of the array must be constant

7.B

C language keywords: C language identifiers defined by C language with specific meanings and specially used for special purposes, also known as reserved words

A: Error, keywords are defined by the language itself

B: correct

C: error, keyword has special meaning and cannot be used as a variable name

D: Error, typedef is a keyword used to alias a type

8.C

static can modify local variables, global variables, functions

Variables modified by static can be changed

9.C

strlen is used to obtain the effective length of the string, the end mark '\0' is not included.

The rules acquired by strlen are very simple: check from front to back, until the detection is terminated when '\0' is encountered.

The arr in the upper body is a character array, not a valid string, because there is no '\0' behind it, so when strlen solves the problem, after detecting the valid characters, it will continue to detect backwards until it encounters ' \0' is terminated, so the answer is not sure, it depends on the position of the first '\0' immediately after 't'

10.B

When the names of local variables and global variables conflict, the local variables will be compiled first at compile time

11.A

strlen: Get the effective length of the string, excluding '\0'

"c:\test\121": In this string, \t is a transition character, tabulate horizontally, and jump to the position of the next tab; and \121 represents a character, which means that 121 is regarded as an octal array, Converted to 81 in decimal, the operation is the character of ASCII code value, that is: the character 'Q', so the above string is actually: "c: esty", only 7 valid characters

12.B

static modifies local variables, which will not disappear with the end of the function, and are only initialized when the function is called for the first time. When the function is called later, the value of the variable before the end of the last time is used.

First cycle: a=2 b=5 c=1 a+b+c=8

The second cycle: a=2 b=7 c=1 a+b+c=10

The second cycle: a=2 b=9 c=1 a+b+c=12

The second cycle: a=2 b=11 c=1 a+b+c=14

The second cycle: a=2 b=13 c=1 a+b+c=16

13.B

A: Error, the pointer is a compound data type, and the content of the pointer variable is an address, so a pointer can represent the entire address collection of the system,

Therefore, when compiling the code according to 32-bit, the pointer occupies 4 bytes; when compiling the code according to 64-bit, the pointer occupies 8 bytes (note: not 64-bit system must occupy 8 bytes, the key is to compile according to 64-bit mode)

B: correct

C: error, refer to option A for explanation

D: Error, the description of this article is vague. The pointer can be considered as a data type, or as a defined pointer variable.

Programming questions:

Not only one method in parsing, just for reference

1.

#include<stdio.h>
int main()
{
    int x = 0;
    int y = 0;
    scanf("%d", &x);
    if (x > 0)
        y = 1;
    else if (x == 0)
        y = 0;
    else
        y = -1;
    printf("%d\n", y);
    return 0;
}

2.

#include<stdio.h>
int main()
{
    printf("   * *   \n");
    printf("   * *   \n");
    printf("*********\n");
    printf("*********\n");
    printf("*********\n");
    printf("  *   *  \n");
    printf("  *   *  \n");
    return 0;
}

3.

#include<stdio.h>
int main()
{
    int iq = 0;
    scanf("%d", &iq);
    if (iq > 140)
        printf("Genius\n");
    
    return 0;
}

4.

#include<stdio.h>
int main()
{
    int a = 40;
    int c = 212;
    int sum = (-8 + 22) * a - 10 + c / 2;
    printf("%d", sum);

    return 0;
}

5.

#include<stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    scanf("%d %d", &a, &b);
    if (a > b)
    {
        printf("%d>%d\n", a, b);
    }
    else if(a == b)
    {
        printf("%d=%d\n", a, b);
    }
    else
    {
        printf("%d<%d\n", a, b);
    }
    return 0;
}

6.

Mainly understand the two operators '/' and '%'

#include <stdio.h>

int main()
{
    int a = 0;
    int b = 0;
    scanf("%d %d", &a, &b);
    int m = a / b;
    int n = a % b;
    printf("%d %d\n", m, n);

    return 0;
}

7.

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d", &n);
    if (n % 5 == 0)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

8.

#include <stdio.h>
int main()
{
    int x = 0;
    scanf("%d", &x);
    if (x % 5 == 0)
    {
        printf("Even\n");
    }
    else
        printf("Odd\n");
    return 0;
}

9.

#include<stdio.h>
int main()
{
    int x = 0;
    int y = 0;
    int z = 0;
    int c = 0;
    int e = 0;
    scanf("%d %d %d %d %d", &x, &y, &z, &c, &e);
    int sum = x + y + z + c + e;
    printf("%.1f\n", sum / 5.0);
    return 0;
}
//This method is more troublesome, and there will be an easier method after learning to play with the loop

10.

#include<stdio.h>
int Max(int x, int y)
{
    if (x > y)
        return x;
    else
        return y;
}
int main()
{
    int x = 0;
    int y = 0;
    int max = 0;
    scanf("%d %d", &x, &y);
    max=Max(x, y);
    printf("max=%d\n", max);
    return 0;
}

11.

#include <stdio.h>
int main()
{
    int score = 0;
    scanf("%d", &score);
    if (score < 0 || score>100)
        printf("F\n");
    else if (score >= 90 && score <= 100)
        printf("A\n");
    else if (score >= 80 && score <= 89)
        printf("B\n");
    else if (score >= 70 && score <= 79)
        printf("C\n");
    else if (score >= 60 && score <= 69)
        printf("D\n");
    else
        printf("E\n");
    return 0;
}

12.

#include<stdio.h>
int main()
{
    char arr[] = { 73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    int n = 0;
    while (n < sz)
    {
        printf("%c", arr[n]);
        n++;
        if (n > sz)
            break;
    }
    return 0;
}

13.

#include <stdio.h>
int main()
{
    int i = 0;
    int positive = 0;
    int negative = 0;
    int tmp = 0;
    while (i < 10)
    {
        scanf("%d", &tmp);    //Using a loop to implement multiple inputs
        i++;
        if (tmp >= 0)
            positive++;
        else
            negative++;
        if (i > 10)
            break;
    }
    printf("positive:%d\n", positive);
    printf("negative:%d\n", negative);
    return 0;
}

The practice questions are shared here, and there will be more sharing in the future. Thank you, if you have any shortcomings or questions, please private message or post them in the comment area. Thank you for your support💯

Tags: C

Posted by c815902 on Tue, 24 Jan 2023 15:27:46 +0530