Collection of must-do questions for overtaking on curves 2 (C language multiple choice questions)

Foreword:

If you want to learn programming well, you need to learn more questions. We not only need to do more questions, but also do well! For this reason, I started a series of must-do questions for overtaking on corners, each with about 10 questions. This is the second multiple-choice article. This series will be updated irregularly, and a series of programming questions will be opened in the future . Stay tuned!


1. When the context and header files are normal, the output of the following code is ( ) (Note: print has been declared)
int main ()
{
char str [] = "Geneius" ;
print ( str );
return 0 ;
}
print ( char * s )
{
if ( * s )
{
print ( ++ s );
printf ( "%c" , * s );
}
}

A: suiene         B: neius       C: run-time error        D: suieneG 

Answer analysis:
Correct answer: A
The code implements the function of recursively printing strings in reverse order, but ++s changes the value of s and cannot return to the position of 'G' , so 'G' is not printed

2. If there is a definition: int a[2][3]; , the correct reference to the a array element in the following options is ( )
A: a[2][0]            B: a[2][3]           C: a[0][3]          D: a[1>2][1]
Answer analysis:
Correct answer: D
This question mainly considers the case of array out-of-bounds access. The rows and columns of a two-dimensional array start from 0. For the a array, the maximum row subscript is 1 , and the maximum column subscript is 2. In the D option, 1>2 expresses The value of the formula is 0 , which is correct. Other options may have out-of-bounds row and column, A means out-of-range row, B means out-of-range both row and column, and C means out-of-range column.

3. Which of the following options can correctly describe sizeof(double) ( )
A: an integer expression      B: a double expression      C: an illegal expression        D: a function call

Answer analysis:
Correct answer: A
sizeof is an operator in C language, not a function call. Simply put, its function is to return the number of memory bytes occupied by an object or type. The result is an unsigned integer, so it can be regarded as an integer expression . So choose A

4. The two-dimensional array X is stored in row order, where each element occupies 1 storage unit. If the storage address of X[4][4] is Oxf8b82140 , and the storage address of X[9][9] is Oxf8b8221c , then the storage address of X[7][7] is ( )

A: Oxf8b821c4            B: Oxf8b821a6           C: Oxf8b82198              D: Oxf8b821c0

 

Answer analysis:
Correct answer: A
Suppose there are n elements in each row : that is, X[4][4] only needs to add 5 to get X[4][9], and the obtained position is 5 rows different from X[9][9], then The address of the x[9][9] element - the address of the x[4][4] element = 0x21c-0x140=5n+5 (21c and 140 are the hexadecimal numbers of the last three digits of the address) , where n is 43 , assuming that the address of x[7][7] is z, the address of x[7][7] element - the address of x[4][4] element = z-0x140 = 3n+3 , z = 3n+3+ 140 = 3*43+3+0x140 = 0x84+0x140 = 0x1c4, look at the mantissa of the address, choose A

5. Find the return value of the function, pass in -1 , then the function returns ( ) on a 64 -bit machine
int func ( int x )
{
int count = 0 ;
while ( x )
{
count ++ ;
x = x & ( x - 1 ); // AND operation
}
return count ;
}
A: Infinite loop        B: 64        C: 32        D: 16
Answer analysis:
Correct answer: C
x=x&(x-1) This expression will remove the rightmost 1 in the binary system of x when it is executed once . Before x becomes 0 , the expression can be executed several times, and a few 1s will be removed , so this The code implements the function of finding the number of 1s in the two’s complement of a signed integer . We know that the complement of -1 is all 1s , and the int type has 4 bytes and 32 bits . Choose C

6. Read the code selection result ( )

int count = 0 ;
int x = - 1 ;
while ( x )
{
count ++ ;
x = x >> 1 ;
}
printf ( "%d" , count );
A: 1           B: 2           C: 32           D: Infinite loop, no result
Additional knowledge points:
1. Shift left << : The leftmost bit is discarded, and 0 is added to the rightmost.
2. Shift right >> : The rightmost bit is no longer needed, and the leftmost sign bit is complemented (positive numbers are complemented with 0, negative numbers are complemented with 1).
3. Unsigned right shift >>>: The rightmost bit is not needed, and the leftmost bit is filled with 0. (c language does not have it, java has it)
Answer analysis:
Correct answer: D
The key to this question is that the high bit of the signed right shift operation is the complement sign bit, and the sign bit of the negative number is 1 , so x will never become 0 , which is an endless loop

7. Which of the following assignment statements is wrong ( )

A: a = (b = (c = 2 , d = 3))       B: i++        C: a/b = 2          D: a = a < a + 1
Answer analysis:
Correct answer: C
In option C , a/b is an expression , and the result of the expression calculation is a value that cannot be an lvalue

8. The output of the following program after running is ()

int main ()
{
int a = 1 , b = 2 , m = 0 , n = 0 , k ;
k = ( n = b < a ) && ( m = a );
printf ( "%d,%d\n" , k , m );
return 0 ;
}
A: 0,0              B: 0,1            C: 1,0               D: 1,1
Answer analysis:
Correct answer: A
k=(n=b<a)&&(m=a); The execution order of this part is as follows: first execute the n=b<a part, where the relational operator has a higher priority than the assignment operator, so b< is calculated first a , get 0 , the result of the n=0 assignment operation will be used as the result of the expression in the brackets, that is, (n=b<a)&&(m=a) is converted into (0)&&(m=a) , before the && operation If the expression is false, the following parentheses (m=a) will not operate, and the value of m is still 0. Finally, the result of && is 0 , that is, k=0

9. The output of the following function is ( )

int main ()
{
int k = 1 ^ ( 1 << 31 >> 31 );
printf ( "%d\n" , k );
}

A: 0           B: -1           C: -2        D: 1  

Answer analysis:
Correct answer: C
(1<<31); Shift 31 bits to the left and fill 0 on the right to get 0x80000000 , that is, the sign bit is 1 , and the others are 0 , that is -2147483648 int k =1^(1<<31>>31); Note that when shifting to the right, the sign bit remains at 1 , and after shifting to the right, it is filled with 1 , and the result is 0xFFFFFFFF , which is -1 , 0x00000001^0xFFFFFFFF, which is 0xFFFFFFFE(-2)

10. The output of the following code is ( )

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

A: 1               B:               4 C: 2             D: 8  

Answer analysis:
Correct answer: A
The operation of general expressions is executed at runtime, and sizeof is an operator executed at the compilation stage. Any operation in it is not executed , only the type of the expression result is guessed to find its size, so before and after i The value of is unchanged.

The above is my personal sharing, if you have any questions, welcome to discuss! !

I've seen this, why don't you pay attention and give a free like 

 

Guess you like

Origin blog.csdn.net/WHabc2002/article/details/132478495