C Language Exercises – Day 14

01

C Language Exercises - Day 14

If there is an array int a[10];, the correct reference to the elements of the array a is

A) a[10]

B) a[3.5]

C) a(5)

D) a[10-10]

C Language Exercises - Day 14

Answer: D

Explanation:

For option A: Incorrect, the valid indices of the array are 0 to 9, a[10] is out of bounds and will lead to undefined behavior.

For option B: Incorrect, array indices must be of integer type, 3.5 is a floating-point number, C language will implicitly truncate it to 3, but this syntax is not recommended and may trigger compiler warnings.

For option C: Incorrect, a(5) is a function call syntax, while a is an array name and cannot be called as a function.

For option D: Correct, 10-10 evaluates to 0, which means a[0], a valid array element reference.

02

C Language Exercises - Day 14

Read the following program:

#include <stdio.h>

main()

{

int a,b;

for(a=1,b=1;a<=100;a++)

{

if(b>=20) break;

if(b%3==1)

{

b+=3;

continue;

}

b-=5;

}

printf(“%d\n”,a);

}

The output of the above program is

A) 7

B) 8

C) 9

D) 10

C Language Exercises - Day 14

Answer: B

Explanation:

The pattern of change for b:

Each time b % 3 == 1, b increases by 3, and b -= 5 is skipped. Since the initial b = 1, and each time the condition is met, b will become 4, 7, 10, … (always satisfying b % 3 == 1), thus b -= 5 is never executed. Only b += 3 is repeatedly executed until b >= 20.

Loop termination condition:

When a = 8, b = 22, at this point b >= 20, the loop terminates. Therefore, the value of a printed by printf is 8.

03 (Common Mistake)

C Language Exercises - Day 14

Read the following program:

#include <string.h>

main()

{ char p[20]={‘a’,’b’,’c’,’d’},q[]=”abc”,r[]=”abcde”;

strcpy(p+strlen(q),r);

strcat(p,q);

printf(“%d,%d\n”,sizeof(p),strlen(p));

}

The output after running the program is

A) 20,9

B) 9,9

C) 20,11

D) 11,11

C Language Exercises - Day 14

Answer: C

Explanation:

strlen(q) = 3 (the length of “abc”, not including \0). p + 3 points to p[3] (the position of character ‘d’). strcpy will copy the content of r (“abcde\0”) starting from p + 3, overwriting the original ‘d’ and subsequent content. After execution, p becomes: “abcabcde\0…”

strcat(p, q); strcat will append the content of q to the end of p; currently p is “abcabcde\0…”, so it will append “abc” starting from \0 and automatically add \0. After execution, p becomes: ‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘a’, ‘b’, ‘c’, ‘\0’, … (i.e., “abcabcdeabc\0…”).

sizeof(p); p is char[20], fixed size of 20. strlen(p): calculates the length of the string p, which is 11.

04

C Language Exercises - Day 14

Read the following program segment:

main()

{ int a=3;

printf(“%d\n”,(a+=a-=a*a) );

}

The final output is

A) -6

B) 12

C) 0

D) -12

C Language Exercises - Day 14

Answer: D

Explanation:

Calculate a * a:

The initial value of a is 3, so a * a = 3 * 3 = 9.

Calculate a -= a * a:

(i.e., a -= 9)

a -= 9 is equivalent to a = a – 9.

The current a = 3, so a = -6.

At this point, the value of a is updated to -6.

Calculate a += (a -= a * a):

(i.e., a += -6)

a += -6 is equivalent to a = a + (-6).

The current a = -6, so a = -6 + (-6) = -12.

The final value of a is -12.

05

C Language Exercises - Day 14

If there is a declaration: int a,b,c,*d=&c;, the statement that can correctly read three integers from the keyboard and assign them to the variables is

A) scanf(“%d%d%d”,&a,&b,d);

B) scanf(“%d%d%d”,&a,&b,&d);

C) scanf(“%d%d%d”,a,b,d);

D) scanf(“%d%d%d”,a,b,*d);

C Language Exercises - Day 14

Answer: A

Explanation:

For ordinary variables (like a, b, c), the address operator & is needed to get their address, such as &a;

For pointer variables (like d), it stores the address itself, so it can be used directly without &;

06

C Language Exercises - Day 14

The function of the following program is: after inputting data for r, calculate the area s of a circle with radius r, but the program has a compilation error.

main()

{

int r; float s;

scanf(“%d”,&r);

s=π*r*r;

printf(“s=%f\n”,s);

}

The reason for the error in the program is

A) Incorrect position of comment statement

B) The variable r for storing the radius of the circle should not be defined as an integer

C) The format specifier in the output statement is illegal

D) The assignment statement for calculating the area of the circle uses an illegal variable

C Language Exercises - Day 14

Answer: D

Explanation:

In C language, Greek letter π cannot be used directly; it needs to be represented and used correctly.

Directly using the approximate value of π:

The most direct method is to use the approximate value of π (like 3.14159)

Using macro definition:

Define π as a macro constant using #define

For example: #define PI 3.1415926

float s=PI*r*r;

Using M_PI from math.h:

If the system supports C99 standard or POSIX standard, M_PI from <math.h> can be called

Using const constant variable:

C99 supports defining constant variables with const

For example:

const double PI=3.1415926;

float s=PI*r*r;

07

C Language Exercises - Day 14

Read the following program segment:

func(int a, int b)

{ static int m=0,i=2;

i+=m+1;

m=i+a+b;

return(m); }

main()

{ int k=4,m=1,p;

p=func(k,m);printf(“%d,”,p);

p=func(k,m);printf(“%d\n”,p);

}

The final output of this program segment is

A) 8,15

B) 8,16

C) 8,17

D) 8,8

C Language Exercises - Day 14

Answer: C

Explanation:

The persistence of static variables:static int m, i retains values between function calls and will not be re-initialized to 0 and 2 each time.

The independence of local variables:m in main and static variable m in func are two different variables.

Order of calculation:Each time the function is called, i is updated first, then m is calculated.

08

C Language Exercises - Day 14

Given the definition: int a, *pa=&a;, the scanf statement that can correctly read data for variable a is

A) scanf(“%d”,pa);

B) scanf(“%d”,a);

C) scanf(“%d”,&pa);

D) scanf(“%d”,*pa);

C Language Exercises - Day 14

Answer: A

Explanation:

For ordinary variables (like a), the address operator & is needed to get their address (i.e., &a).

For pointer variables (like pa), it stores the address itself, so it can be used directly (no need to add &).

09

C Language Exercises - Day 14

Read the following program segment:

main()

{ int a=0,i;

for(i=1;i<5;i++)

{ switch(i)

{ case 0:

case 3:a+=2;

case 1:

case 2:a+=3;

default:a+=5;

}

}

printf(“%d\n”,a);

}

The final output is

A) 31

B) 13

C) 10

D) 20

C Language Exercises - Day 14

Answer: A

Explanation:

Initial state:

a = 0

for loop: i from 1 to 4 (i < 5)

Behavior of switch statement:

switch(i) jumps to the corresponding case based on the value of i. There are no break statements, so it will fall through and execute all subsequent case and default code.

10 (Common Mistake)

C Language Exercises - Day 14

The function of the following function is

int funl(char * x)

{ char * y=x;

while(*y++);

return(y-x-1);}

A) To find the length of the string

B) To compare the size of two strings

C) To copy string x to string y

D) To concatenate string x to the end of string y

C Language Exercises - Day 14

Answer: A

Explanation:

Initialize pointer:char *y = x;

y and x point to the same starting address of the string.

while (*y++);

*y++ will first dereference y (get the current character), then move the y pointer one position back. The loop condition is *y (the current character), when it encounters the string terminator \0, *y becomes 0, and the loop terminates. Therefore, y will move to the position after the end of the string.

return (y – x – 1);

y – x calculates the offset between pointers y and x (i.e., the total distance y has moved). Since y ultimately stops at the position after \0, it needs to subtract 1 to get the actual length of the string.

Leave a Comment