Chapter 2 Flashcards
Which of the following are valid java identifiers?
A. _ B. _helloWorld$ C. true D. java.lang E. Public F. 1980_s G. _Q2_
B, E, G
- Single underscore is not allowed.
- It cannot be a java keyword (case sensitive)
- Dot is not allowed
- It cannot start with a number
What is an instance initializer?
A code block that is inside a class but outside a method.
What is the output?
public class Egg { public Egg() { number = 5; } public static void main(String[] args) { Egg egg = new Egg(); System.out.println(egg.number); } private int number = 3; { number = 4; } }
The output is 5.
Fields and instance initializers are run in the order in which they appear in the file.
The constructor runs after all fields and instance initializer blocks have run.
Java has 8 built in data types. Which are they?
boolean, byte, short, int, long, float, double, char
What is the size of the primitive type ‘boolean’?
1 bit
What is the size of the primitive type ‘byte’?
8 bits / 1 byte
What is the size of the primitive type ‘short’?
16 bits / 2 bytes
What is the size of the primitive type ‘int’?
32 bits / 4 bytes
What is the size of the primitive type ‘long’?
64 bits / 8 bytes
What is the size of the primitive type ‘float’?
32 bit / 4 bytes
What is the size of the primitive type ‘double’?
64 bits / 8 bytes
What is the size of the primitive type ‘char’?
16 bits / 2 bytes
True or false
A float does not require the letter ‘f’ following the number in Java.
False.
The letter ‘f’ is required so Java knows its a float.
A byte can hold a value from … to …
-128 to 127
What is the difference between a short and a char?
Short and char are closely related, as both as stored as integral types with 16 bit length. The difference is that short is signed (which means it can hold positive and negative numbers) and char is unsigned (which means it can only hold positive numbers, including 0).
When writing a numeric literal, what data type does java assume you are writing?
An int. This is why we need to specify an ‘L’ after the number if it should be a long.
Java allows other formats other than the decimal system to write numbers. Name the following formats:
- 017
- 0xFF
- 0b10
- Octal
- Hexadecimal
- Binary
Explain why the following does or does not compile:
- 1000_.00
- _1000.00
- 1_00_0.0_0
- 1000.00_
- 1_____2
- Does not compile. Underscore cannot be next to decimal point.
- Does not compile. Underscore cannot be at the start.
- Compiles fine.
- Does not compile. Underscore cannot be at the end.
- Compiles file.
Which if these can compile?
- int value = null;
- String value = null;
String value = null compiles fine.
A primitive data type cannot be assigned null.
Does this code compile or not? Why?
String reference = “Hello”;
int len = reference.length();
int len2 = len.length();
Reference types can be used to call methods but primitives to not have methods declared on them. Thus len.length() is not valid.
An identifier is the name of a variable, method, class, interface, or package. There are 4 rules for legal identifiers. What are they?
- Identifiers must begin with a letter, a $ symbol, or a _ symbol.
- Identifiers can include numbers but not start with them.
- A single underscore _ is not allowed
- A java reserved word is not allowed (case sensitive)
Whcih of these identifiers are legal?
- someidentifier
- hollywood@vine
- __SomeIdentifier$
- 3DPointClass
1 and 3.
@ is not allowed and an identifier cannot start with a number.
Which of the following are legal/illegal declarations? why?
- boolean b1, b2;
- String s1 = “1”, s2;
- double d1, double d2;
- int i1; int i2;
- int i3; i4;
- legal. It declares two variables without initializing them.
- legal. It declares two variables and initailized only one of them.
- not legal. they must share the same type declaration and not repeat it: ‘double d1, d2’ is legal.
- legal. Although they are seperate statements (;).
- not legal. The second statement doesn’t have a type.
What is a local variable?
A variable defined in a constructor, method or initializer block.