Most important Flashcards

1
Q

What is a constructor

A

A block of code that is executed when a class is instantiated.

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

By default if you create a class and don’t specify a constructor the compiler will generate one for you. The one generated is called the default ________

A

no-arg constructor

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

What is a method in Java?

A

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

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

In a Java method, what is the difference between a parameter & an argument?

A

Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type & order.

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

What is Method Overloading in Java?

A

If a class has multiple methods having the same name but different in parameters, it is known as Method overloading.

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

What are the different ways to overload a method in Java?

A
  1. By changing number of arguments

2. By changing the data type

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

What is State in Java?

A

State is a behavioral design pattern that allows an object to change the behavior when its internal state changes.

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

What is the Object Class in Java?

A

The object class is the parent class of all classes defined in java even if you do not explicitly extend it.

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

What is Polymorphism?

What is it’s most common use in OOP?

A

The ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

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

What is a reference variable in Java?

A

A link to an instance of an object in memory

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

What is an API?

A

Short for “Application Programming Interface”, it is a library of functions you can use to interact with a program or component.

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

What is the java.lang package?

A

The default package, it is implicitly imported into all java classes, so it requires no declaration. Many useful classes come from this package including system and string.

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

The string class in Java is an “immutable” class, what does this mean?

A

It can not be changed

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

What is special about the string class in Java?

A
  1. It can be instantiated in two different ways unlike all other classes, (with or without the new keyword)
  2. String objects are often recycled by the JVM instead of creating new ones in what is called the “String Pool” Which saves memory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between equals() & ==?

A

The operator == is used to compare the value of primitives ex.
int x = 6;
int y = 5;
if (x ==6) { .. }
If you compare two reference variables using the == operator will compare the reference address of the objects each variable points to. It may not return true even if the contents of those two objects are the same.

This is why we use the equals() method with objects. It is expected to that it will help us compare the values of objects with one another ex.
String s = "Hello";
String s2 = new String("Hello");
if (s.equals(s2)) {..}
This is because the equals() method of the String class has been overridden to compare the contents of the current String and one passed in as an argument.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If you want to create a mutable, i.e. changeable string what would you use?

A

A StringBuilder or StringBuffer object.

17
Q

When Should you use a String builder Object?

A

You use a StringBuilder when you are not using threads, or are performing thread control safely somewhere else.

18
Q

When should you use a String buffer object?

A

You can use a StringBuffer whenever you need a mutable string object, and also need it to be thread-safe. A StringBuffer object will guarantee that only one thread can access it at a time, preventing confusion. This can slow performance

19
Q

What are “keywords” in Java?

A

Reserved words in Java that give the compiler specific instructions (so they can’t be used to name fields, methods, or classes)

20
Q

What is the “static” keyword in Java?

A

This modifier defines “class” variable & methods, as opposed to “instance” members

21
Q

What does the “final” keyword do in Java?

A
  1. Makes things immutable (unchangeable)
  2. Primitive vars can’t modify values
  3. Reference vars can’t switch references
  4. Methods can’t be overridden
  5. Classes can’t be extended
    * Your application will not compile if you try to break these rules
22
Q

What does the keyword “Abstract” do in Java?

A
It defines an incomplete entity, each concrete subclass will be forced to define implementation details for itself.
An abstract class cannot be instantiated, it can only ever be extended. Probably research more on this
23
Q

What can the keyword “final” be applied to in Java?

A

A class, method, or variable. In all cases it means the structure cannot be changed.

24
Q

When applied to a variable what does the keyword “final” mean?

A

That you cannot reassign the variable to another value. You must initialize the final variable when declaring it.

25
Q

When applied to a method what does the keyword “final” mean?

A

That you cannot override the method

26
Q

When applied to a class what does the keyword “final” mean?

A

That you cannot create a subclass of this class

27
Q

What is a common example of a “final” class in Java?

A

The string class