West Post Linux interest group 2019 interview questions

1. How many "=" are output?

int main()
{
    for(unsigned int i=3;i>=0;i--)
    putchar('=');
}

Output countless' = '.

The type defined in the loop condition is unsigned int, which is an unsigned integer and has no negative form. That is, the for loop always holds.

2. How do the following three methods of exchanging integers realize the exchange?  

/* (1) */ int c = a; a = b; b = c;
/* (2) */ a = a - b; b = b + a; a = b - a; 
/* (3) */ a ^= b   ; b ^= a    ; a ^= b; 

(1) By exchanging values with intermediate variables

(2) The difference between a and b is used for calculation to realize exchange

(3) Use XOR operation to record the corresponding bits of a and b as 0 if they are the same and 1 if they are different, and the value is passed to a. Then b and a are XORed to realize the transformation from b to A. finally, a and b are XOR to realize the transformation from a to b.

3. There is a function f shown in the following code segment. What output results will be generated when we execute this function? If the function is executed multiple times in the same program, are the output results consistent?  

void f() { 
    static int a = 0; 
    int b = 0; 
    printf("%d, %d\n", ++a, ++b); 
}

Output: 1,     one

Multiple times, the value of b remains unchanged, while the value of a increases all the time.

  The static modifier is used to define the static local variable. Its life cycle is the running time of the whole application, and it will be initialized only once.

4. What is the output of the following program segment?

int main(void) { 
    printf("%d\n", printf("Xiyou Linux Group2%d", printf(""))); 
}

Xiyou Linux Group2019

  The nesting order of printf is: from inside to outside, from left to right. And the return value of this function is the number of characters printed, so the output of% d in the inner printf function is 0 and the output of% d in the outer printf function is 19.

5. What will be output by executing the following code segment?

int main(int argc, char *argv[]) { 
    char ch = 255; 
    int d = a + 1; 
    printf("%d %d", ch, d); 
}

Because a is not declared, the compiler will report an error. If a is ignored, the value of output ch is - 1.  

char occupies one byte, and its range is - 128 ~ 127. 255 is represented in binary as 1111   1111. Because it is a signed type, the first 1 is treated as a sign bit and the output is - 1

6. What kind of output will be produced by executing the following code segment?

int main(int argc, char *argv[]) { 
    char x = -2, y = 3;
    char t = (++x) | (y++); 
    printf("x = %d, y = %d, t = %d\n", x, y, t); 
    t = (++x) || (y++); 
    printf("x = %d, y = %d, t = %d\n", x, y, t); 
}

x = -1, y = 4, t = -1
x = 0, y = 5, t = 1

  "|" is an or operation, and + + i is self addition first and then used. i + + is the opposite. so   

t=    1111 1111   |   0000 0011   =  1111 1111    

  "|" is "or". (a) | (b) if a is judged to be true, it will not continue to judge whether B is true or false, and return to true (i.e. 1). In question   ++ x=0   Then continue to judge y + +, and make y add itself after judgment.

  7. What is the output of the following code segment?

#define X a+b
int main(int argc, char *argv[]) { 
    int a = 1, b = 1; 
    printf("%d\n", X*X);
}

 3

The work of C's macro preprocessor is just a simple text search and replacement, that is, in the code   X*X   Replace with   a+b*a+b   , Instead of (a + b) * (a + b). To prevent such errors, macros can be defined as: #define X (a + b)

8. Please explain the effect of each sentence in the following code snippet.  

int val = 2018; 
int *pi = 2019; 
pi = &val; 
*pi = 0; 

  First sentence: declare a variable Val of type int and assign 2018 to val.

The second sentence: declare a pointer to int type and make the pointer point to 2019 (0x07E3). (this is risky)

The third sentence: assign the address of val to the pointer pi.

The fourth sentence: change the data in the address referred to by pi to 0.

9. Execute the following program segments and enter "Xiyou Linux" (without quotation marks), what is the output result of the program?

int main(int argc, char *argv[]) { 
     char *p = (char *)malloc(sizeof(char) * 20),
         *q = p; 
     scanf("%s %s", p, q); 
     printf("%s %s\n", p, q); 
}

Linux Linux

  p points to the address of the allocated space and declares the pointer variable q to point to the space indicated by p. That is, p and q point to the same space.

Scanf ("% s% s", p, q) changes the value of * p after inputting Xiyou (ending the first% s with a space), and changes the value of * q after inputting Linux (enter) (actually changes the data in the same address). The input Linux overwrites the previously input Xiyou, so it is output as two Linux.

10. Execute the following program segments. Are the output results consistent each time?

int main(int argc, char *argv[]) { 
      int a[4] = { 2, 0, 1, 9 }; 
      printf("%p, %p\n", a, &a); 
      printf("%p, %p\n", a + 1, &a + 1); 
}

0x7ffd2ec76b00, 0x7ffd2ec76b00
0x7ffd2ec76b04, 0x7ffd2ec76b10

(the first time for this machine)

0x7ffe0c0da930, 0x7ffe0c0da930
0x7ffe0c0da934, 0x7ffe0c0da940

(second time)

The output results of each execution are inconsistent

Since the memory allocated to a is dynamic every time the program is executed, the address of a is always changing.

11. Fibonacci sequence is such a sequence: 1,1,2,3,5,8,13. In this series, the first and second terms are 1, and the other terms are the sum of the first two terms. The X term of this series can be expressed as the following function. Please write a program according to the description so that after inputting x, it can output the X item of Fibonacci sequence (where x < 30). When you are finished, you can try to solve this problem using recursion.  

#include<stdio.h>
int f(int n)
{
	if (n <= 2)
		return 1;//When n=1,2, the return value is 1, that is, f(0)=f(1)=f(2)=1
	else
		return f(n - 1) + f(n - 2);//When n > 2, return the sum of the first two numbers
}
int main()
{ 
	int n = 0;
	scanf("%d", &n);
	printf("%d\n", f(n));
	return 0;
}

12. The following code segment is a simple implementation of a sorting algorithm. Do you know what it is? Please explain its principle and try to improve it.

int main(int argc, char *argv[])
{
    int nums[6] = {6, 3, 2, 4, 5, 1};
    for (int i = 0; i < 6; i++)
    {
        for (int j = i; j < 6; j++)
        {
            if (nums[i] > nums[j])
            {
                int c = nums[i];
                nums[i] = nums[j];
                nums[j] = c;
            }
        }
    }
}

Select sort.  

Principle: compare with the following numbers one by one. If the number is greater than another number, it will be exchanged. One cycle is realized to put the minimum value in the first place. The second cycle is realized to put the second smallest number in the second place, and so on.

The optimization is as follows:

int main(){
    int len=sizeof(a)/sizeof(int);
    int t;
    /*Initialize left and right element Subscripts*/
    int left = 0;
    int right = len - 1;
    while (left < right){
    	/*Initializes the subscripts of the minimum and maximum elements*/
        int min = left;
        int max = right;
        for (int i = left; i <= right; i++){
        	/*Mark the subscripts min and max corresponding to the elements of the maximum and minimum values in each comparison*/
            if (a[i] < a[min])
                min = i;
            if (a[i] > a[max])
                max = i;
        }
        /*The maximum value is placed at the rightmost end*/
        int t = a[max];
        a[max] = a[right];
        a[right] = t;
        /*Here is the position where the maximum value is arranged first, so the case where the minimum value (a[min]) is at the maximum position (right) must be considered*/
        if (min == right)
            min = max;
        /*The minimum value is placed at the leftmost end*/
        t = a[min];
        a[min] = a[left];
        a[left] = t;
        /*In each traversal, the total number of elements is reduced by 2, the left and right ends are reduced by 1, and the left and right indexes are moved inward by 1 respectively*/
        left++;
        right--;
    }
}

13. Please briefly describe the concepts of two byte orders (large end and small end). What byte order is your machine? Try to write a C language program to verify it.

Large end storage mode: it means that the low byte sequence of data is saved in the high address of memory, while the high byte sequence of data is saved in the low address of memory (the high byte sequence comes first)
Small end storage mode: it means that the low order byte sequence of data is saved in the low address of memory, while the high order byte sequence of data is saved in the high address of memory (the low order is first)

#include <stdio.h>
int Check()
{
    union Un
    {
        char c;
        int i;
    }un;
    un.i = 1;
    return un.c;
}

int main()
{
    int c=Check();   
    if (1 == c)
    {
        printf("The current mode is small end storage\n");
    }
    else
    {
        printf("The current mode is big end storage\n");
    }
    return 0;
}
    #include<stdio.h>
    int Check()
    {
        int i = 1;
        return (*((char*)&i));
        //Return 1 for small end
        //Return 0 for big end
    }
    int main()
    {
        int c = Check();
        if (1 == c)
        {
            printf("Small end\n");
        }
        else
        {
            printf("Small end\n");
        }
        return 0;
    }

14. The following is a partial output (with deletion) of the Linux command ls executed on a machine. With reference to this output, what can you say about this command and other Linux related knowledge?  

[root@xiyoulinux /]# ls -al 
total 36  
drwxr-xr-x  17 root root  4096 Sep 21 23:45 .  
drwxr-xr-x  17 root root  4096 Sep 21 23:45 ..  
lrwxrwxrwx   1 root root     7 Aug 21 22:21 bin -> usr/bin  
drwxr-xr-x   4 root root  2048 Jan  1  1970 boot  
drwxr-xr-x  21 root root  3580 Nov 21 21:16 dev  
drwxr-xr-x  83 root root  4096 Nov 21 22:12 etc  
drwxr-xr-x   4 root root  4096 Sep 22 00:07 home  
drwxr-xr-x   2 root root  4096 Aug 21 22:21 mnt  
drwxr-x---   9 root root  4096 Nov 19 19:15 root  
dr-xr-xr-x  13 root root     0 Nov 21 21:15 sys  
drwxrwxrwt  10 root root   380 Nov 21 22:30 tmp  
drwxr-xr-x   9 root root  4096 Nov 21 22:12 usr  

This command displays the contents of the specified directory (including the files and their subdirectories in the current directory).

-al refers to checking whether the device has read-write permission.

The first column indicates the file type and permissions

The second column indicates the number of links

The third column indicates the owner of the file

The fourth column indicates the group to which the file belongs

The fifth column represents the file size (bytes)

The last three columns are the last modification time of the file

The last column is the file name

Tags: C Linux

Posted on Tue, 30 Nov 2021 10:10:59 -0500 by gt500pwr