Fundamentals Flashcards

1
Q

What is a function?

A

A function is a block of code that performs a task.

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

What is the function main?

A

It is the entry point of our program.

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

What is a class?

A

Using a basic perspective, it is a container for one or more related functions, but using a more advance logic it is a blueprint that enable us to create objects.

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

What does classes are useful for?

A

They help us organize our code.

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

What is a method?

A

It is a function that is part of a class.

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

What is an access modifier?

A

It is a special keyword that determines if other classes and methods of the program can access these classes and methods.

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

Give me some examples of access modifiers.

A

Private and public.

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

What does PascalNamingConvention means?

A

It means that the first letter of every word should be uppercase.

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

What does camelNamingConvention means?

A

It means that the first letter of every word should be uppercase except the first word.

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

What type of naming convention we use for classes?

A

PascalNamingConvention

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

What type of naming convention we use for methods?

A

camelNamingConvention

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

What is a package used for?

A

It is used to group related classes.

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

What do we use to comment our code?

A

Two slashes //

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

What type of quotes should we use with textual data?

A

We should use double quotes.

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

What is a String?

A

A sequence of characters.

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

What are variables used for?

A

They are used to temporarily store data on the computer’s memory.

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

What is an identifier?

A

Is the name or label we give to our variables.

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

What other name we give to the equal sign?

A

Assignment operator.

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

What do we mean to initialize a variable?

A

To give the variable an initial value.

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

How many types we have in Java?

A

Two types: primitive types and non-primitive types also called reference types.

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

What do we store in primitive types?

A

Simple values.

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

What do we store in reference types?

A

Complex objects.

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

How many primitive types we have?

A

We have eight: byte, short, int, long, float, double, char, boolean.

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

Which primitives store whole numbers?

A

Byte, short, int and long.

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

Which primitives store decimal numbers?

A

Float and double.

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

What does the char primitive type represents?

A

Represents one character like ‘A’ or ‘B’.

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

Mention a best practice when declaring variables.

A

Always use meaningful descriptive names.

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

What can you do in Java whenever you deal with a large number?

A

You can use an underscore to separate every three digits: 100_000.

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

What should we do to resolve errors shown in the code editor when using long and float types of variables?

A

We simply add an L at the end of the long number before closing with a semicolon, and an F at the end of a float number before closing with a semicolon.

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

List the number of bytes for each primitive type.

A

Byte = 1, short = 2, int = 4, long = 8, float = 4, double = 8, char = 2, boolean = 1.

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

What type of quotes should we use with a single character?

A

We should use single quotes.

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

What does the words coded in the color orange inside our code editor means?

A

They mean they are reserved words in Java.

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

What do we need to do in order to use a class from a different package?

A

We need to import that package.

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

What does the new operator do?

A

It allocates memory for the newly created variable.

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

What are the differences between primitive and reference types?

A

There are two differences, the first one is that reference types allocate memory whereas primitives don’t. The second difference is that primitives don’t have members (also methods).

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

What is an object?

A

An object is an instance (single ocurrence) of a class.

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

How can we access objects or classes?

A

We can access using the dot operator.

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

What does code snippets allow us to do?

A

They allow us to quickly generate code.

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

Give me an example of a code snippet.

A

Typing the word sout in the code editor will automatically produce the following: System.out.println( )

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

What are parameters?

A

They are the holes that we define in our methods.

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

What are arguments?

A

They are the actual values that we pass to the method.

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

What do we use escape sequence for?

A

We use it to type special characters without breaking our code.

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

How do we use escape sequences?

A

We type a backward slash: \

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

What is an array?

A

A list of items.

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

What do we add to create arrays?

A

We add square brackets [ ] in front of the variable:

int [ ] numbers = number [5];

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

What should we pay attention when creating arrays?

A

Our variable name should be in plural, since we are creating a list with multiple items.

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

What are exceptions in Java?

A

Exceptions are the way Java reports errors.

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

Mention one characteristic of arrays in Java.

A

They have a fixed size, so they have a fixed length, meaning that we cannot add or remove additional items to them.

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

When declaring a two-dimensional array, what does the first pair of brackets mean?

A

It means the number of rows.

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

When declaring a two-dimensional array, what does the second bracket means?

A

It means the number of columns.

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

How do we create a constant?

A

We type the reserved word final before the variable type:

final float PI = 3.1416F;

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

By convention, what do we do with the name of a constant?

A

We use all capital letters.

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

What is an expression?

A

It is a piece of code that produces a value.

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

Do we need to type the new operator when using abstract classes?

A

No

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

Give an example of an abstract class.

A

NumberFormat

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

What does factory method means?

A

It means calling a method of an abstract class, creating an instance and returning it.

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

Give an example of creating an object using the abstract class NumberFormat.

A

NumberFormat currency = NumberFormat.getCurrencyInstance( );

58
Q

What does the method chaining means?

A

Chaining methods together.

59
Q

Give an example of method chaining.

A

String result = NumberFormat.getCurrencyInstance( ).format(1234.56);

60
Q

What is a field?

A

Is a variable defined in a class.

61
Q

What are the methods inside the Scanner class used for?

A

They are used to read data.

62
Q

What do we use to compare primitive values?

A

We use comparison operators.

63
Q

What do we use to combine multiple boolean expressions?

A

We use logical operators.

64
Q

Why are if statements extremely important?

A

Because they allow us to build programs that can make decisions based on certain conditions.

65
Q

In which situation curly braces are required when using an if statement?

A

They are required when we have multiple statements.

66
Q

What symbols do we use in a ternary operator?

A

Question mark ? and colon :

67
Q

In programming, what does de acronym DRY stands for?

A

Do not repeat yourself.

68
Q

Write an example of an if statement.

A
int temp = 32;
if (temp > 30) 
   System.out.println("It's a hot day");
else if (temp > 20)
   System.out.println("Beautiful day);
else
   System.out.println("Cold day");
69
Q

Write an example of a switch statement.

A
String role = "admin"
Switch (role) {
   Case "admin":
      System.out.println("You are the admin");
      break;
   Case "moderator":
      System.out.println("You are the moderator");
      break;
    default: 
      System.out.println("You a guest");
70
Q

What do we use break for in switch statements?

A

We use it to jump out of the block.

71
Q

What is the default code inside a switch statement for?

A

If none of the cases apply, this code will be executed.

72
Q

Give an example of a for loop statement.

A

for (int i = 0; i < 5; i++)

System.out.println(“Hello World”);

73
Q

What are the parts that conform a loop?

A

The first part is called counter variable declaration, the second part is the boolean expression which determines how many times this loop is going to be executed and the third part is the increment or decrement of the counter variable.

74
Q

What is iteration?

A

Is the action of running the code in a for loop statement.

75
Q

In what situations it is better to use a for loop statement?

A

When you know ahead of time how many times you want to execute one or more statements.

76
Q

In what situations it is better to use a while statement?

A

When you know don’t know ahead of time how many times you want to execute one or more statements.

77
Q

Give an example of a while loop.

A

Scanner scanner = new Scanner (System.in);
String input = “ “;
While ( !input.equals(“quit”)) {
System.out.println(“Input”);
input = scanner.next( ).toLowerCase( );
System.out.println(input);
}

78
Q

What does the continue statement inside a while loop does?

A

It moves the control to the beginning of the loop.

79
Q

What is the beauty of creating methods?

A

Instead of repeating the same lines of codes over and over inside the main method, we define them inside the new method once, and then we reuse them with different arguments.

80
Q

What does refactoring means?

A

Changing the structure of the code without changing its behaviour.

81
Q

Ideally, how many lines of code should our methods have?

A

Our methods should be between five and ten lines of code, no more than twenty.

82
Q

Whenever we refactor we should look for what two things?

A
  1. Concepts or lines of code that always go together.

2. Repetitive patterns in your code.

83
Q

What are the two types of errors in Java?

A
  1. Compile errors

2. Run-time errors

84
Q

What are compile errors?

A

These errors occur when we don’t follow the grammar or syntax of Java language.

85
Q

What are run-time errors?

A

These errors occur when our logic in our code is faulty.

86
Q

What does debugging mean?

A

It means finding and removing errors in a program.

87
Q

Name a synonym of program paradigm.

A

Style programming.

88
Q

What are the two most popular styles of programming?

A

Object-Oriented programming and functional programming.

89
Q

What does an object contains?

A

Data, also called state and operations on the data, also called behavior.

90
Q

Name the benefits or OOP.

A
  1. Reduced complexity
  2. Easier maintenance
  3. Reusable code
  4. Faster development
91
Q

What can we do in situations where the name of the field is the same as the name of the parameter?

A

We can use the this keyword.

92
Q

Give an example of how to use the this keyword.

A
public class Textbox {
   public String text;
   public void setText (String text) {
      this.text = text;
   }
}
93
Q

What happens if we don’t initialize string type variables?

A

They are set to null, and can crash our programs.

94
Q

What are NullPointerExceptions?

A

They are errors that mean we are not referencing a real object in memory.

95
Q

What are the two different areas of the memory that Java manages?

A

The heap and the stack.

96
Q

What is the heap?

A

It’s where Java stores its objects.

97
Q

What is the stack?

A

It’s where Java stores short lived variables.

98
Q

What does deallocating memory in Java means?

A

It means that when we exit a method, Java runtime will immediately remove all the variables that are stored in the stack.

99
Q

What does garbage recollection mean?

A

It is a process running in the background that is watching objects in the heap, if an object becomes unused for a certain period of time, that process is going to automatically remove that object from the heap.

100
Q

What is procedural programming?

A

It is when you are constantly calling methods and passing many arguments.

101
Q

What is the difference between a class and an object?

A

A class is a blueprint or template for creating objects. An object is an instance of a class.

102
Q

What does instantiating mean?

A

Instantiating means creating an instance of a class: new Customer()

103
Q

What is the difference between stack and heap memory? How are they managed?

A

Stack is used for storing primitive types (numbers, boolean and character) and variables that store references to objects in the heap. Variables stored in the stack are immediately cleared when they go out of scope (eg when a method finishes execution). Objects stored in the heap get removed later on when they’re no longer references. This is done by Java’s garbage collector.

104
Q

What are the problems of procedural code? How does object-oriented programming help solve these problems?

A

Big classes with several unrelated methods focusing on different concerns and responsibilities. These methods often have several parameters. You often see the same group of parameters repeated across these methods. All you see is procedures calling each other passing arguments around.

By applying object-oriented programming techniques, we extract these repetitive parameters and declare them as fields in our classes. Our classes will then encapsulate both the data and the operations on the data (methods). As a result, our methods will have fewer parameters and our code will be cleaner and more reusable.

105
Q

What is encapsulation?

A

Encapsulation is the first principle of object-oriented programming. It suggests that we should bundle the data and operations on the data inside a single unit (class).

106
Q

Why should we declare fields as private?

A

How we store data in an object is considered an implementation detail. We may change how we store the data internally. Plus, we don’t want our objects to go into a bad state (hold bad data). That’s why we should declare fields as private and provide getters and or setters only if required. These setters can ensure our objects don’t go into a bad state by validating the values that are passed to them.

107
Q

What is abstraction?

A

Abstraction is the second principle of object-oriented programming. It suggests that we should reduce complexity by hiding the unnecessary implementation details. As a metaphor, think of the remote control of your TV. All the complexity inside the remote control is hidden from you. It’s abstracted away. You just work with a simple interface to control your TV. We want our objects to be like our remote controls.

108
Q

What is coupling?

A

Coupling represents the level of dependency between software entities (eg classes). The more our classes are dependent on each other, the harder it is to change them. Changing one class may result in several cascading and breaking changes.

109
Q

How does the abstraction principle help reduce coupling?

A

By hiding the implementation details, we prevent other classes from getting affected when we change these details. For example, if the logic board and transistors inside a remote control change from one model to another, we’re not affected. We still use the same interface to work with our TV. Also, reducing these details and exposing fewer methods makes
our classes easier to use. For example, remote controls with fewer buttons are easier to use.

110
Q

What are constructors?

A

Constructors are called when we instantiate our class. We use them to initialize our objects. Initialization means putting an object into an early or initial state (eg giving it initial values).

111
Q

What is method overloading?

A

Method overloading means declaring a method with the same name but with different signatures. The number, type and order of its parameters will be different.

112
Q

What are static methods?

A

Static methods are accessible via classes, not objects.

113
Q

How can we have ClassA inherit from ClassB?

A

class ClassA extends ClassB

114
Q

What will be printed on the console and why? var point1 = new Point(1, 2);var point2 = new Point(1, 2); System.out.println(point1.equals(point2));

A

False - even though point1 and point2 have the same coordinates, the default implementation of the equals() method compares objects for reference equality. These two objects are in two different locations in memory, that’s why the equals() method returns false.

115
Q

What does hashCode() method of the Object class return?

A

The hashCode() methods returns a numeric value that is calculated based on the address of the object in memory.

116
Q

What is a default constructor?

A

A constructor without any parameters. If we don’t create it, the Java compiler will automatically add one to our classes.

117
Q

How can we add a constructor to the Customer class?

A
public Customer(String name) { }
Constructors don’t have a return type, not even void.
118
Q

What is the super keyword?

A

The super keyword is a reference to the base or parent class. We can use it to access the members (fields and methods) or call the constructors of the base class. In contrast, the this keyword returns a reference to the current object.

119
Q

What is the difference between private and protected access modifiers?

A

Members marked with protected or private access modifiers are only accessible inside of a class. Protected members are inherited and are accessible by child (derived) classes. Private members are not.

120
Q

How accessible is a field or method if it’s declared without an access modifier?

A

If we omit the access modifier, the member will have the default access modifier which makes that member public in package. In other words, that member will be public in the package but private outside of the package.

121
Q

What is method overriding? How is it different from method overloading?

A

Method overriding means changing the implementation of an inherited method in a subclass. For example, we can override the equals() or hashCode() methods of the Object class. Method overloading means declaring a method with different signatures (different number, type and order of parameters).

122
Q

What is the benefit of applying the @Override annotation when overriding a method?

A

It signals the Java compiler that we’re overriding a method in the base class and this helps the compiler check our code for correctness. It will ensure the signature of the method in the subclass matches the on declared in the base class. Also, if we remove this method from the base class, the compiler will let us know and we can remove the method in the subclass as well.

123
Q

The Customer class inherits from the User class. Can we pass a Customer object to this method? Why? public void print(User user) {}

A

Yes. We can pass an instance of any classes that inherit directly or indirectly from the User class. In this case, the customer object will get automatically upcast (meaning it’ll get converted to its base type - User). If we need to work with members of the customer object in this method, we need to explicitly downcast it by prefixing the object with (Customer).

124
Q

What is the usage of the instanceof operator?

A

It tells us if an object is an instance of a class. We use it before casting an object to a different type to make sure we don’t get a casting exception.

125
Q

What are the four principles of object-oriented programming?

A

Encapsulation, Abstraction, Inheritance and Polymorphism.

126
Q

What is the Encapsulation principle of OOP?

A

Bundling the data and operations on the data inside a single unit (class).

127
Q

What is the Abstraction principle of OOP?

A

Reducing complexity by hiding unnecessary details (metaphor: the implementation detail of a remote control is hidden from us. We only work with its public interface.)

128
Q

What is the Inheritance principle of OOP?

A

A mechanism for reusing code.

129
Q

What is the Polymorphism principle of OOP?

A

A mechanism that allows an object to take many forms and behave differently. This will help us build extensible applications.

130
Q

When do we use abstract classes?

A

An abstract class is a partially-implemented (half-cooked) class. We cannot instantiate them. But we use them to share some common code across their subclasses.

131
Q

Can we have an abstract class without any abstract methods?

A

Yes! An abstract class does not need abstract methods. But if we mark a method as abstract, we should mark the class as abstract as well.

132
Q

When do we use final classes?

A

Final classes cannot be inherited. We use them when we’ve made certain assumptions about a class and we want to prevent other classes extending our class and break those assumptions.

133
Q

What is the diamond problem?

A

The diamond problem happens in languages that support multiple inheritance. If two classes (B, C) derive from A and are also the parents
of another class (D), we see a diamond. If the top class (A) declares a method (eg toString) and its children (B and C) override this method, it’s not clear which implementation will be inherited by D.

134
Q

Does Java support multiple inheritance?

A

No

135
Q

Why do we use interfaces?

A

We use interfaces to build loosely-coupled, extensible and testable applications.

136
Q

What is tightly-coupled code?

A

Tightly-coupled code is code that is hard to change because there is a strong dependency between the entities (eg classes) in the code. Changing one class may result in several cascading, breaking changes in the code.

137
Q

What is dependency injection?

A

Dependency injection refers to passing or injecting dependencies of a class.

138
Q

What are the various types of dependency injection?

A

We can inject dependencies via constructors, setters and regular methods.

139
Q

What is the Interface Segregation Principle (ISP)?

A

The Interface Segregation Principle (ISP) suggests that we should segregate or divide big, fat interfaces into smaller ones, each focusing on a single responsibility or capability. Smaller interfaces are less likely to change. Changes to one capability, will only affect a single interface and fewer classes that depend on that interface.

140
Q

Why shouldn’t we declare fields, static or private methods in interfaces?

A

Fields, static and private methods are all about implementation. Interfaces are contracts and should not have any implementation.

141
Q

What are the similarities and differences between interfaces and abstract classes?

A

Both are abstract concepts and we cannot instantiate them. Interfaces are contracts and should only have method declarations. Abstract classes are partially-implemented classes. We use them to share some common code across their derivates. The new features in Java allow writing code and logic in interfaces but this is a bad practice and should be avoided.

142
Q

Should we extract an interface from every class? Why?

A

Blindly extracting interfaces doesn’t solve any problems nor is it considered a best practice. If you extract an interface from every single class, you’ll end up with an explosion of interfaces that don’t necessarily add any values. You should use interfaces in situations where you want to decouple a class from its dependencies so you can swap these dependencies. This allows building applications that are extensible and testable.