C Language Exercises – Day 12

01

C Language Exercises - Day 12

Read the following program:

#include <stdio.h>

main()

{ int i,j, m=55;

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

for(j=3; j<=i; j++) m=m%j;

printf(“%d\n “, m);

}

The output of the program is

A) 0

B) 1

C) 2

D) 3

C Language Exercises - Day 12

Answer: B

Explanation: Brief

02

C Language Exercises - Day 12

Read the following program:

main()

{ int a=5,b=4,c=3,d=2;

if(a>b>c)

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

else if((c-1>=d)==1)

printf(“%d\n”,d+1);

else

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

}

The output after execution is

A) 2

B) 3

C) 4

D) Compilation error

C Language Exercises - Day 12

Answer: B

Explanation:

The evaluation of a > b > c: In C language, a > b > c is parsed as (a > b) > c, not a > b && b > c; a > b returns 1, then 1 > c returns 0, so the overall condition is false.

03

C Language Exercises - Day 12

If the variables in the following options are correctly defined, the correct assignment statement is

A) x1=26.8%3;

B) 1+2=x2;

C) x3=0x12;

D) x4=1+2=3;

C Language Exercises - Day 12

Answer: C

Explanation:

For option A: % is the modulus operator, which requires both operands to be integers; 26.8 is a floating-point number and cannot be used directly for modulus operation.

For option B: The left side of the assignment statement must be a variable (modifiable lvalue), not a constant.

For option C: 0x12 is a hexadecimal integer, representing decimal 18.

For option D: The assignment operator = is right associative, so the expression is parsed as x4 = (1 + 2 = 3); 1 + 2 = 3 is illegal because 1 + 2 is not a variable and cannot be the target of an assignment.

04

C Language Exercises - Day 12

When executing the following program, if the input from the keyboard is ABCdef<enter>, the output will be

#include<stdio.h>

main()

{ char ch;

while((ch=getchar())!=’\n’)

{ if(ch>=’A’ && ch<=’Z’) ch=ch+32;

else if(ch>=’a’ && ch<=’z’) ch=ch-32;

printf(“%c”,ch);

}

printf(“\n”);

}

A) ABCdef

B) abcDEF

C) abc

D) DEF

C Language Exercises - Day 12

Answer: B

Explanation:

Program functionality

Input processing: Read characters from the keyboard until a newline character \n (i.e., the user inputs <enter>).

Character conversion: If the character is an uppercase letter (‘A’ to ‘Z’), convert it to a lowercase letter (ASCII value plus 32); if the character is a lowercase letter (‘a’ to ‘z’), convert it to an uppercase letter (ASCII value minus 32), non-letter characters remain unchanged.

Output the converted characters: Each converted character is immediately output, followed by a newline character.

05 (Common Mistake)

C Language Exercises - Day 12

Read the following program:

#include <string.h>

main()

{

char p[]={‘a’,’b’,’c’}, q[10]={‘a’,’b’,’c’};

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

}

Among the following statements, the correct one is

A) When initializing arrays p and q, the system automatically adds a string terminator, so the output length is both 3

B) Since the p array does not have a string terminator, the length cannot be determined; but the q array string length is 3

C) Since the q array does not have a string terminator, the length cannot be determined; but the p array string length is 3

D) Since neither the p nor q arrays have a string terminator, the lengths cannot be determined

C Language Exercises - Day 12

Answer: B

Explanation:

Array p: Defined as char p[] = {‘a’, ‘b’, ‘c’}; This is a character array initialized to {‘a’, ‘b’, ‘c’}, without explicitly including the string terminator ‘\0’; since there is no ‘\0’, p is not a valid C string.

Array q: Defined as char q[10] = {‘a’, ‘b’, ‘c’}; This is a character array of length 10, with the first three elements initialized to {‘a’, ‘b’, ‘c’}, and the remaining elements automatically filled with ‘\0’ (i.e., the string terminator); therefore, q is a valid C string with the content “abc”.

06

C Language Exercises - Day 12

When the value of variable c is not 2, 4, or 6, the expression that is also “true” is

A) (c==2)||(c==4)||(c==6)

B) (c>=2 && c<=6)||(c!=3)||(c!=5)

C) (c>=2 && c<=6)&&!(c%2)

D) (c>=2 && c<=6)&&(c%2!=1)

C Language Exercises - Day 12

Answer: B

Explanation:

Problem-solving technique: Special value substitution method, let c=1, analyze and verify each option.

07

C Language Exercises - Day 12

The output of the following program is

main()

{int n=4;

while(n–)

printf(“%d “,–n);}

A) 2 0

B) 3 1

C) 3 2 1

D) 2 1 0

C Language Exercises - Day 12

Answer: A

Explanation:

First loop:

while (n–): n = 4 (true), then n decreases to 3.

–n: n decreases from 3 to 2, output 2.

Current n = 2.

Second loop:

while (n–): n = 2 (true), then n decreases to 1.

–n: n decreases from 1 to 0, output 0.

Current n = 0.

Third loop:

while (n–): n = 0 (false), loop terminates.

08 (Common Mistake)

C Language Exercises - Day 12

Read the following program:

main()

{ int x=102, y=012;

printf(“%2d,%2d\n”,x,y);

}

The output of the program is

A) 10,01

B) 02,12

C) 102,10

D) 02,10

C Language Exercises - Day 12

Answer: C

Explanation:

printf formatted output

“%2d,%2d\n”:

%2d: Represents output of at least 2 digits of decimal integer, right-aligned (left side filled with spaces) if less than 2 digits

x = 102: 102 is a 3-digit number, directly output 102 (exceeding 2 digits ignores width limit)

y = 10 (octal 012 in decimal): 10 is a 2-digit number, output 10.

Introduction to printf output format:

printf is the most commonly used formatted output function in C language, its core is to control the output style through format strings.

Specifier Type

%d Decimal integer

%f Floating point (default 6 digits)

%c Single character

%s String

%x Hexadecimal (lowercase)

%X Hexadecimal (uppercase)

%o Octal

%p Pointer address

%% Output percentage sign

Formatting control (width\precision\alignment)

Width and alignment

%Nd: Output at least N digits, if insufficient defaults to right alignment (left side filled with spaces)

%-Nd: Left alignment, right side filled with spaces

Floating point data precision

%.Nf: Control decimal places to N

Combined use:

%N.Nf total width N, decimal places N

[A little test]

#include <stdio.h>

int main() {

float num = 3.1415926;

printf(“|%8.2f|%-8.2f|%08.2f|”, num, num, num);

return 0;

}

A) | 3.14|3.14 |00003.14|B) |3.14 | 3.14|00003.14|C) |00003.14|3.14 | 3.14|D) |3.14 |00003.14| 3.14|

Answer: A

09

C Language Exercises - Day 12

The output of the following program segment is

main()

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

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

printf(“%d,”,x[i][2-i]);

}

A) 1,5,9

B) 1,4,7

C) 3,5,7

D) 3,6,9

C Language Exercises - Day 12

Answer: C

Explanation:

Column 0 Column 1 Column 2

Row 0 1 2 3

Row 1 4 5 6

Row 2 7 8 9

10

C Language Exercises - Day 12

Read the following statement:

char s1[ ]=”12345″,s2[ ]=”1234″;

printf(“%d\n”,strlen(strcpy(s1,s2)));

Then the final output result is

A) 4

B) 5

C) 9

D) 10

C Language Exercises - Day 12

Answer: A

Explanation:

1. String initialization

s1[] = “12345”:

Length of 5 string, actually occupies 6 bytes (including the ending \0).

Memory layout: ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘\0’.

s2[] = “1234”:

Length of 4 string, actually occupies 5 bytes (including the ending \0).

Memory layout: ‘1’ ‘2’ ‘3’ ‘4’ ‘\0’.

2. Execution of strcpy(s1, s2)

Copies the content of s2 to s1, including the ending \0

After copying, the content of s1: ‘1’ ‘2’ ‘3’ ‘4’ ‘\0’ (overwrites the first 5 bytes of original s1).

Note: s1’s original length is sufficient to accommodate s2

3. Calculation of strlen(strcpy(s1, s2))

strcpy returns the address of the target string s1. strlen calculates the length of s1 (from the start to the first \0). At this point, the content of s1 is “1234” (‘1’ ‘2’ ‘3’ ‘4’ ‘\0’), length is 4.

Leave a Comment