Learn Java In One Day Flashcards
What are the 8 primitive data types in Java?
Byte
Short
Integer
Long
Float
Double
Char
Boolean
What data range does a byte hold?
-128 to 127
How much storage space does a byte use?
1 byte
What range(Approx) does a short hold and how much space does it use?
-32k to 32k
2 bytes
What range(Approx) does an int hold and how much space does it use?
-2.14 bn to 2.14bn
4 bytes
What range(Approx) does a long hold and how much space does it use?
Less than 2.14bn to more than 2.14bn
8 bytes
What’s the precision of a float?
7 digits
How much memory does a float use?
4 bytes
What’s the precision of a double?
15 digits
How much memory does a double use?
8 bytes
Which primitive data types require a suffix in order to be used. What are the suffixes?
Long and Float.
L and F
What are two good practices to follow when naming variables?
The name should be:
Meaningful
Convey Intent
Camel Casing is used for variables in Java. What is that?
Capitalise the first letter of each word except the first e.g. thisIsGreat
What’s the difference between these statements:
System.out.println(counter++);
System.out.println(++counter);
In the first, Counter is printed then incremented.
In the second, Counter is incremented and then printed
In Java, 7/2 = ?
3
The result is an integer. The result is truncated after the decimal point. Change to 7.0/2 to get a floating point result
What is type casting?
It is where we convert from one data type to another
What is widening primitive conversion?
This is where we convert from a smaller data type to a larger one
Declare an integer array called userAge
int[] userAge;
Declare an integer array of 5 empty elements
int[] anArray = new int[5]
Declare an integer array and initialise it with three integers
int[] anArray = {1,2,3}
How can we quickly sort an array?
Use Arrays.sort(array name)
What’s the syntax for a divide and conquer search of an array in Java?
binarySearch()
E.g. int foundIndex2 = Arrays.binarySearch(myInt, 39);
If the Arrays.binarySearch method cannot find what you’re looking for what will the result be?
A negative and a number that tells you where the thing should have been i.e. it should be at that index number minus 1.
Which algorithm does the Arrays.sort method use?
Quicksort
How do you find the length of a string versus the length of an array?
stringname. length() gives the length of a string
arrayname. length gives the length of the array (note, no parenthesis after length
What are the two types of data in Java?
Primitive types of which there are 8 and reference types.
What are three methods to print to the screen in Java?
print()
println()
printf()
When would you use these - what do they do?
%.3f %d %.2f %n %8.2f %,n
Use these with printf. They format the output.
%.3f display the result to three decimal places
%d display an integer
%.2f display the result to two decimal places
%n newline
%8.2f the output should take up 8 spaces and be to two decimal places
%,n display the number with a thousands separator
What library do we have to import to use array functions?
import java.util.Arrays;
What’s the import for taking user input library?
import.java.util.Scanner;
Write code that will prepare for input to be received?
Scanner userInput = new Scanner(System.in);
There are two forms of if statement in Java, what are they? Give an example of each.
Normal if:
if (condition){ //do some stuff }//an else or else if can go here if we want
Excel style if - the ternary operator
condition ? value if true: value if false;
results = 3>2?”Yes”:”No”;
Give an example of a switch statement.
switch (userGrade) { case "A+": case "A": System.out.println("Distinction"); break; case "B": System.out.println("B Grade"); break; default: System.out.println("Fail"); break;
when using the Scanner object, how do you get integer, floating number, and text input?
Say Scanner object is called userInput, use
userInput.nextInt();
userInput.nextDouble();
userInput.nextLine();
There are others e.g. next() for a single word and nextByte()
What are the four commonly used looping statements in Java?
For, enhanced for, while and do while
What does the for statement do in Java?
The for statement executes a block of code repeatedly until the test condition is no longer valid.