Test Your Knowledge (Ch 6)

The following questions are based on what we have learned so far in Chapter 6 – Variables in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

Which of the following is a valid variable name?

B’day
$hello
#HASH
temp_in_Deg
explanation

B’day is not a valid name because “single inverted comma” is not allowed in variable names.  $hello is not a valid name because “dollar” is a special character, and hence not allowed in a variable name. “#” is not allowed in a variable name. Words of a variable can be separated by underscores, therefore temp_in_Deg is a valid name.

Please be aware that in some older C programming implementations, you can use the dollar ($) symbol in variable names. While it might be possible to create a variable name with a dollar symbol, it’s not advisable because certain machines may not be compatible with it. It is better to avoid using dollar symbols in variable names to ensure broader compatibility.

Question 2

Which of the following is an invalid variable name?

dot
number
perimeter
stack-queue
explanation

Hyphens (-) are not allowed in variable names.

Question 3

State whether the following statement is True or False:
“A character variable can store only one character at a time.”

True
False
explanation

It is true that character variables can hold only one character at a time.

Question 4

Is there any error(s) in the following program?

#include <stdio.h>

int main() {
   int a, float b, int c;
   a = 2, b = 3.14, c = b * b;
   printf("%d %f %d", a, b, c);
   return 0;
}
Yes
No
I have no idea what’s going on
explanation

The first line in the main() function is erroneous. This is not the correct way to declare and define multiple variables in the same line. If a, b, and c are of same type (let say, int), then they can be separated by commas as follows:

int a, b, c;

But, if the variables are of different types, then they need to be separated by semicolons as follows:

int a; float b; int c;

The above definition is a valid definition.

In the second line, all three variables are initialized with some values, and it is totally fine to separate them by commas. So, the second line is not erroneous. The rest of the code is fine.

The updated program looks like the following:

#include <stdio.h>

int main() {
   int a; float b; int c;
   a = 2, b = 3.14, c = b * b;
   printf("%d %f %d", a, b, c);
   return 0;
}

Output:

Question 5

Determine the output of the following program:

#include <stdio.h>

int main() {
   int x = 10, y = 20, z = 30;
   x + z = y;
   printf("%d", x+z);
}
20
40
Compilation error
x + z
explanation

When we attempt to compile the provided code, we will encounter an error, especially when using Code::Blocks. The error message will be:

Error: lvalue required as left operand of assignment

This error occurs because of the statement “x + z = y;”. In this statement, “x + z” is considered an rvalue because it is an expression that evaluates to the constant value of 40. Constants do not have the ability to store values, so they cannot be used on the left side of an assignment statement. That’s why we receive the compilation error.

Your score is out of



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.