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

1. Multiple Choice Questions

  1. Which of the following statements about variables in C language is correct? ( )A. Variable names must start with a digitB. Variable names can contain special charactersC. Variable names must start with a letter or underscoreD. There is no limit to the length of variable namesAnswer: CExplanation: In C language, variable names must start with a letter or underscore, cannot start with a digit, and cannot contain special characters. The length of variable names is limited, but usually long enough.

2. The output of the following program is ( )

#include <stdio.h>int main() {    int x = 10;    int y = x;    x = 20;    printf("x = %d, y = %d\n", x, y);    return 0;}

A. x = 10, y = 10B. x = 20, y = 10C. x = 10, y = 20D. x = 20, y = 20Answer: BExplanation: The variable<span><span>y</span></span> is assigned the initial value of<span><span>x</span></span>, which is 10. After that, the value of<span><span>x</span></span> is changed to 20, but the value of<span><span>y</span></span> remains unchanged.

2. True or False Questions

  1. In C language, the type of a variable determines the range of values it can hold and the amount of memory it occupies. ( )Answer: √Explanation: The type of a variable (such as<span><span>int</span></span>,<span><span>float</span></span>,<span><span>char</span></span>, etc.) determines the range of values it can hold and the amount of memory it occupies.

  2. In C language, a variable can be used without declaration before its use. ( )Answer: ×Explanation: In C language, a variable must be declared before it is used, specifying its type and name.

3. Fill in the Blanks

The output of the following program is ______.

#include <stdio.h>int main() {    int a = 5, b = 3;    int sum = a + b;    int product = a * b;    printf("Sum = %d, Product = %d\n", sum, product);    return 0;}

Answer: Sum = 8, Product = 15Explanation: The program calculates the sum and product of the variables<span><span>a</span></span> and <span><span>b</span></span>, stores the results in the variables<span><span>sum</span></span> and <span><span>product</span></span>, and then outputs the results.

4. Application Questions

Write a C language program to achieve the following functionality: input two integers, swap their values, and output the swapped results.Reference Answer:

#include <stdio.h>int main() {    int a, b;    printf("Please enter two integers (separated by space):");    scanf("%d %d", &a, &b);    int temp = a;    a = b;    b = temp;    printf("Swapped results: a = %d, b = %d\n", a, b);    return 0;}

Explanation: This question mainly tests the swapping of variables. A temporary variable<span><span>temp</span></span> is used to swap the values of<span><span>a</span></span> and <span><span>b</span></span>.

Leave a Comment