Java Basics Part 1 Flashcards

1
Q

What is Java?

Explain some features of Java

A

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Some features are as followed:

Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

Platform Independent
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.

Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

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

What is JDK / JRE / JVM?

A

JDK (Java Development Kit) is a software development kit.

JRE (Java Runtime Environment) is a software bundle that allows Java program to run.

JVM (Java Virtual Machine) is an environment for executing bytecode.

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

What is the difference between an object and a class?

A

A class is a template used for the creation of objects. An object is an instance of a class.

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

What is the root class from which every class extends?

A

Every Java class extends the class Object, which means that every class we create has at its disposal all the methods defined in the Object class.

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

What are the primitive data types in Java?

A

Primitive data types - includes
byte , short , int , long , float , double , boolean and char.

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

Where are Strings stored?

A

In Java Strings are stored on the heap area in a separate memory location known as String Constant pool.

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

Explain stack vs heap

A

Stack is a linear data structure whereas Heap is a hierarchical data structure.

Stack memory will never become fragmented whereas Heap memory can become fragmented as blocks of memory are first allocated and then freed.

Stack accesses local variables only while Heap allows you to access variables globally.

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

Are variable references stored on the stack or heap? What about the objects they refer to?

A

All objects in Java are stored on the heap. The “variables” that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.

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

What is a stack frame? When are these created?

A

A stack frame is a memory management technique used in some programming languages for generating and eliminating temporary variables. Stack frames are only existent during the runtime process.

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

What are annotations?

A

Annotations are used to provide supplemental information about a program. Annotations start with ‘@’ character.

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

What is a POJO? How is it different from a bean?

A

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.

All JavaBeans are POJOs but not all POJOs are JavaBeans.

A JavaBean is a Java object that satisfies certain programming conventions:

-the JavaBean class must implement either Serializable or Externalizable;

-the JavaBean class must have a public no-arg constructor;

-all JavaBean properties must have public setter and getter methods (as appropriate);

-all JavaBean instance variables should be private.

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

Can you force garbage collection in Java? When is an object eligible for GC?

A

Unfortunately, this desire to immediately free up memory must go unrequited, as there is no way to force garbage collection (GC) in Java. An object is eligible to be garbage collected if its reference variable is lost from the program during execution.

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

Why are strings immutable in java? How would you make your own objects immutable?

A

The String is immutable in Java because of the security, synchronization and concurrency, caching, and class loading. You can make an object immutable by declaring a class as final or setting fields to private.

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

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

A

String is immutable whereas StringBuffer and StringBuilder are mutable classes.

StringBuffer is thread-safe and synchronized whereas StringBuilder is not.

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

What are the different variable scopes in Java?

A

There are three variable scopes:

Instance
Class
Local

Instance variables have a scope throughout the class except in static methods.

Class variables have a scope throughout the class.

Local variables have a scope within the block in which it is declared.

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

What are the access modifiers in Java? Explain them.

A

Default - declarations are visible only within the package (package private)

Private - declarations are visible within the class only

Protected - declarations are visible within the package or all subclasses

Public - declarations are visible everywhere

17
Q

What are the non-access modifiers in Java?

A

Non-Access modifiers

static.
final.
abstract.
synchronized.
volatile.
transient.
native.