Lectures Flashcards

(77 cards)

1
Q

Where to declare the result of the method?

A

When the method is created the type of the result is declared.

The result value is passed with the return keyword.

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

What does it mean that Java is statically typed?

A

The type of a variable is explicitly defined at compile time.

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

What does it mean that Java is strongly typed?

A

The compiler gives an error when types do not match.

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

How to iterate over each element in a list?

A

List numbers = new ArrayList<>;

for (Interger number : numbers) {…}

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

What are the values of byte type?

A

-128 until 127

8 bits

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

What are the values of short type?

A

-32768 until 32767

16 bits

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

What are the values of int type?

A

-2^31 until 2^31-1

32 bits

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

What are the values of long type?

A

-2^63 until 2^63-1

64 bits

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

What are the values of float type?

A

Real number approximation.

32 bits

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

What are the values of double type?

A

More precise real number approximation

64 bits

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

What are the values of char type?

A

A single Unicode character

16 bits

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

What are the values of boolean type?

A

True or False

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

When can you convert a primitive variable to a different type?

A

Automatic conversion is only possible if the conversion goes from a more specific to a more general type.

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

What is a static method?

A

We can write static methods within a class, but we are not allowed to use the current or local instance variables. Only instance variables of objects of whom the reference was passed as an argument can be accessed.

You are never allowed to use this keyword, for example.

public static A_combine(A object1, A object2)

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

What is “this” keyword?

A

This allows to explicitly refer the current or local instance variables.

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

How to indicate that no object was found while executing a method?

A

Use the null reference.

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

What is a null reference?

A

The null reference is used to indicate that there is no object.

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

What is the default value of a instance variable of a non-primitive type?

A

Instance variables with a non-primitive type always get null as the default value.

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

What happens if we try to call a method on a variable that is null?

A

will get a NullPointerExceptionand the program will stop.•

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

How to generate a random numbers?

A

import java.util.Random;

Random ran = new Random(1234);
int myDie = 1+ran.nextInt(6);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a random seed?

A

import java.util.Random;

Random ran = new Random(1234);

The argument 1234is called the random seed. You can use any number here. It fixes the random sequence to be generated (so 1234gives one random sequence, 5231another).It is useful to fix the random seed so the results of your run becomes reproducible. If you want to try how your program works with a different random sequence, just change the seed.

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

What are different types of errors in Java?

A
1. Compiler
Syntax Errors
Type errrs
Structural errors (example with return)
2. Runtime errors
3. Logical errors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are different types of errors in Java?

A
1. Compiler
Syntax Errors
Type errors
Structural errors (example with return)
2. Runtime errors
Gives an exception
3. Logical errors
The program executes, but you get a wrong answer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to raise an exceptions?

A

“throw” keyword

if (condition) {
throw new TATATAException("Something went wrong")
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the difference between throw and trows?
Throw is used to actually to raise an exception? Throws Indicates something can go wrong in this method by defining throws in the method signature.
26
How to catch an exception?t
``` try - catch if try { ... If there is the problem in the try block, than the catch part is immediately executed. } catch (TypeOfYourException1 e){ System.out.println ("Try again") } catch (TypeOfYourException2 e){ System.out.println ("Try again") } ```
27
What is an interface in Java?
A way to get a polymorphism. Instead of having multiple specific methods that only accept objects of a single class, one can write a single method that works for all kinds of the INTERFACE objects. Example with the games.
28
What is an interface in Java?
A way to get a polymorphism. Instead of having multiple specific methods that only accept objects of a single class, one can write a single method that works for all kinds of the INTERFACE objects. Example with the games. public interface GameValue { }
29
What is an interface in Java?
A way to get a polymorphism. Instead of having multiple specific methods that only accept objects of a single class, one can write a single method that works for all kinds of the INTERFACE objects. Example with the games. A list with methods. public interface GameValue { }
30
What is interface inheritance?
If you need additional features in the interface X you can create a new interface new X and mention that it extends X. public interface new_X extends X {...} Implicit casting from the more general interface to the extended interface is not possible.
31
WHat is a subtype?
public interface BankScore extends GameValue {...} BankScore is a subtype A subtype can always do at least as much as its supertype.
32
WHat is a supertype?
public interface BankScore extends GameValue {...} | GameValue is a supertype.
33
What is class inheritance?
The subclass inherits the methods and instance vars of the superclass. Methods of the superclass as if they are defined in the current class (unless they are private).
34
How many superclasses can a class have?
A class can have at most one direct superclass
35
How many interfaces can a class implement?
A class can implement any non-negative number of interfaces.
36
How many subclasses can a class have?
A class can have any number of subclasses, unless the class is final.
37
How to call another cinstructor of the same class
this ()
38
How to call a constructor of a super class?
super()
39
If the superclass has one or more specific constructors then how to make sure everything is being initialized?
The subclass must call for one of them in its method.
40
How to redefine the body of one or more methods of the superclass?
Use | @ Override
41
When a super keyword should be used?
1. To call the constructor of a superclass. 2. When overriding a method, but still want to use the overridden method of the superclass. super.method()
42
Liskov Substitution Principle
When you write methods that use your class, you don't know whether it will be executed by your original class, or by a subclass implemented by someone else. ``` The LSP: Subclasses of a class should not change the behavior specified in its superclass. Properties that are true for the superclass should sill be true for a subclass. ```
43
Is the type of variable referring to the object or type of the object defines which method is executed in the subclasses of a certain class?
Type of the object
44
How to forbid a method to be overridden by subclasses?
With a final | public final void method () {...}
45
What is the difference between methods / variable of private and public classes?
The public can be called from anywhere (any class) | Private can be called only from within the same class.
46
WHat are protected methods and variables?
``` Can be called from the same class Can be called from within subclasses Can be called from within the same package. ```
47
What is the instanceof operator?
It is used to check whether it is safe to cast a supertype to subtype.
48
What is the cosmic superclass?
Object is the class of all classes that do not have an explicit superclass defined on it. There are number of methods defined in the Object class. Close, finalize getClass etc. Important methods : equals(), hashCode(), toString().
49
What is the difference between == and .equals?
== is the objects are exactly the same. (for a non-primitive variables are considered different, since they have different mempry locations). equals checks if the content of the object is the same.
50
How to write a class?
Own .java file Basic syntax Def vars, constructors and methods
51
Difference between instance vars, class (static) vars and local vars?
instance vars - individual for each object of a class. class (static) vars - single global var, correspond to a class. single value local vars - live locally in a method.
52
Difference between static and regular methods?
regular methods - operate in an object directly. | static methods - do not have an instance they are working on, but work with objects in genral.
53
What do Accessor methods do
Only read information from the object.
54
What do Mutator methods do
Changes the state / info stored in the object
55
JAVA is a strongly typed language. what does it mean?
All variables and expression have a type, which must be declared
56
What is a distinction between primitive types and non-primitive types?
8 primitive types Primitive variable - hold actual values Non-prim vars - hold references There is a non-prim for each primitive variables.
57
What is a distinction between primitive types and non-primitive types?
8 primitive types Primitive variable - hold actual values. Cannot be null Non-prim vars - hold references. Can be null if unassigned There is a non-prim for each primitive variables.
58
What is implicit casting?
Allows to convert vars safely: int to long.
59
What is explicit casting?
Allows to convert vars unsafely: long to int. | How?
60
What are generic types?
... Allows to create the ArrayList
61
What is the inharitence between subclasses and superclasses?
subclasses are of the type of their superclasses, the other way around needs explicit casting.
62
How to visualize the relations between types?
Class inharitance digram
63
What are immutable objects?
String, Non-prim prim vars (Integer, Boolean)
64
WHat is the name of the calsses, whose objects can be modified?
Mutable
65
What is a Memory State Digram?
Shows references and assignments in the class.
66
Which keyword calls an exception?
trow
67
What is a type of an exeption object?
Throwables
68
How to indicate that a method can throw an exception?
Using throws keyword in the method header
69
What is the difference between checked and unchecked exceptions?
unchecked exceptions are error and tuntimeexception. We are not too concerned with it. checked exceptions - all other exceptions. need to be caught or thrown to the calles When you are working with files we have to take care of IOExeption FileNotFoundExeption using try - catch w
70
How to achieve polymorphysm?
Interfaces A class implements interface with implements keyword. Inheritance Indicated with the keyword extends. Can be for classes and interfaces. There is a relation beween superclass and the subclass relation.
71
Abstruct classes
Used in case we want to implement part of a class, we leave some methods open for subclasses to implemnt. ``` Called with the keyword abstract in the header. New can be only on the subclass of an abstruct class. ```
72
What is the class object?
If a class is not explicitly extending another clas, it implicitly extends Object.
73
hashCode()
gives memore adress in a form of a number
74
equals()
uses memory adress of an object to determine wheather two objects are equal.
75
Comperator interface
Compares two objects
76
Comparable interface
Compare this object to another
77
IS there an index / order in a set?
No. There is no order in the set. | Enhanced-loop should be used.