Table of contents
Hello everyone, I am Ji Ning.
Starting today, the blogger will update some classic C language written test questions every day, and it will last for about 20 days. The types of questions are 5 multiple-choice questions plus 2 programming questions. I hope to make progress together with everyone.
first question
1. Read the program, the correct output of the following program is ()
#include<stdio.h>
int x = 5, y = 7;
void swap()
{
int z;
z = x;
x = y;
y = z;
}
int main()
{
int x = 3, y = 8;
swap();
printf("%d,%d\n",x, y);
return 0;
}
A: 5,7 B: 7,5 C: 3,8 D: 8,3
When the swap function is called, global variables are used. The variables defined in the main function are only valid in the main function, because the main function is also a function, and it is in parallel with other functions; here, the principle of local priority is considered in the output statement . So the answer is C
Knowledge points:
Detailed explanation of C language functions http://t.csdn.cn/wkkmF
second question
2. The following incorrect definition statement is ( )
A: double x[5] = {2.0, 4.0, 6.0, 8.0, 10.0} ;
B: char c2[] = {'\x10', '\xa', ' \8'} ;
C: char c1[] = {'1','2','3','4','5'} ; D:
int y[5+3]= {0, 1, 3 , 5, 7, 9} ;
Option B of this question examines the application of escape characters
\ddd ddd represents 1 to 3 octal numbers such as: \130 is escaped to character X
\xhh hh represents 1 to 2 hexadecimal numbers such as \x30 escaped to the character '0'
There is no x after \ in the third character of option B, so it means an octal number, but the range of an octal number is 0~7, so B is wrong, choose B
third question
3. The test.c file includes the following statement. Among the four variables defined in the file, the variable of pointer type is [multiple choice] ( )
#define INT_PTR int*
typedef int* int_ptr;
INT_PTR a, b;
int_ptr c, d;
A: a B: b C: c D: d
This question examines the replacement of #define definitions and the renaming of types.
INT_PTR is replaced by int* in the preprocessing stage. According to the grammar rules, its * will only be used by a, and the type of b becomes int type; while typedef redefines the type, and the type of cd behind it is Both are of type int*.
So the answer is A, C, D
Knowledge points:
C environment and preprocessing http://t.csdn.cn/6fP4a
fourth question
4. Programming question: print the n digits from 1 to the largest
topic description
Input the number n, and print out the decimal numbers from 1 to the largest n in order. For example, if you input 3, it will print out 1, 2, 3 up to the maximum 3-digit number 999.
1. Return a list of integers instead of printing
2. n is a positive integer, 0 < n <= 5
Knowledge points:
Example of C language for loop http://t.csdn.cn/0xvjx :
enter:
1
return value:
[1,2,3,4,5,6,7,8,9]
This question uses the core code mode on Niuke.com
//* @param n int整型 最大位数
//* @return int整型一维数组
//* @return int* returnSize 返回数组行数
int* printNumbers(int n, int* returnSize ) {
int Max=0;
while(n--)
Max=Max*10+9;
int *arr=(int*)malloc(Max*sizeof(int));
for(int i=0;i<Max;i++)
{
arr[i]=i+1;
}
*returnSize=Max;
return arr;
}
Among them, returnSize is the number of printed numbers, and the address of the first element of the array needs to be returned.
fifth question
5. Programming question: Calculate date to day conversion
Description: According to the date entered, calculate the day of the year, and ensure that the year has 4 digits and the date is legal. Time complexity: O(n), space complexity: O(1).
Input description: Enter one line, each line is separated by spaces, which are year, month, day
Output description: The output is the day of the year
Example 1:
Input: 2012 12 31 Output: 366
Example 2:
Input: 1982 3 4 Output: 6
In the detailed explanation of C language operators, there is a method of how to calculate the leap year in the logical operator part
Niuke.com adopts ACM mode in this question
#include <stdio.h>
int main() {
int year=0,month=0,day=0;
scanf("%d %d %d",&year,&month,&day);
int arr1[11]={31,29,31,30,31,30,31,31,30,31,30};
int arr2[11]={31,28,31,30,31,30,31,31,30,31,30};
int days=0;
if((year%4==0&&year%100!=0)||(year%400==0))
{
for(int i=0;i<month-1;i++)
{
days+=arr1[i];
}
}
else
{
for(int i=0;i<month-1;i++)
{
days+=arr2[i];
}
}
days+=day;
printf("%d",days);
return 0;
}
The idea is to open up two array spaces first, then judge whether the year is a leap year, and add the number of days in the month one by one using a loop.