Study Guide 1 Answers

  1. a. True
  2. a. True
  3. b. statement
  4. a. expression
  5. d. definition
  6. c. declaration
  7. b. False. The char data type stores 8-bits (a wchar stores 16-bits). A char is signed on some computer hardware and unsigned on others. On signed systems, a char can store values in the range of -128 to 127; on unsigned systems, it can store values in the range of 0 to 255.

Questions 10 - 16

These questions are my attempt to focus your attention on the often subtle details of different programming elements. Compilers (for Java, C++, and other languages) rely partly on where symbols are used in a program to determine what the symbols mean. We say that the symbols are "context sensitive" - that is, the meaning depends on the context in which the symbols are used.

This set of questions removes the symbols from their normal context, depriving us of some information. Nevertheless, if we focus on the given information and apply some knowledge about how a given language (C++) deals with data and data types, we can still tell a bit about the symbols. For example, all expressions have a data type. There are many data types that can represent 5 - char, short, int, float, and double - but what type does the compiler use to represent a given constant? When you read code, you need to glean all available information.

Most of the answers can be found in Program Data.

  1. a. integer constant. 12 is an integer constant. Both C++ and Java treat numbers that don't have a decimal point or period in them as type int.
  2. b. character constant. 'a' the single quotation marks make this a character constant (without the quotation marks it would be a variable).
  3. g. a string constant or a string literal. The double quotation marks make this a string constant (also known as a string literal). Although 'a' and "a" look almost identical in a program, their storage in a computer and the way the computer uses them are very different.
  4. d. a double constant. 4.28915 is a numeric constant of type double. Yes, it would fit in a float, but whenever the compiler processes a numeric constant that has a decimal point (a period), its data type is automatically set as double (Java follows this same rule).
  5. e. a variable name or identifier. All we really know about FooBar is that it's an identifier. The naming convention for both C++ and Java is that symbolic or named constants are written with all upper case letters - so this eliminates all of the constants (see Symbolic Constants). FooBar could also be the name of a class (and follows the Java naming convention for a class), but that wasn't one of the choices. It might also be a function name, but the next question should clarify that situation. So, we're left with a variable name or identifier.
  6. f. a function name. The parentheses are the key: they make this into a function call, and so FooBar must be the name of a function. We'll cover functions in detail in a month or so, but C++ functions are basically the same as Java's methods (see General Method and Function Calls).
  7. a. an integer constant. 0xAF is an integer constant expressed in hexadecimal or base-16. Both C++ and Java express integers in hexadecimal by beginning the number with 0x or 0X.
  8. c. There are too many characters inside the quotation marks - single quotation marks denote a single character, the spaces are not allowed: ' x '
  9. a. There is nothing wrong with the expression. The escape sequence \n represents a single, new-line character
  10. d: counter_2_index. Identifiers must begin with an alphabetic character; subsequent characters may be alphabetic (which includes the underscore) or numeric. Other characters are not allowed.
  11. b. Look for header1 where system header files are stored and header2.h in the current directory
  12. b, c, and d: the preprocessor, the compiler component, and the linker (aka the loader on macOS and Linux)
  13. c.
  14. a.
  15. Every discipline has its own vocabulary or terminology. You need to focus on and learn that terminology as it allows you to communicate quickly and clearly with other computer scientists.

    The data type serves two important purposes:

    1. It informs the compiler of how much memory to allocate to hold a variable or data - different kinds of data use different amounts of space: char uses one byte, int typically uses 4 bytes, a double 8 bytes, etc.
    2. It tells the computer how to interpret the string of 1's and 0's (all data, regardless of type, is a string of 1's and 0's in memory) stored in a variable. Although an integer 1 and a double 1.0 look similar, their bit-patterns are completely different.

  16. (1) define, (2) initialize, and (3) use. Most variables are not automatically initialized - until they are initialized, they contain a random pattern of 1's and 0's (often called garbage). Variables must be defined and initialized before they can be used in any calculation. Variables can be initialized with the assignment operator or with an input statement. For example:
    int x;
    cin >> x;
    int sum = 0;
    sum += x;
    We didn't initialize x when we defined it because the input operation initializes it. (It wouldn't have hurt to initialize x in the definition, but it would have been a wasted step.) sum was initialized to 0 so that it is ready for use by the following += operation (sum is being used as an accumulator - a term introduced in chapter 3).
  17. b. False. The computer allocates memory to store static variables when it loads a program into memory, and that memory remains allocated until the program terminates. However, static variables still follow all scoping rules, and describing and understanding the implications requires us to be very precise in what we say. When we say that we can "access a variable," we usually mean by name, which we can only do if the name is in scope. The variable's contents remain intact even when the name goes out of scope. So, if we create an alias for the variable in another scope (which we will see how to do in chapters 4 and 6), we can access the variable's contents using the alias. However, the question stated, "That means that the variable name can be used anywhere in the program," which is false.
  18. double total;. Take note of the semicolon; both C++ and Java terminate each statement with a semicolon.
  19. int sum = 0;. Again, notice the semicolon that terminates each statement.
  20. c.