Learn Java In One Day Flashcards

1
Q

What are the 8 primitive data types in Java?

A

Byte
Short
Integer
Long

Float
Double

Char
Boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What data range does a byte hold?

A

-128 to 127

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How much storage space does a byte use?

A

1 byte

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What range(Approx) does a short hold and how much space does it use?

A

-32k to 32k

2 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What range(Approx) does an int hold and how much space does it use?

A

-2.14 bn to 2.14bn

4 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What range(Approx) does a long hold and how much space does it use?

A

Less than 2.14bn to more than 2.14bn

8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the precision of a float?

A

7 digits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How much memory does a float use?

A

4 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the precision of a double?

A

15 digits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How much memory does a double use?

A

8 bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which primitive data types require a suffix in order to be used. What are the suffixes?

A

Long and Float.

L and F

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are two good practices to follow when naming variables?

A

The name should be:

Meaningful
Convey Intent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Camel Casing is used for variables in Java. What is that?

A

Capitalise the first letter of each word except the first e.g. thisIsGreat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the difference between these statements:
System.out.println(counter++);
System.out.println(++counter);

A

In the first, Counter is printed then incremented.

In the second, Counter is incremented and then printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In Java, 7/2 = ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is type casting?

A

It is where we convert from one data type to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is widening primitive conversion?

A

This is where we convert from a smaller data type to a larger one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Declare an integer array called userAge

A

int[] userAge;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Declare an integer array of 5 empty elements

A

int[] anArray = new int[5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Declare an integer array and initialise it with three integers

A

int[] anArray = {1,2,3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can we quickly sort an array?

A

Use Arrays.sort(array name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What’s the syntax for a divide and conquer search of an array in Java?

A

binarySearch()

E.g. int foundIndex2 = Arrays.binarySearch(myInt, 39);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

If the Arrays.binarySearch method cannot find what you’re looking for what will the result be?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Which algorithm does the Arrays.sort method use?

A

Quicksort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you find the length of a string versus the length of an array?

A

stringname. length() gives the length of a string

arrayname. length gives the length of the array (note, no parenthesis after length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are the two types of data in Java?

A

Primitive types of which there are 8 and reference types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What are three methods to print to the screen in Java?

A

print()
println()
printf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

When would you use these - what do they do?

%.3f
%d 
%.2f
%n
%8.2f
%,n
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What library do we have to import to use array functions?

A

import java.util.Arrays;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What’s the import for taking user input library?

A

import.java.util.Scanner;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Write code that will prepare for input to be received?

A

Scanner userInput = new Scanner(System.in);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

There are two forms of if statement in Java, what are they? Give an example of each.

A

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”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Give an example of a switch statement.

A
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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

when using the Scanner object, how do you get integer, floating number, and text input?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What are the four commonly used looping statements in Java?

A

For, enhanced for, while and do while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What does the for statement do in Java?

A

The for statement executes a block of code repeatedly until the test condition is no longer valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the syntax for the enhanced for loop?

A

for (variable declaration : name of array) { }

38
Q

What is a while statement?

A

A while statement repeatedly executes instructions inside the loop while a certain condition remains valid.

39
Q

What’s the syntax of the do while loop?

A

do {
System.out.println(“Counter = “ + counter);
counter++;
} while (counter<0);

40
Q
What's wrong with this code?
do { 
System.out.println("Counter = " + counter); 
counter++; 
} while (counter<0)
A

The semicolon if missing after the wile condition -it should be:
do {
System.out.println(“Counter = “ + counter);
counter++;
} while (counter<0);

41
Q

When used in a loop what is the difference between a break and continue statement?

A

Break exits the loop entirely but continue stops the current iteration of the loop

42
Q

What’s the syntax for a try catch block

A
try{
//do stuff
}catch(type of error)(
//do this if try didnt work
}finally{
//do this regardless - this is optional
}
43
Q

What’s the syntax for the length of a string?

A

myString.length();

44
Q

What’s the syntax to convert to lower and upper case?

A

myString.toLowerCase();

myString.toUpperCase();

45
Q

How do you get part of a String?

A

myString.subString(starting Index, end index(optional));

46
Q

How do we get the character at a specific location in a String?

A

mtString.charAt(index);

47
Q

Compare two strings.

A

string1.equals(string2);

48
Q

Split a string by spaces. What does this return? What other split options are there?

A

myString.split(“ “)
This returns an array of strings.
Other options - any character

49
Q

What does the equals() array method do?

A

Compares whether two arrays have the same length and array elements in the same places i.e. are copies of one another

50
Q

How do you call a static method?

A

name of class.name of method(argumentss);

51
Q

Given the following array, how do we copy from elements 3 to 6 (inclusive) into another array?

int [] source = {12, 1, 5, -2, 16, 14, 18, 20, 25};

A

int[] dest = Arrays.CopyOf(source, 2, 6)

Note the question asked elements and not indexes, otherwise, we would have said 3,7

52
Q

How do we convert an array to a string?

A

We can use the Arrays method Arrays.toString(arrayName)

53
Q

What’s the function to sort an array? What does it return?

A

Arrays.sort(arrayname)

It returns the original array sorted i.e. it modifies the original array

54
Q

What type of sort does the Arrays.sort() method use?

A

It uses a quick sort.

55
Q

How do you search an array in Java?

A

Arrays.binarySearch(arrayName, value being searched for)

56
Q

How can you tell from the output if Arrays.binarySearch has failed to find the value

A

The return value will be a negative number - the negative part and the number - the number informs at what index the number should have been in the array

57
Q

How do we find the length of an array?

A

arrayname.length

Note there are no brackets after the method name

58
Q

what’s the syntax of the ternary operator?

A

condition?valueiftrue:value of false

59
Q

What’s the syntax for a switch statement?

A
switch(variable used for switching){
case firstCse:
     do some stuff
     break; (breakout of this construct, otherwise the next 
     cases will execute.
}
60
Q

What’s the difference between break and continue?

A

Break gets you out of the loop altogether, continue gets you out of that iteration.

61
Q

if we have aNumber with a value of 2010.321548, how do we print this as a decimal to three decimal place with a thousands separator and ocupping 7 spaces?

A

printf(“%,7.3f

62
Q

What’s the syntax of a try catch block

A
try{
}catch{
} finally{
this bit regardless of whether the try or catch was executed
}
The finally is optional
63
Q

In the catch part of a try catch, if we write catch(Exception e), what does this mean?

A

If means that we want the system to return a general error in case of an error. e is the object and we can say in the catch block something like:

System.out.println(e.getMessage());

which will print the general error message e.g. division by zero.

64
Q

Which exception would you use to catch an input error - where the input is not of the expected type

A

InputMismatchException e

65
Q

Which exception would you use to catch an Array out of bounds error - where one attempts to access an array element out of the specified range

A

ArrayIndexOutOfBoundsException e

66
Q

What is ‘throwing and exception’?

A

It is where we define our own rules for when an exception should be thrown

67
Q

How can we throw an exception when using try, catch?

A
In our main code, writ something like:
if (choice == 0) throw new ArrayIndexOutOfBoundsException();

and then make sure you have a catch with the ArrayIndexOutOfBoundsException() exception. Once the exception in the main code is triggered it will automatically go to the catch for exectution.

68
Q

What are the two access levels for a class? What is the default?

A

The two are public and Package-private. The default is Package-private which means only classes within the same package have access to it.

69
Q

What are the access levels for a field? What if we don’t choose one?

A

public, private, and protected.

If we don’t choose then it is package-private i.e. public outside the class

70
Q

What’s wrong with this code?

private final int hourlyRate;

A

We have declared the variable to be final but we have not initialized it.

71
Q

What does a constructor do and how is it declared?

A
public theclassname( parameters){
}

Upon initializing the class, we can use it to initialize variables and to perform some actions.

72
Q

What’s the point of a static method or field?

A

You can use them without instantiating the class to which they belong. One simply has to use the class name, dot and then the name of the field or method.

73
Q

Write a line which declares a method - it returns an integer array and takes an integer array.

A

public int[] myMethod(int[] a){

}

74
Q

In inheritance, what properties and methods are inherited by a child class?

A

Public and protected - not private

75
Q

How would you call a non parameterless constructor in a parent class?

A

Use the super keyword so, you write the constructor as normal and, within its body, you use the super keyword so you will say
super(the variables to pass into the constructor)

76
Q

How do we override a method in a parent class

A
Use the following
@Override
same method name and parameters as in parent{
     different logic
}
77
Q

What is polymorphism? What does it relate to?

A

It refers to a program’s ability to use the correct method for an object based on its run-time type.

78
Q

What is an abstract class? What is its primary use?

A

It is a class that cannot be instantiated. It is used solely as a base for other classes to derive from.

79
Q

What are the two types of method that an abstract class can have?

A

It can have normal methods and it can have abstract methods - those that have no body and which must be implemented in the derived class

80
Q

Write an abstract public method called calculateFee

A

abstract public void calculateFee();

81
Q

In which subclasses does an abstract method need to be implemented?

A

All of the child classes

82
Q

What are the 4 access modifiers in Java?

A

private
public
protected
package-private

If an access modifier is not stated, it is taken to be package-private i.e. public within the package

83
Q

If something is declared without access modifies, what is the default and what is its effect?

A

package-private

The item is only accessible within the package in which it was declared.

84
Q

What type of data does an arraylist hold?

A

It can only hold object types. Use Integer to hold int values as this holds an int as an object

85
Q

Declare an arraylist of integers

A

ArrayList myList = new ArrayList<>0;

86
Q

How do we add members to an arraylist?

A

Use the add() method?

87
Q

How does one add a member to an arraylist at a specific location?

A

listname.add(index,itemtoadd);

88
Q

How does one replace an element at a certain index in an arraylist?

A

listname.set(index, value)

89
Q

What does arraylist.get() do?

A

Retrieves an item a the index

90
Q

How does one remove an element from an Arraylist?

A

arraylist.remove(indexNo)

91
Q

I want to know at what index number in an arraylist a certain value appears - how do i do this?

A

arraylist.indexOf(value)

92
Q

When is it best to use linkedlists and arraylists

A

Linkedlists are best when you have a need to frequently add and delete stuff from the list. It is quicker to manipulate data

Arraylist is best when you need good search capability.