Basic data types, operations, and flow control (decision-making statements) Module 2 Test Answers Full 100%

  1. What happens if you try to compile and run this program?

    #include <stdio.h>
    int main(void)
    {
       int i, j, k;
          i = 2;
       j = -2;
           if(i)
           i--;
       if(j)
           j++;
       k = i * j;
           printf("%d",k);
       return 0;
    }
    • -1
    • -2
    • 1
    • 2
      Explanation & Hints:

      With the provided C program, here’s what will happen when it’s compiled and run:

      1. Variable Initialization:
        • i is initialized to 2.
        • j is initialized to -2.
      2. Conditional Operations:
        • if(i) checks if i is non-zero. Since i is 2, the condition is true, and i-- is executed, decrementing i to 1.
        • if(j) checks if j is non-zero. Since j is -2, the condition is also true, and j++ is executed, incrementing j to -1.
      3. Multiplication and Output:
        • k is calculated as the product of i and j, which are now 1 and -1 respectively, so k = 1 * -1 = -1.
        • The program then prints -1.
      4. Return: The program returns 0, indicating successful execution.

      Thus, when this program is compiled and executed, it outputs -1.

  2. What is the value of the x variable at the end of the following snippet?

    float x;
    x = 1. / 2 * 3;
    /***
    • 1.5
    • 1
    • 2
    • 0
    • Explanation & Hints:

      To determine the value of the x variable in the given snippet, we need to evaluate the expression x = 1. / 2 * 3;.

      Here, the expression involves:

      1. 1. which is a floating-point number equivalent to 1.0.
      2. Division by 2, resulting in 1.0 / 2 = 0.5.
      3. Multiplication of the result by 3, leading to 0.5 * 3 = 1.5.

      Thus, the value of x at the end of this snippet will be 1.5.

  3. What is the decimal value of the following integer literal?

    0x8
    • 8
    • 100
    • 10
    • the literal is invalid
    • Explanation & Hints:

      The integer literal you provided, 0x8, represents a hexadecimal (base 16) value. In hexadecimal, digits range from 0 to 15, with the letters A through F representing values 10 through 15. Here’s how you convert 0x8 to decimal (base 10):

      • 8 in hexadecimal is simply 8 in decimal.

      So, the decimal value of the hexadecimal integer literal 0x8 is 8. This means the correct answer from your list is 8.

  4. What is the value of the c variable at the end of the following snippet?

    char c;
    c = '\';
    • the assignment is invalid and causes a compilation error
    • \
    • '
    • \0
    • Explanation & Hints:

      Cosd is indeed invalid due to a syntax error. In C programming, the backslash (\) is used as an escape character to denote special characters like newline (\n), tab (\t), null (\0), etc. When you write c = '\';, the compiler expects something to follow the backslash to complete the escape sequence.

      Since the backslash is used improperly here without a following character that forms a valid escape sequence, this line of code will cause a compilation error. To correctly assign a backslash to the character variable c, you should use two backslashes:

      c = '\\';

      This correctly represents a single backslash character. Thus, in the context of the options you provided, the most accurate response is:

      • the assignment is invalid and causes a compilation error
  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()
    {
        var = 1;
        printf("%d \n", var);
        return 0;
    }
    • int var;
      placed above var = 1;
    • int var;
      placed below var = 1;
    • int var;
      placed below printf("%d \n", var);
    • No change are required – the program will compile and produce the expceted output
    • Explanation & Hints:

      The code has an issue because the variable var is used in the main function before it’s declared and defined. Here’s the relevant part of the code:

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

      In this layout, var is declared after its usage in main, which is not valid in C. C requires that variables be declared before they are used. This means you need to declare var at a scope accessible to main before it is used.

      To fix the code, you should place the declaration of var at a global level before the main function begins. This would make var a global variable accessible within main and correctly initialized before it’s used. Here’s how you should modify the code:

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

      The correct choice is:

      • placed above var = 1;

      This option places the declaration of var where it needs to be for the code to compile successfully and produce the expected output of 1.

  6. What is the value of the var variable at the end of the following snippet?

    int var;
    var = 2;
    var = var * var;
    var = var + var;
    /*
    var = var / var;
    var = var % var;
    */
    • 8
    • 1
    • 0
    • 16
    • Explanation & Hints:

      To find the value of the variable var at the end of the given C code snippet, let’s walk through the code step by step:

      Initialization:

      int var;
      var = 2;

      Here, var is initialized to 2.

      First Update:

      var = var * var;

      The value of var is squared, so 2 * 2 = 4. Now var equals 4.

      Second Update:

      var = var + var;

      The value of var is added to itself, so 4 + 4 = 8. Now var equals 8.

      The lines within the comment block are not executed, as they are commented out:

      /*
      var = var / var;
      var = var % var;
      */

      Thus, these lines do not affect the value of var.

      Therefore, the value of var at the end of this snippet is 8.

  7. What is the value of the k variable at the end of the following snippet?

    int i, j, k;
    
    i = 3;
    j = -3;
    
    k = (i >= i) + (j <= j) + (i == j) + (i > j);
    • 3
    • 2
    • 1
    • 0
    • Explanation & Hints:

      To determine the value of the variable k at the end of the provided C code snippet, we will evaluate each of the expressions in the assignment to k based on the values of i and j. Here’s the detailed evaluation:

      Variable Initialization:

      int i, j, k;
      i = 3;
      j = -3;

      Assignment to k:

      k = (i >= i) + (j <= j) + (i == j) + (i > j);

      Breaking down each part:

      • (i >= i): This checks if i is greater than or equal to i. Since i is equal to itself, this is true, and thus, evaluates to 1.
      • (j <= j): This checks if j is less than or equal to j. Since j is equal to itself, this is true, and thus, evaluates to 1.
      • (i == j): This checks if i is equal to j. Since 3 is not equal to -3, this is false, and thus, evaluates to 0.
      • (i > j): This checks if i is greater than j. Since 3 is greater than -3, this is true, and thus, evaluates to 1.

      Summing these results gives:

      k = 1 + 1 + 0 + 1;
      k = 3;

      Thus, the value of k at the end of this code snippet is 3.

  8. What is the value of the k variable at the end of the following snippet?

    int i, j, k;
    
    i = 3;
    j = -3;
    
    k = i * j;
    k += j;
    k /= i;
    • -4
    • -8
    • 4
    • 8
    • Explanation & Hints:

      Let’s evaluate the code snippet step by step to find the value of the variable k:

      Initialization of Variables:

      int i, j, k;
      i = 3;
      j = -3;

      Calculation of k:

      • First, k is calculated by multiplying i and j:
      k = i * j;
      Substituting the values, =3×−3=−9.
      • Next, j is added to k:
      k += j;
      This translates to =−9+(−3)=−12.
      • Finally, k is divided by i:
      k /= i;
      This results in =−12÷3=−4.
      Therefore, the value of k at the end of this snippet is -4.
  9. What is the value of the k variable at the end of the following snippet?

    int i, j, k;
    
    i = 4;
    j = 5;
    
    k = i-- * ++j;
    • 24
    • 28
    • 21
    • 18
    • Explanation & Hints:

      Let’s break down the operations step-by-step:

      int i, j, k;
      
      i = 4;
      j = 5;
      
      k = i-- * ++j;
      1. Initial values:
        • i = 4
        • j = 5
      2. Pre-increment j (++j):
        • ++j increments j by 1 before using its value.
        • So, j becomes 6.
      3. Post-decrement i (i--):
        • i-- uses the current value of i (which is 4) and then decrements i by 1.
        • So, the value of i used in the multiplication is 4, and after the operation, i becomes 3.
      4. Multiplication:
        • k = i-- * ++j
        • k = 4 * 6
        • k = 24

      So, the value of k at the end of the snippet is 24.

  10. Which of the following identifiers is a valid variable name?

    • Monte_Carlo
    • Monte-Carlo
    • Monte Carlo
    • Monte@Carlo
    • Explanation & Hints:

      Generally, variable names must:

      1. Begin with a letter (uppercase or lowercase) or an underscore (_).
      2. Be followed by letters, digits, or underscores.

      Based on these rules, let’s evaluate the given identifiers:

      1. Monte_Carlo: This is a valid variable name. It starts with a letter and contains only letters and underscores.
      2. Monte-Carlo: This is not a valid variable name because it contains a hyphen (-), which is not allowed in variable names.
      3. Monte Carlo: This is not a valid variable name because it contains a space, which is not allowed in variable names.
      4. Monte@Carlo: This is not a valid variable name because it contains an at symbol (@), which is not allowed in variable names.

      So, the valid variable name is Monte_Carlo.

  11. What is the value of the var variable at the end of the following snippet?

    int var;
    var = 2;
    var = var * var;
    var = var + var;
    var = var / var;
    var = var % var;
    • 0
    • 1
    • 8
    • 16
    • Explanation & Hints:

      Let’s break down the operations step-by-step:

      int var;
      var = 2;
      var = var * var;
      var = var + var;
      var = var / var;
      var = var % var;
      1. Initial value:
        • var = 2
      2. Multiplication:
        • var = var * var
        • var = 2 * 2
        • var = 4
      3. Addition:
        • var = var + var
        • var = 4 + 4
        • var = 8
      4. Division:
        • var = var / var
        • var = 8 / 8
        • var = 1
      5. Modulo:
        • var = var % var
        • var = 1 % 1
        • var = 0 (since any number modulo itself is 0)

      So, the value of var at the end of the snippet is 0.

  12. What is the value of the x variable at the end of the following snippet?

    int x;
    x = 1 / 2;
    • 0
    • 1
    • 2
    • 0.5
    • Explanation & Hints:

      Let’s break down the assignment:

      int x;
      x = 1 / 2;
      • 1 / 2 is an integer division. Since both 1 and 2 are integers, the result is also an integer.
      • 1 / 2 equals 0 in integer division because the fractional part (0.5) is discarded.

      So, the value of x at the end of the snippet is 0.

  13. What is the value of the following floating-point literal?

    8765E-2
    • 87.65
    • 8.765
    • 876.5
    • 0.8765
    • Explanation & Hints:

      The floating-point literal 8765E-2 is expressed in scientific notation, where E-2 denotes multiplication by 10−2.

      To calculate the value:

      8765×10−2=8765×0.01=87.65

      So, the value of the floating-point literal 8765E-2 is 87.65.

  14. What is the value of the x variable at the end of the following snippet?

    int x;
    x = 1 / 2 * 3;
    /* */
    • 0
    • 1
    • 2
    • 1.5
    • Explanation & Hints:

      Let’s break down the operations step-by-step:

      int x;
      x = 1 / 2 * 3;

      In C, the operations are performed from left to right due to the same precedence level (both / and * have the same precedence and associate left to right):

      1. Integer division:
        • 1 / 2 is an integer division. Since both 1 and 2 are integers, the result is also an integer.
        • 1 / 2 equals 0 in integer division because the fractional part (0.5) is discarded.
      2. Multiplication:
        • The result of the division (0) is then multiplied by 3.
        • 0 * 3 equals 0.

      So, the value of x at the end of the snippet is 0.

  15. Which of the following identifiers is an invalid variable name?

    • 0_
    • _0_
    • _0
    • ___
    • Explanation & Hints:

      In most programming languages, variable names must adhere to specific rules. Typically, these rules include:

      1. The name must start with a letter (uppercase or lowercase) or an underscore (_).
      2. The name can only contain letters, digits, and underscores.

      Given these rules, let’s evaluate the provided identifiers:

      1. 0_: This is an invalid variable name because it starts with a digit.
      2. 0: This is a valid variable name. It starts with an underscore and contains only letters and underscores.
      3. _0: This is a valid variable name. It starts with an underscore and contains only letters and underscores.
      4. ___: This is a valid variable name. It starts with an underscore and contains only underscores.

      So, the invalid variable name is 0_.

  16. What is the decimal value of the following integer literal?

    08
    • the literal is invalid
    • 8
    • 1000
    • 10
    • Explanation & Hints:

      In most programming languages, including C and C++, an integer literal that starts with a 0 is interpreted as an octal (base-8) number. However, the digits in an octal number can only range from 0 to 7. The presence of the digit 8 makes 08 an invalid octal number.

      Therefore, the correct response is:

      the literal is invalid.

  17. What is the value of the k variable at the end of the following snippet?

    int i, j, k;
    i = 4;
    j = 5;
    
    k = --i * j++;
    • 15
    • 18
    • 16
    • 12
    • Explanation & Hints:

      Let’s break down the operations step-by-step:

      int i, j, k;
      
      i = 4;
      j = 5;
      
      k = --i * j++;
      1. Initial values:
        • i = 4
        • j = 5
      2. Pre-decrement i (--i):
        • --i decrements i by 1 before using its value.
        • So, i becomes 3.
      3. Post-increment j (j++):
        • j++ uses the current value of j (which is 5) and then increments j by 1.
        • So, the value of j used in the multiplication is 5, and after the operation, j becomes 6.
      4. Multiplication:
        • k = --i * j++
        • k = 3 * 5
        • k = 15

      So, the value of k at the end of the snippet is 15.

  18. What is the value of the c variable at the end of the following snippet?

    char c;
    
    c = 'a';
    c -= ' ';
    • A
    • a
    • \0
    • the assignment is invalid and causes a compilation error
    • Explanation & Hints:

      In C, characters are represented as integers according to their ASCII values. The ASCII value of 'a' is 97 and the ASCII value of the space character ' ' is 32.

      Here’s the snippet you provided:

      char c;
      
      c = 'a';
      c -= ' ';

      Let’s break down the operations step-by-step:

      1. Initial assignment:
        • c = 'a' assigns the character 'a' to c. The ASCII value of 'a' is 97.
      2. Subtraction operation:
        • c -= ' ' subtracts the ASCII value of ' ' (which is 32) from the ASCII value of 'a'.
        • So, the calculation is: 97 - 32 = 65.
      3. Resulting character:
        • The resulting ASCII value, 65, corresponds to the character 'A'.

      Therefore, the value of the c variable at the end of the snippet is ‘A’.

  19. Is the following declaration valid?

    int var, var;
    • No
    • Yes
    • Explanation & Hints:

      No, the declaration int var, var; is not valid in C (and most other programming languages) because it attempts to declare the same variable var twice in the same scope. This results in a compilation error due to the redefinition of the variable name. Each variable in a given scope must have a unique identifier.

  20. Which of the following is a proper integer number (in the “C” language sense)?

    • 123456
    • 123,456
    • 123.456
    • 123_456
    • Explanation & Hints:

      In C, a proper integer number is a sequence of digits without any separators such as commas, decimal points, or underscores. Based on this, let’s evaluate the provided options:

      1. 123456: This is a proper integer number.
      2. 123,456: This is not a proper integer number because it contains a comma.
      3. 123.456: This is not a proper integer number because it contains a decimal point.
      4. 123_456: This is not a proper integer number because it contains an underscore.

      Therefore, the proper integer number in the “C” language sense is 123456.

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