C language written test training [second day]

Article Directory

first question

second question

third question

fourth question 

fifth question

Sixth question

Seventh question


  Hello everyone, I am Ji Ning.

  Today is the second day of C language written test training, let's work together!

first question

1. The output of the following program segment is ( )

#include<stdio.h>
int main()
{
	char s[] = "\\123456\123456\t";
	printf("%d\n", strlen(s));
	return 0;
}

A : 12 B : 13 C : 16 D : none of the above is correct 

  So we are going to use vs, let me show you the effect of this code in vs

  Haha, there are a few characters that have been clearly distinguished by the compiler. But still, an explanation.

\\: Indicates the character '\', preventing it from being escaped.

\t: Indicates a horizontal tab character, which is the Tab key on the computer, in short it also represents a character.

\ddd: \Add 1~3 numbers, these numbers must be in octal, escaped to a character equivalent to ASCII.

So \\ counts as 1 character, 123456 counts as 6 numeric characters, \123 counts as 1 character, 456 counts as 3 numeric characters, \t counts as 1 character, a total of 12 characters, so choose A.

second question

2. If there is the following program, the output after running is ( )

#include <stdio.h>
#define N 2
#define M N + 1
#define NUM (M + 1) * M / 2
int main()
{
	printf("%d\n", NUM);
	return 0;
}

 A :  4   B : 8   C : 9   D : 6

  This question examines #define to define macros. The identifiers defined by define are only replaced. If there are no brackets, it may indeed cause some side effects.

  Replace the values ​​of N and M into the macro as follows

The value obtained is 8, so choose  B.

Knowledge points investigated in this question: C environment and preprocessing

third question

3. The value of f(1) of the following function is ( )

int f(int n)
{
	static int i = 1;
	if (n >= 5)
		return n;
	n = n + i;
	i++;
	return f(n);
}

A : 5   B : 6   C : 7   D : 8

  This question examines function recursion and static definition of local variables.

  When a variable is modified by static, it will not be destroyed after leaving the scope, which means that the value of i enters the function for the first time is 1, and the value of i enters the function for the second time is 2, not every time the value of i is it's 1.

n=1 Enter the function and start recursion

i=1,n=n+i=2

i=2,n=n+i=4

i=3,n=n+i=7

n=7>5 returns n

So choose C for this question .

Function recursion knowledge points: C language function detailed explanation

fourth question 

4. Do the following three pieces of program code have the same effect? ​​( 

int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;

A : (2)=(3) B : (1)=(2) C : Not the same D : All the same 

This question examines const modified pointer variables

const modified pointer

When const is on the left of *, the content pointed to by the pointer cannot be changed.

When const is on the right side of *, the pointer variable cannot change its pointing.

In this question, both (1) and (2) const are on the left side of *, belonging to a type, and both restrict the content pointed to by pointer a to be unchangeable; while const in (3) is on the right side of *, belonging to a type, restricting The pointing of the pointer cannot be changed. So choose B for this question .

fifth question

5. Which of the following statements is correct? ( )

A: For struct X{short s;int i;char c;}, sizeof(X) is equal to sizeof(s) + sizeof(i) + sizeof(c)

B: For a double variable a, you can use a == 0.0 to determine whether it is zero

C: The initialization method char a[14] = "Hello, world!"; has the same effect as char a[14]; a = "Hello, world!";

D: None of the above statements are true

  This question examines a little more knowledge.

  Option A examines the memory alignment content of the structure. If you don’t understand, you can read   the structure memory alignment content in the C language custom type , which is written in great detail.

  Option B examines floating-point related content. In computer storage, floating-point numbers cannot be stored accurately. To judge whether floating-point numbers are equal, it is generally to judge whether the difference between the two numbers is less than a certain minimum value.

  Option C examines concepts related to character pointers and arrays . Its first definition is to store each character of Hello, world into array a; and the second is to assign the address of character 'H' to pointer a, but Hello, world is actually stored in the code in the district.

Statements A, B, and   C are all incorrect, so choose D.

Sixth question

6. Nicoches Theorem

Verify the Nicorches theorem, that is: the cube of any integer m can be written as the sum of m consecutive odd numbers.

For example:

1^3=1

2^3=3+5

3^3=7+9+11

4^3=13+15+17+19

Input a positive integer m (m≤100), write the cube of m as the sum of m consecutive odd numbers and output.

Advanced: Time Complexity: O(m), Space Complexity: O(1) 

Input description: Input an int integer

Output description: Output the decomposed string

Example:
Input: 6 Output: 31+33+35+37+39+41

  This question looks difficult, but it is actually a question of finding the law

Assuming that the cube of m is written as the sum of m consecutive odd numbers, the first output number is:

You only need to output m-1 numbers that add 2 each time and add them together

#include <stdio.h>
int main()
{
    int m=0;
    scanf("%d",&m);
    int i=0;
    int sum=0;
    for(i=0;i<m;i++)
    {
        sum+=i;
    }
    i=sum*2+1;
    printf("%d+",i);
    m-=2;
    while(m--)
    {
        i+=2;
        printf("%d+",i);
    }
    i+=2;
    printf("%d",i);
    return 0;
}

The difficulty of this question is to correctly control the format of the output.

Seventh question

7, arithmetic progression

Arithmetic sequence 2, 5, 8, 11, 14... (starting from 2, 3 is an arithmetic sequence with a tolerance), output the sum of the first n terms of the arithmetic sequence

Input description: Enter a positive integer n.

Output description: Output an added integer.

Example 1 

Input: 2 Output: 7

Example 2

Input: 275 Output: 113575

This question is relatively simple, just give the code to everyone.

#include <stdio.h>
int main()
{
    int n=0;
    scanf("%d",&n);
    int i=0;
    int ret=2;
    int sum=0;
    for(i=0;i<n;i++)
    {
        sum+=ret;
        ret+=3;
    }
    printf("%d",sum);
}

Guess you like

Origin blog.csdn.net/zyb___/article/details/132092668