Java Basics Part 2 Flashcards

1
Q

What is the difference between static and final variables?

A

The main difference between static and final is that the static is used to define the class member that can be used independently of any object of the class. In contrast, final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.

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

What are the default values for all data types in Java?

A

byte - 0
short - 0
int - 0
long - 0L
float - 0.0f
double - 0.0d
char - ‘\u0000’
boolean - false

String (or any object) - null

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

What is a wrapper class? List them.

A

A Wrapper class is a class whose object wraps or contains primitive data types.

Boolean
Character
Byte
Short
Integer
Long
Float
Double

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

What is autoboxing / unboxing?

A

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

Unboxing on the other hand refers to converting an object of a wrapper type to its corresponding primitive value. For example conversion of Integer to int.

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

Is Java pass-by-value or pass-by-reference?

A

Java is officially always pass-by-value.

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

What makes a class immutable?

A

Immutable class means once the object of the class is created its fields cannot be modified or changed.

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

If two objects are equal, do they have the same hashcode? If not equal?

A

If two objects have the same hashcode then they are NOT necessarily equal. Otherwise you will have discovered the perfect hash function.

But the opposite is true: if the objects are equal, then they must have the same hashcode.

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

What data types are supported in switch statements?

A

A switch works with the byte , short , char , and int primitive data types.

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

List all non-access modifiers.

A

static
final
abstract
synchronized
volatile
transient
native

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

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

A

The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem.

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

What is a static block?

A

A static block in Java is a block of code that is executed at the time of loading a class for use in a Java application.

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

What are static imports?

A

Static imports allows fields and methods that have been scoped as public static to be used without specifying their class.

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

What methods are available in the Object class?

A

The following are methods for the java Object super class:

toString() : toString() provides String representation of an Object and used to convert an object to String. The default toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object

hashCode() : For every object, JVM generates a unique number which is hashcode. It returns distinct integers for distinct objects. A common misconception about this method is that hashCode() method returns the address of object, which is not correct. It convert the internal address of object to an integer by using an algorithm.

equals(Object obj) : Compares the given object to “this” object (the object on which the method is called). It gives a generic way to compare objects for equality.

getClass() : Returns the class object of “this” object and used to get actual runtime class of the object. It can also be used to get metadata of this class. The returned Class object is the object that is locked by static synchronized methods of the represented class. As it is final so we don’t override it.

finalize() method : This method is called just before an object is garbage collected. It is called by the Garbage Collector on an object when garbage collector determines that there are no more references to the object.

clone() : It returns a new object that is exactly the same as this object. For clone() method refer Clone()

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

What is the difference between == and .equals()?

A

In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

Main difference between .equals() method and == operator is that one is method and other is operator.

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

What is an enhanced for loop and what is a forEach loop?

A

It provides an alternative approach to traverse the array or collection in Java. It is mainly used to traverse the array or collection elements.

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

What are the 3 usages of “super” keyword?

A

Accessing parent class:

Variables
Methods
Constructor

In java super keyword refers immediate super(parent) class objects.

In java super keyword is only used in case of Inheritance. Whenever we create instance of class, parent class instance is also created. The parent class instance can be accessed by ‘ super’ keyword.

17
Q

What is the first line of any constructor?

A

public and then the Class name

example for a constructor for an object called Person is:

public Person() {}

18
Q

How would you perform constructor chaining?

A

Within same class: It can be done using this() keyword for constructors in the same class. From base class: By using super() keyword to call a constructor from the base class.

19
Q

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

A

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument.

You will get a default constructor during runtime.