C Language Exercise Class – Day 17

01

C Language Exercise Class - Day 17

Among the following function definitions, the one that will cause a compilationerror is

A) max(int x, int y, int* z)

{ *z = x>y?x:y; }

B) int max(int x, y)

{ int z;

z = x>y?x:y;

return z;}

C) max(int x, int y)

{ int z;

z = x>y?x:y;

return(z);}

D) int max(int x, int y)

{ return (x>y ? x : y); }

C Language Exercise Class - Day 17

Answer: B

Explanation:

For option B: The function parameter y does not have a declared type, which is illegal. The correct syntax should be int max(int x, int y)

02

C Language Exercise Class - Day 17

Assuming a and b are int type variables, after executing the following statements, the value of b will be

a=1;b=10;

do

{b-=a;a++;}

while(b–<0);

A) 9

B) -2

C) -1

D) 8

C Language Exercise Class - Day 17

Answer: D

Explanation: Brief

03

C Language Exercise Class - Day 17

If we assume a=2, b=3, x=3.5, y=2.5

Then the value of the following arithmetic expression is

(float)(a+b)/2+(int)x%(int)y

A) 2

B) 3

C) 3.5

D) 2.5

C Language Exercise Class - Day 17

Answer: C

Explanation:

Step-by-step calculation

(a + b):

a + b = 2 + 3 = 5 (int)

(float)(a + b):

Cast to float → 5.0f

(float)(a + b) / 2:

5.0f / 2 → 2.5f (float)

(int)x:

x = 3.5, cast to int → 3

(int)y:

y = 2.5, cast to int → 2

(int)x % (int)y:

3 % 2 → 1 (int)

Final addition:

2.5f + 1 → 3.5f (float)

04

C Language Exercise Class - Day 17

The following code segment inputs data into all elements of the array, which statement should be filled in the blank

#include <stdio.h>

main()

{

int a[10],i=0;

while(i<10)

scanf(“%d”,______);

}

A) a+(++i)

B) &a[i+1]

C) a+i

D) &a[i++]

C Language Exercise Class - Day 17

Answer: D

Explanation:

For option A: ++i will increment i first, causing the first loop to skip a[0] and possibly access a[10] (error)

For option B: In the first loop i=0, input to a[1], skipping a[0], in the last loop i=9, input to a[10] (out of bounds)

For option C: The syntax is correct (a+i is equivalent to &a[i]), but the loop variable i is not updated, leading to an infinite loop (error)

05

C Language Exercise Class - Day 17

Which of the following options can be a valid integer in C language

A) 10110B

B) 0386

C) 0Xffa

D) x2a2

C Language Exercise Class - Day 17

Answer: C

Explanation:

In C language, valid integers can take various forms:

1. Decimal: ordinary numbers;

2. Octal: starts with 0;

3. Hexadecimal: starts with 0x or 0X;

4. Binary: starts with 0b or 0B;

For option A:

Binary must include 0b or 0B

For option B:

Octal cannot contain 8

For option D:

Hexadecimal must include 0x or 0X

06

C Language Exercise Class - Day 17

Read the following code segment:

main()

{ int i, s=0, t[]={1,2,3,4,5,6,7,8,9};

for(i=0;i<9;i+=2) s+=*(t+i);

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

}

The output result after the program execution is

A) 45

B) 20

C) 25

D) 36

C Language Exercise Class - Day 17

Answer: C

Explanation:

Define an array t and initialize it

Using a for loop, starting from i=0, increasing by 2 (i.e., i=0, 2, 4, 6, 8).

In the loop, calculate *(t + i) (i.e., t[i]), and accumulate it into variable s, finally output the value of s.

07

C Language Exercise Class - Day 17

(Multiple choice) Among the following statementscorrect are

A) User-defined identifiers are allowed to use keywords

B) User-defined identifiers should aim to be “self-explanatory”

C) User-defined identifiers must start with a letter or underscore

D) User-defined identifiers are case-sensitive

C Language Exercise Class - Day 17

Answer: BCD

Explanation:

For option A: User-defined identifiers are not allowed to use keywords

Keywords in C language (reserved words) cannot be used as identifiers, the following are the standard C language (C89/C99) keywords:

Data type related

int, char, float, double

short, long, signed, unsigned

void, _Bool (C99), _Complex (C99), _Imaginary (C99)

Control flow related

if, else, switch, case, default

for, while, do, break, continue

goto, return

Storage class and modifiers

auto, register, static, extern

const, volatile, restrict (C99)

Structures and unions

struct, union, enum

Others

sizeof, typedef

C11 new keywords (partially supported by some compilers)

_Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, _Thread_local

08

C Language Exercise Class - Day 17

(Multiple choice) Among the statements defining variablescorrect are

A) int _int;

B) double int_;

C) char For;

D) float US$;

C Language Exercise Class - Day 17

Answer: ABC

Explanation:

For option D: Identifiers can only contain letters, digits, and underscores, $ is an illegal character, so US$ is an illegal variable name.

09 (Common Mistake)

C Language Exercise Class - Day 17

(Multiple choice) Given the definitions: int k=1, m=2; float f=7.0; which of the following options conforms to C language syntax

A) k=k>=k

B) -k++

C) k%int(f)

D) k/(int)f

C Language Exercise Class - Day 17

Answer: ABD

Explanation:

For option A: k >= k compares whether k is greater than or equal to k. Then assigns the result to k, which is completely legal.

For option B: k++ is a post-increment, first returning the value of k, then incrementing k to 2. Then taking the negative, this is a legal expression.

For option C: The correct syntax for type casting in C language is (int)f, not int(f), the correct syntax is k % (int)f.

For option D: (int)f casts f to int type. k / (int)f means 1 / 7, the result is 0.

10 (Common Mistake)

C Language Exercise Class - Day 17

(Multiple choice) If there is a definition statement:

double x[5]={1.0,2.0,3.0,4.0,5.0}, *p=x; thenthe correct references to the elements of the x array are

A) *p

B) x[5]

C) *(p+1)

D) *x

C Language Exercise Class - Day 17

Answer: ACD

Explanation:

For option B: A common mistake, the valid indices of the array x are 0 to 4, x[5] is out of bounds.

For option D: The array name x degrades to a pointer in the expression, *x is equivalent to x[0].

Leave a Comment