Java Basics and OO Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Name the different class modifiers and what they do

A
  • Access modifies: private, public and protected

abstract methods may or may not have abstract methods but they do not allow for instantiation, they do however allow for the class to be extended

final classes cannot be instantiated, inherited neither can their values ever be changed or updated

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

What are the different modifiers for methods

A

Private, public, static, final, abstract, protected

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

Upcasting vs Downcatsing

A

Downcasting is when an object is narrowed down into it’s more specific object. It is specializing or narrowing

Upcasting is when an object is taken up further up the hierarchy, so a child is represented as it’s parent. It is widening/generalization

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

Implicit vs Explicit casting

A

Implicit casting is done internally by a java compiler

Explicit casting data loss occurs and it involves using the casting operator (int)blah. Can result in data loss if you are performing a narrowing cast

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

What is an ADT?

A

An abstract data type is type or class that gives no information on how specific functions will be performed, only what those functions are. Gives an implementation independent view.

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

Why would someone not want to use arrays, when do they stop becoming relevant?

A

Arrays are very good at storing data and accessing data stored in the cells but removal and insertion into the array is very complicated and costly

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

Different collections spoken of and their pros vs cons?

A

Arrays, DLL, SLL and CLL.

  • Arrays: - Arrays are fixed size, they have a limit that has to be adjusted
  • Array cell removal and insertion expensive
  • good for data that will be accessed sequentially
  • SLL: - Great for elements in which you won’t have to access in reverse.
  • Less memory
  • Deletion at the end is O(n)
  • DLL: - Uses more memory for the additional storage of a previous node element
  • Great for forward and backward traversal
  • removal from the tail is easy since it is possible traverse backwards
  • CLL: -is made up of SLN but there is no distinct head or tail
  • can also be made of DLL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the goals of OO?

A

Robust: can cope well with unexpected input (error handling and exception)
Adaptability: can run efficiently on different types of hardware and software
Reusability: The ability to reuse a peice of code as a component to different systems

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

What is position ADT?

A

It is a model that helps us conceptualize the position in a data structure where a single element is stored

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

Give the algorithm for removing at the head, then removing at the tail in SLL and DLL

A

SLL:
-removal at the head:
*set the head to point to the element after it was previously pointing to
set that element’s next to be null and the garbage collecter will handle it

  • removal at tail:
  • if next node == node to be removed
  • make current node point to tail
  • get next node and make it point to null
DLL:
* head.setNext(nodeInSearch.getNext())
*nodeInSearch.getPrev().setPrev(head)
*NIS.setNext(null)
NIS.setPrev(null)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Stack with a SLL, what is the space used and what is the O of operations

A
  • we store n nodes, hence the space used is O(n)

* time taken is O(1) {because we pop, push, isEMpty and size }

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

Define inheritance

A

When a class acquires properties of another class

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

Define inheritance

A

When a class acquires properties of another class

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

What is a literal? Name the different kinds of literals for the different datatypes

A

a literal is a constant value used to initialize a variable or other expressions. String literal “fieri erhg edj” intl, double, float, long, true or false.

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

When would you use a do while instead of a while loop?

A

A do while loop will always allow the body to run a t least once since the condition is checked after. Whereas a while loop will check the condition first before it executes the body.

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

When would you use a for loop instead of a while loop?

A

A for loop is used when the number of iterations are known, while loops can be used if the number of iterations are not known but a certain condition can lead to the exit of the function

17
Q

What are explicit flow control statements, name them and state when each would be used.

A

Return: to return a value from a function and exit the function
break: used to immediately exit for, for each, while, do while and switch statement
continue is used within loops to ensure that in the case of a particular iteration the loop will disregard the remaining statement and continue to the next iteration

18
Q

What is the capacity of an array?

A

The length/ number of elements that it is able to accommodate for

19
Q

What are the different ways to create an array?

A

Using the new operator and specifying the size or not specifying the size and initializing

20
Q

What are the design principles of OOP?

A
Abstraction: distilling complicated parts into their most simple components. We can also use ADT that allow us to specify what a specific class that realizes that interface will do but not how it will do it
Encapsulation: speaks of hiding away coupling details from components that do not need access to their implementation details 
Modularity: dividing components into organized separate functional units.
21
Q

What are design patterns and how are they helpful?

A

Design patterns are a general template that can be applied to solve different types of problems.

They consist of a name (identifies the pattern), a template(specifies how the template is to be used) and a context(specifies in which scenarios the design pattern would be used)

22
Q

A class can implement multiple interfaces but cannot extend multiple classes. T/F

A

True

23
Q

overloading vs overriding

A

overloading is when methods of the same class have the same name but may have different return types and parameters. It a compile time polymorphism since the compiler can differentiate between the methods.

overriding is runtime polymorphism, it cannot happen without inheritance. The method signature and return types must be same across all the different classes

24
Q

Dynamic dispatch

A

Dynamic dis: a mechanism where a call to an overridden method is resolved at run-time

25
Q

Interface vs abstract classes

A

Interface classes have only abstract methods whereas abstract classes needn’t have only abstract method. Neither of them can be instantiated. Interface is declared using the interface keyword and not the abstract keyword. We can have multiple implementation of an interface but not multiple inheritance with abstrcact classes.

26
Q

What does the finally clause do in an exception

A

It insures that the code within those braces run whether or not the exception happens

27
Q

An interface class can only have one constructor

A

False, all the variables in an interface class are public static final by default so there is no need for a constructor, hence it does not have one

28
Q

Checked vs unchecked exceptions and give an example of each

A

checked exceptions are outside the control of the programmer and the are verified during compile time e.g filenotfoundexception

unchecked exceptions: java does not verify these at compile time, it specifies an error in the code of the programmer .i.e. dividing by 0. Java does not force you to handle these types of exceptions

29
Q

What is type erasure?

A

mechanism in java that removes all the parameters and replaces them with the actual datatypes specified by the user.

30
Q

Why do parameters in geneerics allow for primitive datatypes?

A

During compile time, type erasure will take plsce and suppose instead of T, int is placed there but this is a problem becasue int does not extend Object

31
Q

What are the critical members of a class

A
Instance variable(fields): represent the data associated with the class, it must have types which can be base types(int, dbl, bool) or refence types also known as class types
Methods: blocks of code called to perform a specific action
32
Q

What does the new operator do?

A

Returns a reference to a newly created instance/object and it is then stored in a variable for further use

33
Q

What 3 things occur with the creation of a new instance of a class/an object

A
  • memory is allocated for the object/instance that has just been created
  • the new operator returns the memory address for where the object is stored and it is often assigned to an object variable for further use
  • The constructor is called with the specified parameters to perform any additional computations that must be done
34
Q

what is the dot operator used for?

A

It is used for accessing methods and properties of an object

35
Q

What does the String datatype store and what methods does it offer

A
  • Stores a sequence of characters
  • Allows for indexing of characters with a
  • concatenate which uses the + to join 2 strings together
36
Q

Why does the stringbuilder run faster than + operator

A
  • the + operator when used, a new string is created and then copies all the characters into the new string instance
  • Stringbuilder will not do this, it is a mutable version of a string
37
Q

immutable vs mutable

A

Immutable cannot be changed after creating but objects mutable can