Daily C Language Practice: Mastering Programming Basics with 4 Types of Questions (4)

1. Multiple Choice Questions

  1. In the following code snippet, the final value of <span><span>x</span></span> is ( )

int x = 10;x += 5 * 2;x /= 3;

A. 5 B. 6 C. 15 D. 20Answer: BExplanation:<span><span>x += 5 * 2</span></span> is equivalent to <span><span>x = x + 5 * 2</span></span>, thus <span><span>x = 10 + 10 = 20</span></span>. Next, <span><span>x /= 3</span></span> is equivalent to <span><span>x = x / 3</span></span>, therefore <span><span>x = 20 / 3 = 6</span></span> (integer division). The final value of <span><span>x = 6</span></span>.

2. Which of the following definitions of an array in C is correct? ( )

A. <span><span>int array[5];</span></span>B. <span><span>int array[];</span></span>C. <span><span>int array;</span></span>D. <span><span>int array[ ];</span></span>Answer: AExplanation:The definition of an array requires specifying the type and size of the array. Option A correctly defines an array containing 5 integers. Options B and D do not specify the size of the array, while option C defines a regular variable instead of an array.

2. True or False Questions

  1. In C language, the <span><span>do...while</span></span> loop executes the loop body at least once. ( )Answer: √Explanation<span><span>do...while</span></span> loop’s characteristic is that it executes the loop body once before checking the condition, thus it will execute the loop body at least once.

2. In C language, local variables can be defined outside of functions. ( )Answer: ×Explanation:Local variables are defined within functions, and their scope is limited to that function. Variables defined outside of functions are global variables.

3. Fill in the Blanks

The output of the following program is ______.

#include <stdio.h>int main() {    int a = 5, b = 3;    if (a > b) {        printf("a is greater than b\n");    } else {        printf("a is not greater than b\n");    }    return 0;}

Answer: a is greater than bExplanation<span><span>a = 5</span></span>, <span><span>b = 3</span></span>, thus the condition <span><span>a > b</span></span> holds true, outputting <span><span>"a is greater than b"</span></span>.4. Application QuestionsWrite a C program to implement the following functionality: input an integer and determine whether it is an Armstrong number (i.e., a three-digit number whose digits’ cubes sum to the number itself, for example, 153=13+53+33 and output the corresponding prompt information.Reference Answer

#include <stdio.h>int main() {    int num, originalNum, remainder, result = 0;    printf("Please enter an integer:");    scanf("%d", &num);    originalNum = num;    while (originalNum != 0) {        remainder = originalNum % 10;        result += remainder * remainder * remainder;        originalNum /= 10;    }    if (result == num) {        printf("%d is an Armstrong number\n", num);    } else {        printf("%d is not an Armstrong number\n", num);    }    return 0;}

Explanation:This question mainly tests the use of loops and conditional statements. By looping through each digit of the input integer, calculating the sum of their cubes, and finally checking if the sum equals the original number. If it does, the number is an Armstrong number; otherwise, it is not.

Leave a Comment