CPSC 124, Spring 2014
Sample Answers to Quiz #1
Question 1. What is a variable (as the term is used in computer programming)?
Answer. A variable is a name for a location in memory where values can be stored. A variable can be used in a program to refer to its value. (In Java, a given variable can only hold one type of value.)
Question 2. An important part of a computer is the main memory (or RAM), which holds programs and data. What is the structure of main memory? (What is it made up of?)
Answer. Main memory consists of a numbered sequence of locations. Each location holds an 8-bit binary number. A location has an address, which is its numerical position in the list. An address is used to specify which address to use. For example, a variable has an address, which tells where in memory the value of that variable is stored.
Question 3. What is accomplished by the command javac Foo.java and what does "Foo.java" have to be?
Answer. "Foo.java" must be the name of text file that contains Java source code. This command compiles the Java source code (and checks it for syntax errors). The result is a file named Foo.class that contains the compiled program. That is, the Java code has been translated into the machine language of the Java Virtual Machine. Once a Java program has been compiled by the javac command, it can be run by the java command.
Question 4. Explain the term syntax, and give an example of a syntax rule for Java.
Answer. Syntax is grammar. Syntax rules determine what is "legal" in a program. A program that has syntax errors cannot be compiled. A program that is syntactically correct can be compiled and run (but might still contain semantic errors).
Some examples of syntax rules for Java: The name of program must be made up only of letters, digits, and the underscore character and must start with a letter or underscore. A variable must be declared before it can be used. A literal of type char must be enclosed in single quotes. An assignment statement consists of a variable, a "=", something that can be evaluated to get a value, and a semicolon.