Java Basics Flashcards

1
Q

What is Java?

A

Java is a C-like, multiparadigmed, compiled programming language used for back-end development projects.

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

What are the advantages of Java? (7 are listed here)

A

It: 1) is a mature language, 2) is maintained by Oracle, 3) is portable, 4) is platform independent, 5) supports multithreading, 6) is compiled, 7) has automatic garbage collection

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

What is the JDK?

A

Java Development Kit. Provides an environment to develop and execute a Java program. Contains the compiler, JRE, and JVM.

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

What is the JRE?

A

The Java Runtime Environment. It is all that’s needed to run an application. Contains the core library and JVM.

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

JVM

A

Java Virtual Machine. Takes the byte code and executes it. Different operating systems can have different implementations of the JVM.

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

What is a class?

A

A class is the blueprint of an object. It defines the states (fields), and behaviors (methods) that its instances may have.

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

What is an object?

A

An object is an instance of the class. It is the representation of a real-world entity and has its own implementations of states and behaviors that are defined in its class.

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

What is the root class from which every class extends.

A

The object class. Every other class extends the object class and inherits methods from it.

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

What are some methods from the object class?

A

Equals(), getClass(), hashCode(), toString()

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

What are the 8 primitive types in Java?

A

There is: byte, short, int, long, float, double, boolean, and char.

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

Where are Strings stored?

A

Strings are stored in the heap. Strings declared literally are specifically stored in the string pool.

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

Explain stack vs heap?

A

Stack is short term storage used for methods currently running while the heap is long term storage used throughout the application.

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

What is the stack?

A

Short term storage that holds primitive and reference variables. It follows a LIFO (last in First out) pattern.

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

What is the heap?

A

It is long term memory storage. Holds objects and is managed by the garbage collector

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

What are annotations?

A

Annotations start with the ‘@’ and are used to provide supplemental information about a program. They can change the way a program is treated by the compiler. They help to associate metadata to the program elements.

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

Why are strings immutable in Java?

A

Strings in Java are immutable, meaning they cannot be changed at a later time. This is because when a string is created, it is stored as a new object in the string pool, or it points to a string object of the same value. This helps the program save memory.

17
Q

What is the difference between String, StringBuilder, and StringBuffer?

A

String is immutable. StringBuilder and StringBuffer are mutable alternatives with methods that allow for manipulation.

18
Q

Difference between StringBuilder and StringBuffer?

A

StringBuilder is not thread safe, but faster. StringBuffer is thread safe, but slower.

19
Q

What are different variable scopes in Java?

A

There is class scope, instance scope, method scope, and block scope. Each one defines different “lifetimes” or the variables declared in them.

20
Q

What are the access modifiers in Java?

A

Public, default, protected, and private.

21
Q

Public classes are accessible where?

A

Throughout the program

22
Q

Default classes are accessible where?

A

Throughout the same package.

23
Q

Protected classes are accessible where?

A

Throughout the same package and subclasses.

24
Q

Private classes are accessible where?

A

Only within the same class.

25
Q

What are non access modifiers in Java?

A

Non-access modifiers introduce additional functionalities and include static, final, and abstract.

26
Q

What makes a class immutable?

A

A class is immutable when it is declared as final and private. A class is also immutable if no setter functions are declared within it. Objects within an immutable class are also immutable themselves.

27
Q

What data types are supported in switch statements?

A

Byte, short, char, int, and string.

28
Q

How to pass multiple values with a single parameter into a method?

A

By using varargs, also known as variable arguments. Varargs are shown as three dots at the end of a parameters sequence, and is used to indicate that the method may have a variable number of parameters.

29
Q

What methods are available in the object class?

A

There are 11 methods. Some of which are clone(), equals(), toString(), finalize(), hashCode, getClass().

30
Q

What is the difference between == and .equals?

A

== compares the memory address while .equals() compares the value.

31
Q

What is an enhanced for loop?

A

An enhanced for loop is also known as a for-each loop. And is used to iterate through elements of arrays and collections.

32
Q

What are the 3 usages of the super keyword?

A

1) refer to immediate parent class instance. 2) Invoke immediate parent class method. 3) Act as a constructor of the immediate parent class.

33
Q

What is the first line of any constructor?

A

The first line of a constructormust either be a call on another constructor in the same class (using this.), or a call on the superclass constructor (using super).

34
Q

How would you perform constructor chaining?

A

By calling a constructor within another constructor.

35
Q

What happens if you don’t define a constructor for a class? Can you still instantiate it?

A

You are given a default constructor and yes you can instantiate it.