Docsity

C++ Identifiers and Variables: Naming Conventions and Usage, Study notes of Computer Science

Toggle sidebar
bg1
True False:
Comment required.
1. In C++ the variables Alpha, ALPHA and AlphA are the same identifier.
Answer: False.
Explanation: C++ is case sensitive, these are different identifiers.
2. In C++ the compiler will infer the type intended for a variable from the context in
which the variable occurs.
Answer: False.
Explanation: C++ requires every identifier to be declared prior to use. The type is
specified in the declaration.
3. The range of values for an int variable is from about 0 to +2 billion.
Answer: False:
Explanation: The correct range is about –2 Billion to +2 Billion.
4. The names x, y, and z are satisfactory variable names for the lengths of the legs and
hypotenuse of a triangle.
Answer: False.
Explanation: Names should communicate to the human reader the meaning of the value.
These identifiers do not communicate the meaning of their values..
5. Explanation: In general assigning a floating point value to an integer variable mostly
loses information. A warning message (or an error message) is issued by C++
compilers.
6. C++ uses only /* */ for comments.
Answer: False.
Explanation: C++ uses /* */ comments and // “here to the end of the line”
comments.
7. A program’s comments should connect the program code to the problem being
solved.
Answer: True.
Explanation: The purpose of comments in a program is to help the human reader of the
code to connect the program to the problem being solved. Comments are so important
pf3
pf4
pf5

Partial preview of the text

Download C++ Identifiers and Variables: Naming Conventions and Usage and more Study notes Computer Science in PDF only on Docsity!

True False: Comment required.

  1. In C++ the variables Alpha, ALPHA and AlphA are the same identifier. Answer: False. Explanation: C++ is case sensitive, these are different identifiers.
  2. In C++ the compiler will infer the type intended for a variable from the context in which the variable occurs. Answer: False. Explanation: C++ requires every identifier to be declared prior to use. The type is specified in the declaration.
  3. The range of values for an int variable is from about 0 to +2 billion. Answer: False: Explanation: The correct range is about –2 Billion to +2 Billion.
  4. The names x, y, and z are satisfactory variable names for the lengths of the legs and hypotenuse of a triangle. Answer: False. Explanation: Names should communicate to the human reader the meaning of the value. These identifiers do not communicate the meaning of their values..
  5. Explanation: In general assigning a floating point value to an integer variable mostly loses information. A warning message (or an error message) is issued by C++ compilers.
  6. C++ uses only /* / for comments. Answer: False. Explanation: C++ uses / */ comments and // “here to the end of the line” comments.
  7. A program’s comments should connect the program code to the problem being solved. Answer: True. Explanation: The purpose of comments in a program is to help the human reader of the code to connect the program to the problem being solved. Comments are so important

there is an aphorism, often quoted by expert programmers: “If the comments and the code disagree, then both are probably wrong.

  1. A program should have a comment on every line. Answer: False. Explanation: This would be satisfactory only on a very complicated assembly language program for an inexperienced programmer.
  2. A computer program is a set of instructions intended for only the computer to follow. Answer: False. Explanation: A computer program is also intended for human beings to read, and the comments are intended to make this job easier. Fill in the blank
  3. In C++, a legal identifiers may contain these kinds of characters _______________, ______________, ______________ Answer: Letters, digits, underscore Explanation: Some implementations allow a very few other characters such as $.
  4. In C++, a variable that has been defined but not initialized may not be used as an ______. ( l-value or r-value ). Answer: r-value Explanation: The value of an uninitialized variable may not be fetched. C++ does not enforce this. If an uninitialized variable’s value is fetched you get whatever value was left in the memory location by a previous user. Note that the value is garbage in the dictionary sense, It is not a random value. Multiple Choice
  5. In C++, some of the following are legal identifiers. Which? Why? a) 9xyz b) Xyz c) X+yz d) xy_z

a) defined b) initialized c) used in some expression d) begin with a capital letter e) contain only letters, digits and underscores. Answer: a) defined.

  1. A l-value is a) an expression that can be only be placed on the left of any operator such as +, * , /, etc. b) assigned a value c) can never have a value fetched from it d) is designed for use by a left-handed person Answer: b) an l-value. Typo should be Answer b) assigned a value Explanation: An l-value may be assigned a value.
  2. An r-value is a) an expression that can be only placed on the right of any operator such as +, *, / etc. b) can never be assigned a value c) can have a value fetched from it d) is designed for use by a right-handed person. Answer: c) an r-value Explanation: An r-value can have a value fetched from it.
  3. In C++, a variable that has been defined but not initialized may a) be used in any way any identifier can be used. b) not be used at all c) not be used as an l-value d) not be used as an r-value e) have its value fetched prior to assignment. Answer: c) is correct.

Explanation: The value of an uninitialized variable may not be fetched, but a value may be assigned to an uninitialized variable.

  1. The if, while and for statements control only one statement. Answer: True Explanation: The one statement may be a block (statements that are enclosed with curly braces { }) or a simple statement.
  2. Suppose we have these declarations, int x = -1, y = 0, z = 1; This Boolean expression is correct and it does what the programmer intends. x < y < z Answer: False Explanation: Unfortunately, the expression compiles without error and runs. The < operator associates (groups) left to right, so the expression evaluates as (x < y) < z The left hand expression evaluates to true, which, if compared to a numeric type, converts to 1. When compared to 1, this is false. What the programmer intends, expressed as mathematacs might is -1 < 0< 1, a result that is clearly true.
  3. The value of count is 0; limit is 10. Evaluate: (count == 0)&&(limit < 20) Answer: true
  4. The value of count is 0; limit is 10. Evaluate: count == 0 && limit < 20 Answer: true Explanation: The operators == and < have higher precedences than &&, hence the expressions, count == 0 and limit < 10 are evaluated (to true) then the && is executed.
  5. The value of count is 0 ; limit is 10. Evaluate: (count != 0)||(limit < 20) Answer: true.