Java Flashcards

1
Q

What is the lifecycle of a java program

A

Source code -> Compiler -> Output -> JVM (Java virtual machine

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

name the primitive variables in java

A

short, bit, int, float, long, boolean, byte, double

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

What is a local variable

A

defined in method, constructor, or block

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

what is an instance variable

A

non-static variables defined in a class outside of a method or any block

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

what is a static OR class variable

A

a variable declared using the static keyword in a class outside of any method

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

what are the differences between a static variable and an instance variable?

A
an instance variable is unique to the object that defines it.
there is only one class variable for all the instance of a class, if the class variable is changed it effects all the instances of the class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are primary OOP concepts in java

A

abstraction, encapsulation, polymorphism, inheritance

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

What is abstraction?

A

The process of identifying the essential details to be known and ignoring the non essential details from the perspective of the system

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

What is encapsulation?

A

controlling the read and write access of classes, it provides the ability to hide data and methods from other classes

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

What is an access modifier?

A

Key words that define the accessibility for a class, method, or attribute.

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

What are the access modifier keywords

A

private, default, protected, public

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

What is polymorphism?

A

Poly: many, Morph: forms

using a method with the same name to perform different tasks

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

What is an example of polymorphism?

A

Method overloading or Method overridding

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

What are the two types of polymorphism that java supports?

A

Static and Dynamic

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

What is static polymorphism?

A

Allows you to implement multiple methods within the same class with the same name.

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

How would you implement method overloading? 3 answers

A
  1. Have a different number of parameters
  2. The types of the parameters are different
  3. The order of the parameters are different
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is method overriding?

A

Using inheritance to customize or completely replace the behavior of a method defined in the parent class.

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

What is inheritance?

A

Deriving a class from a class or hierarchy of classes that share a set of attributes and methods

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

What is an interface?

A

A completely abstract class that contains:
Only abstract methods
AND / OR
Default methods and final variables

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

How does another class implement an interface?

A
By using the keyword implements:
public class MyClass implements MyInterface { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a functional Interface?

A

An interface that has only one abstract method.

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

A class can implement multiple interfaces?

A

TRUE

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

A class can extend multiple classes?

A

FALSE

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

If a method in an interface has an implementation, what access modifier must it have?

A

default OR static

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

Interface variables are what by definition?

A

public static and final, visibility cannot be changed

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

if an interface is implemented by a non abstract class, what must the class do?

A

override all of the interfaces abstract methods

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

What are Lambda expressions?

A

functional constructs without classes.
they can be passed like objects and executed as required.
they also make the modifiers, return type and parameter types completely optional.

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

What is the purpose of a Lambda function?

A

implementation logic for a functional interface.

29
Q

What is the syntax of a Lambda expression?

A
  1. (arguments)
  2. arrow ->
  3. body: single statement or code block
    (x, y) -> x + y;
30
Q

What is the syntax for a method reference?

A

User::getUserId

31
Q

When can method reference be user?

A

In a lambda expression when you are calling a method already defined with a name.

32
Q

What is the lifecycle of a Stream?

A

Stream -> intermediate operation -> terminal operation -> result

33
Q

Stream intermediate operations are Lazy, explain what that means and how it could help performance.

A

Because the operation is Lazy it does not perform any processing until the terminal operation is called in the stream.

34
Q

Can a non static variable be accessed in a static method or block?

A

No, but a static variable can be accessed in a non static method

35
Q

How many times and when can a final variable be initialized?

A

Once, either at the time of declaration or in the constructor.

36
Q

When does class that implements an interface not have to implement all of its undefined methods.

A

When the class implementing the interface is abstract.

37
Q

Does an abstract class contain only abstract methods?

A

No it can contain either abstract or defined methods.

38
Q

Can an abstract method be private?

A

No, abstract methods must be implemented by child classes. Therefor it may not be private.

39
Q

What is Java

A

Java is a High-Level, Object Oriented, General-Purpose programming language.

40
Q

What is the file extension for the Byte Code after source code has been compiled?

A

.class

41
Q

What are the components of the JVM?

A

Class Loader, Byte Code Verifier, Interpreter, Jit Compiler, Runtime

42
Q

What is the purpose of the Class Loader?

A

It loads all the .class files needed for execution.

43
Q

What is the Byte Code verifier?

A

Checks code fragments for illegal code.

44
Q

What is the Interpreter?

A

Converts byte code instruction to machine code.

45
Q

What is the JIT compiler?

A

Compiles reusable byte code to machine code

46
Q

What is Runtime?

A

Assists in the overall execution of the program.

47
Q

Is the JVM platform independent?

A

No, Java as a whole is platform independent. The JVM is OS specific.

48
Q

What will the output be if a main method has private access modifier?

A

Runtime Exception

49
Q

What are some rules for naming identifiers?

A
  1. Case-Sensitive
  2. Should not start with a number
  3. Can start with letter, $ or _
  4. Should not have spaces
  5. Should not be a keyword or literal
  6. No length restriction
50
Q

What are Non-Primitive data types?

A

String, user defined class, enum

51
Q

What is the order of data types, by size?

A

short -> int -> long -> float -> double

52
Q

What are the two ways an array can be declared?

A

int[] arr;

int arr[];

53
Q

What are all the constants in an Enum set?

A

Implicitly static and final

54
Q

What are some constraints of an abstract class?

A
An abstract class cannot be instantiated using the new keyword.
The subclass of an abstract class can be only instantiated if it provides the implementation for all the abstract methods.
If a class has at least one abstract method, then the class must be declared as abstract.
If the subclass does not implement all the abstract methods, then the subclass must also be declared as abstract.
An abstract class can also have concrete methods i.e. methods with implementation.
An abstract class reference can be assigned an object of its subclass, thereby achieving run-time polymorphism.
55
Q

What are some characteristics of an interface?

A
An interface defines a contract for a class.
Objects can't be created for interface and an interface cannot have private or protected members.
In an interface, all methods are implicitly public and abstract and variables are implicitly public, static, and final.
The class which implements the interface has to provide definitions for all abstract methods.
 If at least one abstract method of the interface has not been overridden by the class that implemented the interface, make it abstract.
Inheritance is possible in an interface and it supports multiple inheritance.
56
Q

When can the extends key word be used on an interface?

A

When an interface is extending another interface.

An interface can extend multiple interfaces.

57
Q

What is a package?

A

A grouping mechanism in Java that contains all related classes and interfaces.

58
Q

What class is at the top of the hierarchy for all java classes?

A

Object

59
Q

What does Object.equals(obj) do when called?

A

Uses memory address to compare objects.

60
Q

What is a wrapper class? What is a common implementation of one?

A

Wrapper classes are used to represent primitive data types as Objects.
Java collections cannot store primitives, they can only store Objects.
Integer, Long, Boolean, Float…

61
Q

What is a String?

A

An immutable sequence of characters in the form of an Object

62
Q

What is the difference between:

  1. String username = “David”;
  2. String username = new String(“David”);
A

1: is represented as a String literal.
2: is represented as a String Object.

63
Q

What is the difference between a String literal and a String Object?

A

String literals are created in a pool section of the heap memory. I.E two strings with the same characters both point to the same literal in the pool.

String Objects with the same characters point to 2 different objects in memory.

String literals are not eligible for garbage collection. String Objects are.

64
Q

When referring to Strings what is the difference between == and .equals() ?

A

== compares the memory address of the two strings

.equals() compares the content of the two strings

65
Q

What class is the superclass of all exceptions? and what class does it extend?

A

Exception, extends Throwable.

66
Q

What is the difference between an Exception and an error? What is a similarity?

A

Error represents a serious abnormal condition that should not be caught.
Exceptions can be caught.
They are both subclasses of the Throwable class.

67
Q

What is the difference between checked and unchecked exceptions?

A

Checked exceptions occur at compile time and should be handled
Unchecked exceptions occur at runtime and do not need to be handled

68
Q

What must a try block be followed by?

A
Either a/several catch block(s)
OR
A finally block
OR 
both
69
Q

What are the difference and similarities of ArrayList and LinkedList?

A

ArrayList: Is an array implementation that allows random access when retrieving list items, Addition and removal of elements requires iteration.
LinkedList: Is a Doubly Linked List that requires iteration over the list to retrieve an item, while addition and removal of elements is faster.
They both implement the List interface