CPSC 124, Fall 2001
Quiz #1, September 10This is the first quiz in CPSC 124: Introductory Programming.
Question 1: Explain the difference between high-level language and machine language.
Answer: Machine language is the native language of a computer. Machine language instructions are binary numbers that are executed directly by the computer hardware. Generally, computer programs are actually written in high-level languages, which use syntax closer to English. A program written in a high-level language must be translated into the computer's machine language before it can be executed by that computer.
Question 2: What is Java "bytecode"? What kind of file is it found in?
Answer: The Java compiler translates programs written in Java into Java bytecode, which is stored in ".class" files. The .class files can then be executed by the Java interpreter. So, Java bytecode is an intermediate step between Java source code and machine language. (Java bytecode is almost as simple as machine language. It is considered to be a machine language for the "Java virtual machine," a computer that doesn't really exist. It must still be translated into the machine language of a real computer before it can be executed. That's what the Java interpreter does.)
Question 3: Explain carefully how a computer executes the assignment statement "A = 3 * B;". What steps does it go through? What exactly does the computer do with the variables A and B?
Answer: To execute the statement "A = 3 * B;", the computer gets the value from the variable named B. It multiplies this value by 3. It then stores the result of this computation in the variable named A. (For this statement to be legal in a program, A and B must have been declared earlier in the program. Furthermore, B must have been assigned a value. Variable A might or might not have been given a value previously. In any case, the previous value of A is not used in this statement. The new value of A will replace any previous value.)
Question 4: Write a Java statement that will output a line like
Here is a random number: 0.2387450017771where the number that is printed is a random number.
Answer: System.out.println( "Here is a random number: " + Math.random() );
Question 5: Why is the char literal 'F' written with quotation marks, rather than simply as F?
Answer: Without the quotes, the computer would think that F was the name of a variable. (With the quotes, 'F' represents the literal value F, of type char.)