Introduction to computer programming, variables, and comments Module 1 Test Answers Full 100%

  1. The feature of a high-level language which makes it possible to use that language on many different computers is called:

    • portability
    • availability
    • compatibility
      Explanation & Hints:

      The feature of a high-level programming language that makes it possible to use the language on many different computers is called portability. Portability refers to the ability of software to be run on various hardware platforms with little or no modification. This is a key feature of high-level languages, as they abstract away hardware-specific details, allowing programs written in one environment to be executed in another relatively easily.

  2. A file which contains a computer program translated into machine language is called:

    • an executable file 
    • a source file
    • an interchange file
    • Explanation & Hints:

      The correct answer is portability.

      Portability in the context of high-level programming languages refers to the ability of software to run on various hardware platforms without needing significant modifications. This is possible because high-level languages abstract away many of the hardware-specific details. For example, a program written in a language like Java or Python can be run on different operating systems (Windows, macOS, Linux) without changing the code. The language’s compiler or interpreter takes care of adapting the program to the specific hardware and operating system. This makes software development more efficient and allows developers to reach a wider audience with the same application.

  3. Select all the true statements:

    • The “C” language compiler is case-sensitive
    • The reserved keywords cannot be used as variable names
    • You can write multiline comments in the “C” language
    • The decimal value of 012 (octal) is 10
    • Nesting comments is a recommended pratice
    • The decimal value of 0x11 (hexadecimal) is 16
    • Explanation & Hints:

      Here are the true statements from the options provided:

      1. The “C” language compiler is case-sensitive – This is true. In C, identifiers (including variable names, function names, etc.) are case-sensitive, meaning that Variable, variable, and VARIABLE are considered different identifiers.
      2. The reserved keywords cannot be used as variable names – This is true. In C, reserved keywords, such as int, return, and for, are part of the language syntax and cannot be used as variable names because they have predefined meanings in the language.
      3. You can write multiline comments in the “C” language – This is true. In C, multiline comments start with /* and end with */, allowing comments to span multiple lines.
      4. The decimal value of 012 (octal) is 10 – This is true. In C, numbers starting with a zero are considered octal (base 8). Therefore, 012 in octal is equal to 10 in decimal.
      5. The decimal value of 0x11 (hexadecimal) is 16 – This is false. 0x11 in hexadecimal actually equals 17 in decimal. The correct computation is 1×161+1×160=16+1=17.
      6. Nesting comments is a recommended practice – This is false. In C, nesting comments is not supported by all compilers and can lead to errors or undefined behavior if attempted. Therefore, it is not a recommended practice.

      Thus, the true statements are the first four options.

  4. The sets of rules that determine the appropriate ways of combining symbols which form correct fragments of code in a programming language are called:

    • syntax
    • semantics
    • lexis
    • Explanation & Hints:

      The correct term for the sets of rules that determine the appropriate ways of combining symbols to form correct fragments of code in a programming language is syntax.

      • Syntax refers to the structure of statements in a programming language, including the correct use of symbols and how commands are arranged. It involves the rules and guidelines that define how code must be written to be correctly interpreted by the compiler or interpreter.
      • Semantics, on the other hand, refers to the meaning of those syntactical elements—what the statements actually do when executed.
      • Lexis is not a standard term used in programming language context; in linguistic terms, it refers to the vocabulary of a language, which is somewhat analogous to the terminology used in specific programming languages but isn’t directly related to the rules for combining symbols in code.
  5. Which line of code must you insert in order for the program to produce the expected output?
    Expected output:

    1

    Code:

    #include <stdio.h>
    int main()
    
    {
           int var = 1;
           print("%d \n", var);
    • }
      placed at the end of the code block
    • return 0;
      placed at the end of the code block
    • int var;
      placed above int var = 1;
    • No change are required – the program will compile and produce the expected output
    • Explanation & Hints:

      The existing code snippet you provided has a typo in the function used to print the variable var. Instead of print, it should be printf. Here’s the correct version of your code to produce the expected output:

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

      So, to answer your question on which line of code must be inserted, “No change are required – the program will compile and produce the expected output” is incorrect due to the typo. The correct function printf must be used instead of print. Once you replace print with printf, the code will compile and produce the expected output, and including return 0; at the end of the main function is a good practice to indicate that the program ended successfully.

  6. What is the value of the var variable after the execution of the following snippet of code:

    int var = 2;
    var = 4
    var = var + 6;
    var = var + var;
    • 20
    • 24
    • 22
    • 10
    • Explanation & Hints:

      Let’s analyze the code snippet and the operations performed on the variable var to determine its final value:

      1. int var = 2; initializes var with the value 2.
      2. var = 4; changes the value of var to 4.
      3. var = var + 6; adds 6 to the current value of var (which is 4), making it 10.
      4. var = var + var; adds the current value of var to itself, so 10 + 10 = 20.

      Therefore, after the execution of the code snippet, the value of var is 20. Hence, the correct answer is 20.

  7. Which of the following identifiers are legal variable names in the C language?

    Select three answers.

    • _3monthsOld
    • three_months_old
    • month3
    • 3monthsOld
    • int
    • three months
    • Explanation & Hints:

      In the C programming language, identifiers (including variable names) must follow specific rules:

      1. Identifiers can consist of letters (both uppercase and lowercase), digits, and underscores.
      2. Identifiers must not begin with a digit.
      3. Identifiers must not be keywords of the C language.
      4. Identifiers should not contain spaces or special characters other than the underscore.

      Based on these rules, the legal variable names from the options you’ve provided are:

      1. three_months_old – This is a legal identifier as it only contains letters and underscores.
      2. month3 – This is a legal identifier as it starts with a letter and is followed by a digit.
      3. _3monthsOld – While it is somewhat unconventional to start with an underscore followed by a digit, this is technically a legal identifier in C.

      The identifiers that are not legal include:

      • 3monthsOld – This is not legal because it starts with a digit.
      • int – This is not legal because “int” is a reserved keyword in C.
      • three months – This is not legal because it contains a space.

      Thus, the three legal variable names are _3monthsOld, three_months_old, and month3.

  8. The return statement used in a function:

    • ends the function execution
    • continuously repeats the function execution
    • repeats the function execution once, and then ends the function execution
    • Explanation & Hints:

      The return statement used in a function ends the function execution. When a return statement is encountered, the execution of the current function is stopped, and control is returned to the point where the function was called. Additionally, the return statement can be used to send a value back to the caller. This is fundamental in many programming languages, including C, C++, Java, and Python, among others. Thus, the correct understanding of a return statement is that it terminates the function’s execution.

  9. Data of type float is:

    • a fractional number
    • an integral number
    • an internal number
    • an integer number
    • Explanation & Hints:

      Data of type float represents a fractional number. In programming, a float typically refers to a floating-point number, which is capable of representing numbers that have a fractional component (numbers with digits beyond the decimal point). This data type is used when more precision is required in calculations involving decimals, unlike integers which represent whole numbers without any fraction or decimal component.

  10. What is the output of the following program?

    #include <stdio.h>
    int main()
    {
        int var;
        var = 1; 
        var = 2;
    /*  var = 3; */
        print("%d", var);
        return 0;
    }
    • 2
    • 3
    • 6
    • 1
    • Explanation & Hints:

      The output of the provided C program will be 2. Here’s a step-by-step explanation:

      1. int var; declares an integer variable named var.
      2. var = 1; sets the value of var to 1.
      3. var = 2; then changes the value of var to 2.
      4. /* var = 3; */ is commented out and thus has no effect on the program’s execution.
      5. print("%d", var); should be printf("%d", var); to correctly compile in C. Assuming this is a typo and should be printf, it will print the current value of var, which is 2.

      Therefore, the correct output, assuming the typo is corrected to printf, is 2.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments