What is computer science?
Understanding how computers and information technology work and applying that knowledge
Why is CS important?
Fundamental to the management of existing systems and development of new systems
Give examples of application domains
Logistics Health care Government Finance Critical Systems Education Entertainment
Define Hardware
Physical part of a computer system
Define software
Instructions that tell the computer what to do
Give some desired qualities of software
Correct
Well designed
Well documented
Easy to understand and integrate
What is a program?
A set of instructions to the computer
What is the name of the method that a Java program executes first
Main
What is object orientation?
A style of programming based on objects, which represent things of relevance to the problem
What does documentation tell us in regards to OO programming?
The purpose of the objects
What methods are avaliable, what they do, what parameters they need if any
What does JVM stand for?
Java Virtual Machine
What is the difference between a class and an object?
Class is a blueprint for an object and an object is a specific instance of a class
What does the this keyword refer to?
The current object
Give examples of primitive types
Numbers, boolean values
What is the default value for reference types?
Null
What are the default values for
a) numbers
b) boolean
a) 0
b) false
What is abstraction?
In programming this means a solution can be expressed at various different levels
Give examples of different levels of abstraction
Binary, low level, high level
What is the software development process
A scheme for producing a piece of software from an initial specification
What is java bytecode executed by?
Java Virtual Machine
What is a variable?
A piece of information with a name
What do variable declarations consist of?
Type
Name
Optionally initial value
What is used to represent assignment in java?
=
What is a type in Java?
A type is a description of the kind of thing a value is
What are the two kinds of type in Java?
Primitive
Reference
What is a key difference between primitive and reference types?
How they are stored
Describe byte
Signed Integer
8 bits
-128 and 127
Describe char
Unicode Character
16 bits
Describe short
Signed Integer
16 bits
-32,768 to 32,767
Describe int
Signed Integer
32 bits
Describe long
Signed Integer
64 bits
Describe float
Floating Point number
32 bits
Describe double
Floating Point number
64 bits
What is a literal?
Representation in a Java Program of a fixed value
How many decimal values of precision does float have?
7
How many decimal values of precision does double have?
14
What are the 2 parts that reference types are held in?
Reference
Object
What is an identifier?
An unlimited series of letters, numbers and underscores
MUST BEGIN WITH A LETTER
Give a few examples of keywords
Abstract
Class
Finally
Throws
What is scope?
The certain restricted part of the program that an identifier is only valid within
Describe the scope of a variable
From the end of its declaration to the nearest enclosing brace
Give the order of operator precedence
()
* / %
+ -
=
(High to low)
What is
x = 2 - 3 * 7
-19
What is 1 + 12 / 8 + 1
3
What is -2 + 4.1 * -2
-10.2
What is (2-3) * 7
-7
What is (1+12)/ 8 + 1
2
What is -(2+4.1) * -2
12.2
What is n+= 300 the same as?
n = n + 300
What is n*=20 the same as?
n = n*20
What does the flow of control refer to?
Which methods get executed, and in which order
What is abstraction?
The process of extracting the general structure to allow the inessential details to be ignored.
What is parameterisation?
Process of writing generic code to which specific parameters may be supplied to perform a specific task
What are modifiers?
Used to control how the method can be used and it’s visibility
What is the difference between formal and actual parameters?
Formal - Identifiers used to stand for the values passed into the method by a caller
Actual - arguments passed into the method when method is invoked
What defines the method signature?
Name and parameters
What happens when a method is invoked?
Actual arguments are copied to formal arguments
Control passes to the method
Method executes
Control and value return to the caller
What is pass by value?
Actual arguments are copied onto the formal arguments of the method invoked
They then behave like variables within the method invoked
What are the boolean operators for not, or and and
!, ||, &&
Which boolean operators are short circuiting?
&&, ||
What are the non short circuiting equivalents of and and or?
& and |
Give the generic method definition
(){
}
Iteration
Repeating actions more than once
Where does the condition come in a while loop
At the start
Write a general format of a for loop
for ( statement; boolean expression; statement){
}
What are the 4 parts of the for loop
Declaration of a counting variable
Test whether loop should continue
Update of counting variable
Instructions to be repeated
What is the scope of the counting variable in a for loop?
From its declaration to the end of the block governed by the for loop
Give some limitations of for loops
Have to know before the loop starts how many times to repeat
If you want to execute a piece of code once and then potentially a number of further times what loop would you use?
Do-while
What is the key difference between a for/while and a do while
Loop block is always executed once in the do while loop
What is method overloading?
Defining two(or more) methods in the same class with the same name but different parameters
Does the arithmetic addition operator has higher precedence than the arithmetic multiplication operator?
No
Does the && operator have higher precedence than the || operator?
No
Does the assignment operator have higher precedence than relational operators?
Yes
What does it mean if an object’s field values are encapsulated?
They can’t be seen directly or accessed by other objects or that object’s methods
Give a benefit of encapsulation
Improves control over how field values are read and updated
Helpful in structuring and maintaining software(can change one part without affecting others)
What is an accessor method?
Returns the value held in a field
What is a mutator method?
Sets the content of a field to certain vaue
What do modifiers in java control?
Visibility
Mutability
Association with class / object
Give examples of some modifiers
Public Private Protected Static Final
Describe a static modifier and state what it can be applied to
Modifier for fields and methods Means that a member is associated with class as a whole rather than individual objects
What is the rule for static and non static accessibility
A static thing cannot access a non-static thing
What are the restrictions on public modifier?
Class or member can be accessed from anywhere, no restrictions
What are the restrictions on a private modifier?
Means that a member can’t be accessed from anywhere outside the class
What are the restrictions of a protected modifier?
Means that a member can only be accessed from the defining class, package or subclass
When should you use a final modifier?
To declare a field to be constant
What is a constructor?
A special method that is executed when an object is created
What are some properties of a constructor?
- can initialise the object as required
- can take parameters, like methods
- doesn’t return any results
- always has the same name as the class
What is a default constructor?
Simple constructor defined automatically if we don’t write any more constructors
Creates instance and fills in default attribute values
How do you link constructors?
Using the this keyword
What is method overloading?
Methods with the same name but different parameters
When is method overloading useful?
When methods are closely related in purpose
What is an array?
Special java object that contains a collection of values
How are arrays declared?
The name of the type followed by square brackets
Declare an array of integers
int[] myArray;
Give a disadvantage of arrays
Have to know how many elements at the time of writing program
At what number do array indexes start?
0
What index place would the nth element have?
n-1
What are magic constants?
Literal constants embedded in the program
What are the solutions for magic constants?
Make constants with a name
How are final variables conventionally written?
In all UPPERCASE
Write the syntax for an enhanced for loop
for(int item: numbers)
system.out.println(“Count is “ + item);
What does item take the value of in the enhanced for loop
Next value in the array
Give a benefit and a disadvantage of the enhanced for loop
More elegant - avoids the need for an explicit counter variable and test
Little less flexible
How are parameters passed in Java?
By value, the parameter is treated just like a variable defined within the method
Define a 2D array
A 2D array is an array of arrays
What is a model
An abstract representation of a scenario/entity/concept
What are some benefits of modelling?
Easier to
- understand
- find errors
- communicate
What are we interested in object orientated models?
Information to be stored
Operations on information
What is the difference between a model and a program?
Different levels of abstraction
What is the point of building a software model?
Too big an abstraction gap between specification and code, especially for large and complex problems
What does UML stand for?
Unified Modelling Language
Give some examples of UML diagram types?
Class, object, use case, sequence
What is a program in an object orientated language?
Set of class definitions At least one class with a main method
What do UML diagrams contain information about?
Attributes
Operations
Relationships with other classes
What are inline attributes?
When there is an attribute in the class diagram with the type and not a relationship arrow
What are multiplicities?
Way to specify how many instances may be associated
Where is the number in the multiplicities?
Each end of association
What does navigability indicate?
Whether instances of a class know about instances of associated class
What does # stand for in modifiers?
Protected
How are static modifiers denoted in UML?
Underline attribute and method names
What are the steps in the modelling process?
Determine classes
Determine fields
Determine methods and parameters
Determine relationships between classes
What is a UML object diagram?
Shows a snapshot of computer memory at a particular point during execution
What does the subclass inherit from?
Superclass
What is inheritance?
Relationship between classes where attributes are inherited from a superclass to a subclass
Give an example of a relationship that may have inheritance involved
Pet - Dog, with Pet being the superclass and Dog being the subclass
Give a rule about inheritance
A subclass can only inherit directly from one superclass
What is the default superclass if none is specified
Object
What keyword is used for calling the constructor in a superclass
Super
What is the basic idea of substitutability?
You can use an instance of a subclass anywhere that an instance of its superclass would be valid
What is an advantage of using inheritance?
Avoids redefining duplicate versions of fields and methods
What is overriding?
Redefining a method in a subclass is called overriding
What must be the same in a method when it is overriding?
Parameters and return type
What is overloading?
Means defining more than one version of a method within a class
What is different between each overloaded method?
Different parameters
What type of classes CANNOT BE EXTENDED
Final
What types of methods cannot be overridden?
Final
What is late binding?
May have a variable/parameter referring to an object whose type we don’t know until runtime
How can we deal safely with late binding?
Check using instanceof
What are the values of b and a after this code?
int a = 1;
int b = a++;
b == 1
a == 2
What are the values of a and b after the following code?
int a = 1;
int b = ++a;
a == 2
b == 2
What is an abstract class?
An abstract class is a class that cannot be instantiated but is still useful in structuring the inheritance hierarchy
Give an example of a potential abstract class?
Mammal
Person
How do you declare an abstract class?
public abstract class Fish{
}
What types of methods can abstract classes contain?
Methods with implementation
Methods without implementation
What must happen when writing a class that extends an abstract class
Programmer must implement ALL of the abstract methods declared in the abstract class UNLESS the class itself is declared as abstract
How does Java accomplish multiple inheritance?
Interfaces
What are interfaces?
Contracts for how different parts of software may interact with one another
What can interfaces contain in java?
Constants
Method headers
Static methods
Can you make instances of Interface
No
Write code for declaring an interface?
public interface IPerson{
}
All methods declared in an interface are implicitly ___?
public
Give some benefits of interfaces
Abstraction
- can write code that uses objects without knowing exactly what class they belong to
Combining code written by different people at different times
Give an example of code where the class MyClass inherits interface 1 and interface 2
public class MyClass implements Interface1, Interface2 {…}
What will happen if you have a class that inherits two interfaces which both have contain constants with the same identifier
There will be an error
When do we use abstract classes?
Sharing code among closely related classes
Many common methods or fields
Need non-static or non-final fields
When do we use interfaces?
Unrelated classes implement same methods
Specify behaviour but do not care who implements it
For multiple inheritance