Head First Java Flashcards

1
Q

What should the first line of your program be in the main class?

A

public static void main (String[] args){

}

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

What are the 3 standard loop constructs in Java?

A

while, do while and for

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

How do you declare and give initial values to a three-element String array in Jave?

A

String[] pets = {“Fido”, “Zeus”, “Bin”};

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

What does random() return in Java? How do you use this?

A

It returns a number between 0 and just less than one. You use it by multiplying the max amount of your desired range by whatever random return to give you your random number.

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

In terms of adaptability, what is one of the biggest advantages of OOP?

A

You can accommodate new requirements without altering previously tested, delivered, and accepted code. There is no need to recompile the existing code.

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

What does the dot operator do in respect of a class?

A

It gives you access to the objects state and behavior i.e. the instance variables and the methods,

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

What’s the function of the main method? Why

A

It serves to test your classes and to start your program off. The real idea of OOP is to get classes talking to classes.

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

How do you write a global variable called totalValue in Java?

A

You don’t. There is no concept of global variables in Java.

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

What is a manfest text file?

A

It is a text file bundled with your application classes that defines which class in the zipped jat file holds the main method.

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

What are 2 types of variable?

A

Primitive and Reference

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

What are the four integer primitives in Java?

A

long, int, short, byte

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

What are the two floating-point primitives in Java?

A

float and double

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

What are the six types of integer and floating-point variables in Java?

A

float double

byte short int long

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

What values can be held in a byte, short, integer, and long?

A

Byte:c. -127 to 127
Short: c.-31k to 31k
Int: c. -2bn to 2bn
long - more than 2bn

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

How many primitive types are there in Java?

A

8

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

Name the primitive variable types in Java

A

char, bool, byte, short, int, long, float, double

17
Q

What does an object variable contain?

A

The address of the object - a reference to the place in memory

18
Q

Declare an empty integer array

Declare an integer array with 7 empty elements

A

int[] a

a = new int[7]

19
Q

Declare an array of Dog objects called pets

A

Dog[] pets;

20
Q

Declare an array of Dog objects called pets of length 7. What do we need to do to use the array?

A

Dog[] pets ;
pets = new Dog[7];
We have declared an array of 7 elements that will hold dog objects but, we have not declared any dogs to go into the array elements.

we could say e.g. pets[0] - new Dog();

21
Q

If we have an array which holds Dog objects (array called myDogs), how do we access methods of elements in the aray

A

myDogs[0].bark();

22
Q

Is it ok not to initialize local and instance variables?

A

Instance variables - yes, because they will always get a default value of 0, false or null. Local variables, no. You must initialize them before use.

23
Q

What’s the code to convert a string to an integer?

A

Integer.parseInt(“3”);

24
Q

Write a for each loop

A

for(int number : listOfNumbers){

}

25
Q

What does break; do?

A

Get’s you out of a loop immediately

26
Q

When we write test code, how do we decide what to test?

A

We test the important stuff in the method/class i.e. anything non trivial so e.g. a simple setter method wont need testing.

27
Q

Broadly, there are 7 steps to coding a method, what are they?

A
  1. Identity what it’s supposed to do - what goes in what comes out, how it gets from one to the other(the algorithm)
  2. List the class instance variables
  3. Write the prep code/pseudo-code
  4. Write the test code for the method
  5. Implement the method
  6. Test
  7. Debug
28
Q

We have an array myArray which has elements {1, 2, 3}. How do we add another element 7?

A

We can’t as arrays are fixed in size. An ArrayList is a better choice - an array that can vary in size as required.

29
Q

A subclass extends the superclass - what is this describing

A

Inheritance

30
Q

What does a subclass actually inherit from the superclass?

A

The instance variables and the methods

31
Q

How do you apply the IS-A test in inheritance?

A

If a class extends another class i.e. is a subclass of the superclass then we can say subclass IS-A superclass e.g. cat IS-A feline, cola IS-A drink. If you can’t do the IS-A test then you have got something wrong

32
Q

Where you have multiple methods in an inheritance tree e.g. doSomething(), which is invoked in a method call?

A

The lowest one in the tree

33
Q

How does a superclass control what a subclass gets access to or not in terms of variables and methods?

A

It uses public and private. Stuff that is marked private will not be inherited.

34
Q

We have supertype Animal and sub types cat, dog, hippo. How can we write efficient code that gets each of the animals to execute their own eat method?

A

We can use polymorphism. Declare an array of type Animal (call it e.g. animals)and put in there an object of type cat, dog and hippo. Iterate through the array and then say animals[i].eat(). The compiler will then access the correct eat method for each of the animals.

35
Q

Is it possible to declare a method parameter as a super class type? Why would we do this?

A

Yes. It is so that we can pass in objects of any of the sub class types and have methods appropriate to that sub class executed. People will also be able to add new subtypes and these methods will still work.

36
Q

If you have used inheritance but you absolutely want to make sure that no one sub classes and overrides a particular method - how do you do this?

A

You can mark that method as final in the super class. This prevents it from being overridden and ensures it works exactly the way you intended it to.

37
Q

What is an abstract class?

A

A class that cannot be instantiated.

38
Q

Why do we need abstract classes?

A

We do not want the class to be instantiated e.g. if we have an animal super class we can say Animal ani = new Animal() but this doesn’t make sense - what exactly does this animal look like? We don’t know- the intention was to create sub classes which borrow behaviour from this class and were flexible enough to have their own behaviour but the main super class was never intended for instantiation.

39
Q

How do we make a class called Canine that extends Animal an abstract class?

A
We add the word abstract before the class declaration. You can say:
Canine c = new Dog(); but you can't say
Canine c = new Canine()