C Language Exercise Class – Day 10

01

C Language Exercise Class - Day 10

Read the following program:

void swap(char *x, char *y)

{ char t;

t=*x; *x=*y; *y=t;

}

main()

{ char *s1=”abc”, *s2=”123″;

swap(s1,s2); printf(“%s,%s\n”,s1,s2);

}

The output of the program is

A) 123,abc

B) abc,123

C) 1bc,a23

D) 321,cba

C Language Exercise Class - Day 10

Answer: C

Analysis:

The function swap works as follows:

t = *x; saves the character pointed to by x into the temporary variable t; *x=*y assigns the character pointed to by y to the location pointed to by x; *y = t; assigns the value of the temporary variable t to the location pointed to by y.

The swap function receives two character pointers x and y, and swaps the characters they point to; the character ‘a’ pointed to by s1 and the character ‘1’ pointed to by s2 are swapped. After the swap, the string pointed to by s1 becomes “1bc”, and the string pointed to by s2 becomes “a23”.

02

C Language Exercise Class - Day 10

Read the following program:

#include<stdio.h>

sub(int x,int y,int * z)

{*z=y-x;}

main()

{

 int a, b, c;

 sub(10,5,&a);

 sub(7,a,&b);

 sub(a,b,&c);

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

}

The output of the program is:

A) 5,2,3

B) -5,-12,-7

C) -5,-12,-17

D) 5,-2,-7

C Language Exercise Class - Day 10

Answer: B

Analysis:

The function sub works as follows: the sub function receives two integer parameters x and y, and an integer pointer z; it calculates y – x and stores the result in the memory location pointed to by z, i.e., *z = y – x.

The execution process of the main function

The first call sub(10, 5, &a):

x = a = -5, y = b = -12, calculate *z = -12 – (-5) = -7, thus c = -7.

The second call sub(7, a, &b):

x = 7, y = a = -5, calculate *z = -5 – 7 = -12, thus b = -12.

The third call sub(a, b, &c):

x = 10, y = 5, calculate *z = 5 – 10 = -5, thus a = -5.

03 (Common Mistake)

C Language Exercise Class - Day 10

Based on the following function

int fun( int *p)

{ return *p; }

What is the return value of the fun function?

A) An uncertain value

B) An integer

C) The value stored in parameter p

D) The address value of parameter p

C Language Exercise Class - Day 10

Answer: B

Analysis:

The function fun works as follows:

The fun function receives an integer pointer p as a parameter. return *p; means returning the integer value pointed to by pointer p.

For option A: The return value is the specific integer pointed to by p, as long as p is a valid pointer, the value is determined.

For option B: *p dereferences and returns the integer value pointed to by pointer p.

For option C: p stores an address, while the return value is the integer *p pointed to by the address.

For option D: The return value is the integer pointed to by p, not the address of p itself.

04

C Language Exercise Class - Day 10

Based on the defined function below:

fun (char* p2, char* p1)

{ while ((*p2=*p1) != ‘\0’) {p1++;p2++;} }

What is the function of this function?

A) Copy the string pointed to by p1 to the memory space pointed to by p2

B) Assign the address of the string pointed to by p1 to pointer p2

C) Compare the strings pointed to by pointers p1 and p2

D) Check if there is a ‘\0’ in the strings pointed to by pointers p1 and p2

C Language Exercise Class - Day 10

Answer: A

Analysis:

The function fun code analysis:

The function receives two character pointers p1 and p2. while ((*p2 = *p1) != ‘\0’) means assigning the character pointed to by p1 to the location pointed to by p2 until the string terminator \0 is encountered. After each assignment, both p1 and p2 move one character position forward (p1++; p2++;).

This function copies the string pointed to by p1 (including the ending \0) character by character to the memory space pointed to by p2.

05

C Language Exercise Class - Day 10

Read the following program:

#include <stdio.h>

int fun(int a, int b)

{ if (b==0)

return a;

else

return (fun(–a, –b));

}

main()

{

printf(“%d\n”, fun(4, 2));

}

The result of the program is

A) 1

B) 2

C) 3

D) 4

C Language Exercise Class - Day 10

Answer: B

Analysis:

The logic of the function fun:

This is a recursive function that receives two parameters a and b. If b == 0, the function directly returns a. Otherwise, the function recursively calls itself with decremented parameters.

The first call fun(4, 2):

b = 2, enters else, recursively calls fun(3, 1)

The second call fun(3, 1):

b = 1, recursively calls fun(2, 0)

The third call fun(2, 0):

b = 0, directly returns the value of a, which is 2

06

C Language Exercise Class - Day 10

Read the following program:

void fun()

{ int a,b;

a=100; b=200; }

main()

{ int a=5, b=7;

fun();

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

}

The output of the program is

A) 100200

B) 57

C) 200100

D) 75

C Language Exercise Class - Day 10

Answer: B

Analysis:

Variable scope:

The variables a and b defined in the main function are local variables, and their scope is limited to the main function.

The variables a and b defined in the fun function are also local variables, and their scope is limited to the fun function.

These two variables, although named the same, are completely independent and do not affect each other.

07

C Language Exercise Class - Day 10

Read the following program:

int f()

{ static int i=0;

int s=1;

s+=i; i++;

return s;

}

main()

{ int i,a=0;

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

a+=f();

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

}

The output of the program is

A) 20

B) 24

C) 25

D) 15

C Language Exercise Class - Day 10

Answer: D

Analysis:

The logic of function f:

static int i=0; defines a static local variable i, which has a lifetime throughout the program’s execution and is initialized to 0 only on the first call. int s=1; defines an automatic local variable s, which is re-initialized to 1 every time f is called. s += i; i++; means adding the value of i to s, then incrementing i by 1. The function returns the value of s.

The return value of each call to f:

First call: s = 1 + 0 = 1, i becomes 1, returns 1.

Second call: s = 1 + 1 = 2, i becomes 2, returns 2.

Third call: s = 1 + 2 = 3, i becomes 3, returns 3.

Fourth call: s = 1 + 3 = 4, i becomes 4, returns 4.

Fifth call: s = 1 + 4 = 5, i becomes 5, returns 5.

a starts at 0, and the return values of the five calls to f are summed:

1 + 2 + 3 + 4 + 5 = 15

08

C Language Exercise Class - Day 10

Read the following program:

fun(int x)

{ static int a=3;

a+=x;

return a;}

main()

{ int k=2, m=1, n;

n=fun(k);

n=fun(m);

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

The output of the program is

A) 3

B) 4

C) 6

D) 9

C Language Exercise Class - Day 10

Answer: C

Analysis:

The change of static variable a:

First call fun(k):

a starts at 3, after executing a += 2, a becomes 5, returns 5.

Second call fun(m):

a’s value is 5 (retained from the last call), after executing a += 1, a becomes 6, returns 6.

The assignment of variable n:

After the first call to fun(k), n is assigned 5.

After the second call to fun(m), n is reassigned 6.

09 (Common Mistake)

C Language Exercise Class - Day 10

If defined: int a=8,b=5,c; after executing the statement c=a/b+0.4; the value of c is

A) 1.4

B) 1

C) 2

D) 2.00000

C Language Exercise Class - Day 10

Answer: B

Analysis:

a and b are both integers (int), so a / b is integer division, resulting in 1 (because 8 / 5 = 1.6, but integer division discards the decimal part) then calculate 1 + 0.4, resulting in 1.4. c is an integer variable, and during assignment, it will automatically truncate the decimal part, so 1.4 is converted to 1.

10

C Language Exercise Class - Day 10

Among the following C language assignment statements, the correct one is

A) a=b=58

B) k=int(a+b);

C) a=58,b=59

D) –i;

C Language Exercise Class - Day 10

Answer: D

Analysis:

For option A: The types of a and b are unknown, and the assignment statement does not end with a semicolon.

For option B: The syntax is incorrect.

For option C: The assignment statement does not end with a semicolon.

Leave a Comment