CMSC 131 (Summer 2019) Week 02 Study Questions Flashcards
Week 02 Study Questions
When your Java program is compiled, what type of file is created? (Hint: It is NOT machine language.)
bytecode
What does it mean for someone to say that a Java program is “portable”?
The same bytecode will run successfully on any platform (provided that the Java Virtual Machine (JVM) is available.)
TRUE/FALSE: Inserting unnecessary spaces and/or blank lines could cause your Java program to malfunction.
FALSE
List the four Java primitive types that can be used to store integer values.
long, int, short, byte
How much memory is required to store each of the four types mentioned in the previous question?
long: 8
int: 4
short: 2
byte: 1
What advantage do you gain from using one of the types of integer types that requires MORE memory?
You can store a wider range of values (larger values).
List the two Java primitive types that can be used to store floating point values.
float, double
How much memory is required to store each of the two types mentioned in the previous question?
double: 8
float: 4
What advantage do you gain from using double instead of float?
more precision (it will retain more digits)
List the two Java types that are used to store values that are not numbers.
char and boolean
Write a statement that declares a variable named counter of type int, and stores the value 182 in the variable.
int counter = 182;
Write a statement that simultaneously declares three variables of type boolean, named x, y, and z.
boolean x, y, z;
What values can a boolean variable achieve?
true or false
Write a java class called “Fred”. Put in a main method. Have the main method store your age in a variable named age. Then main should print out a line that has your name, followed by your age. (Use a ?string literal? for your name, but use the variable to access your age.)
public class Fred { public static void main(String[] args) { int age = 12; System.out.println("Fawzi Emad, age is " + age); } }
Practice using BOTH styles for Java comments.
// comment using first style
/* comment using second style */
Is the following statement valid: int x = 34.7;
no
Is the following statement valid: double y = 12;
yes
Is the following statement valid: boolean q = 17 < 25;
yes
Evaluate the following Java expression: 9 - 15 * 6 / 11 + 4
5
Evaluate the following Java expression: 75 % 7
5
Never forget that you should not compare two Strings with the == operator. Suppose you have two String variables, x and y. Give an expression that can be used to check whether or not the Strings x and y are identical.
x.equals(y)
What is “concatenation”? What operator do you use to concatenate Strings?
+
What is the difference between System.out.print and System.out.println?
System.out.println moves the cursor down to the next line after it does it’s output.
Suppose you have a String variable called s. What expression will return the number of characters in the String?
s.length()
Give examples of “literals” of each of the following types: String, char, long, int, float, double, boolean.
“hi”, ‘x’, 32L, 16, 15.7F, 373.2, true
What “escape sequence” would you use in a String to indicate a “new line”?
\n
What is the difference between “syntax errors” and “logical errors”?
Syntax errors are due to your failure to follow the rules of the language. Logical errors are where you have written a valid Java program, but it does not behave the way it should.
If your program compiles and runs, but behaves incorrectly, are you probably suffering from “syntax” or “logical” errors?
logical
If Eclipse flags your code with a red mark and won’t let you compile it, are you suffering from “syntax” or “logical” errors?
syntax