C Language Exercises – Day 11

01

C Language Exercises - Day 11

The ASCII value of the digit character ‘0’ is 48. Given the following program:

main()

{

char a=’1′,b=’2′;

printf(“%c,”,b++);

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

}

The output of the program is:

A) 3,2

B) 25,2

C) 2,2

D) 2,25

C Language Exercises - Day 11

Answer: C

Explanation:

printf(“%c, b++): The initial value of b is ‘2’ (ASCII value 50); b++ is a post-increment, so it first outputs the current value of b ‘2’, then b becomes ‘3’ (ASCII value 51), resulting in the output 2,.

printf(“%d\n”, b – a): At this point, b is ‘3’ (ASCII value 51) and a is ‘1’ (ASCII value 49); b – a calculates the difference in ASCII values: 51 – 49 = 2, resulting in the output 2.

The combined output of the two printf statements is 2,2

02

C Language Exercises - Day 11

Read the following program segment:

int a, b, c;

a=10; b=50; c=a;

if (a>b) a=b, b=c; c=a;

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

Then the output of the program is ( )

A) a=10 b=50 c=10

B) a=10 b=50 c=30

C) a=10 b=30 c=10

D) a=50 b=30 c=50

C Language Exercises - Day 11

Answer: A

Explanation:

Variable initialization:

a = 10;

b = 50;

c = a; (at this point c = 10)

if (a > b) check:

a is not greater than b, so the code inside the if statement a = b, b = c; will not execute.

c = a; executes; regardless of whether the if condition is true, c = a; will execute, so c is reassigned to 10.

Final variable values:

a remains 10

b remains 50

c is updated to 10

03

C Language Exercises - Day 11

The output of the following program segment is

char a[ ]= “language”, *p;

p=a;

while (*p !=’u’) { printf(“%c”,*p-32); p++; }

A) LANGUAGE

B) language

C) LANG

D) langUAGE

C Language Exercises - Day 11

Answer: C

Explanation:

while (*p != ‘u’) loop:

Loop condition: continue executing while the current *p is not equal to ‘u’.

In “language”, ‘u’ is the 5th character (counting from 0, it’s a[4]), so the loop will process the first 4 characters ‘l’, ‘a’, ‘n’, ‘g’.

*p – 32: converts lowercase letters to uppercase letters (in ASCII, lowercase letters are 32 greater than uppercase letters).

‘l’ – 32 = ‘L’

‘a’ – 32 = ‘A’

‘n’ – 32 = ‘N’

‘g’ – 32 = ‘G’

p++: moves the pointer to the next character; when p points to ‘u’, the loop ends, and LANG is output, and the program only outputs LANG, not processing the subsequent characters.

04 (Common Mistake)

C Language Exercises - Day 11

Given int a; float b; when executing scanf(“%2d%f”,&a,&b); if the input from the keyboard is 876 543.0<enter>, the values of a and b are:

A) 876 and 543.000000

B) 87 and 6.000000

C) 87 and 543.000000

D) 76 and 543.000000

C Language Exercises - Day 11

Answer: B

Explanation:

%2d format specifier: %2d means read at most 2 digits of an integer, so the first two digits of 876, 87, are assigned to a, and the remaining 6 will be processed by the subsequent %f, so a = 87.

%f format specifier: %f will read the remaining input part, i.e., 6 543.0, since there is a space after 6, %f will read 6 and stop (as space is the default delimiter), so b = 6.000000; the remaining 543.0 will be left in the input buffer but will not be assigned to any variable.

05

C Language Exercises - Day 11

Let the variables be defined as int A=5, B=6, the value of the expression (++A==B–)?++A:–B is

A) 5

B) 6

C) 7

D) 8

C Language Exercises - Day 11

Answer: C

Explanation:

Initial values A = 5, B = 6

Evaluating the expression (++A == B–)

++A: first increment A by 1, A becomes 6, then returns 6.

B–: first returns the current value of B 6, then B decrements to 5.

Comparing 6 == 6, the result is true (1).

Execution of the ternary operator ? :

Since (++A == B–) is true, execute ++A. ++A: at this point A is already 6, incrementing it by 1 makes it 7, returning 7; –B will not execute; the final result: the value of the entire expression is 7.

Conditional operator:

Conditional expression?expression1:expression2

The ternary operator is the only three-way operator in C language, its syntax rules are to first evaluate the conditional expression (which must return true or false), if the condition is true, execute expression1 and return its value; if the condition is false, execute expression2 and return its value.

06

C Language Exercises - Day 11

If the following program exists:

int a[12]={1,2,3,4,5,6,7,8,9,10,11,12};

char c=’a’,d,g;

Then the expression that evaluates to 4 is

A) a[g-c]

B) a[4]

C) a[‘d’-‘c’]

D) a[‘d’-c]

C Language Exercises - Day 11

Answer: D

Explanation:

For option A: g is uninitialized, its value is uncertain, so the result of a[g – c] is also uncertain.

For option B: array indexing starts from 0, a[4] is the 5th element, which is 5.

For option C: the ASCII value of ‘d’ is 100, and ‘c’ is 99; ‘d’ – ‘c’ = 100 – 99 = 1; a[1] is the 2nd element, which is 2.

For option D: c = ‘a’, its ASCII value is 97; ‘d’ – c = 100 – 97 = 3; a[3] is the 4th element, which is 4.

07

C Language Exercises - Day 11

Read the following program:

int fun(int u,int v);

main ()

{

int a=24,b=16,c;

c=fun(a,b);

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

}

int fun(int u,int v)

{ int w;

while(v)

{ w=u%v; u=v; v=w }

return u;

}

The output is

A) 6

B) 7

C) 8

D) 9

C Language Exercises - Day 11

Answer: C

Explanation:

Loop iterations u v w u’ v’

1 24 16 8 16 8

2 16 8 0 8 0

End 8 0 Loop terminates

08

C Language Exercises - Day 11

Read the following program:

main()

{ char a,b,c,d;

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

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

}

If the input from the keyboard is: 6,5,65,66<enter>, the output is

A) 6,5,A,B

B) 6,5,65,66

C) 6,5,6,5

D) 6,5,6,6

C Language Exercises - Day 11

Answer: A

Explanation:

According to scanf format parsing:

%c → ‘6’ (character ‘6’, ASCII code 54)

%c → ‘5’ (character ‘5’, ASCII code 53)

%d → 65 (integer 65, corresponding to ASCII character ‘A’)

%d → 66 (integer 66, corresponding to ASCII character ‘B’)

Variable assignments:

a = ‘6’ (character ‘6’)

b = ‘5’ (character ‘5’)

c = 65 (integer 65, but when output with %c it will convert to ‘A’)

d = 66 (integer 66, but when output with %c it will convert to ‘B’)

09

C Language Exercises - Day 11

The output of the following program is

main()

{ char *p1,*p2;

char str1[50]=”xyz”,str2[]=”abcd”;;

p1=str2;

p2=”ABCD”;

strcpy(str+2,strcat(p1+2,p2+1));

printf(“%s”,str); }

A) xyabcAB

B) abcABz

C) ABabcz

D) xycdBCD

C Language Exercises - Day 11

Answer: D

Explanation:

The main function of the program is to manipulate strings and pointers: defining character pointers p1, p2 and character array str initialized to “xyz”; p1 points to the string constant “abcd”, p2 points to “ABCD”; using strcat and strcpy for string concatenation and copying; finally printing the content of str.

strcat function: its function is to append the src string to the end of the dest string, overwriting the terminating null character ‘\0’ of dest, returning a pointer to dest, dest must be a modifiable character array, not a string constant.

10 (Common Mistake)

C Language Exercises - Day 11

Read the following program:

# include <stdio.h>

main()

{ int a[]={1,2,3,4,5,6,7,8,9,10,11,12};

int*p=a+5,*q=NULL;

*q=*(p+5);

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

}

The output of the program is

A) Runtime error

B) 6,6

C) 6,11

D) 5,10

C Language Exercises - Day 11

Answer: A

Explanation:

The reason for the error: q is initialized to NULL, dereferencing *q directly will cause a runtime error. The correction method: q should first point to a valid memory address.

Leave a Comment