CPSC 124, Spring 2014
Sample Answers to Quiz #2
Question 1. Name three different primitive data types in Java. (Just list them.)
Answer. The answer can be any three of the following:
double float int long char short boolean byte
[Note that String is not a primitive type.]
Question 2. Explain the difference between the following two literals: 'a' and "a"
Answer. The first one, 'a', is a literal of type char while the second one, "a" is a literal of type String.
[As a char literal, 'a' represents the single character a stored in memory as a 16-bit Unicode code number. As a String literal, "a" represents an object that contains a character sequence of length 1 along with the usual collection of functions that apply to Strings.]
Question 3. What is meant by comments in a computer program? What are they for? Give an example of an actual comment that might appear in a Java program.
Answer. Comments are text that is added to a program for the benefit of human readers. They are used to provide information to human readers that will help them to understand the code. Comments are ignored by the computer. Two examples are:
/* This program will ask the user for a number and will print out the square root of the user's number. */ // the number that is entered by the user
Question 4. Fill in the following program's code to make it do the following: When the program is run, it will ask the user for a number and read the user's response. Then it will output the square root of the user's number. Use TextIO for input.
public class SquareRoot { public static void main(String[] args) { } }
Answer.
public class SquareRoot { public static void main(String[] args) { double number; // the number that is entered by the user double root; // the square root of the number System.out.print("Please enter a number: "); number = TextIO.getlnDouble(); root = Math.sqrt(number); System.out.println("The square root of " + number + " is " + root); } }