CPSC 124, Spring 2021: Sample Answers to Quiz #??
These are sample answers only.
Other answers might receive full credit.
Question 1. Give the names of two of Java's primitive types.
Answer. int and char.
(There are only 8 primitive types, and those 8 types are the only correct answers: byte, short, int, long, float, double, char, boolean. Note that all primitive type names start with lower case letters. Note that String is a type, but it is not a primitive type.)
Question 2. What is meant by a literal in a program, and what is the difference between a literal and its value. (An example might help.)
Answer. A literal is a sequence of characters in a program that represent some constant value. For example, 'A' is a literal of type char that represents the character A. When the program is compiled, what's actually in the program is the integer 65 expressed as a 16-bit binary number: 0000000010000001. The quote marks in 'A' are part of the literal—you need to type them into your program to express the value—but they are not part of the value that is represented by the literal.
And 17 is a literal of type byte that is stored in the computer as the 8-bit binary number 00010001 "Hello" is a literal of type String that represents the string value consisting of the letters H, e, l, l, and o, actually stored in the computer as a sequence of binary number. Again, the quotation marks are not part of the value.
Question 3.
Give the value of each of the following expressions.
Assume that str is a variable of type String and that its
value is "programming"
.
str.length() is ______________________ str.charAt(4) is ______________________ str.substring(3,7) is ______________________
Answer.
str.length() is 11 str.charAt(4) is 'r' str.substring(3,7) is "gram"
(This question is about the specific string "programming", which is the value of the variable str. There are eleven characters in "programming", numbered from 0 to 10, so the answer to the first part is 11. Character number 4 is 'r', so that is the answer to the second part. (I include the quote marks, since the value is of type char.) The value of str.substring(3,7) is a substring of "programming" consisting of character number 3 through character 6. Note that character number 7 is not included. More generally, str.substring(start,end) is the substring starting with the character at index number start and ending with the character at index number (end−1). Note that if start is 0, then end is the number of characters in the substring. The number of characters in the substring is always (start−end).)
Question 4. Add some Java code to the following program outline, so that it will do the following: Ask the user to enter a number, read the user's input, and print the square root of the number.
import textio.TextIO; public class FindRoot; public static void main(String[] args) { } }
Answer.
import textio.TextIO; public class FindRoot; public static void main(String[] args) { double input; // The user's input number. double root; // The square root of the input number. System.out.println("Enter a number:"); input = TextIO.getlnDouble(); root = Math.sqrt(input); System.out.println("The square root is " + root); } }